From 5ade0bb1757b216ace2f50d2357409bf9876a07a Mon Sep 17 00:00:00 2001 From: Yorke Lee Date: Mon, 28 Oct 2013 11:05:43 -0700 Subject: Move ContactLoader related code to ContactsCommon This CL simply moves classes from Contacts into ContactsCommon. This is needed so that Dialer can use ContactLoader related code for b/11294679. A ContactLoader will also be needed in the future to allow InCallUI to download hi-res photos while in call. Bug: 11294679 Change-Id: If56a60aed2003ac7b8fcedac7ce4f1a7503bce94 --- .../common/test/mocks/MockAccountTypeManager.java | 81 +++++++++++ .../common/test/mocks/MockContactPhotoManager.java | 66 +++++++++ .../common/test/mocks/MockSharedPreferences.java | 149 +++++++++++++++++++++ 3 files changed, 296 insertions(+) create mode 100644 TestCommon/src/com/android/contacts/common/test/mocks/MockAccountTypeManager.java create mode 100644 TestCommon/src/com/android/contacts/common/test/mocks/MockContactPhotoManager.java create mode 100644 TestCommon/src/com/android/contacts/common/test/mocks/MockSharedPreferences.java (limited to 'TestCommon/src') diff --git a/TestCommon/src/com/android/contacts/common/test/mocks/MockAccountTypeManager.java b/TestCommon/src/com/android/contacts/common/test/mocks/MockAccountTypeManager.java new file mode 100644 index 00000000..8aed9891 --- /dev/null +++ b/TestCommon/src/com/android/contacts/common/test/mocks/MockAccountTypeManager.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2010 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.common.test.mocks; + +import com.android.contacts.common.model.AccountTypeManager; +import com.android.contacts.common.model.account.AccountType; +import com.android.contacts.common.model.account.AccountTypeWithDataSet; +import com.android.contacts.common.model.account.AccountWithDataSet; +import com.google.common.base.Objects; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * A mock {@link AccountTypeManager} class. + */ +public class MockAccountTypeManager extends AccountTypeManager { + + public AccountType[] mTypes; + public AccountWithDataSet[] mAccounts; + + public MockAccountTypeManager(AccountType[] types, AccountWithDataSet[] accounts) { + this.mTypes = types; + this.mAccounts = accounts; + } + + @Override + public AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet) { + for (AccountType type : mTypes) { + if (Objects.equal(accountTypeWithDataSet.accountType, type.accountType) + && Objects.equal(accountTypeWithDataSet.dataSet, type.dataSet)) { + return type; + } + } + return null; + } + + @Override + public List getAccounts(boolean writableOnly) { + return Arrays.asList(mAccounts); + } + + @Override + public List getGroupWritableAccounts() { + return Arrays.asList(mAccounts); + } + + @Override + public Map getUsableInvitableAccountTypes() { + return Maps.newHashMap(); // Always returns empty + } + + @Override + public List getAccountTypes(boolean writableOnly) { + final List ret = Lists.newArrayList(); + synchronized (this) { + for (AccountType type : mTypes) { + if (!writableOnly || type.areContactsWritable()) { + ret.add(type); + } + } + } + return ret; + } +} diff --git a/TestCommon/src/com/android/contacts/common/test/mocks/MockContactPhotoManager.java b/TestCommon/src/com/android/contacts/common/test/mocks/MockContactPhotoManager.java new file mode 100644 index 00000000..13986264 --- /dev/null +++ b/TestCommon/src/com/android/contacts/common/test/mocks/MockContactPhotoManager.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2011 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.common.test.mocks; + +import android.graphics.Bitmap; +import android.net.Uri; +import android.widget.ImageView; + +import com.android.contacts.common.ContactPhotoManager; + +/** + * A photo preloader that always uses the "no contact" picture and never executes any real + * db queries + */ +public class MockContactPhotoManager extends ContactPhotoManager { + @Override + public void loadThumbnail(ImageView view, long photoId, boolean darkTheme, + DefaultImageProvider defaultProvider) { + defaultProvider.applyDefaultImage(view, -1, darkTheme); + } + + @Override + public void loadPhoto(ImageView view, Uri photoUri, int requestedExtent, boolean darkTheme, + DefaultImageProvider defaultProvider) { + defaultProvider.applyDefaultImage(view, requestedExtent, darkTheme); + } + + @Override + public void removePhoto(ImageView view) { + view.setImageDrawable(null); + } + + @Override + public void pause() { + } + + @Override + public void resume() { + } + + @Override + public void refreshCache() { + } + + @Override + public void cacheBitmap(Uri photoUri, Bitmap bitmap, byte[] photoBytes) { + } + + @Override + public void preloadPhotosInBackground() { + } +} diff --git a/TestCommon/src/com/android/contacts/common/test/mocks/MockSharedPreferences.java b/TestCommon/src/com/android/contacts/common/test/mocks/MockSharedPreferences.java new file mode 100644 index 00000000..13d035ef --- /dev/null +++ b/TestCommon/src/com/android/contacts/common/test/mocks/MockSharedPreferences.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2010 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.common.test.mocks; + +import android.content.SharedPreferences; + +import com.google.common.collect.Maps; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + + +/** + * A programmable mock content provider. + */ +public class MockSharedPreferences implements SharedPreferences, SharedPreferences.Editor { + + private HashMap mValues = Maps.newHashMap(); + private HashMap mTempValues = Maps.newHashMap(); + + public Editor edit() { + return this; + } + + public boolean contains(String key) { + return mValues.containsKey(key); + } + + public Map getAll() { + return new HashMap(mValues); + } + + public boolean getBoolean(String key, boolean defValue) { + if (mValues.containsKey(key)) { + return ((Boolean)mValues.get(key)).booleanValue(); + } + return defValue; + } + + public float getFloat(String key, float defValue) { + if (mValues.containsKey(key)) { + return ((Float)mValues.get(key)).floatValue(); + } + return defValue; + } + + public int getInt(String key, int defValue) { + if (mValues.containsKey(key)) { + return ((Integer)mValues.get(key)).intValue(); + } + return defValue; + } + + public long getLong(String key, long defValue) { + if (mValues.containsKey(key)) { + return ((Long)mValues.get(key)).longValue(); + } + return defValue; + } + + public String getString(String key, String defValue) { + if (mValues.containsKey(key)) + return (String)mValues.get(key); + return defValue; + } + + @SuppressWarnings("unchecked") + public Set getStringSet(String key, Set defValues) { + if (mValues.containsKey(key)) { + return (Set) mValues.get(key); + } + return defValues; + } + + public void registerOnSharedPreferenceChangeListener( + OnSharedPreferenceChangeListener listener) { + throw new UnsupportedOperationException(); + } + + public void unregisterOnSharedPreferenceChangeListener( + OnSharedPreferenceChangeListener listener) { + throw new UnsupportedOperationException(); + } + + public Editor putBoolean(String key, boolean value) { + mTempValues.put(key, Boolean.valueOf(value)); + return this; + } + + public Editor putFloat(String key, float value) { + mTempValues.put(key, value); + return this; + } + + public Editor putInt(String key, int value) { + mTempValues.put(key, value); + return this; + } + + public Editor putLong(String key, long value) { + mTempValues.put(key, value); + return this; + } + + public Editor putString(String key, String value) { + mTempValues.put(key, value); + return this; + } + + public Editor putStringSet(String key, Set values) { + mTempValues.put(key, values); + return this; + } + + public Editor remove(String key) { + mTempValues.remove(key); + return this; + } + + public Editor clear() { + mTempValues.clear(); + return this; + } + + @SuppressWarnings("unchecked") + public boolean commit() { + mValues = (HashMap)mTempValues.clone(); + return true; + } + + public void apply() { + commit(); + } +} -- cgit v1.2.3