1/*
2 * Copyright (C) 2010 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.app.calllog;
18
19import android.content.Context;
20import android.os.AsyncTask;
21import android.provider.CallLog.Calls;
22import com.android.dialer.common.Assert;
23
24/**
25 * Class to access the call log asynchronously to avoid carrying out database operations on the UI
26 * thread, using an {@link AsyncTask}.
27 *
28 * <pre class="prettyprint"> Typical usage: ==============
29 *
30 * // From an activity... String mLastNumber = "";
31 *
32 * CallLogAsync log = new CallLogAsync();
33 *
34 * CallLogAsync.GetLastOutgoingCallArgs lastCallArgs = new CallLogAsync.GetLastOutgoingCallArgs(
35 * this, new CallLogAsync.OnLastOutgoingCallComplete() { public void lastOutgoingCall(String number)
36 * { mLastNumber = number; } }); log.getLastOutgoingCall(lastCallArgs); </pre>
37 */
38public class CallLogAsync {
39
40  /** CallLog.getLastOutgoingCall(...) */
41  public AsyncTask getLastOutgoingCall(GetLastOutgoingCallArgs args) {
42    Assert.isMainThread();
43    return new GetLastOutgoingCallTask(args.callback).execute(args);
44  }
45
46  /** Interface to retrieve the last dialed number asynchronously. */
47  public interface OnLastOutgoingCallComplete {
48
49    /** @param number The last dialed number or an empty string if none exists yet. */
50    void lastOutgoingCall(String number);
51  }
52
53  /** Parameter object to hold the args to get the last outgoing call from the call log DB. */
54  public static class GetLastOutgoingCallArgs {
55
56    public final Context context;
57    public final OnLastOutgoingCallComplete callback;
58
59    public GetLastOutgoingCallArgs(Context context, OnLastOutgoingCallComplete callback) {
60      this.context = context;
61      this.callback = callback;
62    }
63  }
64
65  /** AsyncTask to get the last outgoing call from the DB. */
66  private class GetLastOutgoingCallTask extends AsyncTask<GetLastOutgoingCallArgs, Void, String> {
67
68    private final OnLastOutgoingCallComplete mCallback;
69
70    public GetLastOutgoingCallTask(OnLastOutgoingCallComplete callback) {
71      mCallback = callback;
72    }
73
74    // Happens on a background thread. We cannot run the callback
75    // here because only the UI thread can modify the view
76    // hierarchy (e.g enable/disable the dial button). The
77    // callback is ran rom the post execute method.
78    @Override
79    protected String doInBackground(GetLastOutgoingCallArgs... list) {
80      String number = "";
81      for (GetLastOutgoingCallArgs args : list) {
82        // May block. Select only the last one.
83        number = Calls.getLastOutgoingCall(args.context);
84      }
85      return number; // passed to the onPostExecute method.
86    }
87
88    // Happens on the UI thread, it is safe to run the callback
89    // that may do some work on the views.
90    @Override
91    protected void onPostExecute(String number) {
92      Assert.isMainThread();
93      mCallback.lastOutgoingCall(number);
94    }
95  }
96}
97