summaryrefslogtreecommitdiffstats
path: root/src/com/android/car/stream/telecom/RecentCallStreamProducer.java
blob: 9581226e5676b23dd24dfe1bb5ccc44a1c97f318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.stream.telecom;

import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import com.android.car.stream.StreamCard;
import com.android.car.stream.StreamProducer;

import java.util.ArrayList;
import java.util.List;

/**
 * Loads recent calls from the call log and produces a {@link StreamCard} for each entry.
 */
public class RecentCallStreamProducer extends StreamProducer
        implements Loader.OnLoadCompleteListener<Cursor> {
    private static final String TAG = "RecentCallProducer";
    private static final long RECENT_CALL_TIME_RANGE = 6 * DateUtils.HOUR_IN_MILLIS;

    /** Number of call log items to query for */
    private static final int CALL_LOG_QUERY_LIMIT = 1;
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    private CursorLoader mCursorLoader;
    private StreamCard mCurrentStreamCard;
    private long mCurrentNumber;
    private RecentCallConverter mConverter = new RecentCallConverter();

    public RecentCallStreamProducer(Context context) {
        super(context);
        mCursorLoader = createCallLogLoader();
    }

    @Override
    public void start() {
        super.start();
        if (!hasReadCallLogPermission()) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "Could not onStart RecentCallStreamProducer, permissions not granted");
            }
            return;
        }

        if (!mCursorLoader.isStarted()) {
            mCursorLoader.startLoading();
        }
    }

    @Override
    public void stop() {
        if (mCursorLoader.isStarted()) {
            mCursorLoader.stopLoading();
            removeCard(mCurrentStreamCard);
            mCurrentStreamCard = null;
            mCurrentNumber = 0;
        }
        super.stop();
    }

    @Override
    public void onLoadComplete(Loader<Cursor> loader, Cursor cursor) {
        if (cursor == null || !cursor.moveToFirst()) {
            return;
        }

        int column = cursor.getColumnIndex(CallLog.Calls.NUMBER);
        String number = cursor.getString(column);
        column = cursor.getColumnIndex(CallLog.Calls.DATE);
        long callTimeMs = cursor.getLong(column);
        // Display if we have a phone number, and the call was within 6hours.
        number = number.replaceAll("[^0-9]", "");
        long timestamp = System.currentTimeMillis();
        long digits = Long.parseLong(number);

        if (!TextUtils.isEmpty(number) &&
                (timestamp - callTimeMs) < RECENT_CALL_TIME_RANGE) {
            if (mCurrentStreamCard == null || mCurrentNumber != digits) {
                removeCard(mCurrentStreamCard);
                mCurrentStreamCard = mConverter.createStreamCard(mContext, number, timestamp);
                mCurrentNumber = digits;
                postCard(mCurrentStreamCard);
            }
        }
    }

    private boolean hasReadCallLogPermission() {
        return mContext.checkSelfPermission(android.Manifest.permission.READ_CALL_LOG)
                == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * Creates a CursorLoader for Call data.
     * Note: NOT to be used with LoaderManagers.
     */
    private CursorLoader createCallLogLoader() {
        // We need to check for NULL explicitly otherwise entries with where READ is NULL
        // may not match either the query or its negation.
        // We consider the calls that are not yet consumed (i.e. IS_READ = 0) as "new".
        StringBuilder where = new StringBuilder();
        List<String> selectionArgs = new ArrayList<String>();

        String selection = where.length() > 0 ? where.toString() : null;
        Uri uri = CallLog.Calls.CONTENT_URI.buildUpon()
                .appendQueryParameter(CallLog.Calls.LIMIT_PARAM_KEY,
                        Integer.toString(CALL_LOG_QUERY_LIMIT))
                .build();
        CursorLoader loader = new CursorLoader(mContext, uri, null, selection,
                selectionArgs.toArray(EMPTY_STRING_ARRAY), CallLog.Calls.DEFAULT_SORT_ORDER);
        loader.registerListener(0, this /* OnLoadCompleteListener */);
        return loader;
    }

}