consp1racy / android-support-preference

Android Preferences according to Material design specs
Apache License 2.0
331 stars 49 forks source link

New API for reporting exceptions in the library #93

Closed consp1racy closed 6 years ago

consp1racy commented 6 years ago

Introduce a new API in the library to report internal errors to client supplied processor, such as Crashlytics. Client can then request support here, in Issues.

This is indented to mend device specific errors. RingtonePreference can benefit the most, I think.

Clients can implement like so:

class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // Setup Crashlytics, do whatever else you need...

        XpSupportPreferencePlugins.registerErrorInterceptor(new ErrorInterceptor() {
            @Override
            public void onError(@NonNull Throwable t, @Nullable String message) {
                Timber.w(t, message);
                if (message != null) Crashlytics.log(message);
                Crashlytics.logException(t);
            }
        });
    }
}

Only register the listener once per app process!

javmarina commented 6 years ago

Where are we supposed to include that snippet? in the XpPreferenceFragment (during onCreatePreferences2()) or in our Application.java class? Btw, thanks for the amazing library.

consp1racy commented 6 years ago

@javmarina Hello, in Application.onCreate. Do it only once when the app starts, otherwise it would pollute your logs. I'll update the readme.

javmarina commented 6 years ago

That's what I was thinking. Makes sense, at least if the user opens the settings screen multiple times

consp1racy commented 6 years ago

You're right! It makes no sense to eagerly do it on app start. That's a good point.

You can do it in settings activity but put it behind a static boolean (single initialization) and make sure you don't leak activity context (error interceptor is static nested class and gets an application context as parameter).