OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.enhancedbookmarks; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.Intent; |
| 9 import android.content.SharedPreferences; |
| 10 import android.preference.PreferenceManager; |
| 11 import android.support.v7.widget.RecyclerView; |
| 12 import android.support.v7.widget.RecyclerView.ViewHolder; |
| 13 import android.view.LayoutInflater; |
| 14 import android.view.View; |
| 15 import android.view.View.OnClickListener; |
| 16 import android.view.ViewGroup; |
| 17 |
| 18 import com.google.android.apps.chrome.R; |
| 19 |
| 20 import org.chromium.base.metrics.RecordUserAction; |
| 21 import org.chromium.chrome.browser.signin.SigninManager; |
| 22 import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver; |
| 23 import org.chromium.sync.AndroidSyncSettings; |
| 24 import org.chromium.sync.AndroidSyncSettings.AndroidSyncSettingsObserver; |
| 25 import org.chromium.sync.signin.ChromeSigninController; |
| 26 |
| 27 /** |
| 28 * Class that manages all the logic and UI behind the signin promo header in the
enhanced bookmark |
| 29 * content UI. The header is shown only on certain situations, (e.g., not signed
in). |
| 30 */ |
| 31 class EnhancedBookmarkPromoHeader implements AndroidSyncSettingsObserver, |
| 32 SignInStateObserver { |
| 33 /** |
| 34 * Interface to listen signin promo header visibility changes. |
| 35 */ |
| 36 interface PromoHeaderShowingChangeListener { |
| 37 /** |
| 38 * Called when signin promo header visibility is changed. |
| 39 * @param isShowing Whether it should be showing. |
| 40 */ |
| 41 void onPromoHeaderShowingChanged(boolean isShowing); |
| 42 } |
| 43 |
| 44 private static final String PREF_SIGNIN_PROMO_DECLINED = |
| 45 "enhanced_bookmark_signin_promo_declined"; |
| 46 |
| 47 private Context mContext; |
| 48 private SigninManager mSignInManager; |
| 49 private boolean mIsShowing; |
| 50 private PromoHeaderShowingChangeListener mShowingChangeListener; |
| 51 |
| 52 /** |
| 53 * Initializes the class. Note that this will start listening to signin rela
ted events and |
| 54 * update itself if needed. |
| 55 */ |
| 56 EnhancedBookmarkPromoHeader(Context context, |
| 57 PromoHeaderShowingChangeListener showingChangeListener) { |
| 58 mContext = context; |
| 59 mShowingChangeListener = showingChangeListener; |
| 60 |
| 61 AndroidSyncSettings.registerObserver(mContext, this); |
| 62 |
| 63 mSignInManager = SigninManager.get(mContext); |
| 64 mSignInManager.addSignInStateObserver(this); |
| 65 |
| 66 updateShowing(false); |
| 67 if (isShowing()) RecordUserAction.record("Stars_SignInPromoHeader_Displa
yed"); |
| 68 } |
| 69 |
| 70 /** |
| 71 * Clean ups the class. Must be called once done using this class. |
| 72 */ |
| 73 void destroy() { |
| 74 AndroidSyncSettings.unregisterObserver(mContext, this); |
| 75 |
| 76 mSignInManager.removeSignInStateObserver(this); |
| 77 mSignInManager = null; |
| 78 } |
| 79 |
| 80 /** |
| 81 * @return Whether it should be showing. |
| 82 */ |
| 83 boolean isShowing() { |
| 84 return mIsShowing; |
| 85 } |
| 86 |
| 87 /** |
| 88 * @param isListMode Whether listview is showing. |
| 89 * @return Signin promo header {@link ViewHolder} instance that can be used
with |
| 90 * {@link RecyclerView}. |
| 91 */ |
| 92 ViewHolder createHolder(ViewGroup parent, boolean isListMode) { |
| 93 ViewGroup promoHeader = (ViewGroup) LayoutInflater.from(mContext) |
| 94 .inflate(R.layout.eb_promo_header, parent, false); |
| 95 if (isListMode) promoHeader.setBackground(null); |
| 96 |
| 97 promoHeader.findViewById(R.id.no_thanks).setOnClickListener(new OnClickL
istener() { |
| 98 @Override |
| 99 public void onClick(View view) { |
| 100 RecordUserAction.record("Stars_SignInPromoHeader_Dismissed"); |
| 101 setSigninPromoDeclined(); |
| 102 updateShowing(true); |
| 103 } |
| 104 }); |
| 105 |
| 106 promoHeader.findViewById(R.id.sign_in).setOnClickListener(new OnClickLis
tener() { |
| 107 @Override |
| 108 public void onClick(View view) { |
| 109 mContext.startActivity(new Intent(mContext, EnhancedBookmarkSign
inActivity.class)); |
| 110 } |
| 111 }); |
| 112 |
| 113 return new ViewHolder(promoHeader) {}; |
| 114 } |
| 115 |
| 116 /** |
| 117 * @return Whether user tapped "No" button on the signin promo header. |
| 118 */ |
| 119 private boolean wasSigninPromoDeclined() { |
| 120 return PreferenceManager.getDefaultSharedPreferences(mContext).getBoolea
n( |
| 121 PREF_SIGNIN_PROMO_DECLINED, false); |
| 122 } |
| 123 |
| 124 /** |
| 125 * Save that user tapped "No" button on the signin promo header. |
| 126 */ |
| 127 private void setSigninPromoDeclined() { |
| 128 SharedPreferences.Editor sharedPreferencesEditor = |
| 129 PreferenceManager.getDefaultSharedPreferences(mContext).edit(); |
| 130 sharedPreferencesEditor.putBoolean(PREF_SIGNIN_PROMO_DECLINED, true); |
| 131 sharedPreferencesEditor.apply(); |
| 132 } |
| 133 |
| 134 private void updateShowing(boolean notifyUI) { |
| 135 boolean oldIsShowing = mIsShowing; |
| 136 mIsShowing = AndroidSyncSettings.isMasterSyncEnabled(mContext) |
| 137 && !ChromeSigninController.get(mContext).isSignedIn() |
| 138 && !wasSigninPromoDeclined(); |
| 139 |
| 140 if (oldIsShowing != mIsShowing && notifyUI) { |
| 141 mShowingChangeListener.onPromoHeaderShowingChanged(mIsShowing); |
| 142 } |
| 143 } |
| 144 |
| 145 // AndroidSyncSettingsObserver implementation |
| 146 |
| 147 @Override |
| 148 public void androidSyncSettingsChanged() { |
| 149 updateShowing(true); |
| 150 } |
| 151 |
| 152 // SignInStateObserver implementations |
| 153 |
| 154 @Override |
| 155 public void onSignedIn() { |
| 156 updateShowing(true); |
| 157 } |
| 158 |
| 159 @Override |
| 160 public void onSignedOut() { |
| 161 updateShowing(true); |
| 162 } |
| 163 } |
OLD | NEW |