1 /*
2  * Copyright (C) 2012 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.contacts.common.dialog;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.app.FragmentManager;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.DialogInterface.OnClickListener;
27 import android.os.AsyncTask;
28 import android.os.Bundle;
29 import android.provider.ContactsContract;
30 
31 import com.android.contacts.common.R;
32 import com.android.contacts.common.util.PermissionsUtil;
33 
34 /**
35  * Dialog that clears the frequently contacted list after confirming with the user.
36  */
37 public class ClearFrequentsDialog extends DialogFragment {
38     /** Preferred way to show this dialog */
show(FragmentManager fragmentManager)39     public static void show(FragmentManager fragmentManager) {
40         ClearFrequentsDialog dialog = new ClearFrequentsDialog();
41         dialog.show(fragmentManager, "clearFrequents");
42     }
43 
44     @Override
onCreateDialog(Bundle savedInstanceState)45     public Dialog onCreateDialog(Bundle savedInstanceState) {
46         final Context context = getActivity().getApplicationContext();
47         final ContentResolver resolver = getActivity().getContentResolver();
48         final OnClickListener okListener = new OnClickListener() {
49             @Override
50             public void onClick(DialogInterface dialog, int which) {
51                 if (!PermissionsUtil.hasContactsPermissions(context)) {
52                     return;
53                 }
54                 final IndeterminateProgressDialog progressDialog = IndeterminateProgressDialog.show(
55                         getFragmentManager(), getString(R.string.clearFrequentsProgress_title),
56                         null, 500);
57                 final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
58                     @Override
59                     protected Void doInBackground(Void... params) {
60                         resolver.delete(ContactsContract.DataUsageFeedback.DELETE_USAGE_URI,
61                                 null, null);
62                         return null;
63                     }
64 
65                     @Override
66                     protected void onPostExecute(Void result) {
67                         progressDialog.dismiss();
68                     }
69                 };
70                 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
71             }
72         };
73         return new AlertDialog.Builder(getActivity())
74             .setTitle(R.string.clearFrequentsConfirmation_title)
75             .setMessage(R.string.clearFrequentsConfirmation)
76             .setNegativeButton(android.R.string.cancel, null)
77             .setPositiveButton(android.R.string.ok, okListener)
78             .setCancelable(true)
79             .create();
80     }
81 }
82