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