Skip to content

Commit bb49b1c

Browse files
committed
Add javadoc to ErrorUtil and ErrorActivity
1 parent 950956e commit bb49b1c

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
5353
*/
5454

55+
/**
56+
* This activity is used to show error details and allow reporting them in various ways. Use {@link
57+
* ErrorUtil#openActivity(Context, ErrorInfo)} to correctly open this activity.
58+
*/
5559
public class ErrorActivity extends AppCompatActivity {
5660
// LOG TAGS
5761
public static final String TAG = ErrorActivity.class.toString();

app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,59 @@ import androidx.fragment.app.Fragment
1515
import com.google.android.material.snackbar.Snackbar
1616
import org.schabi.newpipe.R
1717

18+
/**
19+
* This class contains all of the methods that should be used to let the user know that an error has
20+
* occurred in the least intrusive way possible for each case. This class is for unexpected errors,
21+
* for handled errors (e.g. network errors) use e.g. [ErrorPanelHelper] instead.
22+
* - Use a snackbar if the exception is not critical and it happens in a place where a root view
23+
* is available.
24+
* - Use a notification if the exception happens inside a background service (player, subscription
25+
* import, ...) or there is no activity/fragment from which to extract a root view.
26+
* - Finally use the error activity only as a last resort in case the exception is critical and
27+
* happens in an open activity (since the workflow would be interrupted anyway in that case).
28+
*/
1829
class ErrorUtil {
1930
companion object {
2031
private const val ERROR_REPORT_NOTIFICATION_ID = 5340681
2132

2233
/**
23-
* Reports a new error by starting a new activity.
34+
* Starts a new error activity allowing the user to report the provided error. Only use this
35+
* method directly as a last resort in case the exception is critical and happens in an open
36+
* activity (since the workflow would be interrupted anyway in that case). So never use this
37+
* for background services.
2438
*
25-
* @param context
26-
* @param errorInfo
39+
* @param context the context to use to start the new activity
40+
* @param errorInfo the error info to be reported
2741
*/
2842
@JvmStatic
2943
fun openActivity(context: Context, errorInfo: ErrorInfo) {
3044
context.startActivity(getErrorActivityIntent(context, errorInfo))
3145
}
3246

47+
/**
48+
* Show a bottom snackbar to the user, with a report button that opens the error activity.
49+
* Use this method if the exception is not critical and it happens in a place where a root
50+
* view is available.
51+
*
52+
* @param context will be used to obtain the root view if it is an [Activity]; if no root
53+
* view can be found an error notification is shown instead
54+
* @param errorInfo the error info to be reported
55+
*/
3356
@JvmStatic
3457
fun showSnackbar(context: Context, errorInfo: ErrorInfo) {
3558
val rootView = if (context is Activity) context.findViewById<View>(R.id.content) else null
3659
showSnackbar(context, rootView, errorInfo)
3760
}
3861

62+
/**
63+
* Show a bottom snackbar to the user, with a report button that opens the error activity.
64+
* Use this method if the exception is not critical and it happens in a place where a root
65+
* view is available.
66+
*
67+
* @param fragment will be used to obtain the root view if it has a connected [Activity]; if
68+
* no root view can be found an error notification is shown instead
69+
* @param errorInfo the error info to be reported
70+
*/
3971
@JvmStatic
4072
fun showSnackbar(fragment: Fragment, errorInfo: ErrorInfo) {
4173
var rootView = fragment.view
@@ -45,16 +77,32 @@ class ErrorUtil {
4577
showSnackbar(fragment.requireContext(), rootView, errorInfo)
4678
}
4779

80+
/**
81+
* Shortcut to calling [showSnackbar] with an [ErrorInfo] of type [UserAction.UI_ERROR]
82+
*/
4883
@JvmStatic
4984
fun showUiErrorSnackbar(context: Context, request: String, throwable: Throwable) {
5085
showSnackbar(context, ErrorInfo(throwable, UserAction.UI_ERROR, request))
5186
}
5287

88+
/**
89+
* Shortcut to calling [showSnackbar] with an [ErrorInfo] of type [UserAction.UI_ERROR]
90+
*/
5391
@JvmStatic
5492
fun showUiErrorSnackbar(fragment: Fragment, request: String, throwable: Throwable) {
5593
showSnackbar(fragment, ErrorInfo(throwable, UserAction.UI_ERROR, request))
5694
}
5795

96+
/**
97+
* Create an error notification. Tapping on the notification opens the error activity. Use
98+
* this method if the exception happens inside a background service (player, subscription
99+
* import, ...) or there is no activity/fragment from which to extract a root view.
100+
*
101+
* @param context the context to use to show the notification
102+
* @param errorInfo the error info to be reported; the error message
103+
* [ErrorInfo.messageStringId] will be shown in the notification
104+
* description
105+
*/
58106
@JvmStatic
59107
fun createNotification(context: Context, errorInfo: ErrorInfo) {
60108
val notificationManager =

0 commit comments

Comments
 (0)