Sync is off message fixes

- Link to account setting instead of top level setting for enabling sync
- Do not show any of these "sync is off" messages in folders/labels that are not set to sync
- Some wording tweaks and font color color change

Bug 9825835

Change-Id: I1c0ff33daa049913d6222e109c4e501127eecb53
diff --git a/res/layout/conversation_sync_disabled_tip_view.xml b/res/layout/conversation_sync_disabled_tip_view.xml
index 4cc3556..339c1ec 100644
--- a/res/layout/conversation_sync_disabled_tip_view.xml
+++ b/res/layout/conversation_sync_disabled_tip_view.xml
@@ -14,6 +14,7 @@
         android:orientation="horizontal" >
 
         <LinearLayout
+            android:id="@+id/text_area"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:layout_weight="1"
@@ -35,7 +36,6 @@
                 android:layout_height="wrap_content"
                 android:duplicateParentState="true"
                 android:fontFamily="sans-serif-light"
-                android:text="@string/tap_to_enable_sync"
                 android:textColor="@color/teaser_main_text"
                 android:textSize="16sp" />
 
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9ac321f..f97fe0e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -982,6 +982,9 @@
         receiving any new mail. [CHAR LIMIT=250] -->
     <string name="auto_sync_off">Auto-sync is off.</string>
 
+    <!-- Tap to turn on device auto-sync setting.  [CHAR LIMIT=250] -->
+    <string name="tap_to_enable_sync">Tap to turn auto-sync on.</string>
+
     <!-- Tip for letting user know that their account level sync setting
         is turned off, in case they are wondering why they are not
         receiving any new mail. [CHAR LIMIT=250] -->
@@ -992,6 +995,13 @@
         receiving any new mail. [CHAR LIMIT=250] -->
     <string name="airplane_mode_on">Airplane mode is on</string>
 
-    <!-- Tap to turn on sync in settings.  [CHAR LIMIT=250] -->
-    <string name="tap_to_enable_sync" translatable="false">Tap to turn sync on.</string>
+    <!-- Hint text for user to enable sync in Gmail's account settings.
+         The whole string should read "Turn on in Account settings.", but because we need
+         "Account settings" to appear as blue link text, it's a parameter here.
+         [CHAR LIMIT=250] -->
+    <string name="enable_sync_in_account_settings">Turn on in <xliff:g id="account_settings">%1$s</xliff:g>.</string>
+
+    <!-- This is used as a parameter to another string and combined reads
+         "Turn on in Account settings." [CHAR LIMIT=250] -->
+    <string name="account_settings_param">Account settings</string>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 98017ca..781bf10 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -491,4 +491,8 @@
     <style name="SendingTextAppearance" parent="@style/DraftTextAppearance">
     </style>
 
+    <style name="LinksInTipTextAppearance">
+        <item name="android:textColor">?android:attr/textColorLink</item>
+    </style>
+
 </resources>
diff --git a/src/com/android/mail/providers/Folder.java b/src/com/android/mail/providers/Folder.java
index 4829189..0262e06 100644
--- a/src/com/android/mail/providers/Folder.java
+++ b/src/com/android/mail/providers/Folder.java
@@ -55,9 +55,6 @@
     @Deprecated
     private static final Pattern SPLITTER_REGEX = Pattern.compile("\\^\\*\\^");
 
-    /**
-     *
-     */
     private static final String FOLDER_UNINITIALIZED = "Uninitialized!";
 
     // TODO: remove this once we figure out which folder is returning a "null" string as the
diff --git a/src/com/android/mail/ui/ConversationSyncDisabledTipView.java b/src/com/android/mail/ui/ConversationSyncDisabledTipView.java
index 747cb6d..8593f14 100644
--- a/src/com/android/mail/ui/ConversationSyncDisabledTipView.java
+++ b/src/com/android/mail/ui/ConversationSyncDisabledTipView.java
@@ -23,6 +23,9 @@
 import android.content.Intent;
 import android.content.res.Resources;
 import android.provider.Settings;
+import android.text.SpannableString;
+import android.text.SpannableStringBuilder;
+import android.text.style.TextAppearanceSpan;
 import android.util.AttributeSet;
 import android.view.View;
 import android.view.animation.DecelerateInterpolator;
@@ -53,6 +56,7 @@
     private static int sShrinkAnimationDuration;
 
     private Account mAccount = null;
+    private Folder mFolder = null;
     private final MailPrefs mMailPrefs;
     private AccountPreferences mAccountPreferences;
     private AnimatedAdapter mAdapter;
@@ -60,11 +64,12 @@
     private View mSwipeableContent;
     private TextView mText1;
     private TextView mText2;
+    private View mTextArea;
+    private SpannableString mEnableSyncInAccountSettingsText;
     private final OnClickListener mAutoSyncOffTextClickedListener;
     private final OnClickListener mAccountSyncOffTextClickedListener;
 
     private int mAnimatedHeight = -1;
-    private boolean mAcceptUserTaps = false;
 
     private int mReasonSyncOff = ReasonSyncOff.NONE;
 
@@ -111,10 +116,22 @@
         mAccountSyncOffTextClickedListener = new OnClickListener() {
             @Override
             public void onClick(View v) {
-                // TODO: Link to account level settings instead of top level settings.
-                Utils.showSettings(getContext(), mAccount);
+                Utils.showAccountSettings(getContext(), mAccount);
             }
         };
+
+        // Create the "Turn on in Account settings." text where "Account settings" appear as
+        // a blue link.
+        final String subString = resources.getString(R.string.account_settings_param);
+        final String entireString = resources.getString(
+                R.string.enable_sync_in_account_settings, subString);
+        mEnableSyncInAccountSettingsText = new SpannableString(entireString);
+        final int index = entireString.indexOf(subString);
+        mEnableSyncInAccountSettingsText.setSpan(
+                new TextAppearanceSpan(context, R.style.LinksInTipTextAppearance),
+                index,
+                index + subString.length(),
+                0);
     }
 
     public void bindAccount(Account account) {
@@ -133,6 +150,7 @@
 
         mText1 = (TextView) findViewById(R.id.text_line1);
         mText2 = (TextView) findViewById(R.id.text_line2);
+        mTextArea = findViewById(R.id.text_area);
 
         findViewById(R.id.dismiss_button).setOnClickListener(new OnClickListener() {
             @Override
@@ -144,7 +162,7 @@
 
     @Override
     public void onUpdate(String account, Folder folder, ConversationCursor cursor) {
-        // do nothing
+        mFolder = folder;
     }
 
     @Override
@@ -153,8 +171,10 @@
             return false;
         }
 
-        // TODO: do not show this message for folders/labels that are not set to sync.
-        // We need a solution that works for both Gmail and Email.
+        // Do not show this message for folders/labels that are not set to sync.
+        if (mFolder == null || mFolder.syncWindow <= 0) {
+            return false;
+        }
 
         setReasonSyncOff(calculateReasonSyncOff(
                 getContext(), mMailPrefs, mAccount, mAccountPreferences));
@@ -181,6 +201,8 @@
             // Global sync is turned off
             accountPreferences.resetNumOfDismissesForAccountSyncOff();
             mailPrefs.resetNumOfDismissesForAirplaneModeOn();
+            // Logging to track down bug where this tip is being showing when it shouldn't be.
+            LogUtils.i(LOG_TAG, "getMasterSyncAutomatically() return false");
             return ReasonSyncOff.AUTO_SYNC_OFF;
         } else {
             // Global sync is on, clear the number of times users has dismissed this
@@ -194,6 +216,9 @@
             if (!ContentResolver.getSyncAutomatically(acct, account.syncAuthority)) {
                 // Account level sync is off
                 mailPrefs.resetNumOfDismissesForAirplaneModeOn();
+                // Logging to track down bug where this tip is showing when it shouldn't be.
+                LogUtils.i(LOG_TAG, "getSyncAutomatically() return false for %s %s %s",
+                        account.name, account.type, account.syncAuthority);
                 return ReasonSyncOff.ACCOUNT_SYNC_OFF;
             } else {
                 // Account sync is on, clear the number of times users has dismissed this
@@ -217,20 +242,22 @@
             switch (mReasonSyncOff) {
                 case ReasonSyncOff.AUTO_SYNC_OFF:
                     mText1.setText(R.string.auto_sync_off);
-                    mText2.setClickable(true);
+                    mText2.setText(R.string.tap_to_enable_sync);
                     mText2.setVisibility(View.VISIBLE);
-                    mText2.setOnClickListener(mAutoSyncOffTextClickedListener);
+                    mTextArea.setClickable(true);
+                    mTextArea.setOnClickListener(mAutoSyncOffTextClickedListener);
                     break;
                 case ReasonSyncOff.ACCOUNT_SYNC_OFF:
                     mText1.setText(R.string.account_sync_off);
-                    mText2.setClickable(true);
+                    mText2.setText(mEnableSyncInAccountSettingsText);
                     mText2.setVisibility(View.VISIBLE);
-                    mText2.setOnClickListener(mAccountSyncOffTextClickedListener);
+                    mTextArea.setClickable(true);
+                    mTextArea.setOnClickListener(mAccountSyncOffTextClickedListener);
                     break;
                 case ReasonSyncOff.AIRPLANE_MODE_ON:
                     mText1.setText(R.string.airplane_mode_on);
-                    mText2.setClickable(false);
                     mText2.setVisibility(View.GONE);
+                    mTextArea.setClickable(false);
                     break;
                 default:
                     // Doesn't matter what mText is since this view is not displayed
@@ -270,7 +297,7 @@
 
     @Override
     public boolean acceptsUserTaps() {
-        return mAcceptUserTaps;
+        return true;
     }
 
     @Override
diff --git a/src/com/android/mail/utils/Utils.java b/src/com/android/mail/utils/Utils.java
index be57c51..620234a 100644
--- a/src/com/android/mail/utils/Utils.java
+++ b/src/com/android/mail/utils/Utils.java
@@ -852,7 +852,7 @@
     }
 
     /**
-     * Show the settings screen for the supplied account.
+     * Show the top level settings screen for the supplied account.
      */
     public static void showSettings(Context context, Account account) {
         if (account == null) {
@@ -865,6 +865,22 @@
     }
 
     /**
+     * Show the account level settings screen for the supplied account.
+     */
+    public static void showAccountSettings(Context context, Account account) {
+        if (account == null) {
+            LogUtils.e(LOG_TAG, "Invalid attempt to show setting screen with null account");
+            return;
+        }
+        final Intent settingsIntent = new Intent(Intent.ACTION_EDIT,
+                appendVersionQueryParameter(context, account.settingsIntentUri));
+
+        settingsIntent.putExtra(EditSettingsExtras.EXTRA_ACCOUNT, account);
+        settingsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
+        context.startActivity(settingsIntent);
+    }
+
+    /**
      * Show the settings screen for the supplied account.
      */
      public static void showFolderSettings(Context context, Account account, Folder folder) {