mobile/android/base/home/PanelAuthCache.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     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/. */
     6 package org.mozilla.gecko.home;
     8 import android.content.Context;
     9 import android.content.SharedPreferences;
    10 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
    11 import android.util.Log;
    13 import org.mozilla.gecko.GeckoSharedPrefs;
    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";
    26     // Keep this in sync with the constant defined in Home.jsm
    27     private static final String PREFS_PANEL_AUTH_PREFIX = "home_panels_auth_";
    29     private final Context mContext;
    30     private SharedPrefsListener mSharedPrefsListener;
    31     private OnChangeListener mChangeListener;
    33     public interface OnChangeListener {
    34         public void onChange(String panelId, boolean isAuthenticated);
    35     }
    37     public PanelAuthCache(Context context) {
    38         mContext = context;
    39     }
    41     private SharedPreferences getSharedPreferences() {
    42         return GeckoSharedPrefs.forProfile(mContext);
    43     }
    45     private String getPanelAuthKey(String panelId) {
    46         return PREFS_PANEL_AUTH_PREFIX + panelId;
    47     }
    49     public boolean isAuthenticated(String panelId) {
    50         final SharedPreferences prefs = getSharedPreferences();
    51         return prefs.getBoolean(getPanelAuthKey(panelId), false);
    52     }
    54     public void setOnChangeListener(OnChangeListener listener) {
    55         final SharedPreferences prefs = getSharedPreferences();
    57         if (mChangeListener != null) {
    58             prefs.unregisterOnSharedPreferenceChangeListener(mSharedPrefsListener);
    59             mSharedPrefsListener = null;
    60         }
    62         mChangeListener = listener;
    64         if (mChangeListener != null) {
    65             mSharedPrefsListener = new SharedPrefsListener();
    66             prefs.registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
    67         }
    68     }
    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);
    77                 Log.d(LOGTAG, "Auth state changed: panelId=" + panelId + ", isAuthenticated=" + isAuthenticated);
    78                 mChangeListener.onChange(panelId, isAuthenticated);
    79             }
    80         }
    81     }
    82 }

mercurial