1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mail.ui; 18 19 import android.app.Activity; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.res.Resources; 23 import android.text.Html; 24 import android.text.Spannable; 25 import android.text.SpannableString; 26 import android.text.Spanned; 27 import android.text.TextUtils; 28 import android.text.style.TextAppearanceSpan; 29 import android.util.Log; 30 import android.view.View; 31 32 import com.android.mail.R; 33 import com.android.mail.analytics.Analytics; 34 import com.android.mail.browse.ConversationCursor; 35 import com.android.mail.preferences.AccountPreferences; 36 import com.android.mail.preferences.MailPrefs; 37 import com.android.mail.providers.Account; 38 import com.android.mail.providers.Folder; 39 import com.android.mail.utils.LogTag; 40 import com.android.mail.utils.LogUtils; 41 import com.android.mail.utils.StyleUtils; 42 import com.android.mail.utils.Utils; 43 44 /** 45 * A tip displayed on top of conversation view to indicate that Gmail sync is 46 * currently disabled on this account. 47 */ 48 public class ConversationSyncDisabledTipView extends ConversationTipView { 49 public static final String LOG_TAG = LogTag.getLogTag(); 50 private Account mAccount = null; 51 private Folder mFolder = null; 52 private final MailPrefs mMailPrefs; 53 private AccountPreferences mAccountPreferences; 54 private Activity mActivity; 55 56 private int mReasonSyncOff = ReasonSyncOff.NONE; 57 58 public interface ReasonSyncOff { 59 // Background sync is enabled for current account, do not display this tip 60 public static final int NONE = 0; 61 // Global auto-sync (affects all apps and all accounts) is turned off 62 public static final int AUTO_SYNC_OFF = 1; 63 // Global auto-sync is on, but Gmail app level sync is disabled for this particular account 64 public static final int ACCOUNT_SYNC_OFF = 2; 65 } 66 67 public ConversationSyncDisabledTipView(final Context context) { 68 super(context); 69 70 mMailPrefs = MailPrefs.get(context); 71 } 72 73 @Override 74 protected OnClickListener getTextAreaOnClickListener() { 75 return new OnClickListener() { 76 @Override 77 public void onClick(View view) { 78 if (mReasonSyncOff == ReasonSyncOff.AUTO_SYNC_OFF) { 79 final TurnAutoSyncOnDialog dialog = TurnAutoSyncOnDialog.newInstance( 80 mAccount.getAccountManagerAccount(), mAccount.syncAuthority); 81 dialog.show(mActivity.getFragmentManager(), TurnAutoSyncOnDialog.DIALOG_TAG); 82 } else if (mReasonSyncOff == ReasonSyncOff.ACCOUNT_SYNC_OFF) { 83 Utils.showAccountSettings(getContext(), mAccount); 84 } 85 } 86 }; 87 } 88 89 public void bindAccount(Account account, ControllableActivity activity) { 90 mAccount = account; 91 mAccountPreferences = AccountPreferences.get(getContext(), account); 92 mActivity = (Activity) activity; 93 } 94 95 @Override 96 public void onUpdate(Folder folder, ConversationCursor cursor) { 97 mFolder = folder; 98 } 99 100 @Override 101 public boolean getShouldDisplayInList() { 102 if (mAccount == null || mAccount.syncAuthority == null) { 103 return false; 104 } 105 106 // Do not show this message for folders/labels that are not set to sync. 107 if (mFolder == null || mFolder.syncWindow <= 0) { 108 return false; 109 } 110 111 setReasonSyncOff(calculateReasonSyncOff(mMailPrefs, mAccount, mAccountPreferences)); 112 113 if (mReasonSyncOff != ReasonSyncOff.NONE) { 114 LogUtils.i(LOG_TAG, "Sync is off with reason %d", mReasonSyncOff); 115 } 116 117 switch (mReasonSyncOff) { 118 case ReasonSyncOff.AUTO_SYNC_OFF: 119 return (mMailPrefs.getNumOfDismissesForAutoSyncOff() == 0); 120 case ReasonSyncOff.ACCOUNT_SYNC_OFF: 121 return (mAccountPreferences.getNumOfDismissesForAccountSyncOff() == 0); 122 default: 123 return false; 124 } 125 } 126 127 public static int calculateReasonSyncOff(MailPrefs mailPrefs, 128 Account account, AccountPreferences accountPreferences) { 129 if (!ContentResolver.getMasterSyncAutomatically()) { 130 // Global sync is turned off 131 accountPreferences.resetNumOfDismissesForAccountSyncOff(); 132 // Logging to track down bug where this tip is being showing when it shouldn't be. 133 LogUtils.i(LOG_TAG, "getMasterSyncAutomatically() return false"); 134 return ReasonSyncOff.AUTO_SYNC_OFF; 135 } else { 136 // Global sync is on, clear the number of times users has dismissed this 137 // warning so that next time global sync is off, warning gets displayed again. 138 mailPrefs.resetNumOfDismissesForAutoSyncOff(); 139 140 // Now check for whether account level sync is on/off. 141 android.accounts.Account acct = account.getAccountManagerAccount(); 142 if (!TextUtils.isEmpty(account.syncAuthority) && 143 !ContentResolver.getSyncAutomatically(acct, account.syncAuthority)) { 144 // Account level sync is off 145 return ReasonSyncOff.ACCOUNT_SYNC_OFF; 146 } else { 147 // Account sync is on, clear the number of times users has dismissed this 148 // warning so that next time sync is off, warning gets displayed again. 149 accountPreferences.resetNumOfDismissesForAccountSyncOff(); 150 return ReasonSyncOff.NONE; 151 } 152 } 153 } 154 155 private void setReasonSyncOff(int reason) { 156 if (mReasonSyncOff != reason) { 157 mReasonSyncOff = reason; 158 final Resources resources = getResources(); 159 switch (mReasonSyncOff) { 160 case ReasonSyncOff.AUTO_SYNC_OFF: 161 setText(resources.getString(R.string.auto_sync_off)); 162 break; 163 case ReasonSyncOff.ACCOUNT_SYNC_OFF: 164 // Create the "Turn on in Account settings." text where "Account settings" appear as 165 // a blue link. 166 Spannable accountSyncOff = new SpannableString( 167 Html.fromHtml(resources.getString(R.string.account_sync_off))); 168 StyleUtils.stripUnderlinesAndLinkUrls(accountSyncOff, null); 169 setText(accountSyncOff); 170 break; 171 default: 172 } 173 } 174 } 175 176 @Override 177 public void dismiss() { 178 final String reason; 179 switch (mReasonSyncOff) { 180 case ReasonSyncOff.AUTO_SYNC_OFF: 181 mMailPrefs.incNumOfDismissesForAutoSyncOff(); 182 reason = "auto_sync_off"; 183 break; 184 case ReasonSyncOff.ACCOUNT_SYNC_OFF: 185 mAccountPreferences.incNumOfDismissesForAccountSyncOff(); 186 reason = "account_sync_off"; 187 break; 188 default: 189 reason = null; 190 break; 191 } 192 Analytics.getInstance().sendEvent("list_swipe", "sync_disabled_tip", reason, 0); 193 super.dismiss(); 194 } 195 }