Fix NPEs in shortcuts am: 9b1b93772d

Change-Id: Iedd641ae9bbb18a9a1cbf5d64806ca24ce869e7d
diff --git a/src/com/android/contacts/DynamicShortcuts.java b/src/com/android/contacts/DynamicShortcuts.java
index fc0d05a..ac950d9 100644
--- a/src/com/android/contacts/DynamicShortcuts.java
+++ b/src/com/android/contacts/DynamicShortcuts.java
@@ -297,6 +297,7 @@
         final ShortcutInfo.Builder builder = new ShortcutInfo.Builder(mContext, id)
                 .setIntent(action)
                 .setIcon(icon)
+                .setExtras(extras)
                 .setDisabledMessage(mContext.getString(R.string.dynamic_shortcut_disabled_message));
 
         setLabel(builder, label);
@@ -305,6 +306,9 @@
 
     public ShortcutInfo getQuickContactShortcutInfo(long id, String lookupKey, String displayName) {
         final ShortcutInfo.Builder builder = builderForContactShortcut(id, lookupKey, displayName);
+        if (builder == null) {
+            return null;
+        }
         addIconForContact(id, lookupKey, displayName, builder);
         return builder.build();
     }
diff --git a/src/com/android/contacts/ShortcutIntentBuilder.java b/src/com/android/contacts/ShortcutIntentBuilder.java
index e90e786..b3bd85d 100644
--- a/src/com/android/contacts/ShortcutIntentBuilder.java
+++ b/src/com/android/contacts/ShortcutIntentBuilder.java
@@ -276,6 +276,9 @@
     private void createContactShortcutIntent(Uri contactUri, String contentType, String displayName,
             String lookupKey, byte[] bitmapData) {
         Intent intent = null;
+        if (TextUtils.isEmpty(displayName)) {
+            displayName = mContext.getResources().getString(R.string.missing_name);
+        }
         if (BuildCompat.isAtLeastO()) {
             final long contactId = ContentUris.parseId(contactUri);
             final ShortcutManager sm = (ShortcutManager)
@@ -283,12 +286,11 @@
             final DynamicShortcuts dynamicShortcuts = new DynamicShortcuts(mContext);
             final ShortcutInfo shortcutInfo = dynamicShortcuts.getQuickContactShortcutInfo(
                     contactId, lookupKey, displayName);
-            intent = sm.createShortcutResultIntent(shortcutInfo);
+            if (shortcutInfo != null) {
+                intent = sm.createShortcutResultIntent(shortcutInfo);
+            }
         }
         final Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey);
-        if (TextUtils.isEmpty(displayName)) {
-            displayName = mContext.getResources().getString(R.string.missing_name);
-        }
 
         final Intent shortcutIntent = ImplicitIntentsUtil.getIntentForQuickContactLauncherShortcut(
                 mContext, contactUri);
@@ -346,7 +348,9 @@
             final DynamicShortcuts dynamicShortcuts = new DynamicShortcuts(mContext);
             final ShortcutInfo shortcutInfo = dynamicShortcuts.getActionShortcutInfo(
                     id, displayName, shortcutIntent, compatAdaptiveIcon.toIcon());
-            intent = sm.createShortcutResultIntent(shortcutInfo);
+            if (shortcutInfo != null) {
+                intent = sm.createShortcutResultIntent(shortcutInfo);
+            }
         }
 
         intent = intent == null ? new Intent() : intent;
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 3a69f23..5da7502 100644
--- a/src/com/android/contacts/quickcontact/QuickContactActivity.java
+++ b/src/com/android/contacts/quickcontact/QuickContactActivity.java
@@ -35,6 +35,7 @@
 import android.content.Loader;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.content.pm.ShortcutInfo;
 import android.content.pm.ShortcutManager;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
@@ -2697,21 +2698,26 @@
      * Creates a launcher shortcut with the current contact.
      */
     private void createLauncherShortcutWithContact() {
-        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
-                new OnShortcutIntentCreatedListener() {
+        if (BuildCompat.isAtLeastO()) {
+            final ShortcutManager shortcutManager = (ShortcutManager)
+                    getSystemService(SHORTCUT_SERVICE);
+            final DynamicShortcuts shortcuts =
+                    new DynamicShortcuts(QuickContactActivity.this);
+            String displayName = mContactData.getDisplayName();
+            if (displayName == null) {
+                displayName = getString(R.string.missing_name);
+            }
+            final ShortcutInfo shortcutInfo = shortcuts.getQuickContactShortcutInfo(
+                    mContactData.getId(), mContactData.getLookupKey(), displayName);
+            if (shortcutInfo != null) {
+                shortcutManager.requestPinShortcut(shortcutInfo, null);
+            }
+        } else {
+            final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
+                    new OnShortcutIntentCreatedListener() {
 
-                    @Override
-                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
-                        if (BuildCompat.isAtLeastO()) {
-                            final ShortcutManager shortcutManager = (ShortcutManager)
-                                    getSystemService(SHORTCUT_SERVICE);
-                            final DynamicShortcuts shortcuts =
-                                    new DynamicShortcuts(QuickContactActivity.this);
-                            shortcutManager.requestPinShortcut(
-                                    shortcuts.getQuickContactShortcutInfo(
-                                            mContactData.getId(), mContactData.getLookupKey(),
-                                            mContactData.getDisplayName()), null);
-                        } else {
+                        @Override
+                        public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
                             // Broadcast the shortcutIntent to the launcher to create a
                             // shortcut to this contact
                             shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
@@ -2727,9 +2733,9 @@
                             Toast.makeText(QuickContactActivity.this, toastMessage,
                                     Toast.LENGTH_SHORT).show();
                         }
-                    }
-                });
-        builder.createContactShortcutIntent(mContactData.getLookupUri());
+                    });
+            builder.createContactShortcutIntent(mContactData.getLookupUri());
+        }
     }
 
     private boolean isShortcutCreatable() {