|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.home; |
|
7 |
|
8 import android.content.Context; |
|
9 import android.content.SharedPreferences; |
|
10 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
|
11 import android.util.Log; |
|
12 |
|
13 import org.mozilla.gecko.GeckoSharedPrefs; |
|
14 |
|
15 /** |
|
16 * Cache used to store authentication state of dynamic panels. The values |
|
17 * in this cache are set in JS through the Home.panels API. |
|
18 * |
|
19 * {@code DynamicPanel} uses this cache to determine whether or not to |
|
20 * show authentication UI for dynamic panels, including listening for |
|
21 * changes in authentication state. |
|
22 */ |
|
23 class PanelAuthCache { |
|
24 private static final String LOGTAG = "GeckoPanelAuthCache"; |
|
25 |
|
26 // Keep this in sync with the constant defined in Home.jsm |
|
27 private static final String PREFS_PANEL_AUTH_PREFIX = "home_panels_auth_"; |
|
28 |
|
29 private final Context mContext; |
|
30 private SharedPrefsListener mSharedPrefsListener; |
|
31 private OnChangeListener mChangeListener; |
|
32 |
|
33 public interface OnChangeListener { |
|
34 public void onChange(String panelId, boolean isAuthenticated); |
|
35 } |
|
36 |
|
37 public PanelAuthCache(Context context) { |
|
38 mContext = context; |
|
39 } |
|
40 |
|
41 private SharedPreferences getSharedPreferences() { |
|
42 return GeckoSharedPrefs.forProfile(mContext); |
|
43 } |
|
44 |
|
45 private String getPanelAuthKey(String panelId) { |
|
46 return PREFS_PANEL_AUTH_PREFIX + panelId; |
|
47 } |
|
48 |
|
49 public boolean isAuthenticated(String panelId) { |
|
50 final SharedPreferences prefs = getSharedPreferences(); |
|
51 return prefs.getBoolean(getPanelAuthKey(panelId), false); |
|
52 } |
|
53 |
|
54 public void setOnChangeListener(OnChangeListener listener) { |
|
55 final SharedPreferences prefs = getSharedPreferences(); |
|
56 |
|
57 if (mChangeListener != null) { |
|
58 prefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener); |
|
59 mSharedPrefsListener = null; |
|
60 } |
|
61 |
|
62 mChangeListener = listener; |
|
63 |
|
64 if (mChangeListener != null) { |
|
65 mSharedPrefsListener = new SharedPrefsListener(); |
|
66 prefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener); |
|
67 } |
|
68 } |
|
69 |
|
70 private class SharedPrefsListener implements OnSharedPreferenceChangeListener { |
|
71 @Override |
|
72 public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
|
73 if (key.startsWith(PREFS_PANEL_AUTH_PREFIX)) { |
|
74 final String panelId = key.substring(PREFS_PANEL_AUTH_PREFIX.length()); |
|
75 final boolean isAuthenticated = prefs.getBoolean(key, false); |
|
76 |
|
77 Log.d(LOGTAG, "Auth state changed: panelId=" + panelId + ", isAuthenticated=" + isAuthenticated); |
|
78 mChangeListener.onChange(panelId, isAuthenticated); |
|
79 } |
|
80 } |
|
81 } |
|
82 } |