Merge "Import translations. DO NOT MERGE"
diff --git a/InCallUI/src/com/android/incallui/CallerInfo.java b/InCallUI/src/com/android/incallui/CallerInfo.java
index f270678..f3d0e07 100644
--- a/InCallUI/src/com/android/incallui/CallerInfo.java
+++ b/InCallUI/src/com/android/incallui/CallerInfo.java
@@ -322,7 +322,7 @@
                 info.userType = ContactsUtils.determineUserType(directoryId, contactId);
 
                 info.nameAlternative = ContactInfoHelper.lookUpDisplayNameAlternative(
-                        context, info.lookupKeyOrNull, info.userType);
+                        context, info.lookupKeyOrNull, info.userType, directoryId);
             }
             cursor.close();
         }
diff --git a/InCallUI/src/com/android/incallui/InCallPresenter.java b/InCallUI/src/com/android/incallui/InCallPresenter.java
index 2a3fd3e..609eceb 100644
--- a/InCallUI/src/com/android/incallui/InCallPresenter.java
+++ b/InCallUI/src/com/android/incallui/InCallPresenter.java
@@ -42,7 +42,6 @@
 
 import com.android.contacts.common.GeoUtil;
 import com.android.contacts.common.compat.CompatUtils;
-import com.android.contacts.common.compat.SdkVersionOverride;
 import com.android.contacts.common.compat.telecom.TelecomManagerCompat;
 import com.android.contacts.common.interactions.TouchPointManager;
 import com.android.contacts.common.testing.NeededForTesting;
@@ -174,6 +173,9 @@
     private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
         public void onCallStateChanged(int state, String incomingNumber) {
             if (state == TelephonyManager.CALL_STATE_RINGING) {
+                if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
+                    return;
+                }
                 // Check if the number is blocked, to silence the ringer.
                 String countryIso = GeoUtil.getCurrentCountryIso(mContext);
                 mFilteredQueryHandler.isBlockedNumber(
@@ -497,10 +499,7 @@
     }
 
     public void onCallAdded(final android.telecom.Call call) {
-        // Check if call should be blocked.
-        if (!TelecomCallUtil.isEmergencyCall(call)
-                && !FilteredNumbersUtil.hasRecentEmergencyCall(mContext)
-                && call.getState() == android.telecom.Call.STATE_RINGING) {
+        if (shouldAttemptBlocking(call)) {
             maybeBlockCall(call);
         } else {
             mCallList.onCallAdded(call);
@@ -511,6 +510,22 @@
         call.registerCallback(mCallCallback);
     }
 
+    private boolean shouldAttemptBlocking(android.telecom.Call call) {
+        if (call.getState() != android.telecom.Call.STATE_RINGING) {
+            return false;
+        }
+        if (TelecomCallUtil.isEmergencyCall(call)) {
+            Log.i(this, "Not attempting to block incoming emergency call");
+            return false;
+        }
+        if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
+            Log.i(this, "Not attempting to block incoming call due to recent emergency call");
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call
      * to the CallList so it can proceed as normal. There is a timeout, so if the function for
@@ -549,8 +564,8 @@
                         mCallList.onCallAdded(call);
                     }
                 } else {
+                    Log.i(this, "Rejecting incoming call from blocked number");
                     call.reject(false, null);
-                    Log.d(this, "checkForBlockedCall: " + Log.pii(number) + " blocked.");
                     Logger.logInteraction(InteractionEvent.CALL_BLOCKED);
 
                     mFilteredQueryHandler.incrementFilteredCount(id);
diff --git a/src/com/android/dialer/calllog/CallLogFragment.java b/src/com/android/dialer/calllog/CallLogFragment.java
index d01537d..67b72a5 100644
--- a/src/com/android/dialer/calllog/CallLogFragment.java
+++ b/src/com/android/dialer/calllog/CallLogFragment.java
@@ -474,13 +474,9 @@
         // We don't want to update any call data when keyguard is on because the user has likely not
         // seen the new calls yet.
         // This might be called before onCreate() and thus we need to check null explicitly.
-        if (mKeyguardManager != null && !mKeyguardManager.inKeyguardRestrictedInputMode()) {
-            // On either of the transitions we update the missed call and voicemail notifications.
-            // While exiting we additionally consume all missed calls (by marking them as read).
-            mCallLogQueryHandler.markNewCallsAsOld();
-            if (mCallTypeFilter == Calls.VOICEMAIL_TYPE) {
-                CallLogNotificationsHelper.updateVoicemailNotifications(getActivity());
-            }
+        if (mKeyguardManager != null && !mKeyguardManager.inKeyguardRestrictedInputMode()
+                && mCallTypeFilter == Calls.VOICEMAIL_TYPE) {
+            CallLogNotificationsHelper.updateVoicemailNotifications(getActivity());
         }
     }
 
diff --git a/src/com/android/dialer/calllog/ContactInfoHelper.java b/src/com/android/dialer/calllog/ContactInfoHelper.java
index 6e84a92..abbddd2 100644
--- a/src/com/android/dialer/calllog/ContactInfoHelper.java
+++ b/src/com/android/dialer/calllog/ContactInfoHelper.java
@@ -14,6 +14,8 @@
 
 package com.android.dialer.calllog;
 
+import com.google.common.primitives.Longs;
+
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
@@ -33,6 +35,7 @@
 import com.android.contacts.common.ContactsUtils;
 import com.android.contacts.common.ContactsUtils.UserType;
 import com.android.contacts.common.compat.CompatUtils;
+import com.android.contacts.common.compat.DirectoryCompat;
 import com.android.contacts.common.util.Constants;
 import com.android.contacts.common.util.PermissionsUtil;
 import com.android.contacts.common.util.PhoneNumberHelper;
@@ -163,6 +166,9 @@
             return ContactInfo.EMPTY;
         }
 
+        final String directory = uri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
+        final Long directoryId = directory == null ? null : Longs.tryParse(directory);
+
         Cursor phoneLookupCursor = null;
         try {
             String[] projection = PhoneQuery.getPhoneLookupProjection(uri);
@@ -183,7 +189,7 @@
             String lookupKey = phoneLookupCursor.getString(PhoneQuery.LOOKUP_KEY);
             ContactInfo contactInfo = createPhoneLookupContactInfo(phoneLookupCursor, lookupKey);
             contactInfo.nameAlternative = lookUpDisplayNameAlternative(mContext, lookupKey,
-                    contactInfo.userType);
+                    contactInfo.userType, directoryId);
             return contactInfo;
         } finally {
             phoneLookupCursor.close();
@@ -210,11 +216,24 @@
     }
 
     public static String lookUpDisplayNameAlternative(Context context, String lookupKey,
-            @UserType long userType) {
+            @UserType long userType, @Nullable Long directoryId) {
         // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
         if (lookupKey == null || userType == ContactsUtils.USER_TYPE_WORK) {
             return null;
         }
+
+        if (directoryId != null) {
+            // Query {@link Contacts#CONTENT_LOOKUP_URI} with work lookup key is not allowed.
+            if (DirectoryCompat.isEnterpriseDirectoryId(directoryId)) {
+                return null;
+            }
+
+            // Skip this to avoid an extra remote network call for alternative name
+            if (DirectoryCompat.isRemoteDirectory(directoryId)) {
+                return null;
+            }
+        }
+
         final Uri uri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
         Cursor cursor = null;
         try {
@@ -224,6 +243,9 @@
             if (cursor != null && cursor.moveToFirst()) {
                 return cursor.getString(PhoneQuery.NAME_ALTERNATIVE);
             }
+        } catch (IllegalArgumentException e) {
+            // Avoid dialer crash when lookup key is not valid
+            Log.e(TAG, "IllegalArgumentException in lookUpDisplayNameAlternative", e);
         } finally {
             if (cursor != null) {
                 cursor.close();
diff --git a/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java b/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java
index e53eb78..df385f4 100644
--- a/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java
+++ b/tests/src/com/android/dialer/calllog/ContactInfoHelperTest.java
@@ -17,6 +17,7 @@
 package com.android.dialer.calllog;
 
 import android.net.Uri;
+import android.provider.ContactsContract;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.PhoneLookup;
 import android.test.AndroidTestCase;
@@ -101,14 +102,14 @@
 
     public void testLookupDisplayNameAlternative_NullLookup() {
         Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, null,
-                ContactsUtils.USER_TYPE_CURRENT));
+                ContactsUtils.USER_TYPE_CURRENT, null));
     }
 
     public void testLookupDisplayNameAlternative_NoResults() {
         setUpQueryExpectations(displayNameAlternativeUri,
                 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION);
         Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext,
-                TEST_LOOKUP_KEY, ContactsUtils.USER_TYPE_CURRENT));
+                TEST_LOOKUP_KEY, ContactsUtils.USER_TYPE_CURRENT, null));
         mContext.verify();
     }
 
@@ -117,10 +118,32 @@
                 PhoneQuery.DISPLAY_NAME_ALTERNATIVE_PROJECTION, TEST_DISPLAY_NAME_ALTERNATIVE_ROW);
         Assert.assertEquals(TEST_DISPLAY_NAME_ALTERNATIVE,
                 mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY
-                , ContactsUtils.USER_TYPE_CURRENT));
+                , ContactsUtils.USER_TYPE_CURRENT, null));
         mContext.verify();
     }
 
+    public void testLookupDisplayNameAlternative_EnterpriseLocalDirectory() {
+        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY,
+                ContactsUtils.USER_TYPE_WORK, ContactsContract.Directory.ENTERPRISE_DEFAULT));
+        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY,
+                ContactsUtils.USER_TYPE_CURRENT, ContactsContract.Directory.ENTERPRISE_DEFAULT));
+    }
+
+    public void testLookupDisplayNameAlternative_EnterpriseRemoteDirectory() {
+        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY,
+                ContactsUtils.USER_TYPE_WORK,
+                ContactsContract.Directory.ENTERPRISE_DEFAULT + 10));
+        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, TEST_LOOKUP_KEY,
+                ContactsUtils.USER_TYPE_CURRENT,
+                ContactsContract.Directory.ENTERPRISE_DEFAULT + 10));
+    }
+
+    public void testLookupDisplayNameAlternative_PersonalRemoteDirectory() {
+        Assert.assertNull(mContactInfoHelper.lookUpDisplayNameAlternative(mContext, null,
+                ContactsUtils.USER_TYPE_CURRENT,
+                ContactsContract.Directory.DEFAULT + 10));
+    }
+
     /*
     * Sets up query expectations to return the given row for all queries for the given
     * uri and projection. If row is null, an empty cursor is returned for query calls