Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.dialog; |
Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.app.Dialog; |
Brian Attwell | c2e912c | 2014-10-27 12:29:44 -0700 | [diff] [blame] | 21 | import android.app.DialogFragment; |
Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 22 | import android.app.FragmentManager; |
| 23 | import android.content.ContentResolver; |
Yorke Lee | 3a52e63 | 2015-05-21 11:04:14 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 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 | |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame] | 31 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 32 | import com.android.contacts.util.PermissionsUtil; |
Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Dialog that clears the frequently contacted list after confirming with the user. |
| 36 | */ |
Brian Attwell | c2e912c | 2014-10-27 12:29:44 -0700 | [diff] [blame] | 37 | public class ClearFrequentsDialog extends DialogFragment { |
Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 38 | /** Preferred way to show this dialog */ |
| 39 | public static void show(FragmentManager fragmentManager) { |
| 40 | ClearFrequentsDialog dialog = new ClearFrequentsDialog(); |
| 41 | dialog.show(fragmentManager, "clearFrequents"); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Yorke Lee | 3a52e63 | 2015-05-21 11:04:14 -0700 | [diff] [blame] | 46 | final Context context = getActivity().getApplicationContext(); |
Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 47 | final ContentResolver resolver = getActivity().getContentResolver(); |
| 48 | final OnClickListener okListener = new OnClickListener() { |
| 49 | @Override |
| 50 | public void onClick(DialogInterface dialog, int which) { |
Yorke Lee | 3a52e63 | 2015-05-21 11:04:14 -0700 | [diff] [blame] | 51 | if (!PermissionsUtil.hasContactsPermissions(context)) { |
| 52 | return; |
| 53 | } |
Chiao Cheng | a6b4c79 | 2012-10-31 15:18:29 -0700 | [diff] [blame] | 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 | } |