1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts.socialwidget;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.content.SharedPreferences.Editor;
22import android.net.Uri;
23import android.preference.PreferenceManager;
24import android.util.Log;
25
26public class SocialWidgetSettings {
27    private static final String TAG = "SocialWidgetSettings";
28
29    // To migrate from earlier versions...
30    private static final String LEGACY_PREFS_NAME = "WidgetSettings";
31
32    // Prefix to use for all preferences used by this class.
33    private static final String PREFERENCES_PREFIX = "SocialWidgetSettings_";
34
35    private static final String CONTACT_URI_PREFIX = "CONTACT_URI_";
36
37    private static final String KEY_MIGRATED = PREFERENCES_PREFIX + "settings_migrated";
38
39    private static final SocialWidgetSettings sInstance = new SocialWidgetSettings();
40
41    public static SocialWidgetSettings getInstance() {
42        return sInstance;
43    }
44
45    private final String getPreferenceKey(int widgetId) {
46        return PREFERENCES_PREFIX + CONTACT_URI_PREFIX + Integer.toString(widgetId);
47    }
48
49    public void remove(Context context, int[] widgetIds) {
50        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
51        final Editor editor = settings.edit();
52        for (int widgetId : widgetIds) {
53            if (Log.isLoggable(TAG, Log.DEBUG)) {
54                Log.d(TAG, "remove(" + widgetId + ")");
55            }
56            editor.remove(getPreferenceKey(widgetId));
57        }
58        editor.apply();
59    }
60
61    public Uri getContactUri(Context context, int widgetId) {
62        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
63
64        ensureMigrated(context, settings);
65
66        final String resultString = settings.getString(getPreferenceKey(widgetId), null);
67        final Uri result = resultString == null ? null : Uri.parse(resultString);
68        if (Log.isLoggable(TAG, Log.DEBUG)) {
69            Log.d(TAG, "getContactUri(" + widgetId + ") --> " + result);
70        }
71        return result;
72    }
73
74    public void setContactUri(Context context, int widgetId, Uri contactLookupUri) {
75        if (Log.isLoggable(TAG, Log.DEBUG)) {
76            Log.d(TAG, "setContactUri(" + widgetId + ", " + contactLookupUri + ")");
77        }
78        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
79        final Editor editor = settings.edit();
80        if (contactLookupUri == null) {
81            editor.remove(getPreferenceKey(widgetId));
82        } else {
83            editor.putString(getPreferenceKey(widgetId), contactLookupUri.toString());
84        }
85        editor.apply();
86    }
87
88    private void ensureMigrated(Context context, SharedPreferences settings) {
89        if (settings.getBoolean(KEY_MIGRATED, false)) {
90            return; // Migrated already
91        }
92
93        Log.i(TAG, "Migrating widget settings...");
94
95        // Old preferences only had the "CONTACT_URI_" prefix.
96        // New preferences have the "SocialWidgetSettings_CONTACT_URI_" prefix.
97        // So just copy all the entries with adding "SocialWidgetSettings_" to their key names.
98
99        final SharedPreferences.Editor editor = settings.edit();
100
101        final SharedPreferences legacySettings =
102            context.getSharedPreferences(LEGACY_PREFS_NAME, Context.MODE_PRIVATE);
103        for (String key : legacySettings.getAll().keySet()) {
104            final String value = legacySettings.getString(key, null);
105            if (value == null) continue; // Just in case.
106
107            Log.i(TAG, "Found: " + key + ": " + value);
108
109            editor.putString(PREFERENCES_PREFIX + key, value);
110        }
111
112        editor.apply();
113        settings.edit().putBoolean(KEY_MIGRATED, true).apply();
114    }
115}
116