1 /*
2  * Copyright (C) 2011 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.dialer.calllog;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.app.FragmentManager;
24 import android.app.ProgressDialog;
25 import android.content.ContentResolver;
26 import android.content.Context;
27 import android.content.DialogInterface;
28 import android.content.DialogInterface.OnClickListener;
29 import android.os.AsyncTask;
30 import android.os.Bundle;
31 import android.provider.CallLog.Calls;
32 
33 import com.android.dialer.R;
34 import com.android.dialer.service.CachedNumberLookupService;
35 import com.android.dialerbind.ObjectFactory;
36 
37 /**
38  * Dialog that clears the call log after confirming with the user
39  */
40 public class ClearCallLogDialog extends DialogFragment {
41     private static final CachedNumberLookupService mCachedNumberLookupService =
42             ObjectFactory.newCachedNumberLookupService();
43 
44     /** Preferred way to show this dialog */
show(FragmentManager fragmentManager)45     public static void show(FragmentManager fragmentManager) {
46         ClearCallLogDialog dialog = new ClearCallLogDialog();
47         dialog.show(fragmentManager, "deleteCallLog");
48     }
49 
50     @Override
onCreateDialog(Bundle savedInstanceState)51     public Dialog onCreateDialog(Bundle savedInstanceState) {
52         final ContentResolver resolver = getActivity().getContentResolver();
53         final Context context = getActivity().getApplicationContext();
54         final OnClickListener okListener = new OnClickListener() {
55             @Override
56             public void onClick(DialogInterface dialog, int which) {
57                 final ProgressDialog progressDialog = ProgressDialog.show(getActivity(),
58                         getString(R.string.clearCallLogProgress_title),
59                         "", true, false);
60                 progressDialog.setOwnerActivity(getActivity());
61                 final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
62                     @Override
63                     protected Void doInBackground(Void... params) {
64                         resolver.delete(Calls.CONTENT_URI, null, null);
65                         if (mCachedNumberLookupService != null) {
66                             mCachedNumberLookupService.clearAllCacheEntries(context);
67                         }
68                         return null;
69                     }
70                     @Override
71                     protected void onPostExecute(Void result) {
72                         final Activity activity = progressDialog.getOwnerActivity();
73 
74                         if (activity == null || activity.isDestroyed() || activity.isFinishing()) {
75                             return;
76                         }
77 
78                         if (progressDialog != null && progressDialog.isShowing()) {
79                             progressDialog.dismiss();
80                         }
81                     }
82                 };
83                 // TODO: Once we have the API, we should configure this ProgressDialog
84                 // to only show up after a certain time (e.g. 150ms)
85                 progressDialog.show();
86                 task.execute();
87             }
88         };
89         return new AlertDialog.Builder(getActivity())
90             .setTitle(R.string.clearCallLogConfirmation_title)
91             .setIconAttribute(android.R.attr.alertDialogIcon)
92             .setMessage(R.string.clearCallLogConfirmation)
93             .setNegativeButton(android.R.string.cancel, null)
94             .setPositiveButton(android.R.string.ok, okListener)
95             .setCancelable(true)
96             .create();
97     }
98 }
99