Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9ed91a95 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

[Phone] Don't use custom shared preferences

Just use the app-wide default shared preferences, so that it'll read from the
cache rather than the filesystem.
The default shared preferences should be preloaded by
ContactsApplication.onCreate using a worker thread when Dialtacts needs it.

It was actually a strict mode violation causing something like > 100ms penalty,
but I assumed it was beacuse it was the first read and unavoidable.
I hadn't realized dialtacts had its own preferences.

I also fixed the social widget settings.  Existing settings will all be
migrated.

Also fixed the log spew from SocialWidgetSettings.

Bug 5296990

Change-Id: Idb8e393ebfd6c77b14906ea4ee26dfd032da2770
parent 5f338fa2
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.PreferenceManager;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents.UI;
@@ -94,12 +95,11 @@ public class DialtactsActivity extends Activity {

    private static final int TAB_INDEX_COUNT = 3;

    /** Name of the dialtacts shared preferences */
    static final String PREFS_DIALTACTS = "dialtacts";
    static final boolean PREF_FAVORITES_AS_CONTACTS_DEFAULT = false;
    private SharedPreferences mPrefs;

    /** Last manually selected tab index */
    private static final String PREF_LAST_MANUALLY_SELECTED_TAB = "last_manually_selected_tab";
    private static final String PREF_LAST_MANUALLY_SELECTED_TAB =
            "DialtactsActivity_last_manually_selected_tab";
    private static final int PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT = TAB_INDEX_DIALER;

    /**
@@ -391,8 +391,8 @@ public class DialtactsActivity extends Activity {
        getActionBar().setDisplayShowHomeEnabled(false);

        // Load the last manually loaded tab
        final SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE);
        mLastManuallySelectedFragment = prefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB,
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        mLastManuallySelectedFragment = mPrefs.getInt(PREF_LAST_MANUALLY_SELECTED_TAB,
                PREF_LAST_MANUALLY_SELECTED_TAB_DEFAULT);
        if (mLastManuallySelectedFragment >= TAB_INDEX_COUNT) {
            // Stored value may have exceeded the number of current tabs. Reset it.
@@ -496,11 +496,8 @@ public class DialtactsActivity extends Activity {
    protected void onPause() {
        super.onPause();

        final SharedPreferences.Editor editor =
                getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE).edit();
        editor.putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment);

        editor.apply();
        mPrefs.edit().putInt(PREF_LAST_MANUALLY_SELECTED_TAB, mLastManuallySelectedFragment)
                .apply();
    }

    private void fixIntent(Intent intent) {
+58 −16
Original line number Diff line number Diff line
@@ -20,54 +20,96 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;

public class SocialWidgetSettings {
    private static final String TAG = "SocialWidgetSettings";

    private static final String PREFS_NAME = "WidgetSettings";
    // To migrate from earlier versions...
    private static final String LEGACY_PREFS_NAME = "WidgetSettings";

    // Prefix to use for all preferences used by this class.
    private static final String PREFERENCES_PREFIX = "SocialWidgetSettings_";

    private static final String CONTACT_URI_PREFIX = "CONTACT_URI_";

    private static final String KEY_MIGRATED = PREFERENCES_PREFIX + "settings_migrated";

    private static final SocialWidgetSettings sInstance = new SocialWidgetSettings();

    public static SocialWidgetSettings getInstance() {
        return sInstance;
    }

    private final String getSettingsString(int widgetId) {
        return CONTACT_URI_PREFIX + Integer.toString(widgetId);
    private final String getPreferenceKey(int widgetId) {
        return PREFERENCES_PREFIX + CONTACT_URI_PREFIX + Integer.toString(widgetId);
    }

    public void remove(Context context, int[] widgetIds) {
        final SharedPreferences settings =
            context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        final Editor editor = settings.edit();
        for (int widgetId : widgetIds) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "remove(" + widgetId + ")");
            editor.remove(getSettingsString(widgetId));
            }
            editor.remove(getPreferenceKey(widgetId));
        }
        editor.apply();
    }

    public Uri getContactUri(Context context, int widgetId) {
        final SharedPreferences settings =
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        final String resultString = settings.getString(getSettingsString(widgetId), null);
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

        ensureMigrated(context, settings);

        final String resultString = settings.getString(getPreferenceKey(widgetId), null);
        final Uri result = resultString == null ? null : Uri.parse(resultString);
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "getContactUri(" + widgetId + ") --> " + result);
        }
        return result;
    }

    public void setContactUri(Context context, int widgetId, Uri contactLookupUri) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "setContactUri(" + widgetId + ", " + contactLookupUri + ")");
        final SharedPreferences settings =
                context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        }
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        final Editor editor = settings.edit();
        if (contactLookupUri == null) {
            editor.remove(getSettingsString(widgetId));
            editor.remove(getPreferenceKey(widgetId));
        } else {
            editor.putString(getSettingsString(widgetId), contactLookupUri.toString());
            editor.putString(getPreferenceKey(widgetId), contactLookupUri.toString());
        }
        editor.apply();
    }

    private void ensureMigrated(Context context, SharedPreferences settings) {
        if (settings.getBoolean(KEY_MIGRATED, false)) {
            return; // Migrated already
        }

        Log.i(TAG, "Migrating widget settings...");

        // Old preferences only had the "CONTACT_URI_" prefix.
        // New preferences have the "SocialWidgetSettings_CONTACT_URI_" prefix.
        // So just copy all the entries with adding "SocialWidgetSettings_" to their key names.

        final SharedPreferences.Editor editor = settings.edit();

        final SharedPreferences legacySettings =
            context.getSharedPreferences(LEGACY_PREFS_NAME, Context.MODE_PRIVATE);
        for (String key : legacySettings.getAll().keySet()) {
            final String value = legacySettings.getString(key, null);
            if (value == null) continue; // Just in case.

            Log.i(TAG, "Found: " + key + ": " + value);

            editor.putString(PREFERENCES_PREFIX + key, value);
        }

        editor.apply();
        settings.edit().putBoolean(KEY_MIGRATED, true).apply();
    }
}