summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrandon Maxwell <maxwelb@google.com>2016-03-14 17:50:21 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-03-14 17:50:21 +0000
commitf561b058245c3976f6efc9c420c3cc7cef8c358a (patch)
tree84d02924cfa81d23b9e30a90aa0e7189707fa642
parent414cf94924769f8bc4ca72a8d198c7d278b1f958 (diff)
parent9df658ca49c4b4571fca91eef7920e6d974ecc85 (diff)
Merge "Adding sharedPref for blocked number migration" into nyc-dev
am: 9df658ca49 * commit '9df658ca49c4b4571fca91eef7920e6d974ecc85': Adding sharedPref for blocked number migration
-rw-r--r--src/com/android/dialer/DialerApplication.java17
-rw-r--r--src/com/android/dialer/compat/FilteredNumberCompat.java30
-rw-r--r--tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java57
3 files changed, 97 insertions, 7 deletions
diff --git a/src/com/android/dialer/DialerApplication.java b/src/com/android/dialer/DialerApplication.java
index 078b5510c..189c68221 100644
--- a/src/com/android/dialer/DialerApplication.java
+++ b/src/com/android/dialer/DialerApplication.java
@@ -17,16 +17,23 @@
package com.android.dialer;
import android.app.Application;
+import android.content.Context;
import android.os.Trace;
+import android.support.annotation.Nullable;
import com.android.contacts.common.extensions.ExtensionsFactory;
+import com.android.contacts.common.testing.NeededForTesting;
+import com.android.dialer.compat.FilteredNumberCompat;
public class DialerApplication extends Application {
private static final String TAG = "DialerApplication";
+ private static Context sContext;
+
@Override
public void onCreate() {
+ sContext = this;
Trace.beginSection(TAG + " onCreate");
super.onCreate();
Trace.beginSection(TAG + " ExtensionsFactory initialization");
@@ -34,4 +41,14 @@ public class DialerApplication extends Application {
Trace.endSection();
Trace.endSection();
}
+
+ @Nullable
+ public static Context getContext() {
+ return sContext;
+ }
+
+ @NeededForTesting
+ public static void setContextForTest(Context context) {
+ sContext = context;
+ }
}
diff --git a/src/com/android/dialer/compat/FilteredNumberCompat.java b/src/com/android/dialer/compat/FilteredNumberCompat.java
index 63a2a32cd..3ad45e87d 100644
--- a/src/com/android/dialer/compat/FilteredNumberCompat.java
+++ b/src/com/android/dialer/compat/FilteredNumberCompat.java
@@ -20,12 +20,15 @@ import com.google.common.base.Preconditions;
import android.content.ContentUris;
import android.content.ContentValues;
+import android.content.Context;
import android.net.Uri;
+import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.telephony.PhoneNumberUtils;
import com.android.contacts.common.compat.CompatUtils;
import com.android.contacts.common.testing.NeededForTesting;
+import com.android.dialer.DialerApplication;
import com.android.dialer.database.FilteredNumberContract.FilteredNumber;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberSources;
@@ -43,9 +46,12 @@ import java.util.List;
*/
public class FilteredNumberCompat {
+ protected static final String HAS_MIGRATED_TO_NEW_BLOCKING_KEY = "migratedToNewBlocking";
+
// Flag to enable feature.
// TODO(maxwelb) remove when ready to enable new filtering.
private static final boolean isNewFilteringEnabled = false;
+
private static Boolean isEnabledForTest;
/**
@@ -114,8 +120,28 @@ public class FilteredNumberCompat {
* migration has been performed, {@code false} otherwise.
*/
public static boolean useNewFiltering() {
- // TODO(maxwelb): Add shared preference for when the Dialer blocked list has been migrated
- return canUseNewFiltering();
+ return canUseNewFiltering() && hasMigratedToNewBlocking();
+ }
+
+ /**
+ * @return {@code true} if the user has migrated to use
+ * {@link android.provider.BlockedNumberContract} blocking, {@code false} otherwise.
+ */
+ public static boolean hasMigratedToNewBlocking() {
+ return PreferenceManager.getDefaultSharedPreferences(DialerApplication.getContext())
+ .getBoolean(HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false);
+ }
+
+ /**
+ * Called to inform this class whether the user has fully migrated to use
+ * {@link android.provider.BlockedNumberContract} blocking or not.
+ *
+ * @param hasMigrated {@code true} if the user has migrated, {@code false} otherwise.
+ */
+ @NeededForTesting
+ public static void setHasMigratedToNewBlocking(boolean hasMigrated) {
+ PreferenceManager.getDefaultSharedPreferences(DialerApplication.getContext()).edit()
+ .putBoolean(HAS_MIGRATED_TO_NEW_BLOCKING_KEY, hasMigrated).apply();
}
@NeededForTesting
diff --git a/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java b/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java
index b9eb21424..a57e1c29c 100644
--- a/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java
+++ b/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java
@@ -16,19 +16,30 @@
package com.android.dialer.compat;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.when;
+
import android.content.ContentUris;
import android.content.ContentValues;
+import android.content.Context;
+import android.content.SharedPreferences;
import android.net.Uri;
import android.provider.BlockedNumberContract.BlockedNumbers;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.contacts.common.compat.CompatUtils;
+import com.android.dialer.DialerApplication;
import com.android.dialer.database.FilteredNumberContract.FilteredNumber;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberSources;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberTypes;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
import java.util.Arrays;
@SmallTest
@@ -41,27 +52,52 @@ public class FilteredNumberCompatTest extends AndroidTestCase {
private static final Uri EXPECTED_BASE_URI = CompatUtils.isNCompatible()
? BlockedNumbers.CONTENT_URI : FilteredNumber.CONTENT_URI;
+ @Mock private Context mContext;
+ @Mock private SharedPreferences mSharedPreferences;
+
@Override
public void setUp() throws Exception {
super.setUp();
+ MockitoAnnotations.initMocks(this);
+ DialerApplication.setContextForTest(mContext);
+ when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mSharedPreferences);
FilteredNumberCompat.setIsEnabledForTest(true);
}
public void testIsNewFilteringEnabled_TestValueFalse() {
FilteredNumberCompat.setIsEnabledForTest(false);
- assertFalse(FilteredNumberCompat.useNewFiltering());
+ assertFalse(FilteredNumberCompat.canUseNewFiltering());
}
public void testIsNewFilteringEnabled_TestValueTrue() {
FilteredNumberCompat.setIsEnabledForTest(true);
- assertEquals(CompatUtils.isNCompatible(), FilteredNumberCompat.useNewFiltering());
+ assertEquals(CompatUtils.isNCompatible(), FilteredNumberCompat.canUseNewFiltering());
+ }
+
+ public void testHasMigratedToNewBlocking_False() {
+ assertFalse(FilteredNumberCompat.hasMigratedToNewBlocking());
+ }
+
+ public void testHasMigratedToNewBlocking_Migrated() {
+ when(mSharedPreferences
+ .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false))
+ .thenReturn(true);
+ assertTrue(FilteredNumberCompat.hasMigratedToNewBlocking());
}
public void testGetContentUri_NullId() {
- assertEquals(EXPECTED_BASE_URI, FilteredNumberCompat.getContentUri(null));
+ assertEquals(FilteredNumber.CONTENT_URI, FilteredNumberCompat.getContentUri(null));
}
- public void testGetContentUri() {
+ public void testGetContentUri_NotMigrated() {
+ assertEquals(ContentUris.withAppendedId(FilteredNumber.CONTENT_URI, 1),
+ FilteredNumberCompat.getContentUri(1));
+ }
+
+ public void testGetContentUri_Migrated() {
+ when(mSharedPreferences
+ .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false))
+ .thenReturn(true);
assertEquals(ContentUris.withAppendedId(EXPECTED_BASE_URI, 1),
FilteredNumberCompat.getContentUri(1));
}
@@ -87,12 +123,23 @@ public class FilteredNumberCompatTest extends AndroidTestCase {
} catch (NullPointerException e) {}
}
- public void testNewBlockNumberContentValues_N() {
+ public void testNewBlockNumberContentValues_N_NotMigrated() {
+ if (!CompatUtils.isNCompatible()) {
+ return;
+ }
+ assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, null, null),
+ FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, null, null));
+ }
+
+ public void testNewBlockNumberContentValues_N_Migrated() {
if (!CompatUtils.isNCompatible()) {
return;
}
ContentValues contentValues = new ContentValues();
contentValues.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, NON_E164_NUMBER);
+ when(mSharedPreferences
+ .getBoolean(FilteredNumberCompat.HAS_MIGRATED_TO_NEW_BLOCKING_KEY, false))
+ .thenReturn(true);
assertEquals(contentValues, FilteredNumberCompat.newBlockNumberContentValues(
NON_E164_NUMBER,
null, null));