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.Context;
26 import android.content.DialogInterface.OnClickListener;
27 import android.os.Bundle;
28 import android.provider.CallLog.Calls;
29 import android.support.annotation.Nullable;
30 import android.support.design.widget.Snackbar;
31 import com.android.dialer.app.R;
32 import com.android.dialer.common.Assert;
33 import com.android.dialer.common.concurrent.DialerExecutor;
34 import com.android.dialer.common.concurrent.DialerExecutor.Worker;
35 import com.android.dialer.common.concurrent.DialerExecutorComponent;
36 import com.android.dialer.enrichedcall.EnrichedCallComponent;
37 import com.android.dialer.phonenumbercache.CachedNumberLookupService;
38 import com.android.dialer.phonenumbercache.PhoneNumberCache;
39 
40 /** Dialog that clears the call log after confirming with the user */
41 public class ClearCallLogDialog extends DialogFragment {
42 
43   private DialerExecutor<Void> clearCallLogTask;
44   private ProgressDialog progressDialog;
45 
46   /** Preferred way to show this dialog */
show(FragmentManager fragmentManager)47   public static void show(FragmentManager fragmentManager) {
48     ClearCallLogDialog dialog = new ClearCallLogDialog();
49     dialog.show(fragmentManager, "deleteCallLog");
50   }
51 
52   @Override
onCreate(Bundle savedInstanceState)53   public void onCreate(Bundle savedInstanceState) {
54     super.onCreate(savedInstanceState);
55 
56     clearCallLogTask =
57         DialerExecutorComponent.get(getContext())
58             .dialerExecutorFactory()
59             .createUiTaskBuilder(
60                 getFragmentManager(),
61                 "clearCallLogTask",
62                 new ClearCallLogWorker(getActivity().getApplicationContext()))
63             .onSuccess(this::onSuccess)
64             .build();
65   }
66 
67   @Override
onCreateDialog(Bundle savedInstanceState)68   public Dialog onCreateDialog(Bundle savedInstanceState) {
69     OnClickListener okListener =
70         (dialog, which) -> {
71           progressDialog =
72               ProgressDialog.show(
73                   getActivity(), getString(R.string.clearCallLogProgress_title), "", true, false);
74           progressDialog.setOwnerActivity(getActivity());
75           CallLogNotificationsService.cancelAllMissedCalls(getContext());
76 
77           // TODO: Once we have the API, we should configure this ProgressDialog
78           // to only show up after a certain time (e.g. 150ms)
79           progressDialog.show();
80 
81           clearCallLogTask.executeSerial(null);
82         };
83     return new AlertDialog.Builder(getActivity())
84         .setTitle(R.string.clearCallLogConfirmation_title)
85         .setIconAttribute(android.R.attr.alertDialogIcon)
86         .setMessage(R.string.clearCallLogConfirmation)
87         .setNegativeButton(android.R.string.cancel, null)
88         .setPositiveButton(android.R.string.ok, okListener)
89         .setCancelable(true)
90         .create();
91   }
92 
93   private static class ClearCallLogWorker implements Worker<Void, Void> {
94     private final Context appContext;
95 
ClearCallLogWorker(Context appContext)96     private ClearCallLogWorker(Context appContext) {
97       this.appContext = appContext;
98     }
99 
100     @Nullable
101     @Override
doInBackground(@ullable Void unused)102     public Void doInBackground(@Nullable Void unused) throws Throwable {
103       appContext.getContentResolver().delete(Calls.CONTENT_URI, null, null);
104       CachedNumberLookupService cachedNumberLookupService =
105           PhoneNumberCache.get(appContext).getCachedNumberLookupService();
106       if (cachedNumberLookupService != null) {
107         cachedNumberLookupService.clearAllCacheEntries(appContext);
108       }
109       return null;
110     }
111   }
112 
onSuccess(Void unused)113   private void onSuccess(Void unused) {
114     Assert.isNotNull(progressDialog);
115     Activity activity = progressDialog.getOwnerActivity();
116 
117     if (activity == null || activity.isDestroyed() || activity.isFinishing()) {
118       return;
119     }
120 
121     maybeShowEnrichedCallSnackbar(activity);
122 
123     if (progressDialog != null && progressDialog.isShowing()) {
124       progressDialog.dismiss();
125     }
126   }
127 
maybeShowEnrichedCallSnackbar(Activity activity)128   private void maybeShowEnrichedCallSnackbar(Activity activity) {
129     if (EnrichedCallComponent.get(activity).getEnrichedCallManager().hasStoredData()) {
130       Snackbar.make(
131               activity.findViewById(R.id.calllog_frame),
132               activity.getString(R.string.multiple_ec_data_deleted),
133               5_000)
134           .show();
135     }
136   }
137 }
138