1ce7f645638c56e097781664c101fac392b6072e1Alice Yang/* 2ce7f645638c56e097781664c101fac392b6072e1Alice Yang * Copyright (C) 2013 The Android Open Source Project 3ce7f645638c56e097781664c101fac392b6072e1Alice Yang * 4ce7f645638c56e097781664c101fac392b6072e1Alice Yang * Licensed under the Apache License, Version 2.0 (the "License"); 5ce7f645638c56e097781664c101fac392b6072e1Alice Yang * you may not use this file except in compliance with the License. 6ce7f645638c56e097781664c101fac392b6072e1Alice Yang * You may obtain a copy of the License at 7ce7f645638c56e097781664c101fac392b6072e1Alice Yang * 8ce7f645638c56e097781664c101fac392b6072e1Alice Yang * http://www.apache.org/licenses/LICENSE-2.0 9ce7f645638c56e097781664c101fac392b6072e1Alice Yang * 10ce7f645638c56e097781664c101fac392b6072e1Alice Yang * Unless required by applicable law or agreed to in writing, software 11ce7f645638c56e097781664c101fac392b6072e1Alice Yang * distributed under the License is distributed on an "AS IS" BASIS, 12ce7f645638c56e097781664c101fac392b6072e1Alice Yang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ce7f645638c56e097781664c101fac392b6072e1Alice Yang * See the License for the specific language governing permissions and 14ce7f645638c56e097781664c101fac392b6072e1Alice Yang * limitations under the License. 15ce7f645638c56e097781664c101fac392b6072e1Alice Yang */ 16ce7f645638c56e097781664c101fac392b6072e1Alice Yang 17ce7f645638c56e097781664c101fac392b6072e1Alice Yangpackage com.android.mail.ui; 18ce7f645638c56e097781664c101fac392b6072e1Alice Yang 19ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.accounts.Account; 20ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.app.AlertDialog; 21ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.app.Dialog; 22ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.app.DialogFragment; 23ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.content.ContentResolver; 24ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.content.DialogInterface; 25ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.content.res.Resources; 26ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport android.os.Bundle; 279783bc4d981acd042314a9194c9ef8b693877811Alice Yangimport android.text.TextUtils; 28ce7f645638c56e097781664c101fac392b6072e1Alice Yang 29ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport com.android.mail.R; 30ce7f645638c56e097781664c101fac392b6072e1Alice Yangimport com.android.mail.utils.Utils; 31ce7f645638c56e097781664c101fac392b6072e1Alice Yang 32ce7f645638c56e097781664c101fac392b6072e1Alice Yang/** 33ce7f645638c56e097781664c101fac392b6072e1Alice Yang * Confirmation dialog for turning global auto-sync setting on. 34ce7f645638c56e097781664c101fac392b6072e1Alice Yang */ 35ce7f645638c56e097781664c101fac392b6072e1Alice Yangpublic class TurnAutoSyncOnDialog extends DialogFragment { 36ce7f645638c56e097781664c101fac392b6072e1Alice Yang 373e16687b074895bc6cc63a1ed93abf7033586fd2Tony Mantler private static final String ACCOUNT = "account"; 38ce7f645638c56e097781664c101fac392b6072e1Alice Yang private static final String SYNC_AUTHORITY = "syncAuthority"; 39ce7f645638c56e097781664c101fac392b6072e1Alice Yang 40ce7f645638c56e097781664c101fac392b6072e1Alice Yang public static final String DIALOG_TAG = "auto sync"; 41ce7f645638c56e097781664c101fac392b6072e1Alice Yang 429783bc4d981acd042314a9194c9ef8b693877811Alice Yang private static String sDefaultSyncAuthority; 439783bc4d981acd042314a9194c9ef8b693877811Alice Yang 449783bc4d981acd042314a9194c9ef8b693877811Alice Yang // Should be called once per app. 459783bc4d981acd042314a9194c9ef8b693877811Alice Yang static public void setDefaultSyncAuthority(String defaultSyncAuthority) { 469783bc4d981acd042314a9194c9ef8b693877811Alice Yang sDefaultSyncAuthority = defaultSyncAuthority; 479783bc4d981acd042314a9194c9ef8b693877811Alice Yang } 489783bc4d981acd042314a9194c9ef8b693877811Alice Yang 4945347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang public interface TurnAutoSyncOnDialogListener { 5045347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang void onEnableAutoSync(); 5145347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang void onCancelAutoSync(); 5245347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang } 5345347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang 5445347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang private TurnAutoSyncOnDialogListener mListener; 5545347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang 56ce7f645638c56e097781664c101fac392b6072e1Alice Yang // Public no-args constructor needed for fragment re-instantiation 57ce7f645638c56e097781664c101fac392b6072e1Alice Yang public TurnAutoSyncOnDialog() {} 58ce7f645638c56e097781664c101fac392b6072e1Alice Yang 593e16687b074895bc6cc63a1ed93abf7033586fd2Tony Mantler public static TurnAutoSyncOnDialog newInstance(Account account, 60ce7f645638c56e097781664c101fac392b6072e1Alice Yang String syncAuthority) { 61ce7f645638c56e097781664c101fac392b6072e1Alice Yang final TurnAutoSyncOnDialog frag = new TurnAutoSyncOnDialog(); 62ce7f645638c56e097781664c101fac392b6072e1Alice Yang final Bundle args = new Bundle(3); 633e16687b074895bc6cc63a1ed93abf7033586fd2Tony Mantler args.putParcelable(ACCOUNT, account); 64ce7f645638c56e097781664c101fac392b6072e1Alice Yang args.putString(SYNC_AUTHORITY, syncAuthority); 65ce7f645638c56e097781664c101fac392b6072e1Alice Yang frag.setArguments(args); 66ce7f645638c56e097781664c101fac392b6072e1Alice Yang return frag; 67ce7f645638c56e097781664c101fac392b6072e1Alice Yang } 68ce7f645638c56e097781664c101fac392b6072e1Alice Yang 69ce7f645638c56e097781664c101fac392b6072e1Alice Yang @Override 70ce7f645638c56e097781664c101fac392b6072e1Alice Yang public Dialog onCreateDialog(Bundle savedInstanceState) { 713e16687b074895bc6cc63a1ed93abf7033586fd2Tony Mantler final Account account = getArguments().getParcelable(ACCOUNT); 72ce7f645638c56e097781664c101fac392b6072e1Alice Yang final String syncAuthority = getArguments().getString(SYNC_AUTHORITY); 73ce7f645638c56e097781664c101fac392b6072e1Alice Yang final Resources resources = getResources(); 74ce7f645638c56e097781664c101fac392b6072e1Alice Yang final boolean isTablet = Utils.useTabletUI(resources); 75ce7f645638c56e097781664c101fac392b6072e1Alice Yang final String bodyText = resources.getString( 76ce7f645638c56e097781664c101fac392b6072e1Alice Yang R.string.turn_auto_sync_on_dialog_body, 77ce7f645638c56e097781664c101fac392b6072e1Alice Yang resources.getString(isTablet ? R.string.tablet : R.string.phone)); 78ce7f645638c56e097781664c101fac392b6072e1Alice Yang return new AlertDialog.Builder(getActivity()) 79ce7f645638c56e097781664c101fac392b6072e1Alice Yang .setMessage(bodyText) 80ce7f645638c56e097781664c101fac392b6072e1Alice Yang .setTitle(R.string.turn_auto_sync_on_dialog_title) 81ce7f645638c56e097781664c101fac392b6072e1Alice Yang .setPositiveButton(R.string.turn_auto_sync_on_dialog_confirm_btn, 82ce7f645638c56e097781664c101fac392b6072e1Alice Yang new DialogInterface.OnClickListener() { 83ce7f645638c56e097781664c101fac392b6072e1Alice Yang @Override 84ce7f645638c56e097781664c101fac392b6072e1Alice Yang public void onClick(DialogInterface dialog, int whichButton) { 85ce7f645638c56e097781664c101fac392b6072e1Alice Yang // Turn on auto-sync 86ce7f645638c56e097781664c101fac392b6072e1Alice Yang ContentResolver.setMasterSyncAutomatically(true); 879783bc4d981acd042314a9194c9ef8b693877811Alice Yang // Also enable auto-sync for Gmail. 889783bc4d981acd042314a9194c9ef8b693877811Alice Yang // Note it's possible for syncAuthority to be empty on the 899783bc4d981acd042314a9194c9ef8b693877811Alice Yang // account (used for constructing this dialog) was 909783bc4d981acd042314a9194c9ef8b693877811Alice Yang // cached from shared prefs. This can happen during app upgrade. 919783bc4d981acd042314a9194c9ef8b693877811Alice Yang // As work around use default value set by app. 929783bc4d981acd042314a9194c9ef8b693877811Alice Yang final String authority = TextUtils.isEmpty(syncAuthority) ? 939783bc4d981acd042314a9194c9ef8b693877811Alice Yang sDefaultSyncAuthority : syncAuthority; 949783bc4d981acd042314a9194c9ef8b693877811Alice Yang if (!TextUtils.isEmpty(authority)) { 959783bc4d981acd042314a9194c9ef8b693877811Alice Yang ContentResolver.setSyncAutomatically( 969783bc4d981acd042314a9194c9ef8b693877811Alice Yang account, 979783bc4d981acd042314a9194c9ef8b693877811Alice Yang authority, 989783bc4d981acd042314a9194c9ef8b693877811Alice Yang true); 999783bc4d981acd042314a9194c9ef8b693877811Alice Yang if (mListener != null) { 1009783bc4d981acd042314a9194c9ef8b693877811Alice Yang mListener.onEnableAutoSync(); 1019783bc4d981acd042314a9194c9ef8b693877811Alice Yang } 10245347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang } 103ce7f645638c56e097781664c101fac392b6072e1Alice Yang } 104ce7f645638c56e097781664c101fac392b6072e1Alice Yang }) 10545347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 10645347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang @Override 10745347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang public void onClick(DialogInterface dialog, int which) { 10845347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang if (mListener != null) { 10945347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang mListener.onCancelAutoSync(); 11045347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang } 11145347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang } 11245347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang }) 113ce7f645638c56e097781664c101fac392b6072e1Alice Yang .create(); 114ce7f645638c56e097781664c101fac392b6072e1Alice Yang } 11545347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang 11645347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang public void setListener(final TurnAutoSyncOnDialogListener listener) { 11745347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang mListener = listener; 11845347d0d44374faf8ba7884fd69da2cb07576cb4Alice Yang } 119ce7f645638c56e097781664c101fac392b6072e1Alice Yang} 120