• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.accounts.Account;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.content.ContentResolver;
24 import android.content.DialogInterface;
25 import android.content.res.Resources;
26 import android.os.Bundle;
27 import android.text.TextUtils;
28 
29 import com.android.mail.R;
30 import com.android.mail.utils.Utils;
31 
32 /**
33  * Confirmation dialog for turning global auto-sync setting on.
34  */
35 public class TurnAutoSyncOnDialog extends DialogFragment {
36 
37     private static final String ACCOUNT = "account";
38     private static final String SYNC_AUTHORITY = "syncAuthority";
39 
40     public static final String DIALOG_TAG = "auto sync";
41 
42     private static String sDefaultSyncAuthority;
43 
44     // Should be called once per app.
setDefaultSyncAuthority(String defaultSyncAuthority)45     static public void setDefaultSyncAuthority(String defaultSyncAuthority) {
46         sDefaultSyncAuthority = defaultSyncAuthority;
47     }
48 
49     public interface TurnAutoSyncOnDialogListener {
onEnableAutoSync()50         void onEnableAutoSync();
onCancelAutoSync()51         void onCancelAutoSync();
52     }
53 
54     private TurnAutoSyncOnDialogListener mListener;
55 
56     // Public no-args constructor needed for fragment re-instantiation
TurnAutoSyncOnDialog()57     public TurnAutoSyncOnDialog() {}
58 
newInstance(Account account, String syncAuthority)59     public static TurnAutoSyncOnDialog newInstance(Account account,
60             String syncAuthority) {
61         final TurnAutoSyncOnDialog frag = new TurnAutoSyncOnDialog();
62         final Bundle args = new Bundle(3);
63         args.putParcelable(ACCOUNT, account);
64         args.putString(SYNC_AUTHORITY, syncAuthority);
65         frag.setArguments(args);
66         return frag;
67     }
68 
69     @Override
onCreateDialog(Bundle savedInstanceState)70     public Dialog onCreateDialog(Bundle savedInstanceState) {
71         final Account account = getArguments().getParcelable(ACCOUNT);
72         final String syncAuthority = getArguments().getString(SYNC_AUTHORITY);
73         final Resources resources = getResources();
74         final boolean isTablet = Utils.useTabletUI(resources);
75         final String bodyText = resources.getString(
76                 R.string.turn_auto_sync_on_dialog_body,
77                 resources.getString(isTablet ? R.string.tablet : R.string.phone));
78         return new AlertDialog.Builder(getActivity())
79                 .setMessage(bodyText)
80                 .setTitle(R.string.turn_auto_sync_on_dialog_title)
81                 .setPositiveButton(R.string.turn_auto_sync_on_dialog_confirm_btn,
82                         new DialogInterface.OnClickListener() {
83                             @Override
84                             public void onClick(DialogInterface dialog, int whichButton) {
85                                 // Turn on auto-sync
86                                 ContentResolver.setMasterSyncAutomatically(true);
87                                 // Also enable auto-sync for Gmail.
88                                 // Note it's possible for syncAuthority to be empty on the
89                                 // account (used for constructing this dialog) was
90                                 // cached from shared prefs.  This can happen during app upgrade.
91                                 // As work around use default value set by app.
92                                 final String authority = TextUtils.isEmpty(syncAuthority) ?
93                                         sDefaultSyncAuthority : syncAuthority;
94                                 if (!TextUtils.isEmpty(authority)) {
95                                     ContentResolver.setSyncAutomatically(
96                                             account,
97                                             authority,
98                                             true);
99                                     if (mListener != null) {
100                                         mListener.onEnableAutoSync();
101                                     }
102                                 }
103                             }
104                         })
105                 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
106                     @Override
107                     public void onClick(DialogInterface dialog, int which) {
108                         if (mListener != null) {
109                             mListener.onCancelAutoSync();
110                         }
111                     }
112                 })
113                 .create();
114     }
115 
116     public void setListener(final TurnAutoSyncOnDialogListener listener) {
117         mListener = listener;
118     }
119 }
120