Use a custom dialog for enabling auto-sync

Latest mocks at:
https://docs.google.com/a/google.com/file/d/0B8v7jImPsDi-SXgzMjFhdjlCcjA/edit

Bug 9825835, 9016800

Change-Id: I6f6955f72fbab105ade03514dea79b0f238f4757
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 61fd7fa..721cc3c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -892,7 +892,7 @@
     <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>
+    <string name="tap_to_enable_sync">Touch to turn 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
@@ -918,4 +918,24 @@
     Outbox. [CHAR LIMIT=250] -->
     <string name="unsent_messages_in_outbox"><xliff:g id="number">%1$s</xliff:g>
     unsent in <xliff:g id="outbox">%2$s</xliff:g></string>
+
+    <!-- Title of dialog to turn auto-sync on [CHAR LIMIT=100] -->
+    <string name="turn_auto_sync_on_dialog_title">Turn auto-sync on?</string>
+
+    <!-- Text of dialog to turn auto-sync on [CHAR LIMIT=500] -->
+    <string name="turn_auto_sync_on_dialog_body">Changes you make to
+    all apps and accounts, not just Gmail, will be synchronized
+    between the web, your other devices, and your <xliff:g
+    id="phone_or_tablet">%1$s</xliff:g>.</string>
+
+    <!-- Phone (vs. tablet) for describing this device [CHAR LIMIT=30] -->
+    <string name="phone">phone</string>
+
+    <!-- Tablet (vs. phone) for describing this device [CHAR LIMIT=30] -->
+    <string name="tablet">tablet</string>
+
+    <!-- Confirm button text for dialog to turn auto-sync on [CHAR
+         LIMIT=30] -->
+    <string name="turn_auto_sync_on_dialog_confirm_btn">Turn on</string>
+
 </resources>
diff --git a/src/com/android/mail/ui/ConversationListHelper.java b/src/com/android/mail/ui/ConversationListHelper.java
index aa2a122..b55dd68 100644
--- a/src/com/android/mail/ui/ConversationListHelper.java
+++ b/src/com/android/mail/ui/ConversationListHelper.java
@@ -36,7 +36,7 @@
         final ConversationSyncDisabledTipView conversationSyncDisabledTipView =
                 (ConversationSyncDisabledTipView) LayoutInflater.from(context)
                         .inflate(R.layout.conversation_sync_disabled_tip_view, null);
-        conversationSyncDisabledTipView.bindAccount(account);
+        conversationSyncDisabledTipView.bindAccount(account, activity);
 
         final ConversationsInOutboxTipView conversationsInOutboxTipView =
                 (ConversationsInOutboxTipView) LayoutInflater.from(context)
diff --git a/src/com/android/mail/ui/ConversationSyncDisabledTipView.java b/src/com/android/mail/ui/ConversationSyncDisabledTipView.java
index be4cd9b..993c62b 100644
--- a/src/com/android/mail/ui/ConversationSyncDisabledTipView.java
+++ b/src/com/android/mail/ui/ConversationSyncDisabledTipView.java
@@ -19,6 +19,7 @@
 import android.animation.Animator;
 import android.animation.ObjectAnimator;
 import android.animation.Animator.AnimatorListener;
+import android.app.Activity;
 import android.app.LoaderManager;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -61,6 +62,7 @@
     private final MailPrefs mMailPrefs;
     private AccountPreferences mAccountPreferences;
     private AnimatedAdapter mAdapter;
+    private Activity mActivity;
 
     private View mSwipeableContent;
     private TextView mText1;
@@ -116,7 +118,9 @@
         mAutoSyncOffTextClickedListener = new OnClickListener() {
             @Override
             public void onClick(View v) {
-                openGlobalAutoSyncSettingDialog();
+                final TurnAutoSyncOnDialog dialog = TurnAutoSyncOnDialog.newInstance(
+                        mAccount.name, mAccount.type, mAccount.syncAuthority);
+                dialog.show(mActivity.getFragmentManager(), TurnAutoSyncOnDialog.DIALOG_TAG);
             }
         };
 
@@ -144,9 +148,10 @@
         mListCollapsible = resources.getBoolean(R.bool.list_collapsible);
     }
 
-    public void bindAccount(Account account) {
+    public void bindAccount(Account account, ControllableActivity activity) {
         mAccount = account;
         mAccountPreferences = AccountPreferences.get(getContext(), account.name);
+        mActivity = (Activity) activity;
     }
 
     @Override
@@ -413,11 +418,4 @@
             setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mAnimatedHeight);
         }
     }
-
-    private void openGlobalAutoSyncSettingDialog() {
-        final Intent intent = new Intent(Settings.ACTION_SYNC_SETTINGS);
-        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
-        intent.putExtra(Settings.EXTRA_AUTHORITIES, new String[] {mAccount.syncAuthority});
-        getContext().startActivity(intent);
-    }
 }
diff --git a/src/com/android/mail/ui/TurnAutoSyncOnDialog.java b/src/com/android/mail/ui/TurnAutoSyncOnDialog.java
new file mode 100644
index 0000000..28e0d3e
--- /dev/null
+++ b/src/com/android/mail/ui/TurnAutoSyncOnDialog.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mail.ui;
+
+import android.accounts.Account;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.content.ContentResolver;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.os.Bundle;
+
+import com.android.mail.R;
+import com.android.mail.utils.Utils;
+
+/**
+ * Confirmation dialog for turning global auto-sync setting on.
+ */
+public class TurnAutoSyncOnDialog extends DialogFragment {
+
+    private static final String ACCOUNT_NAME = "accountName";
+    private static final String ACCOUNT_TYPE = "accountType";
+    private static final String SYNC_AUTHORITY = "syncAuthority";
+
+    public static final String DIALOG_TAG = "auto sync";
+
+    // Public no-args constructor needed for fragment re-instantiation
+    public TurnAutoSyncOnDialog() {}
+
+    public static TurnAutoSyncOnDialog newInstance(String accountName, String accountType,
+            String syncAuthority) {
+        final TurnAutoSyncOnDialog frag = new TurnAutoSyncOnDialog();
+        final Bundle args = new Bundle(3);
+        args.putString(ACCOUNT_NAME, accountName);
+        args.putString(ACCOUNT_TYPE, accountType);
+        args.putString(SYNC_AUTHORITY, syncAuthority);
+        frag.setArguments(args);
+        return frag;
+    }
+
+    @Override
+    public Dialog onCreateDialog(Bundle savedInstanceState) {
+        final String accountName = getArguments().getString(ACCOUNT_NAME);
+        final String accountType = getArguments().getString(ACCOUNT_TYPE);
+        final String syncAuthority = getArguments().getString(SYNC_AUTHORITY);
+        final Resources resources = getResources();
+        final boolean isTablet = Utils.useTabletUI(resources);
+        final String bodyText = resources.getString(
+                R.string.turn_auto_sync_on_dialog_body,
+                resources.getString(isTablet ? R.string.tablet : R.string.phone));
+        return new AlertDialog.Builder(getActivity())
+                .setMessage(bodyText)
+                .setTitle(R.string.turn_auto_sync_on_dialog_title)
+                .setPositiveButton(R.string.turn_auto_sync_on_dialog_confirm_btn,
+                        new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(DialogInterface dialog, int whichButton) {
+                                // Turn on auto-sync
+                                ContentResolver.setMasterSyncAutomatically(true);
+                                // Since we're enabling auto-sync from within Gmail, should
+                                // almost always enable sync for Gmail as well:
+                                ContentResolver.setSyncAutomatically(
+                                        new Account(accountName, accountType),
+                                        syncAuthority,
+                                        true);
+                            }
+                        })
+                .setNegativeButton(R.string.cancel, null)
+                .create();
+    }
+}