Search in sources :

Example 1 with OnSharedPreferenceChangeListener

use of android.content.SharedPreferences.OnSharedPreferenceChangeListener in project acra by ACRA.

the class ACRA method init.

/**
     * <p>
     * Initialize ACRA for a given Application. The call to this method should
     * be placed as soon as possible in the {@link Application#attachBaseContext(Context)}
     * method.
     * </p>
     *
     * @param app       Your Application class.
     * @param config    ACRAConfiguration to manually set up ACRA configuration.
     * @param checkReportsOnApplicationStart    Whether to invoke ErrorReporter.checkReportsOnApplicationStart().
     * @throws IllegalStateException if it is called more than once.
     */
public static void init(@NonNull Application app, @NonNull ACRAConfiguration config, boolean checkReportsOnApplicationStart) {
    final boolean senderServiceProcess = isACRASenderServiceProcess();
    if (senderServiceProcess) {
        if (ACRA.DEV_LOGGING)
            log.d(LOG_TAG, "Not initialising ACRA to listen for uncaught Exceptions as this is the SendWorker process and we only send reports, we don't capture them to avoid infinite loops");
    }
    final boolean supportedAndroidVersion = Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
    if (!supportedAndroidVersion) {
        // NB We keep initialising so that everything is configured. But ACRA is never enabled below.
        log.w(LOG_TAG, "ACRA 4.7.0+ requires Froyo or greater. ACRA is disabled and will NOT catch crashes or send messages.");
    }
    if (mApplication != null) {
        log.w(LOG_TAG, "ACRA#init called more than once. Won't do anything more.");
        return;
    }
    mApplication = app;
    //noinspection ConstantConditions
    if (config == null) {
        log.e(LOG_TAG, "ACRA#init called but no ACRAConfiguration provided");
        return;
    }
    configProxy = config;
    final SharedPreferences prefs = new SharedPreferencesFactory(mApplication, configProxy).create();
    new LegacyFileHandler(app, prefs).updateToCurrentVersionIfNecessary();
    // Initialize ErrorReporter with all required data
    final boolean enableAcra = supportedAndroidVersion && !shouldDisableACRA(prefs);
    if (!senderServiceProcess) {
        // Indicate that ACRA is or is not listening for crashes.
        log.i(LOG_TAG, "ACRA is " + (enableAcra ? "enabled" : "disabled") + " for " + mApplication.getPackageName() + ", initializing...");
    }
    errorReporterSingleton = new ErrorReporter(mApplication, configProxy, prefs, enableAcra, supportedAndroidVersion, !senderServiceProcess);
    // NB don't check if senderServiceProcess as it will gather these reports itself.
    if (checkReportsOnApplicationStart && !senderServiceProcess) {
        final ApplicationStartupProcessor startupProcessor = new ApplicationStartupProcessor(mApplication, config);
        if (config.deleteOldUnsentReportsOnApplicationStart()) {
            startupProcessor.deleteUnsentReportsFromOldAppVersion();
        }
        if (config.deleteUnapprovedReportsOnApplicationStart()) {
            startupProcessor.deleteAllUnapprovedReportsBarOne();
        }
        if (enableAcra) {
            startupProcessor.sendApprovedReports();
        }
    }
    // We HAVE to keep a reference otherwise the listener could be garbage
    // collected:
    // http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently/3104265#3104265
    mPrefListener = new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(@NonNull SharedPreferences sharedPreferences, String key) {
            if (PREF_DISABLE_ACRA.equals(key) || PREF_ENABLE_ACRA.equals(key)) {
                final boolean enableAcra = !shouldDisableACRA(sharedPreferences);
                getErrorReporter().setEnabled(enableAcra);
            }
        }
    };
    // This listener has to be set after initAcra is called to avoid a
    // NPE in ErrorReporter.disable() because
    // the context could be null at this moment.
    prefs.registerOnSharedPreferenceChangeListener(mPrefListener);
}
Also used : OnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener) SharedPreferences(android.content.SharedPreferences) SharedPreferencesFactory(org.acra.prefs.SharedPreferencesFactory) LegacyFileHandler(org.acra.legacy.LegacyFileHandler) ApplicationStartupProcessor(org.acra.util.ApplicationStartupProcessor)

Example 2 with OnSharedPreferenceChangeListener

use of android.content.SharedPreferences.OnSharedPreferenceChangeListener in project emerald by HenriDellal.

the class Apps method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Log.v(APP_TAG, "onCreate");
    super.onCreate(savedInstanceState);
    options = PreferenceManager.getDefaultSharedPreferences(this);
    Themer.theme = Integer.parseInt(options.getString(Keys.THEME, getResources().getString(R.string.defaultThemeValue)));
    layoutInit();
    if (options.getBoolean(Keys.SHOW_TUTORIAL, true)) {
        startActivity(new Intent(this, TutorialActivity.class));
    }
    if (Build.VERSION.SDK_INT >= 11 && options.getBoolean(Keys.KEEP_IN_MEMORY, false)) {
        Notification noti = new Notification.Builder(this).setContentTitle("Emerald").setContentText(" ").setSmallIcon(R.mipmap.icon).build();
        NotificationManager notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notiManager.notify(0, noti);
    }
    // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setRequestedOrientation(Integer.parseInt(options.getString(Keys.ORIENTATION, "2")));
    setContentView(mainLayout);
    options.edit().putBoolean(Keys.MESSAGE_SHOWN, false).commit();
    prefListener = new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            if (key.equals(Keys.ICON_PACK) || key.equals(Keys.TRANSFORM_DRAWABLE)) {
                MyCache.deleteIcons(Apps.this);
                LauncherApp.getInstance().getIconPackManager().setIconPack(sharedPreferences.getString(Keys.ICON_PACK, "default"));
                if (scanner != null && scanner.getStatus() == AsyncTask.Status.RUNNING)
                    return;
                scanner = new GetApps(Apps.this);
                scanner.execute(true);
                loadFilteredApps();
            } else if (key.equals(Keys.DIRTY) && sharedPreferences.getBoolean(Keys.DIRTY, false)) {
                if (scanner == null || scanner.getStatus() != AsyncTask.Status.RUNNING) {
                    scanner = new GetApps(Apps.this);
                    scanner.execute(false);
                }
            } else if (!sharedPreferences.getBoolean(Keys.MESSAGE_SHOWN, false) && Arrays.asList(Keys.restart).contains(key)) {
                Toast.makeText(Apps.this, getResources().getString(R.string.restart_needed), Toast.LENGTH_LONG).show();
                sharedPreferences.edit().putBoolean(Keys.MESSAGE_SHOWN, true).commit();
            }
        }
    };
    options.registerOnSharedPreferenceChangeListener(prefListener);
    if (Build.VERSION.SDK_INT >= 19) {
        Themer.setWindowDecorations(this, options);
    }
    Themer.applyTheme(this, options);
    dock = new Dock(this);
    changePrefsOnRotate();
    gestureDetector = new GestureDetector(this, new SwipeListener(this));
    grid.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View view, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });
}
Also used : NotificationManager(android.app.NotificationManager) OnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) GestureDetector(android.view.GestureDetector) GridView(android.widget.GridView) View(android.view.View) AbsListView(android.widget.AbsListView) Notification(android.app.Notification) MotionEvent(android.view.MotionEvent)

Example 3 with OnSharedPreferenceChangeListener

use of android.content.SharedPreferences.OnSharedPreferenceChangeListener in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SubScreenFragment method onCreate.

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
            final SubScreenFragment fragment = SubScreenFragment.this;
            final Context context = fragment.getActivity();
            if (context == null || fragment.getPreferenceScreen() == null) {
                final String tag = fragment.getClass().getSimpleName();
                // TODO: Introduce a static function to register this class and ensure that
                // onCreate must be called before "onSharedPreferenceChanged" is called.
                Log.w(tag, "onSharedPreferenceChanged called before activity starts.");
                return;
            }
            new BackupManager(context).dataChanged();
            fragment.onSharedPreferenceChanged(prefs, key);
        }
    };
    getSharedPreferences().registerOnSharedPreferenceChangeListener(mSharedPreferenceChangeListener);
}
Also used : Context(android.content.Context) OnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener) SharedPreferences(android.content.SharedPreferences) BackupManager(android.app.backup.BackupManager)

Aggregations

SharedPreferences (android.content.SharedPreferences)3 OnSharedPreferenceChangeListener (android.content.SharedPreferences.OnSharedPreferenceChangeListener)3 Notification (android.app.Notification)1 NotificationManager (android.app.NotificationManager)1 BackupManager (android.app.backup.BackupManager)1 Context (android.content.Context)1 Intent (android.content.Intent)1 GestureDetector (android.view.GestureDetector)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 GridView (android.widget.GridView)1 LegacyFileHandler (org.acra.legacy.LegacyFileHandler)1 SharedPreferencesFactory (org.acra.prefs.SharedPreferencesFactory)1 ApplicationStartupProcessor (org.acra.util.ApplicationStartupProcessor)1