Delete voicemails from blocked users.

+ Delete voicemail when the notification is received if the number
is blocked and the "hide" setting is set to true.

+ Changed utility to take time into account; only block voicemail
notifications and delete voicemails if the voicemails are from
after the time when the number was blocked.

Bug: 24134270
Change-Id: I2f3b24d6291a6cec4a7d8530b00dd5a4656ab024
diff --git a/res/layout/blocked_number_fragment.xml b/res/layout/blocked_number_fragment.xml
index 906e8a1..c5760da 100644
--- a/res/layout/blocked_number_fragment.xml
+++ b/res/layout/blocked_number_fragment.xml
@@ -52,36 +52,28 @@
 
     </android.support.v7.widget.CardView>
 
-    <android.support.v7.widget.CardView
+
+    <LinearLayout
+        android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        card_view:cardCornerRadius="0dp">
+        android:background="@color/background_dialer_white">
 
-        <LinearLayout
-            android:orientation="vertical"
+        <ListView android:id="@+id/blocked_numbers_list"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:background="@color/background_dialer_white">
+            android:layout_height="match_parent"
+            android:layout_weight="1"
+            android:drawSelectorOnTop="false"
+            android:headerDividersEnabled="false" />
 
-            <include layout="@layout/blocked_number_header" />
+        <TextView android:id="@android:id/empty"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:paddingStart="@dimen/blocked_number_horizontal_margin"
+            android:paddingTop="@dimen/blocked_number_top_margin"
+            android:paddingBottom="@dimen/blocked_number_bottom_margin"
+            android:text="@string/listNoBlockedNumbers" />
 
-            <ListView android:id="@id/android:list"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:layout_weight="1"
-                android:drawSelectorOnTop="false"
-                android:headerDividersEnabled="false" />
-
-            <TextView android:id="@android:id/empty"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:paddingStart="@dimen/blocked_number_horizontal_margin"
-                android:paddingTop="@dimen/blocked_number_top_margin"
-                android:paddingBottom="@dimen/blocked_number_bottom_margin"
-                android:text="@string/listNoBlockedNumbers" />
-
-        </LinearLayout>
-
-    </android.support.v7.widget.CardView>
+    </LinearLayout>
 
 </LinearLayout>
diff --git a/res/layout/blocked_number_header.xml b/res/layout/blocked_number_header.xml
index e16efbc..0c8c086 100644
--- a/res/layout/blocked_number_header.xml
+++ b/res/layout/blocked_number_header.xml
@@ -13,7 +13,12 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<merge xmlns:android="http://schemas.android.com/apk/res/android">
+<android.support.v7.widget.CardView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:card_view="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    card_view:cardCornerRadius="0dp">
 
     <TextView android:id="@+id/textView"
         android:layout_width="wrap_content"
@@ -72,4 +77,4 @@
         android:layout_marginTop="8dp"
         android:text="@string/blockNumber" />
 
-</merge>
+</android.support.v7.widget.CardView>
diff --git a/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java b/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java
index 594ceeb..1987b80 100644
--- a/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java
+++ b/src/com/android/dialer/calllog/DefaultVoicemailNotifier.java
@@ -131,8 +131,13 @@
             NewCall newCall = itr.next();
 
             // Skip notifying for numbers which are blocked.
-            if (FilteredNumbersUtil.isBlocked(mContext, newCall.number, newCall.countryIso)) {
+            if (FilteredNumbersUtil.shouldBlockVoicemail(
+                    mContext, newCall.number, newCall.countryIso, newCall.dateMs)) {
                 itr.remove();
+
+                if (FilteredNumbersUtil.shouldHideBlockedCalls(mContext)) {
+                    mContext.getContentResolver().delete(newCall.voicemailUri, null, null);
+                }
                 continue;
             }
 
@@ -236,6 +241,7 @@
         public final String accountId;
         public final String transcription;
         public final String countryIso;
+        public final long dateMs;
 
         public NewCall(
                 Uri callsUri,
@@ -245,7 +251,8 @@
                 String accountComponentName,
                 String accountId,
                 String transcription,
-                String countryIso) {
+                String countryIso,
+                long dateMs) {
             this.callsUri = callsUri;
             this.voicemailUri = voicemailUri;
             this.number = number;
@@ -254,6 +261,7 @@
             this.accountId = accountId;
             this.transcription = transcription;
             this.countryIso = countryIso;
+            this.dateMs = dateMs;
         }
     }
 
@@ -284,7 +292,8 @@
             Calls.PHONE_ACCOUNT_COMPONENT_NAME,
             Calls.PHONE_ACCOUNT_ID,
             Calls.TRANSCRIPTION,
-            Calls.COUNTRY_ISO
+            Calls.COUNTRY_ISO,
+            Calls.DATE
         };
         private static final int ID_COLUMN_INDEX = 0;
         private static final int NUMBER_COLUMN_INDEX = 1;
@@ -294,6 +303,7 @@
         private static final int PHONE_ACCOUNT_ID_COLUMN_INDEX = 5;
         private static final int TRANSCRIPTION_COLUMN_INDEX = 6;
         private static final int COUNTRY_ISO_COLUMN_INDEX = 7;
+        private static final int DATE_COLUMN_INDEX = 8;
 
         private final ContentResolver mContentResolver;
         private final Context mContext;
@@ -345,7 +355,8 @@
                     cursor.getString(PHONE_ACCOUNT_COMPONENT_NAME_COLUMN_INDEX),
                     cursor.getString(PHONE_ACCOUNT_ID_COLUMN_INDEX),
                     cursor.getString(TRANSCRIPTION_COLUMN_INDEX),
-                    cursor.getString(COUNTRY_ISO_COLUMN_INDEX));
+                    cursor.getString(COUNTRY_ISO_COLUMN_INDEX),
+                    cursor.getLong(DATE_COLUMN_INDEX));
         }
     }
 
diff --git a/src/com/android/dialer/filterednumber/BlockedNumberFragment.java b/src/com/android/dialer/filterednumber/BlockedNumberFragment.java
index 728d8b2..06ae0e0 100644
--- a/src/com/android/dialer/filterednumber/BlockedNumberFragment.java
+++ b/src/com/android/dialer/filterednumber/BlockedNumberFragment.java
@@ -17,12 +17,14 @@
 
 import android.app.ListFragment;
 import android.app.LoaderManager;
+import android.content.Context;
 import android.content.CursorLoader;
 import android.content.Intent;
 import android.content.Loader;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.widget.CompoundButton;
+import android.widget.ListView;
 import android.widget.Switch;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -87,6 +89,12 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+
+        LayoutInflater inflater =
+                (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        ListView listView = (ListView) getActivity().findViewById(R.id.blocked_numbers_list);
+        listView.addHeaderView(inflater.inflate(R.layout.blocked_number_header, null));
+
         getLoaderManager().initLoader(0, null, this);
     }
 
diff --git a/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java b/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java
index 0e162ac..ac3d16e 100644
--- a/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java
+++ b/src/com/android/dialer/filterednumber/FilteredNumbersUtil.java
@@ -191,7 +191,8 @@
      * WARNING: This method should NOT be executed on the UI thread.
      * Use {@code FilteredNumberAsyncQueryHandler} to asynchronously check if a number is blocked.
      */
-    public static boolean isBlocked(Context context, String number, String countryIso) {
+    public static boolean shouldBlockVoicemail(
+            Context context, String number, String countryIso, long dateMs) {
         final String normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryIso);
         if (TextUtils.isEmpty(normalizedNumber)) {
             return false;
@@ -199,21 +200,26 @@
 
         final Cursor cursor = context.getContentResolver().query(
                 FilteredNumber.CONTENT_URI,
-                new String[] { FilteredNumberColumns._ID },
+                new String[] {
+                    FilteredNumberColumns.CREATION_TIME
+                },
                 FilteredNumberColumns.NORMALIZED_NUMBER + "=?",
                 new String[] { normalizedNumber },
                 null);
 
-        boolean isBlocked = false;
+        boolean shouldBlock = false;
         if (cursor != null) {
             try {
-                isBlocked = cursor.getCount() > 0;
+                cursor.moveToFirst();
+
+                // Block if number is found and it was added before this voicemail was received.
+                shouldBlock = cursor.getCount() > 0 && dateMs > cursor.getLong(0);
             } finally {
                 cursor.close();
             }
         }
 
-        return isBlocked;
+        return shouldBlock;
     }
 
     public static boolean shouldHideBlockedCalls(Context context) {