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 android.content.Context; |
michael@0 | 9 | import android.content.SharedPreferences; |
michael@0 | 10 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
michael@0 | 11 | import android.util.Log; |
michael@0 | 12 | |
michael@0 | 13 | import org.mozilla.gecko.GeckoSharedPrefs; |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * Cache used to store authentication state of dynamic panels. The values |
michael@0 | 17 | * in this cache are set in JS through the Home.panels API. |
michael@0 | 18 | * |
michael@0 | 19 | * {@code DynamicPanel} uses this cache to determine whether or not to |
michael@0 | 20 | * show authentication UI for dynamic panels, including listening for |
michael@0 | 21 | * changes in authentication state. |
michael@0 | 22 | */ |
michael@0 | 23 | class PanelAuthCache { |
michael@0 | 24 | private static final String LOGTAG = "GeckoPanelAuthCache"; |
michael@0 | 25 | |
michael@0 | 26 | // Keep this in sync with the constant defined in Home.jsm |
michael@0 | 27 | private static final String PREFS_PANEL_AUTH_PREFIX = "home_panels_auth_"; |
michael@0 | 28 | |
michael@0 | 29 | private final Context mContext; |
michael@0 | 30 | private SharedPrefsListener mSharedPrefsListener; |
michael@0 | 31 | private OnChangeListener mChangeListener; |
michael@0 | 32 | |
michael@0 | 33 | public interface OnChangeListener { |
michael@0 | 34 | public void onChange(String panelId, boolean isAuthenticated); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | public PanelAuthCache(Context context) { |
michael@0 | 38 | mContext = context; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | private SharedPreferences getSharedPreferences() { |
michael@0 | 42 | return GeckoSharedPrefs.forProfile(mContext); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | private String getPanelAuthKey(String panelId) { |
michael@0 | 46 | return PREFS_PANEL_AUTH_PREFIX + panelId; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | public boolean isAuthenticated(String panelId) { |
michael@0 | 50 | final SharedPreferences prefs = getSharedPreferences(); |
michael@0 | 51 | return prefs.getBoolean(getPanelAuthKey(panelId), false); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | public void setOnChangeListener(OnChangeListener listener) { |
michael@0 | 55 | final SharedPreferences prefs = getSharedPreferences(); |
michael@0 | 56 | |
michael@0 | 57 | if (mChangeListener != null) { |
michael@0 | 58 | prefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener); |
michael@0 | 59 | mSharedPrefsListener = null; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | mChangeListener = listener; |
michael@0 | 63 | |
michael@0 | 64 | if (mChangeListener != null) { |
michael@0 | 65 | mSharedPrefsListener = new SharedPrefsListener(); |
michael@0 | 66 | prefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | private class SharedPrefsListener implements OnSharedPreferenceChangeListener { |
michael@0 | 71 | @Override |
michael@0 | 72 | public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
michael@0 | 73 | if (key.startsWith(PREFS_PANEL_AUTH_PREFIX)) { |
michael@0 | 74 | final String panelId = key.substring(PREFS_PANEL_AUTH_PREFIX.length()); |
michael@0 | 75 | final boolean isAuthenticated = prefs.getBoolean(key, false); |
michael@0 | 76 | |
michael@0 | 77 | Log.d(LOGTAG, "Auth state changed: panelId=" + panelId + ", isAuthenticated=" + isAuthenticated); |
michael@0 | 78 | mChangeListener.onChange(panelId, isAuthenticated); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | } |