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