Fix up blocked photo scenarios.

+ Show blocked drawable in CallDetailActivity. Rearrange logic in
the activity to facilitate updating the contact photo and block
item action in accordance to blocked state.
+ Fix bug in FilterNumberDialogFragment where callback was not
always invoked because of final/scope issues where it was possible
for the callback to be unintentionally null and not invoked.
+ Clear blocked id cache when pausing the call log adapter, so that
after changing the blocked state in the call detail activity the
changes will be reflected in the call log.

Bug: 24871853
Change-Id: I1d58b1e0c222ead90fa7b6c30a95bc3254a14776
diff --git a/src/com/android/dialer/CallDetailActivity.java b/src/com/android/dialer/CallDetailActivity.java
index 654a96f..54367b1 100644
--- a/src/com/android/dialer/CallDetailActivity.java
+++ b/src/com/android/dialer/CallDetailActivity.java
@@ -106,17 +106,9 @@
 
             // All calls are from the same number and same contact, so pick the first detail.
             mDetails = details[0];
-            mNumber = TextUtils.isEmpty(mDetails.number) ?
-                    null : mDetails.number.toString();
+            mNumber = TextUtils.isEmpty(mDetails.number) ? null : mDetails.number.toString();
             mDisplayNumber = mDetails.displayNumber;
             final int numberPresentation = mDetails.numberPresentation;
-            final Uri contactUri = mDetails.contactUri;
-            final Uri photoUri = mDetails.photoUri;
-            final PhoneAccountHandle accountHandle = mDetails.accountHandle;
-
-            // Cache the details about the phone number.
-            mIsVoicemailNumber =
-                    PhoneNumberUtil.isVoicemailNumber(mContext, accountHandle, mNumber);
 
             final CharSequence callLocationOrType = getNumberTypeOrLocation(mDetails);
 
@@ -137,7 +129,8 @@
                 }
             }
 
-            String accountLabel = PhoneAccountUtils.getAccountLabel(mContext, accountHandle);
+            String accountLabel =
+                    PhoneAccountUtils.getAccountLabel(mContext, mDetails.accountHandle);
             if (!TextUtils.isEmpty(accountLabel)) {
                 mAccountLabel.setText(accountLabel);
                 mAccountLabel.setVisibility(View.VISIBLE);
@@ -150,8 +143,10 @@
             mCallButton.setVisibility(canPlaceCallsTo ? View.VISIBLE : View.GONE);
 
             final boolean isSipNumber = PhoneNumberUtil.isSipNumber(mNumber);
+            final boolean isVoicemailNumber =
+                    PhoneNumberUtil.isVoicemailNumber(mContext, mDetails.accountHandle, mNumber);
             final boolean showEditNumberBeforeCallAction =
-                    canPlaceCallsTo && !isSipNumber && !mIsVoicemailNumber;
+                    canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
             mEditBeforeCallActionItem.setVisibility(
                     showEditNumberBeforeCallAction ? View.VISIBLE : View.GONE);
 
@@ -160,31 +155,13 @@
             mReportActionItem.setVisibility(
                     showReportAction ? View.VISIBLE : View.GONE);
 
-            updateBlockActionItem();
             invalidateOptionsMenu();
 
             mHistoryList.setAdapter(
                     new CallDetailHistoryAdapter(mContext, mInflater, mCallTypeHelper, details));
 
-            String lookupKey = contactUri == null ? null
-                    : UriUtils.getLookupKeyFromUri(contactUri);
+            updatePhotoAndBlockActionItem();
 
-            final boolean isBusiness = mContactInfoHelper.isBusiness(mDetails.sourceType);
-
-            final int contactType =
-                    mIsVoicemailNumber ? ContactPhotoManager.TYPE_VOICEMAIL :
-                    isBusiness ? ContactPhotoManager.TYPE_BUSINESS :
-                    ContactPhotoManager.TYPE_DEFAULT;
-
-            String nameForDefaultImage;
-            if (TextUtils.isEmpty(mDetails.name)) {
-                nameForDefaultImage = mDetails.displayNumber;
-            } else {
-                nameForDefaultImage = mDetails.name.toString();
-            }
-
-            loadContactPhotos(
-                    contactUri, photoUri, nameForDefaultImage, lookupKey, contactType);
             findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
         }
 
@@ -217,7 +194,6 @@
     private PhoneCallDetails mDetails;
     protected String mNumber;
     private Uri mVoicemailUri;
-    private boolean mIsVoicemailNumber;
     private String mDefaultCountryIso;
     private String mDisplayNumber;
 
@@ -275,6 +251,9 @@
         mCallButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
+                if (TextUtils.isEmpty(mNumber)) {
+                    return;
+                }
                 mContext.startActivity(
                         new CallIntentBuilder(mNumber)
                                 .setCallInitiationType(LogState.INITIATION_CALL_DETAILS)
@@ -339,21 +318,6 @@
         return uris;
     }
 
-    /** Load the contact photos and places them in the corresponding views. */
-    private void loadContactPhotos(Uri contactUri, Uri photoUri, String displayName,
-            String lookupKey, int contactType) {
-
-        final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey,
-                contactType, true /* isCircular */);
-
-        mQuickContactBadge.assignContactUri(contactUri);
-        mQuickContactBadge.setContentDescription(
-                mResources.getString(R.string.description_contact_details, displayName));
-
-        mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, photoUri,
-                false /* darkTheme */, true /* isCircular */, request);
-    }
-
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         final MenuItem deleteMenuItem = menu.add(
@@ -399,7 +363,7 @@
                         mBlockedNumberId,
                         null /* normalizedNumber */,
                         mNumber,
-                        null /* countryIso */,
+                        mDetails.countryIso,
                         mDisplayNumber,
                         R.id.call_detail,
                         getFragmentManager(),
@@ -420,32 +384,81 @@
 
     @Override
     public void onChangeFilteredNumberSuccess() {
-        updateBlockActionItem();
+        updatePhotoAndBlockActionItem();
     }
 
     @Override
     public void onChangeFilteredNumberUndo() {
-        updateBlockActionItem();
+        updatePhotoAndBlockActionItem();
     }
 
-    private void updateBlockActionItem() {
+    private void updatePhotoAndBlockActionItem() {
         if (mDetails == null) {
             return;
         }
 
-        mFilteredNumberAsyncQueryHandler.startBlockedQuery(new OnCheckBlockedListener() {
-            @Override
-            public void onCheckComplete(Integer id) {
-                mBlockedNumberId = id;
-                if (mBlockedNumberId == null) {
-                    mBlockNumberActionItem.setText(R.string.action_block_number);
-                } else {
-                    mBlockNumberActionItem.setText(R.string.action_unblock_number);
-                }
+        boolean failed = mFilteredNumberAsyncQueryHandler.startBlockedQuery(
+                new OnCheckBlockedListener() {
+                    @Override
+                    public void onCheckComplete(Integer id) {
+                        mBlockedNumberId = id;
 
-                mBlockNumberActionItem.setVisibility(View.VISIBLE);
-            }
-        }, null, mNumber, mDetails.countryIso);
+                        updateContactPhoto();
+                        updateBlockActionItem();
+                    }
+                }, null, mNumber, mDetails.countryIso);
+
+        if (failed) {
+            updateContactPhoto();
+            updateBlockActionItem();
+        }
+    }
+
+    // Loads and displays the contact photo.
+    private void updateContactPhoto() {
+        if (mDetails == null) {
+            return;
+        }
+
+        if (mBlockedNumberId != null) {
+            mQuickContactBadge.setImageDrawable(mContext.getDrawable(R.drawable.blocked_contact));
+            return;
+        }
+
+        final boolean isVoicemailNumber =
+                PhoneNumberUtil.isVoicemailNumber(mContext, mDetails.accountHandle, mNumber);
+        final boolean isBusiness = mContactInfoHelper.isBusiness(mDetails.sourceType);
+        int contactType = ContactPhotoManager.TYPE_DEFAULT;
+        if (isVoicemailNumber) {
+            contactType = ContactPhotoManager.TYPE_VOICEMAIL;
+        } else if (isBusiness) {
+            contactType = ContactPhotoManager.TYPE_BUSINESS;
+        }
+
+        final String displayName = TextUtils.isEmpty(mDetails.name)
+                ? mDetails.displayNumber : mDetails.name.toString();
+        final String lookupKey = mDetails.contactUri == null
+                ? null : UriUtils.getLookupKeyFromUri(mDetails.contactUri);
+
+        final DefaultImageRequest request =
+                new DefaultImageRequest(displayName, lookupKey, contactType, true /* isCircular */);
+
+        mQuickContactBadge.assignContactUri(mDetails.contactUri);
+        mQuickContactBadge.setContentDescription(
+                mResources.getString(R.string.description_contact_details, displayName));
+
+        mContactPhotoManager.loadDirectoryPhoto(mQuickContactBadge, mDetails.photoUri,
+                false /* darkTheme */, true /* isCircular */, request);
+    }
+
+    private void updateBlockActionItem() {
+        if (mBlockedNumberId == null) {
+            mBlockNumberActionItem.setText(R.string.action_block_number);
+        } else {
+            mBlockNumberActionItem.setText(R.string.action_unblock_number);
+        }
+
+        mBlockNumberActionItem.setVisibility(View.VISIBLE);
     }
 
     private void closeSystemDialogs() {
diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java
index 7f3a779..0ee7e93 100644
--- a/src/com/android/dialer/calllog/CallLogAdapter.java
+++ b/src/com/android/dialer/calllog/CallLogAdapter.java
@@ -314,6 +314,10 @@
 
     public void onPause() {
         pauseCache();
+
+        // Clear blocked id cache so that changes in blocked status will be reflected in the UI.
+        mBlockedIdCache.clear();
+
         if (mHiddenItemUri != null) {
             CallLogAsyncTaskUtil.deleteVoicemail(mContext, mHiddenItemUri, null);
         }
diff --git a/src/com/android/dialer/filterednumber/FilterNumberDialogFragment.java b/src/com/android/dialer/filterednumber/FilterNumberDialogFragment.java
index a1260ee..1e5294c 100644
--- a/src/com/android/dialer/filterednumber/FilterNumberDialogFragment.java
+++ b/src/com/android/dialer/filterednumber/FilterNumberDialogFragment.java
@@ -159,13 +159,14 @@
     private void blockNumber() {
         final String message = getBlockedMessage();
         final String undoMessage = getUnblockedMessage();
+        final Callback callback = mCallback;
 
         final OnUnblockNumberListener onUndoListener = new OnUnblockNumberListener() {
             @Override
             public void onUnblockComplete(int rows, ContentValues values) {
                 Snackbar.make(mParentView, undoMessage, Snackbar.LENGTH_LONG).show();
-                if (mCallback != null) {
-                    mCallback.onChangeFilteredNumberUndo();
+                if (callback != null) {
+                    callback.onChangeFilteredNumberUndo();
                 }
             }
         };
@@ -185,8 +186,8 @@
                         .setAction(R.string.block_number_undo, undoListener)
                         .show();
 
-                if (mCallback != null) {
-                    mCallback.onChangeFilteredNumberSuccess();
+                if (callback != null) {
+                    callback.onChangeFilteredNumberSuccess();
                 }
             }
         };
@@ -201,13 +202,14 @@
     private void unblockNumber() {
         final String message = getUnblockedMessage();
         final String undoMessage = getBlockedMessage();
+        final Callback callback = mCallback;
 
         final OnBlockNumberListener onUndoListener = new OnBlockNumberListener() {
             @Override
             public void onBlockComplete(final Uri uri) {
                 Snackbar.make(mParentView, undoMessage, Snackbar.LENGTH_LONG).show();
-                if (mCallback != null) {
-                    mCallback.onChangeFilteredNumberUndo();
+                if (callback != null) {
+                    callback.onChangeFilteredNumberUndo();
                 }
             }
         };
@@ -227,8 +229,8 @@
                         .setAction(R.string.block_number_undo, undoListener)
                         .show();
 
-                if (mCallback != null) {
-                    mCallback.onChangeFilteredNumberSuccess();
+                if (callback != null) {
+                    callback.onChangeFilteredNumberSuccess();
                 }
             }
         }, getArguments().getInt(ARG_BLOCK_ID));