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