2015-02-09 20:22:47 +00:00
|
|
|
/*
|
|
|
|
* 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.inputmethod.latin;
|
|
|
|
|
2015-07-15 20:32:50 +00:00
|
|
|
import android.Manifest;
|
2015-02-09 20:22:47 +00:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.ContentObserver;
|
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.ContactsManager.ContactsChangedListener;
|
2015-04-09 01:58:13 +00:00
|
|
|
import com.android.inputmethod.latin.define.DebugFlags;
|
2015-07-15 20:32:50 +00:00
|
|
|
import com.android.inputmethod.latin.permissions.PermissionsUtil;
|
2015-02-09 20:22:47 +00:00
|
|
|
import com.android.inputmethod.latin.utils.ExecutorUtils;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2015-02-19 19:54:18 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2015-02-09 20:22:47 +00:00
|
|
|
|
|
|
|
/**
|
2015-02-17 23:12:05 +00:00
|
|
|
* A content observer that listens to updates to content provider {@link Contacts#CONTENT_URI}.
|
2015-02-09 20:22:47 +00:00
|
|
|
*/
|
2015-02-17 23:12:05 +00:00
|
|
|
public class ContactsContentObserver implements Runnable {
|
2015-04-09 01:58:13 +00:00
|
|
|
private static final String TAG = "ContactsContentObserver";
|
2015-02-09 20:22:47 +00:00
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final ContactsManager mManager;
|
2015-07-15 20:32:50 +00:00
|
|
|
private final AtomicBoolean mRunning = new AtomicBoolean(false);
|
2015-02-09 20:22:47 +00:00
|
|
|
|
2015-02-19 19:54:18 +00:00
|
|
|
private ContentObserver mContentObserver;
|
|
|
|
private ContactsChangedListener mContactsChangedListener;
|
|
|
|
|
2015-02-09 20:22:47 +00:00
|
|
|
public ContactsContentObserver(final ContactsManager manager, final Context context) {
|
|
|
|
mManager = manager;
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void registerObserver(final ContactsChangedListener listener) {
|
2015-07-15 20:32:50 +00:00
|
|
|
if (!PermissionsUtil.checkAllPermissionsGranted(
|
|
|
|
mContext, Manifest.permission.READ_CONTACTS)) {
|
|
|
|
Log.i(TAG, "No permission to read contacts. Not registering the observer.");
|
|
|
|
// do nothing if we do not have the permission to read contacts.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-09 01:58:13 +00:00
|
|
|
if (DebugFlags.DEBUG_ENABLED) {
|
|
|
|
Log.d(TAG, "registerObserver()");
|
2015-02-09 20:22:47 +00:00
|
|
|
}
|
2015-02-17 23:12:05 +00:00
|
|
|
mContactsChangedListener = listener;
|
|
|
|
mContentObserver = new ContentObserver(null /* handler */) {
|
2015-02-09 20:22:47 +00:00
|
|
|
@Override
|
|
|
|
public void onChange(boolean self) {
|
2015-03-13 00:06:48 +00:00
|
|
|
ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD)
|
|
|
|
.execute(ContactsContentObserver.this);
|
2015-02-09 20:22:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
final ContentResolver contentResolver = mContext.getContentResolver();
|
2015-02-17 23:12:05 +00:00
|
|
|
contentResolver.registerContentObserver(Contacts.CONTENT_URI, true, mContentObserver);
|
2015-02-09 20:22:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 23:12:05 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-07-15 20:32:50 +00:00
|
|
|
if (!PermissionsUtil.checkAllPermissionsGranted(
|
|
|
|
mContext, Manifest.permission.READ_CONTACTS)) {
|
|
|
|
Log.i(TAG, "No permission to read contacts. Not updating the contacts.");
|
|
|
|
unregister();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRunning.compareAndSet(false /* expect */, true /* update */)) {
|
2015-04-09 01:58:13 +00:00
|
|
|
if (DebugFlags.DEBUG_ENABLED) {
|
2015-02-19 19:54:18 +00:00
|
|
|
Log.d(TAG, "run() : Already running. Don't waste time checking again.");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-02-17 23:12:05 +00:00
|
|
|
if (haveContentsChanged()) {
|
2015-04-09 01:58:13 +00:00
|
|
|
if (DebugFlags.DEBUG_ENABLED) {
|
2015-02-19 19:54:18 +00:00
|
|
|
Log.d(TAG, "run() : Contacts have changed. Notifying listeners.");
|
2015-02-17 23:12:05 +00:00
|
|
|
}
|
|
|
|
mContactsChangedListener.onContactsChange();
|
|
|
|
}
|
2015-07-15 20:32:50 +00:00
|
|
|
mRunning.set(false);
|
2015-02-09 20:22:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 19:39:34 +00:00
|
|
|
boolean haveContentsChanged() {
|
2015-07-15 20:32:50 +00:00
|
|
|
if (!PermissionsUtil.checkAllPermissionsGranted(
|
|
|
|
mContext, Manifest.permission.READ_CONTACTS)) {
|
|
|
|
Log.i(TAG, "No permission to read contacts. Marking contacts as not changed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:22:47 +00:00
|
|
|
final long startTime = SystemClock.uptimeMillis();
|
|
|
|
final int contactCount = mManager.getContactCount();
|
2015-04-14 00:49:17 +00:00
|
|
|
if (contactCount > ContactsDictionaryConstants.MAX_CONTACTS_PROVIDER_QUERY_LIMIT) {
|
2015-02-09 20:22:47 +00:00
|
|
|
// If there are too many contacts then return false. In this rare case it is impossible
|
|
|
|
// to include all of them anyways and the cost of rebuilding the dictionary is too high.
|
2015-04-14 00:49:17 +00:00
|
|
|
// TODO: Sort and check only the most recent contacts?
|
2015-02-09 20:22:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (contactCount != mManager.getContactCountAtLastRebuild()) {
|
2015-04-09 01:58:13 +00:00
|
|
|
if (DebugFlags.DEBUG_ENABLED) {
|
|
|
|
Log.d(TAG, "haveContentsChanged() : Count changed from "
|
|
|
|
+ mManager.getContactCountAtLastRebuild() + " to " + contactCount);
|
2015-02-09 20:22:47 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
final ArrayList<String> names = mManager.getValidNames(Contacts.CONTENT_URI);
|
|
|
|
if (names.hashCode() != mManager.getHashCodeAtLastRebuild()) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-09 01:58:13 +00:00
|
|
|
if (DebugFlags.DEBUG_ENABLED) {
|
|
|
|
Log.d(TAG, "haveContentsChanged() : No change detected in "
|
|
|
|
+ (SystemClock.uptimeMillis() - startTime) + " ms)");
|
2015-02-09 20:22:47 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unregister() {
|
2015-02-17 23:12:05 +00:00
|
|
|
mContext.getContentResolver().unregisterContentObserver(mContentObserver);
|
2015-02-09 20:22:47 +00:00
|
|
|
}
|
|
|
|
}
|