Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.server.notification; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.content.Context; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Chris Wren | 99f4c7d | 2014-09-11 13:38:18 -0400 | [diff] [blame] | 22 | import android.database.ContentObserver; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 23 | import android.database.Cursor; |
| 24 | import android.net.Uri; |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 25 | import android.os.AsyncTask; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 26 | import android.os.Bundle; |
Chris Wren | 99f4c7d | 2014-09-11 13:38:18 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 28 | import android.os.UserHandle; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 29 | import android.provider.ContactsContract; |
| 30 | import android.provider.ContactsContract.Contacts; |
| 31 | import android.provider.Settings; |
| 32 | import android.text.TextUtils; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 33 | import android.util.ArrayMap; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 34 | import android.util.ArraySet; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 35 | import android.util.Log; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 36 | import android.util.LruCache; |
| 37 | import android.util.Slog; |
| 38 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 39 | import java.util.ArrayList; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 40 | import java.util.Arrays; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 41 | import java.util.LinkedList; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 42 | import java.util.List; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 43 | import java.util.Map; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 44 | import java.util.Set; |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 45 | import java.util.concurrent.Semaphore; |
| 46 | import java.util.concurrent.TimeUnit; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 47 | |
Chris Wren | 66dfcc5 | 2015-11-19 12:39:38 -0500 | [diff] [blame] | 48 | import android.os.SystemClock; |
Chris Wren | 66dfcc5 | 2015-11-19 12:39:38 -0500 | [diff] [blame] | 49 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 50 | /** |
| 51 | * This {@link NotificationSignalExtractor} attempts to validate |
| 52 | * people references. Also elevates the priority of real people. |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 53 | * |
| 54 | * {@hide} |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 55 | */ |
| 56 | public class ValidateNotificationPeople implements NotificationSignalExtractor { |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 57 | // Using a shorter log tag since setprop has a limit of 32chars on variable name. |
| 58 | private static final String TAG = "ValidateNoPeople"; |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 59 | private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);; |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 60 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 61 | |
| 62 | private static final boolean ENABLE_PEOPLE_VALIDATOR = true; |
| 63 | private static final String SETTING_ENABLE_PEOPLE_VALIDATOR = |
| 64 | "validate_notification_people_enabled"; |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 65 | private static final String[] LOOKUP_PROJECTION = { Contacts._ID, Contacts.STARRED }; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 66 | private static final int MAX_PEOPLE = 10; |
| 67 | private static final int PEOPLE_CACHE_SIZE = 200; |
| 68 | |
Chris Wren | 99f963e | 2014-05-28 16:52:42 -0400 | [diff] [blame] | 69 | /** Indicates that the notification does not reference any valid contacts. */ |
| 70 | static final float NONE = 0f; |
| 71 | |
| 72 | /** |
| 73 | * Affinity will be equal to or greater than this value on notifications |
| 74 | * that reference a valid contact. |
| 75 | */ |
| 76 | static final float VALID_CONTACT = 0.5f; |
| 77 | |
| 78 | /** |
| 79 | * Affinity will be equal to or greater than this value on notifications |
| 80 | * that reference a starred contact. |
| 81 | */ |
| 82 | static final float STARRED_CONTACT = 1f; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 83 | |
| 84 | protected boolean mEnabled; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 85 | private Context mBaseContext; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 86 | |
| 87 | // maps raw person handle to resolved person object |
| 88 | private LruCache<String, LookupResult> mPeopleCache; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 89 | private Map<Integer, Context> mUserToContextMap; |
Chris Wren | 99f4c7d | 2014-09-11 13:38:18 -0400 | [diff] [blame] | 90 | private Handler mHandler; |
| 91 | private ContentObserver mObserver; |
| 92 | private int mEvictionCount; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 93 | private NotificationUsageStats mUsageStats; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 94 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 95 | public void initialize(Context context, NotificationUsageStats usageStats) { |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 96 | if (DEBUG) Slog.d(TAG, "Initializing " + getClass().getSimpleName() + "."); |
| 97 | mUserToContextMap = new ArrayMap<>(); |
| 98 | mBaseContext = context; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 99 | mUsageStats = usageStats; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 100 | mPeopleCache = new LruCache<String, LookupResult>(PEOPLE_CACHE_SIZE); |
| 101 | mEnabled = ENABLE_PEOPLE_VALIDATOR && 1 == Settings.Global.getInt( |
| 102 | mBaseContext.getContentResolver(), SETTING_ENABLE_PEOPLE_VALIDATOR, 1); |
Chris Wren | 99f4c7d | 2014-09-11 13:38:18 -0400 | [diff] [blame] | 103 | if (mEnabled) { |
| 104 | mHandler = new Handler(); |
| 105 | mObserver = new ContentObserver(mHandler) { |
| 106 | @Override |
| 107 | public void onChange(boolean selfChange, Uri uri, int userId) { |
| 108 | super.onChange(selfChange, uri, userId); |
| 109 | if (DEBUG || mEvictionCount % 100 == 0) { |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 110 | if (VERBOSE) Slog.i(TAG, "mEvictionCount: " + mEvictionCount); |
Chris Wren | 99f4c7d | 2014-09-11 13:38:18 -0400 | [diff] [blame] | 111 | } |
| 112 | mPeopleCache.evictAll(); |
| 113 | mEvictionCount++; |
| 114 | } |
| 115 | }; |
| 116 | mBaseContext.getContentResolver().registerContentObserver(Contacts.CONTENT_URI, true, |
| 117 | mObserver, UserHandle.USER_ALL); |
| 118 | } |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | public RankingReconsideration process(NotificationRecord record) { |
| 122 | if (!mEnabled) { |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 123 | if (VERBOSE) Slog.i(TAG, "disabled"); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 124 | return null; |
| 125 | } |
| 126 | if (record == null || record.getNotification() == null) { |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 127 | if (VERBOSE) Slog.i(TAG, "skipping empty notification"); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 128 | return null; |
| 129 | } |
| 130 | if (record.getUserId() == UserHandle.USER_ALL) { |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 131 | if (VERBOSE) Slog.i(TAG, "skipping global notification"); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 132 | return null; |
| 133 | } |
| 134 | Context context = getContextAsUser(record.getUser()); |
| 135 | if (context == null) { |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 136 | if (VERBOSE) Slog.i(TAG, "skipping notification that lacks a context"); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 137 | return null; |
| 138 | } |
| 139 | return validatePeople(context, record); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void setConfig(RankingConfig config) { |
| 144 | // ignore: config has no relevant information yet. |
| 145 | } |
| 146 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 147 | /** |
| 148 | * @param extras extras of the notification with EXTRA_PEOPLE populated |
| 149 | * @param timeoutMs timeout in milliseconds to wait for contacts response |
| 150 | * @param timeoutAffinity affinity to return when the timeout specified via |
| 151 | * <code>timeoutMs</code> is hit |
| 152 | */ |
| 153 | public float getContactAffinity(UserHandle userHandle, Bundle extras, int timeoutMs, |
| 154 | float timeoutAffinity) { |
Chris Wren | 7381daa | 2014-09-05 11:30:32 -0400 | [diff] [blame] | 155 | if (DEBUG) Slog.d(TAG, "checking affinity for " + userHandle); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 156 | if (extras == null) return NONE; |
| 157 | final String key = Long.toString(System.nanoTime()); |
| 158 | final float[] affinityOut = new float[1]; |
| 159 | Context context = getContextAsUser(userHandle); |
| 160 | if (context == null) { |
| 161 | return NONE; |
| 162 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 163 | final PeopleRankingReconsideration prr = |
| 164 | validatePeople(context, key, extras, null, affinityOut); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 165 | float affinity = affinityOut[0]; |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 166 | |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 167 | if (prr != null) { |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 168 | // Perform the heavy work on a background thread so we can abort when we hit the |
| 169 | // timeout. |
| 170 | final Semaphore s = new Semaphore(0); |
| 171 | AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() { |
| 172 | @Override |
| 173 | public void run() { |
| 174 | prr.work(); |
| 175 | s.release(); |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | try { |
| 180 | if (!s.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { |
| 181 | Slog.w(TAG, "Timeout while waiting for affinity: " + key + ". " |
| 182 | + "Returning timeoutAffinity=" + timeoutAffinity); |
| 183 | return timeoutAffinity; |
| 184 | } |
| 185 | } catch (InterruptedException e) { |
| 186 | Slog.w(TAG, "InterruptedException while waiting for affinity: " + key + ". " |
| 187 | + "Returning affinity=" + affinity, e); |
| 188 | return affinity; |
| 189 | } |
| 190 | |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 191 | affinity = Math.max(prr.getContactAffinity(), affinity); |
| 192 | } |
| 193 | return affinity; |
| 194 | } |
| 195 | |
| 196 | private Context getContextAsUser(UserHandle userHandle) { |
| 197 | Context context = mUserToContextMap.get(userHandle.getIdentifier()); |
| 198 | if (context == null) { |
| 199 | try { |
| 200 | context = mBaseContext.createPackageContextAsUser("android", 0, userHandle); |
| 201 | mUserToContextMap.put(userHandle.getIdentifier(), context); |
| 202 | } catch (PackageManager.NameNotFoundException e) { |
| 203 | Log.e(TAG, "failed to create package context for lookups", e); |
| 204 | } |
| 205 | } |
| 206 | return context; |
| 207 | } |
| 208 | |
| 209 | private RankingReconsideration validatePeople(Context context, |
| 210 | final NotificationRecord record) { |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 211 | final String key = record.getKey(); |
| 212 | final Bundle extras = record.getNotification().extras; |
| 213 | final float[] affinityOut = new float[1]; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 214 | final PeopleRankingReconsideration rr = |
| 215 | validatePeople(context, key, extras, record.getPeopleOverride(), affinityOut); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 216 | final float affinity = affinityOut[0]; |
| 217 | record.setContactAffinity(affinity); |
| 218 | if (rr == null) { |
| 219 | mUsageStats.registerPeopleAffinity(record, affinity > NONE, affinity == STARRED_CONTACT, |
| 220 | true /* cached */); |
| 221 | } else { |
| 222 | rr.setRecord(record); |
| 223 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 224 | return rr; |
| 225 | } |
| 226 | |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 227 | private PeopleRankingReconsideration validatePeople(Context context, String key, Bundle extras, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 228 | List<String> peopleOverride, float[] affinityOut) { |
Chris Wren | 66dfcc5 | 2015-11-19 12:39:38 -0500 | [diff] [blame] | 229 | long start = SystemClock.elapsedRealtime(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 230 | float affinity = NONE; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 231 | if (extras == null) { |
| 232 | return null; |
| 233 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 234 | final Set<String> people = new ArraySet<>(peopleOverride); |
| 235 | final String[] notificationPeople = getExtraPeople(extras); |
| 236 | if (notificationPeople != null ) { |
| 237 | people.addAll(Arrays.asList(getExtraPeople(extras))); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 240 | if (VERBOSE) Slog.i(TAG, "Validating: " + key + " for " + context.getUserId()); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 241 | final LinkedList<String> pendingLookups = new LinkedList<String>(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 242 | int personIdx = 0; |
| 243 | for (String handle : people) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 244 | if (TextUtils.isEmpty(handle)) continue; |
| 245 | |
| 246 | synchronized (mPeopleCache) { |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 247 | final String cacheKey = getCacheKey(context.getUserId(), handle); |
| 248 | LookupResult lookupResult = mPeopleCache.get(cacheKey); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 249 | if (lookupResult == null || lookupResult.isExpired()) { |
| 250 | pendingLookups.add(handle); |
| 251 | } else { |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 252 | if (DEBUG) Slog.d(TAG, "using cached lookupResult"); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 253 | } |
| 254 | if (lookupResult != null) { |
| 255 | affinity = Math.max(affinity, lookupResult.getAffinity()); |
| 256 | } |
| 257 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 258 | if (++personIdx == MAX_PEOPLE) { |
| 259 | break; |
| 260 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // record the best available data, so far: |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 264 | affinityOut[0] = affinity; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 265 | |
| 266 | if (pendingLookups.isEmpty()) { |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 267 | if (VERBOSE) Slog.i(TAG, "final affinity: " + affinity); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 268 | return null; |
| 269 | } |
| 270 | |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 271 | if (DEBUG) Slog.d(TAG, "Pending: future work scheduled for: " + key); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 272 | return new PeopleRankingReconsideration(context, key, pendingLookups); |
| 273 | } |
| 274 | |
| 275 | private String getCacheKey(int userId, String handle) { |
| 276 | return Integer.toString(userId) + ":" + handle; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 279 | // VisibleForTesting |
| 280 | public static String[] getExtraPeople(Bundle extras) { |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 281 | Object people = extras.get(Notification.EXTRA_PEOPLE_LIST); |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 282 | if (people instanceof String[]) { |
| 283 | return (String[]) people; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 286 | if (people instanceof ArrayList) { |
| 287 | ArrayList arrayList = (ArrayList) people; |
| 288 | |
| 289 | if (arrayList.isEmpty()) { |
| 290 | return null; |
| 291 | } |
| 292 | |
| 293 | if (arrayList.get(0) instanceof String) { |
| 294 | ArrayList<String> stringArray = (ArrayList<String>) arrayList; |
| 295 | return stringArray.toArray(new String[stringArray.size()]); |
| 296 | } |
| 297 | |
| 298 | if (arrayList.get(0) instanceof CharSequence) { |
| 299 | ArrayList<CharSequence> charSeqList = (ArrayList<CharSequence>) arrayList; |
| 300 | final int N = charSeqList.size(); |
| 301 | String[] array = new String[N]; |
| 302 | for (int i = 0; i < N; i++) { |
| 303 | array[i] = charSeqList.get(i).toString(); |
| 304 | } |
| 305 | return array; |
| 306 | } |
| 307 | |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 308 | if (arrayList.get(0) instanceof Notification.Person) { |
| 309 | ArrayList<Notification.Person> list = (ArrayList<Notification.Person>) arrayList; |
| 310 | final int N = list.size(); |
| 311 | String[] array = new String[N]; |
| 312 | for (int i = 0; i < N; i++) { |
| 313 | array[i] = list.get(i).resolveToLegacyUri(); |
| 314 | } |
| 315 | return array; |
| 316 | } |
| 317 | |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 318 | return null; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 319 | } |
| 320 | |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 321 | if (people instanceof String) { |
| 322 | String[] array = new String[1]; |
| 323 | array[0] = (String) people; |
| 324 | return array; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 327 | if (people instanceof char[]) { |
| 328 | String[] array = new String[1]; |
| 329 | array[0] = new String((char[]) people); |
| 330 | return array; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 333 | if (people instanceof CharSequence) { |
| 334 | String[] array = new String[1]; |
| 335 | array[0] = ((CharSequence) people).toString(); |
| 336 | return array; |
| 337 | } |
| 338 | |
| 339 | if (people instanceof CharSequence[]) { |
| 340 | CharSequence[] charSeqArray = (CharSequence[]) people; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 341 | final int N = charSeqArray.length; |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 342 | String[] array = new String[N]; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 343 | for (int i = 0; i < N; i++) { |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 344 | array[i] = charSeqArray[i].toString(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 345 | } |
Chris Wren | fb69da3 | 2014-05-15 18:03:11 -0400 | [diff] [blame] | 346 | return array; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 349 | return null; |
| 350 | } |
| 351 | |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 352 | private LookupResult resolvePhoneContact(Context context, final String number) { |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 353 | Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, |
| 354 | Uri.encode(number)); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 355 | return searchContacts(context, phoneUri); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 358 | private LookupResult resolveEmailContact(Context context, final String email) { |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 359 | Uri numberUri = Uri.withAppendedPath( |
| 360 | ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, |
| 361 | Uri.encode(email)); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 362 | return searchContacts(context, numberUri); |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 363 | } |
| 364 | |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 365 | private LookupResult searchContacts(Context context, Uri lookupUri) { |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 366 | LookupResult lookupResult = new LookupResult(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 367 | Cursor c = null; |
| 368 | try { |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 369 | c = context.getContentResolver().query(lookupUri, LOOKUP_PROJECTION, null, null, null); |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 370 | if (c == null) { |
| 371 | Slog.w(TAG, "Null cursor from contacts query."); |
| 372 | return lookupResult; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 373 | } |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 374 | while (c.moveToNext()) { |
| 375 | lookupResult.mergeContact(c); |
| 376 | } |
| 377 | } catch (Throwable t) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 378 | Slog.w(TAG, "Problem getting content resolver or performing contacts query.", t); |
| 379 | } finally { |
| 380 | if (c != null) { |
| 381 | c.close(); |
| 382 | } |
| 383 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 384 | return lookupResult; |
| 385 | } |
| 386 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 387 | private static class LookupResult { |
| 388 | private static final long CONTACT_REFRESH_MILLIS = 60 * 60 * 1000; // 1hr |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 389 | |
| 390 | private final long mExpireMillis; |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 391 | private float mAffinity = NONE; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 392 | |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 393 | public LookupResult() { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 394 | mExpireMillis = System.currentTimeMillis() + CONTACT_REFRESH_MILLIS; |
| 395 | } |
| 396 | |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 397 | public void mergeContact(Cursor cursor) { |
| 398 | mAffinity = Math.max(mAffinity, VALID_CONTACT); |
| 399 | |
| 400 | // Contact ID |
| 401 | int id; |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 402 | final int idIdx = cursor.getColumnIndex(Contacts._ID); |
| 403 | if (idIdx >= 0) { |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 404 | id = cursor.getInt(idIdx); |
| 405 | if (DEBUG) Slog.d(TAG, "contact _ID is: " + id); |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 406 | } else { |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 407 | id = -1; |
| 408 | Slog.i(TAG, "invalid cursor: no _ID"); |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 409 | } |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 410 | |
| 411 | // Starred |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 412 | final int starIdx = cursor.getColumnIndex(Contacts.STARRED); |
| 413 | if (starIdx >= 0) { |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 414 | boolean isStarred = cursor.getInt(starIdx) != 0; |
| 415 | if (isStarred) { |
| 416 | mAffinity = Math.max(mAffinity, STARRED_CONTACT); |
| 417 | } |
| 418 | if (DEBUG) Slog.d(TAG, "contact STARRED is: " + isStarred); |
Chris Wren | 44d81a4 | 2014-05-14 17:38:05 -0400 | [diff] [blame] | 419 | } else { |
| 420 | if (DEBUG) Slog.d(TAG, "invalid cursor: no STARRED"); |
| 421 | } |
| 422 | } |
| 423 | |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 424 | private boolean isExpired() { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 425 | return mExpireMillis < System.currentTimeMillis(); |
| 426 | } |
| 427 | |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 428 | private boolean isInvalid() { |
| 429 | return mAffinity == NONE || isExpired(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | public float getAffinity() { |
| 433 | if (isInvalid()) { |
| 434 | return NONE; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 435 | } |
Christoph Studer | 9ffa500 | 2014-09-26 16:57:28 +0200 | [diff] [blame] | 436 | return mAffinity; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 437 | } |
| 438 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 439 | |
| 440 | private class PeopleRankingReconsideration extends RankingReconsideration { |
| 441 | private final LinkedList<String> mPendingLookups; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 442 | private final Context mContext; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 443 | |
| 444 | private float mContactAffinity = NONE; |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 445 | private NotificationRecord mRecord; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 446 | |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 447 | private PeopleRankingReconsideration(Context context, String key, LinkedList<String> pendingLookups) { |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 448 | super(key); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 449 | mContext = context; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 450 | mPendingLookups = pendingLookups; |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void work() { |
Chris Wren | 66dfcc5 | 2015-11-19 12:39:38 -0500 | [diff] [blame] | 455 | long start = SystemClock.elapsedRealtime(); |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 456 | if (VERBOSE) Slog.i(TAG, "Executing: validation for: " + mKey); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 457 | long timeStartMs = System.currentTimeMillis(); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 458 | for (final String handle: mPendingLookups) { |
| 459 | LookupResult lookupResult = null; |
| 460 | final Uri uri = Uri.parse(handle); |
| 461 | if ("tel".equals(uri.getScheme())) { |
| 462 | if (DEBUG) Slog.d(TAG, "checking telephone URI: " + handle); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 463 | lookupResult = resolvePhoneContact(mContext, uri.getSchemeSpecificPart()); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 464 | } else if ("mailto".equals(uri.getScheme())) { |
| 465 | if (DEBUG) Slog.d(TAG, "checking mailto URI: " + handle); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 466 | lookupResult = resolveEmailContact(mContext, uri.getSchemeSpecificPart()); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 467 | } else if (handle.startsWith(Contacts.CONTENT_LOOKUP_URI.toString())) { |
| 468 | if (DEBUG) Slog.d(TAG, "checking lookup URI: " + handle); |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 469 | lookupResult = searchContacts(mContext, uri); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 470 | } else { |
| 471 | lookupResult = new LookupResult(); // invalid person for the cache |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 472 | if (!"name".equals(uri.getScheme())) { |
| 473 | Slog.w(TAG, "unsupported URI " + handle); |
| 474 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 475 | } |
| 476 | if (lookupResult != null) { |
| 477 | synchronized (mPeopleCache) { |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 478 | final String cacheKey = getCacheKey(mContext.getUserId(), handle); |
| 479 | mPeopleCache.put(cacheKey, lookupResult); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 480 | } |
Chris Wren | f37dae7 | 2014-09-12 12:09:17 -0400 | [diff] [blame] | 481 | if (DEBUG) Slog.d(TAG, "lookup contactAffinity is " + lookupResult.getAffinity()); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 482 | mContactAffinity = Math.max(mContactAffinity, lookupResult.getAffinity()); |
Chris Wren | f37dae7 | 2014-09-12 12:09:17 -0400 | [diff] [blame] | 483 | } else { |
| 484 | if (DEBUG) Slog.d(TAG, "lookupResult is null"); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 485 | } |
| 486 | } |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 487 | if (DEBUG) { |
| 488 | Slog.d(TAG, "Validation finished in " + (System.currentTimeMillis() - timeStartMs) + |
| 489 | "ms"); |
| 490 | } |
Chris Wren | 723aa76 | 2015-04-09 15:35:23 -0400 | [diff] [blame] | 491 | |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 492 | if (mRecord != null) { |
| 493 | mUsageStats.registerPeopleAffinity(mRecord, mContactAffinity > NONE, |
| 494 | mContactAffinity == STARRED_CONTACT, false /* cached */); |
| 495 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void applyChangesLocked(NotificationRecord operand) { |
| 500 | float affinityBound = operand.getContactAffinity(); |
| 501 | operand.setContactAffinity(Math.max(mContactAffinity, affinityBound)); |
Chris Wren | 1a5dad8 | 2015-06-16 11:23:12 -0400 | [diff] [blame] | 502 | if (VERBOSE) Slog.i(TAG, "final affinity: " + operand.getContactAffinity()); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | public float getContactAffinity() { |
| 506 | return mContactAffinity; |
| 507 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 508 | |
| 509 | public void setRecord(NotificationRecord record) { |
| 510 | mRecord = record; |
| 511 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 512 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 513 | } |
| 514 | |