Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.home; |
michael@0 | 7 | |
michael@0 | 8 | import static org.mozilla.gecko.home.HomeConfig.createBuiltinPanelConfig; |
michael@0 | 9 | |
michael@0 | 10 | import java.util.ArrayList; |
michael@0 | 11 | import java.util.EnumSet; |
michael@0 | 12 | import java.util.List; |
michael@0 | 13 | import java.util.Locale; |
michael@0 | 14 | |
michael@0 | 15 | import org.json.JSONArray; |
michael@0 | 16 | import org.json.JSONException; |
michael@0 | 17 | import org.json.JSONObject; |
michael@0 | 18 | import org.mozilla.gecko.GeckoSharedPrefs; |
michael@0 | 19 | import org.mozilla.gecko.home.HomeConfig.HomeConfigBackend; |
michael@0 | 20 | import org.mozilla.gecko.home.HomeConfig.OnReloadListener; |
michael@0 | 21 | import org.mozilla.gecko.home.HomeConfig.PanelConfig; |
michael@0 | 22 | import org.mozilla.gecko.home.HomeConfig.PanelType; |
michael@0 | 23 | import org.mozilla.gecko.home.HomeConfig.State; |
michael@0 | 24 | import org.mozilla.gecko.util.HardwareUtils; |
michael@0 | 25 | |
michael@0 | 26 | import android.content.BroadcastReceiver; |
michael@0 | 27 | import android.content.Context; |
michael@0 | 28 | import android.content.Intent; |
michael@0 | 29 | import android.content.IntentFilter; |
michael@0 | 30 | import android.content.SharedPreferences; |
michael@0 | 31 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
michael@0 | 32 | import android.support.v4.content.LocalBroadcastManager; |
michael@0 | 33 | import android.text.TextUtils; |
michael@0 | 34 | import android.util.Log; |
michael@0 | 35 | |
michael@0 | 36 | class HomeConfigPrefsBackend implements HomeConfigBackend { |
michael@0 | 37 | private static final String LOGTAG = "GeckoHomeConfigBackend"; |
michael@0 | 38 | |
michael@0 | 39 | private static final String PREFS_CONFIG_KEY = "home_panels"; |
michael@0 | 40 | private static final String PREFS_LOCALE_KEY = "home_locale"; |
michael@0 | 41 | |
michael@0 | 42 | private static final String RELOAD_BROADCAST = "HomeConfigPrefsBackend:Reload"; |
michael@0 | 43 | |
michael@0 | 44 | private final Context mContext; |
michael@0 | 45 | private ReloadBroadcastReceiver mReloadBroadcastReceiver; |
michael@0 | 46 | private OnReloadListener mReloadListener; |
michael@0 | 47 | |
michael@0 | 48 | public HomeConfigPrefsBackend(Context context) { |
michael@0 | 49 | mContext = context; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | private SharedPreferences getSharedPreferences() { |
michael@0 | 53 | return GeckoSharedPrefs.forProfile(mContext); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | private State loadDefaultConfig() { |
michael@0 | 57 | final ArrayList<PanelConfig> panelConfigs = new ArrayList<PanelConfig>(); |
michael@0 | 58 | |
michael@0 | 59 | panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.TOP_SITES, |
michael@0 | 60 | EnumSet.of(PanelConfig.Flags.DEFAULT_PANEL))); |
michael@0 | 61 | |
michael@0 | 62 | panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.BOOKMARKS)); |
michael@0 | 63 | |
michael@0 | 64 | // We disable reader mode support on low memory devices. Hence the |
michael@0 | 65 | // reading list panel should not show up on such devices. |
michael@0 | 66 | if (!HardwareUtils.isLowMemoryPlatform()) { |
michael@0 | 67 | panelConfigs.add(createBuiltinPanelConfig(mContext, PanelType.READING_LIST)); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | final PanelConfig historyEntry = createBuiltinPanelConfig(mContext, PanelType.HISTORY); |
michael@0 | 71 | |
michael@0 | 72 | // On tablets, the history panel is the last. |
michael@0 | 73 | // On phones, the history panel is the first one. |
michael@0 | 74 | if (HardwareUtils.isTablet()) { |
michael@0 | 75 | panelConfigs.add(historyEntry); |
michael@0 | 76 | } else { |
michael@0 | 77 | panelConfigs.add(0, historyEntry); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | return new State(panelConfigs, true); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | private State loadConfigFromString(String jsonString) { |
michael@0 | 84 | final JSONArray jsonPanelConfigs; |
michael@0 | 85 | try { |
michael@0 | 86 | jsonPanelConfigs = new JSONArray(jsonString); |
michael@0 | 87 | } catch (JSONException e) { |
michael@0 | 88 | Log.e(LOGTAG, "Error loading the list of home panels from JSON prefs", e); |
michael@0 | 89 | |
michael@0 | 90 | // Fallback to default config |
michael@0 | 91 | return loadDefaultConfig(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | final ArrayList<PanelConfig> panelConfigs = new ArrayList<PanelConfig>(); |
michael@0 | 95 | |
michael@0 | 96 | final int count = jsonPanelConfigs.length(); |
michael@0 | 97 | for (int i = 0; i < count; i++) { |
michael@0 | 98 | try { |
michael@0 | 99 | final JSONObject jsonPanelConfig = jsonPanelConfigs.getJSONObject(i); |
michael@0 | 100 | final PanelConfig panelConfig = new PanelConfig(jsonPanelConfig); |
michael@0 | 101 | panelConfigs.add(panelConfig); |
michael@0 | 102 | } catch (Exception e) { |
michael@0 | 103 | Log.e(LOGTAG, "Exception loading PanelConfig from JSON", e); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | return new State(panelConfigs, false); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | @Override |
michael@0 | 111 | public State load() { |
michael@0 | 112 | final SharedPreferences prefs = getSharedPreferences(); |
michael@0 | 113 | final String jsonString = prefs.getString(PREFS_CONFIG_KEY, null); |
michael@0 | 114 | |
michael@0 | 115 | final State configState; |
michael@0 | 116 | if (TextUtils.isEmpty(jsonString)) { |
michael@0 | 117 | configState = loadDefaultConfig(); |
michael@0 | 118 | } else { |
michael@0 | 119 | configState = loadConfigFromString(jsonString); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | return configState; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | @Override |
michael@0 | 126 | public void save(State configState) { |
michael@0 | 127 | final SharedPreferences prefs = getSharedPreferences(); |
michael@0 | 128 | final SharedPreferences.Editor editor = prefs.edit(); |
michael@0 | 129 | |
michael@0 | 130 | // No need to save the state to disk if it represents the default |
michael@0 | 131 | // HomeConfig configuration. Simply force all existing HomeConfigLoader |
michael@0 | 132 | // instances to refresh their contents. |
michael@0 | 133 | if (!configState.isDefault()) { |
michael@0 | 134 | final JSONArray jsonPanelConfigs = new JSONArray(); |
michael@0 | 135 | |
michael@0 | 136 | for (PanelConfig panelConfig : configState) { |
michael@0 | 137 | try { |
michael@0 | 138 | final JSONObject jsonPanelConfig = panelConfig.toJSON(); |
michael@0 | 139 | jsonPanelConfigs.put(jsonPanelConfig); |
michael@0 | 140 | } catch (Exception e) { |
michael@0 | 141 | Log.e(LOGTAG, "Exception converting PanelConfig to JSON", e); |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | editor.putString(PREFS_CONFIG_KEY, jsonPanelConfigs.toString()); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | editor.putString(PREFS_LOCALE_KEY, Locale.getDefault().toString()); |
michael@0 | 149 | editor.commit(); |
michael@0 | 150 | |
michael@0 | 151 | // Trigger reload listeners on all live backend instances |
michael@0 | 152 | sendReloadBroadcast(); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | @Override |
michael@0 | 156 | public String getLocale() { |
michael@0 | 157 | final SharedPreferences prefs = getSharedPreferences(); |
michael@0 | 158 | |
michael@0 | 159 | String locale = prefs.getString(PREFS_LOCALE_KEY, null); |
michael@0 | 160 | if (locale == null) { |
michael@0 | 161 | // Initialize config with the current locale |
michael@0 | 162 | final String currentLocale = Locale.getDefault().toString(); |
michael@0 | 163 | |
michael@0 | 164 | final SharedPreferences.Editor editor = prefs.edit(); |
michael@0 | 165 | editor.putString(PREFS_LOCALE_KEY, currentLocale); |
michael@0 | 166 | editor.commit(); |
michael@0 | 167 | |
michael@0 | 168 | // If the user has saved HomeConfig before, return null this |
michael@0 | 169 | // one time to trigger a refresh and ensure we use the |
michael@0 | 170 | // correct locale for the saved state. For more context, |
michael@0 | 171 | // see HomePanelsManager.onLocaleReady(). |
michael@0 | 172 | if (!prefs.contains(PREFS_CONFIG_KEY)) { |
michael@0 | 173 | locale = currentLocale; |
michael@0 | 174 | } |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | return locale; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | @Override |
michael@0 | 181 | public void setOnReloadListener(OnReloadListener listener) { |
michael@0 | 182 | if (mReloadListener != null) { |
michael@0 | 183 | unregisterReloadReceiver(); |
michael@0 | 184 | mReloadBroadcastReceiver = null; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | mReloadListener = listener; |
michael@0 | 188 | |
michael@0 | 189 | if (mReloadListener != null) { |
michael@0 | 190 | mReloadBroadcastReceiver = new ReloadBroadcastReceiver(); |
michael@0 | 191 | registerReloadReceiver(); |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | private void sendReloadBroadcast() { |
michael@0 | 196 | final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(mContext); |
michael@0 | 197 | final Intent reloadIntent = new Intent(RELOAD_BROADCAST); |
michael@0 | 198 | lbm.sendBroadcast(reloadIntent); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | private void registerReloadReceiver() { |
michael@0 | 202 | final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(mContext); |
michael@0 | 203 | lbm.registerReceiver(mReloadBroadcastReceiver, new IntentFilter(RELOAD_BROADCAST)); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | private void unregisterReloadReceiver() { |
michael@0 | 207 | final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(mContext); |
michael@0 | 208 | lbm.unregisterReceiver(mReloadBroadcastReceiver); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | private class ReloadBroadcastReceiver extends BroadcastReceiver { |
michael@0 | 212 | @Override |
michael@0 | 213 | public void onReceive(Context context, Intent intent) { |
michael@0 | 214 | mReloadListener.onReload(); |
michael@0 | 215 | } |
michael@0 | 216 | } |
michael@0 | 217 | } |