summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrandon Maxwell <maxwelb@google.com>2016-02-24 18:39:42 -0800
committerBrandon Maxwell <maxwelb@google.com>2016-03-01 10:18:40 -0800
commit9baf4e56672d8beeed6bdd9d1cd9456b32cf83ba (patch)
tree8504070a647e6cdb07ad3eb50bb6a637520e8017
parent6a87f4406af8ba5861c025bab12333ab6d40ae19 (diff)
Implementing Compat class for Filtering
+ FilteredNumberCompat contains the logic needed to switch between the Dialer implemented number filtering and the new number filtering. + Direct uses of the Dialer Filtering code should be replaced to use the fields in this class Bug=26664600 Change-Id: I42db3da4b5ed124a88488713f56ccab7b2290309
-rw-r--r--src-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java32
-rw-r--r--src-pre-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java30
-rw-r--r--src/com/android/dialer/compat/FilteredNumberCompat.java189
-rw-r--r--tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java149
4 files changed, 400 insertions, 0 deletions
diff --git a/src-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java b/src-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java
new file mode 100644
index 000000000..6c1625360
--- /dev/null
+++ b/src-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 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.dialer.compat;
+
+import android.net.Uri;
+import android.provider.BlockedNumberContract.BlockedNumbers;
+
+public class BlockedNumbersSdkCompat {
+
+ public static final Uri CONTENT_URI = BlockedNumbers.CONTENT_URI;
+
+ public static final String _ID = BlockedNumbers.COLUMN_ID;
+
+ public static final String COLUMN_ORIGINAL_NUMBER = BlockedNumbers.COLUMN_ORIGINAL_NUMBER;
+
+ public static final String E164_NUMBER = BlockedNumbers.COLUMN_E164_NUMBER;
+
+}
diff --git a/src-pre-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java b/src-pre-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java
new file mode 100644
index 000000000..cbdc59516
--- /dev/null
+++ b/src-pre-N/com/android/dialer/compat/BlockedNumbersSdkCompat.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2016 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.dialer.compat;
+
+import android.net.Uri;
+
+public class BlockedNumbersSdkCompat {
+
+ public static final Uri CONTENT_URI = null;
+
+ public static final String _ID = null;
+
+ public static final String COLUMN_ORIGINAL_NUMBER = null;
+
+ public static final String E164_NUMBER = null;
+}
diff --git a/src/com/android/dialer/compat/FilteredNumberCompat.java b/src/com/android/dialer/compat/FilteredNumberCompat.java
new file mode 100644
index 000000000..c70df71b7
--- /dev/null
+++ b/src/com/android/dialer/compat/FilteredNumberCompat.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2016 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.dialer.compat;
+
+import com.google.common.base.Preconditions;
+
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.net.Uri;
+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.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 java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Compatibility class to encapsulate logic to switch between call blocking using
+ * {@link com.android.dialer.database.FilteredNumberContract} and using
+ * {@link android.provider.BlockedNumberContract}. This class should be used rather than explicitly
+ * referencing columns from either contract class in situations where both blocking solutions may be
+ * used.
+ */
+public class FilteredNumberCompat {
+
+ // Flag to enable feature.
+ // TODO(maxwelb) remove when ready to enable new filtering.
+ private static final boolean isNewFilteringEnabled = false;
+ private static Boolean isEnabledForTest;
+
+ /**
+ * @return The column name for ID in the filtered number database.
+ */
+ public static String getIdColumnName() {
+ return useNewFiltering() ? BlockedNumbersSdkCompat._ID : FilteredNumberColumns._ID;
+ }
+
+ /**
+ * @return The column name for type in the filtered number database. Will be {@code null} for
+ * the framework blocking implementation.
+ */
+ @Nullable
+ public static String getTypeColumnName() {
+ return useNewFiltering() ? null : FilteredNumberColumns.TYPE;
+ }
+
+ /**
+ * @return The column name for source in the filtered number database. Will be {@code null} for
+ * the framework blocking implementation
+ */
+ @Nullable
+ public static String getSourceColumnName() {
+ return useNewFiltering() ? null : FilteredNumberColumns.SOURCE;
+ }
+
+ /**
+ * @return The column name for the original number in the filtered number database.
+ */
+ public static String getOriginalNumberColumnName() {
+ return useNewFiltering() ? BlockedNumbersSdkCompat.COLUMN_ORIGINAL_NUMBER
+ : FilteredNumberColumns.NUMBER;
+ }
+
+ /**
+ * @return The column name for country iso in the filtered number database. Will be {@code null}
+ * the framework blocking implementation
+ */
+ @Nullable
+ public static String getCountryIsoColumnName() {
+ return useNewFiltering() ? null : FilteredNumberColumns.COUNTRY_ISO;
+ }
+
+ /**
+ * @return The column name for the e164 formatted number in the filtered number database.
+ */
+ public static String getE164NumberColumnName() {
+ return useNewFiltering() ? BlockedNumbersSdkCompat.E164_NUMBER
+ : FilteredNumberColumns.NORMALIZED_NUMBER;
+ }
+
+ /**
+ * @return {@code true} if the new filtering is enabled, {@code false} otherwise.
+ */
+ public static boolean useNewFiltering() {
+ if (isEnabledForTest != null) {
+ return CompatUtils.isNCompatible() && isEnabledForTest;
+ }
+ return CompatUtils.isNCompatible() && isNewFilteringEnabled;
+ }
+
+ @NeededForTesting
+ public static void setIsEnabledForTest(Boolean isEnabled) {
+ isEnabledForTest = isEnabled;
+ }
+
+ /**
+ * Gets the content {@link Uri} for number filtering.
+ *
+ * @param id The optional id to append with the base content uri.
+ * @return The Uri for number filtering.
+ */
+ public static Uri getContentUri(@Nullable Integer id) {
+ if (id == null) {
+ return getBaseUri();
+ }
+ return ContentUris.withAppendedId(getBaseUri(), id);
+ }
+
+
+ private static Uri getBaseUri() {
+ return useNewFiltering() ? BlockedNumbersSdkCompat.CONTENT_URI : FilteredNumber.CONTENT_URI;
+ }
+
+ /**
+ * Removes any null column names from the given projection array. This method is intended to be
+ * used to strip out any column names that aren't available in every version of number blocking.
+ * Example:
+ * {@literal
+ * getContext().getContentResolver().query(
+ * someUri,
+ * // Filtering ensures that no non-existant columns are queried
+ * FilteredNumberCompat.filter(new String[] {FilteredNumberCompat.getIdColumnName(),
+ * FilteredNumberCompat.getTypeColumnName()},
+ * FilteredNumberCompat.getE164NumberColumnName() + " = ?",
+ * new String[] {e164Number});
+ * }
+ *
+ * @param projection The projection array.
+ * @return The filtered projection array.
+ */
+ @Nullable
+ public static String[] filter(@Nullable String[] projection) {
+ if (projection == null) {
+ return null;
+ }
+ List<String> filtered = new ArrayList<>();
+ for (String column : projection) {
+ if (column != null) {
+ filtered.add(column);
+ }
+ }
+ return filtered.toArray(new String[filtered.size()]);
+ }
+
+ /**
+ * Creates a new {@link ContentValues} suitable for inserting in the filtered number table.
+ *
+ * @param number The unformatted number to insert.
+ * @param e164Number (optional) The number to insert formatted to E164 standard.
+ * @param countryIso (optional) The country iso to use to format the number.
+ * @return The ContentValues to insert.
+ * @throws NullPointerException If number is null.
+ */
+ public static ContentValues newBlockNumberContentValues(String number,
+ @Nullable String e164Number, @Nullable String countryIso) {
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(getOriginalNumberColumnName(), Preconditions.checkNotNull(number));
+ if (!useNewFiltering()) {
+ if (e164Number == null) {
+ e164Number = PhoneNumberUtils.formatNumberToE164(number, countryIso);
+ }
+ contentValues.put(getE164NumberColumnName(), e164Number);
+ contentValues.put(getCountryIsoColumnName(), countryIso);
+ contentValues.put(getTypeColumnName(), FilteredNumberTypes.BLOCKED_NUMBER);
+ contentValues.put(getSourceColumnName(), FilteredNumberSources.USER);
+ }
+ return contentValues;
+ }
+}
diff --git a/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java b/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java
new file mode 100644
index 000000000..b9eb21424
--- /dev/null
+++ b/tests/src/com/android/dialer/compat/FilteredNumberCompatTest.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2016 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.dialer.compat;
+
+import android.content.ContentUris;
+import android.content.ContentValues;
+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.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 java.util.Arrays;
+
+@SmallTest
+public class FilteredNumberCompatTest extends AndroidTestCase {
+
+ private static final String E164_NUMBER = "+16502530000";
+ private static final String NON_E164_NUMBER = "6502530000";
+ private static final String COUNTRY_ISO = "US";
+
+ private static final Uri EXPECTED_BASE_URI = CompatUtils.isNCompatible()
+ ? BlockedNumbers.CONTENT_URI : FilteredNumber.CONTENT_URI;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ FilteredNumberCompat.setIsEnabledForTest(true);
+ }
+
+ public void testIsNewFilteringEnabled_TestValueFalse() {
+ FilteredNumberCompat.setIsEnabledForTest(false);
+ assertFalse(FilteredNumberCompat.useNewFiltering());
+ }
+
+ public void testIsNewFilteringEnabled_TestValueTrue() {
+ FilteredNumberCompat.setIsEnabledForTest(true);
+ assertEquals(CompatUtils.isNCompatible(), FilteredNumberCompat.useNewFiltering());
+ }
+
+ public void testGetContentUri_NullId() {
+ assertEquals(EXPECTED_BASE_URI, FilteredNumberCompat.getContentUri(null));
+ }
+
+ public void testGetContentUri() {
+ assertEquals(ContentUris.withAppendedId(EXPECTED_BASE_URI, 1),
+ FilteredNumberCompat.getContentUri(1));
+ }
+
+ public void testFilter_NullProjection() {
+ assertNull(FilteredNumberCompat.filter(null));
+ }
+
+ public void testFilter_NoNulls() {
+ assertArrayEquals(new String[] {"a", "b", "c"},
+ FilteredNumberCompat.filter(new String[] {"a", "b", "c"}));
+ }
+
+ public void testFilter_WithNulls() {
+ assertArrayEquals(new String[] {"a", "b"},
+ FilteredNumberCompat.filter(new String[] {"a", null, "b"}));
+ }
+
+ public void testNewBlockNumberContentValues_NullNumber() {
+ try {
+ FilteredNumberCompat.newBlockNumberContentValues(null, null, null);
+ fail();
+ } catch (NullPointerException e) {}
+ }
+
+ public void testNewBlockNumberContentValues_N() {
+ if (!CompatUtils.isNCompatible()) {
+ return;
+ }
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, NON_E164_NUMBER);
+ assertEquals(contentValues, FilteredNumberCompat.newBlockNumberContentValues(
+ NON_E164_NUMBER,
+ null, null));
+ }
+
+ public void testNewBlockNumberContentValues_N_Disabled() {
+ if (!CompatUtils.isNCompatible()) {
+ return;
+ }
+ FilteredNumberCompat.setIsEnabledForTest(false);
+ assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, E164_NUMBER, COUNTRY_ISO),
+ FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, E164_NUMBER, COUNTRY_ISO));
+ }
+
+ public void testNewBlockNumberContentValues_M_NullE164() {
+ if (CompatUtils.isNCompatible()) {
+ return;
+ }
+ assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, E164_NUMBER, COUNTRY_ISO),
+ FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, null, COUNTRY_ISO));
+ }
+
+ public void testNewBlockNumberContentValues_M_NullCountryIso() {
+ if (CompatUtils.isNCompatible()) {
+ return;
+ }
+ assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, E164_NUMBER, null),
+ FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, E164_NUMBER, null));
+ }
+
+ public void testNewBlockNumberContentValues_M_NullE164AndCountryIso() {
+ if (CompatUtils.isNCompatible()) {
+ return;
+ }
+ // Number can't be formatted properly without country code
+ assertEquals(newExpectedContentValuesM(NON_E164_NUMBER, null, null),
+ FilteredNumberCompat.newBlockNumberContentValues(NON_E164_NUMBER, null, null));
+ }
+
+ private ContentValues newExpectedContentValuesM(String number, String e164Number,
+ String countryIso) {
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(FilteredNumberColumns.NUMBER, number);
+ contentValues.put(FilteredNumberColumns.NORMALIZED_NUMBER, e164Number);
+ contentValues.put(FilteredNumberColumns.COUNTRY_ISO, countryIso);
+ contentValues.put(FilteredNumberColumns.TYPE, FilteredNumberTypes.BLOCKED_NUMBER);
+ contentValues.put(FilteredNumberColumns.SOURCE, FilteredNumberSources.USER);
+ return contentValues;
+ }
+
+ private void assertArrayEquals(String[] expected, String[] actual) {
+ assertEquals(Arrays.toString(expected), Arrays.toString(actual));
+ }
+}