1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/home/PanelAuthCache.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.home; 1.10 + 1.11 +import android.content.Context; 1.12 +import android.content.SharedPreferences; 1.13 +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 1.14 +import android.util.Log; 1.15 + 1.16 +import org.mozilla.gecko.GeckoSharedPrefs; 1.17 + 1.18 +/** 1.19 + * Cache used to store authentication state of dynamic panels. The values 1.20 + * in this cache are set in JS through the Home.panels API. 1.21 + * 1.22 + * {@code DynamicPanel} uses this cache to determine whether or not to 1.23 + * show authentication UI for dynamic panels, including listening for 1.24 + * changes in authentication state. 1.25 + */ 1.26 +class PanelAuthCache { 1.27 + private static final String LOGTAG = "GeckoPanelAuthCache"; 1.28 + 1.29 + // Keep this in sync with the constant defined in Home.jsm 1.30 + private static final String PREFS_PANEL_AUTH_PREFIX = "home_panels_auth_"; 1.31 + 1.32 + private final Context mContext; 1.33 + private SharedPrefsListener mSharedPrefsListener; 1.34 + private OnChangeListener mChangeListener; 1.35 + 1.36 + public interface OnChangeListener { 1.37 + public void onChange(String panelId, boolean isAuthenticated); 1.38 + } 1.39 + 1.40 + public PanelAuthCache(Context context) { 1.41 + mContext = context; 1.42 + } 1.43 + 1.44 + private SharedPreferences getSharedPreferences() { 1.45 + return GeckoSharedPrefs.forProfile(mContext); 1.46 + } 1.47 + 1.48 + private String getPanelAuthKey(String panelId) { 1.49 + return PREFS_PANEL_AUTH_PREFIX + panelId; 1.50 + } 1.51 + 1.52 + public boolean isAuthenticated(String panelId) { 1.53 + final SharedPreferences prefs = getSharedPreferences(); 1.54 + return prefs.getBoolean(getPanelAuthKey(panelId), false); 1.55 + } 1.56 + 1.57 + public void setOnChangeListener(OnChangeListener listener) { 1.58 + final SharedPreferences prefs = getSharedPreferences(); 1.59 + 1.60 + if (mChangeListener != null) { 1.61 + prefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener); 1.62 + mSharedPrefsListener = null; 1.63 + } 1.64 + 1.65 + mChangeListener = listener; 1.66 + 1.67 + if (mChangeListener != null) { 1.68 + mSharedPrefsListener = new SharedPrefsListener(); 1.69 + prefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener); 1.70 + } 1.71 + } 1.72 + 1.73 + private class SharedPrefsListener implements OnSharedPreferenceChangeListener { 1.74 + @Override 1.75 + public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 1.76 + if (key.startsWith(PREFS_PANEL_AUTH_PREFIX)) { 1.77 + final String panelId = key.substring(PREFS_PANEL_AUTH_PREFIX.length()); 1.78 + final boolean isAuthenticated = prefs.getBoolean(key, false); 1.79 + 1.80 + Log.d(LOGTAG, "Auth state changed: panelId=" + panelId + ", isAuthenticated=" + isAuthenticated); 1.81 + mChangeListener.onChange(panelId, isAuthenticated); 1.82 + } 1.83 + } 1.84 + } 1.85 +}