Add setting for hiding blocked calls.

+ Add settings switch to blocked number settings.
+ Add utility for checking/setting whether calls should be blocked.
+ Filter out blocked calls in call log query, if appropriate.
+ Refresh call log fragment after a change in setting.

Followup work: delete voicemails for calls by blocked numbers after
the time when the user blocked them.

Bug: 24134270
Change-Id: I85088697559ef6fb446b82617f14f1c6fa893df1
diff --git a/res/layout/blocked_number_fragment.xml b/res/layout/blocked_number_fragment.xml
index bb4b7f2..906e8a1 100644
--- a/res/layout/blocked_number_fragment.xml
+++ b/res/layout/blocked_number_fragment.xml
@@ -25,6 +25,36 @@
     <android.support.v7.widget.CardView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
+        android:layout_marginBottom="8dp"
+        card_view:cardCornerRadius="0dp">
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="horizontal"
+            android:gravity="center_vertical"
+            android:padding="16dp"
+            android:paddingEnd="8dp">
+
+            <TextView
+                android:layout_width="0dp"
+                android:layout_height="wrap_content"
+                android:layout_weight="1"
+                android:text="@string/blocked_call_settings_hide_setting"
+                style="@style/BlockedNumbersDescriptionTextStyle" />
+
+            <Switch android:id="@+id/hide_blocked_calls_switch"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_marginLeft="40dp" />
+
+        </LinearLayout>
+
+    </android.support.v7.widget.CardView>
+
+    <android.support.v7.widget.CardView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
         card_view:cardCornerRadius="0dp">
 
         <LinearLayout
diff --git a/res/layout/blocked_number_header.xml b/res/layout/blocked_number_header.xml
index 39f5322..e16efbc 100644
--- a/res/layout/blocked_number_header.xml
+++ b/res/layout/blocked_number_header.xml
@@ -33,11 +33,9 @@
             android:layout_height="wrap_content"
             android:text="@string/blocked_call_settings_import_description"
             android:textSize="@dimen/blocked_number_settings_description_text_size"
-            android:lineSpacingMultiplier="1.2"
-            android:paddingTop="8dp"
-            android:paddingBottom="8dp"
             android:paddingStart="@dimen/blocked_number_container_padding"
-            android:paddingEnd="@dimen/blocked_number_container_padding" />
+            android:paddingEnd="@dimen/blocked_number_container_padding"
+            style="@style/BlockedNumbersDescriptionTextStyle" />
 
         <Button android:id="@+id/import_button"
             android:layout_width="wrap_content"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0b7d82c..333a99b 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -833,6 +833,12 @@
         You previously marked some callers to be automatically sent to voicemail via other apps.
     </string>
 
+    <!-- Text for a setting to hide blocked calls and automatically delete voicemail from block
+         numbers. [CHAR LIMIT=NONE] -->
+    <string name="blocked_call_settings_hide_setting">
+        Hide calls and delete voicemails from blocked numbers.
+    </string>
+
     <!-- Label for button to view numbers of contacts previous marked to be sent to voicemail.
          [CHAR_LIMIT=20] -->
     <string name="blocked_call_settings_view_numbers_button">View Numbers</string>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 7828a20..533b88e 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -324,4 +324,11 @@
         <item name="android:paddingStart">@dimen/button_horizontal_padding</item>
         <item name="android:textColor">@color/dialer_flat_button_text_color</item>
     </style>
+
+    <style name="BlockedNumbersDescriptionTextStyle">
+        <item name="android:lineSpacingMultiplier">1.2</item>
+        <item name="android:paddingTop">8dp</item>
+        <item name="android:paddingBottom">8dp</item>
+        <item name="android:textSize">@dimen/blocked_number_settings_description_text_size</item>
+    </style>
 </resources>
diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java
index 0ee7e93..3dccf27 100644
--- a/src/com/android/dialer/calllog/CallLogAdapter.java
+++ b/src/com/android/dialer/calllog/CallLogAdapter.java
@@ -260,10 +260,11 @@
         mCallLogListItemHelper =
                 new CallLogListItemHelper(phoneCallDetailsHelper, resources, mTelecomCallLogCache);
         mCallLogGroupBuilder = new CallLogGroupBuilder(this);
-        mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
-        maybeShowVoicemailPromoCard();
         mFilteredNumberAsyncQueryHandler =
                 new FilteredNumberAsyncQueryHandler(mContext.getContentResolver());
+
+        mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
+        maybeShowVoicemailPromoCard();
     }
 
     public void onSaveInstanceState(Bundle outState) {
@@ -306,7 +307,7 @@
         mBlockedIdCache.clear();
     }
 
-    public void startCache() {
+    public void onResume() {
         if (PermissionsUtil.hasPermission(mContext, android.Manifest.permission.READ_CONTACTS)) {
             mContactInfoCache.start();
         }
diff --git a/src/com/android/dialer/calllog/CallLogFragment.java b/src/com/android/dialer/calllog/CallLogFragment.java
index dff213b..369730d 100644
--- a/src/com/android/dialer/calllog/CallLogFragment.java
+++ b/src/com/android/dialer/calllog/CallLogFragment.java
@@ -42,6 +42,7 @@
 import com.android.contacts.common.GeoUtil;
 import com.android.contacts.common.util.PermissionsUtil;
 import com.android.dialer.R;
+import com.android.dialer.filterednumber.FilteredNumbersUtil;
 import com.android.dialer.util.EmptyLoader;
 import com.android.dialer.voicemail.VoicemailPlaybackPresenter;
 import com.android.dialer.widget.EmptyContentView;
@@ -124,6 +125,7 @@
     private boolean mRefreshDataRequired = true;
 
     private boolean mHasReadCallLogPermission = false;
+    private boolean mShouldHideBlockedCalls = false;
 
     // Exactly same variable is in Fragment as a package private.
     private boolean mMenuVisible = true;
@@ -209,6 +211,8 @@
         resolver.registerContentObserver(Status.CONTENT_URI, true, mVoicemailStatusObserver);
         setHasOptionsMenu(true);
 
+        mShouldHideBlockedCalls = FilteredNumbersUtil.shouldHideBlockedCalls(getActivity());
+
         if (mCallTypeFilter == Calls.VOICEMAIL_TYPE) {
             mVoicemailPlaybackPresenter = VoicemailPlaybackPresenter
                     .getInstance(activity, state);
@@ -339,9 +343,14 @@
             mRefreshDataRequired = true;
             updateEmptyMessage(mCallTypeFilter);
         }
+        if (mShouldHideBlockedCalls != FilteredNumbersUtil.shouldHideBlockedCalls(getActivity())) {
+            mShouldHideBlockedCalls = !mShouldHideBlockedCalls;
+            mRefreshDataRequired = true;
+        }
+
         mHasReadCallLogPermission = hasReadCallLogPermission;
         refreshData();
-        mAdapter.startCache();
+        mAdapter.onResume();
 
         rescheduleDisplayUpdate();
 
diff --git a/src/com/android/dialer/calllog/CallLogQueryHandler.java b/src/com/android/dialer/calllog/CallLogQueryHandler.java
index 81e49d2..01489c1 100644
--- a/src/com/android/dialer/calllog/CallLogQueryHandler.java
+++ b/src/com/android/dialer/calllog/CallLogQueryHandler.java
@@ -36,6 +36,7 @@
 
 import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;
 import com.android.contacts.common.util.PermissionsUtil;
+import com.android.dialer.filterednumber.FilteredNumbersUtil;
 import com.android.dialer.util.AppCompatConstants;
 import com.android.dialer.util.TelecomUtil;
 import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
@@ -177,7 +178,7 @@
 
         if (callType > CALL_TYPE_ALL) {
             where.append(" AND ");
-            where.append(String.format("(%s = ?)", Calls.TYPE));
+            where.append("(" + Calls.TYPE + " = ?)");
             selectionArgs.add(Integer.toString(callType));
         } else {
             where.append(" AND NOT ");
@@ -186,10 +187,18 @@
 
         if (newerThan > 0) {
             where.append(" AND ");
-            where.append(String.format("(%s > ?)", Calls.DATE));
+            where.append("(" + Calls.DATE + " > ?)");
             selectionArgs.add(Long.toString(newerThan));
         }
 
+        final boolean shouldHideBlockedCalls =
+                FilteredNumbersUtil.shouldHideBlockedCalls(mContext);
+        if (shouldHideBlockedCalls) {
+            where.append(" AND ");
+            where.append("(" + Calls.TYPE + " != ?)");
+            selectionArgs.add(Integer.toString(AppCompatConstants.CALLS_BLOCKED_TYPE));
+        }
+
         final int limit = (mLogLimit == -1) ? NUM_LOGS_TO_DISPLAY : mLogLimit;
         final String selection = where.length() > 0 ? where.toString() : null;
         Uri uri = TelecomUtil.getCallLogUri(mContext).buildUpon()
diff --git a/src/com/android/dialer/filterednumber/BlockedNumberFragment.java b/src/com/android/dialer/filterednumber/BlockedNumberFragment.java
index 43f734d..728d8b2 100644
--- a/src/com/android/dialer/filterednumber/BlockedNumberFragment.java
+++ b/src/com/android/dialer/filterednumber/BlockedNumberFragment.java
@@ -22,6 +22,8 @@
 import android.content.Loader;
 import android.database.Cursor;
 import android.os.Bundle;
+import android.widget.CompoundButton;
+import android.widget.Switch;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -36,6 +38,7 @@
 
     private BlockedNumberAdapter mAdapter;
 
+    private Switch mHideSettingSwitch;
     private View mImportSettings;
     private View mImportButton;
 
@@ -49,7 +52,13 @@
         }
         setListAdapter(mAdapter);
 
-        getActivity().findViewById(R.id.add_number_button).setOnClickListener(this);
+        mHideSettingSwitch = (Switch) getActivity().findViewById(R.id.hide_blocked_calls_switch);
+        mHideSettingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+                FilteredNumbersUtil.setShouldHideBlockedCalls(getActivity(), isChecked);
+            }
+        });
 
         mImportSettings = getActivity().findViewById(R.id.import_settings);
         mImportButton = getActivity().findViewById(R.id.import_button);
@@ -65,6 +74,8 @@
                         });
             }
         });
+
+        getActivity().findViewById(R.id.add_number_button).setOnClickListener(this);
     }
 
     @Override
@@ -91,6 +102,8 @@
                         mImportSettings.setVisibility(visibility);
                     }
                 });
+
+        mHideSettingSwitch.setChecked(FilteredNumbersUtil.shouldHideBlockedCalls(getActivity()));
     }
 
     @Override
diff --git a/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java b/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java
index 68f262c..0e162ac 100644
--- a/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java
+++ b/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.database.Cursor;
 import android.os.AsyncTask;
+import android.preference.PreferenceManager;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
@@ -42,6 +43,8 @@
  */
 public class FilteredNumbersUtil {
 
+    private static final String HIDE_BLOCKED_CALLS_PREF_KEY = "hide_blocked_calls";
+
     public interface CheckForSendToVoicemailContactListener {
         public void onComplete(boolean hasSendToVoicemailContact);
     }
@@ -213,6 +216,24 @@
         return isBlocked;
     }
 
+    public static boolean shouldHideBlockedCalls(Context context) {
+        if (context == null) {
+            return false;
+        }
+        return PreferenceManager.getDefaultSharedPreferences(context)
+                .getBoolean(FilteredNumbersUtil.HIDE_BLOCKED_CALLS_PREF_KEY, false);
+    }
+
+    public static void setShouldHideBlockedCalls(Context context, boolean shouldHide) {
+        if (context == null) {
+            return;
+        }
+        PreferenceManager.getDefaultSharedPreferences(context)
+                .edit()
+                .putBoolean(FilteredNumbersUtil.HIDE_BLOCKED_CALLS_PREF_KEY, shouldHide)
+                .apply();
+    }
+
     public static boolean canBlockNumber(Context context, String number) {
         if (PhoneNumberUtils.isEmergencyNumber(number)) {
             return false;