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