summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/interactions/SmsInteractionsLoader.java
blob: 295c99a460e8e7fd7b8c4293705ec4a87da462ba (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
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2014 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.contacts.interactions;

import android.content.AsyncTaskLoader;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.provider.Telephony;
import android.util.Log;

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

/**
 * Loads the most recent sms between the passed in phone numbers.
 *
 * This is a two part process. The first step is retrieving the threadIds for each of the phone
 * numbers using fuzzy matching. The next step is to run another query against these threadIds
 * to retrieve the actual sms.
 */
public class SmsInteractionsLoader extends AsyncTaskLoader<List<ContactInteraction>> {

    private static final String TAG = SmsInteractionsLoader.class.getSimpleName();

    private String[] mPhoneNums;
    private int mMaxToRetrieve;
    private List<ContactInteraction> mData;

    /**
     * Loads a list of SmsInteraction from the supplied phone numbers.
     */
    public SmsInteractionsLoader(Context context, String[] phoneNums,
            int maxToRetrieve) {
        super(context);
        Log.v(TAG, "SmsInteractionsLoader");
        mPhoneNums = phoneNums;
        mMaxToRetrieve = maxToRetrieve;
    }

    @Override
    public List<ContactInteraction> loadInBackground() {
        Log.v(TAG, "loadInBackground");
        // Confirm the device has Telephony and numbers were provided before proceeding
        if (!getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
                || mPhoneNums == null || mPhoneNums.length == 0) {
            return Collections.emptyList();
        }

        // Retrieve the thread IDs
        List<String> threadIdStrings = new ArrayList<>();
        for (String phone : mPhoneNums) {
            threadIdStrings.add(String.valueOf(
                    Telephony.Threads.getOrCreateThreadId(getContext(), phone)));
        }

        // Query the SMS database for the threads
        Cursor cursor = getSmsCursorFromThreads(threadIdStrings);
        if (cursor != null) {
            try {
                List<ContactInteraction> interactions = new ArrayList<>();
                while (cursor.moveToNext()) {
                    ContentValues values = new ContentValues();
                    DatabaseUtils.cursorRowToContentValues(cursor, values);
                    interactions.add(new SmsInteraction(values));
                }

                return interactions;
            } finally {
                cursor.close();
            }
        }

        return Collections.emptyList();
    }

    /**
     * Return the most recent messages between a list of threads
     */
    private Cursor getSmsCursorFromThreads(List<String> threadIds) {
        String selection = Telephony.Sms.THREAD_ID + " IN "
                + ContactInteractionUtil.questionMarks(threadIds.size());

        return getContext().getContentResolver().query(
                Telephony.Sms.CONTENT_URI,
                /* projection = */ null,
                selection,
                threadIds.toArray(new String[threadIds.size()]),
                Telephony.Sms.DEFAULT_SORT_ORDER
                        + " LIMIT " + mMaxToRetrieve);
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();

        if (mData != null) {
            deliverResult(mData);
        }

        if (takeContentChanged() || mData == null) {
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        cancelLoad();
    }

    @Override
    public void deliverResult(List<ContactInteraction> data) {
        mData = data;
        if (isStarted()) {
            super.deliverResult(data);
        }
    }

    @Override
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();
        if (mData != null) {
            mData.clear();
        }
    }
}