|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +package be.hyperrail.android.util; |
| 8 | + |
| 9 | +import android.app.AlertDialog; |
| 10 | +import android.content.Context; |
| 11 | +import android.content.SharedPreferences; |
| 12 | +import android.preference.PreferenceManager; |
| 13 | + |
| 14 | +import org.joda.time.DateTime; |
| 15 | + |
| 16 | +import be.hyperrail.android.R; |
| 17 | +import be.hyperrail.android.logging.HyperRailLog; |
| 18 | + |
| 19 | +public class CrashlyticsOptInDialog { |
| 20 | + |
| 21 | + private static final String PREFERENCES_KEY_OPTIN_REPLIED = "crashreporting_optin_replied"; |
| 22 | + private static final String PREFERENCES_CRASHLYTICS_ENABLED = "pref_crashlytics_enabled"; |
| 23 | + |
| 24 | + private static final HyperRailLog log = HyperRailLog.getLogger(CrashlyticsOptInDialog.class); |
| 25 | + private static SharedPreferences sharedPreferences; |
| 26 | + private static boolean hasOptedInOrOut; |
| 27 | + |
| 28 | + private CrashlyticsOptInDialog() { |
| 29 | + // No public constructor |
| 30 | + } |
| 31 | + |
| 32 | + public static void init(Context context) { |
| 33 | + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); |
| 34 | + hasOptedInOrOut = sharedPreferences.getBoolean(PREFERENCES_KEY_OPTIN_REPLIED, false); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public static void showDialogOnce(Context context) { |
| 39 | + if (!hasOptedInOrOut){ |
| 40 | + showOptInDialog(context); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static void showOptInDialog(final Context context) { |
| 45 | + AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 46 | + |
| 47 | + int titleId = R.string.crashlytics_dialog_title; |
| 48 | + int messageId = R.string.crashlytics_dialog_message; |
| 49 | + int disableButtonText = R.string.crashlytics_dialog_button_disable; |
| 50 | + int enableButtonText = R.string.crashlytics_dialog_button_enable; |
| 51 | + builder.setTitle(titleId); |
| 52 | + builder.setMessage(messageId); |
| 53 | + setDialogButtonActions(builder, enableButtonText, disableButtonText); |
| 54 | + builder.show(); |
| 55 | + } |
| 56 | + |
| 57 | + private static void setDialogButtonActions(AlertDialog.Builder builder, int enableButtonText, int disableButtonText) { |
| 58 | + builder.setPositiveButton(enableButtonText, (dialog, which) -> { |
| 59 | + optIn(); |
| 60 | + setOptedInOrOut(); |
| 61 | + }); |
| 62 | + builder.setNegativeButton(disableButtonText, (dialog, which) -> { |
| 63 | + optOut(); |
| 64 | + setOptedInOrOut(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + private static void optIn() { |
| 69 | + log.info("Storing opt-in from crashlytics"); |
| 70 | + setPreference(PREFERENCES_CRASHLYTICS_ENABLED, true); |
| 71 | + } |
| 72 | + private static void optOut() { |
| 73 | + log.info("Storing opt-out from crashlytics"); |
| 74 | + setPreference(PREFERENCES_CRASHLYTICS_ENABLED, false); |
| 75 | + } |
| 76 | + |
| 77 | + private static void setOptedInOrOut() { |
| 78 | + setPreference(PREFERENCES_KEY_OPTIN_REPLIED, true); |
| 79 | + } |
| 80 | + |
| 81 | + private static void setPreference(String preference, boolean value) { |
| 82 | + if (sharedPreferences != null) { |
| 83 | + sharedPreferences.edit().putBoolean(preference, value).apply(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments