Merge "Use O APIs for shortcuts from QuickContact" into ub-contactsdialer-i-dev
am: d2b97ea42b

Change-Id: I2ed67f11d42426660de7a2f2fef3cf4cca5c03c6
diff --git a/src/com/android/contacts/DynamicShortcuts.java b/src/com/android/contacts/DynamicShortcuts.java
index c7d8456..0901e56 100644
--- a/src/com/android/contacts/DynamicShortcuts.java
+++ b/src/com/android/contacts/DynamicShortcuts.java
@@ -67,7 +67,7 @@
  * Currently it adds shortcuts for the top 3 contacts in the {@link Contacts#CONTENT_STREQUENT_URI}
  *
  * Usage: DynamicShortcuts.initialize should be called during Application creation. This will
- * schedule a Job to keep the shortcuts up-to-date so no further interations should be necessary.
+ * schedule a Job to keep the shortcuts up-to-date so no further interactions should be necessary.
  */
 @TargetApi(Build.VERSION_CODES.N_MR1)
 public class DynamicShortcuts {
@@ -289,11 +289,21 @@
         return builder;
     }
 
+    public ShortcutInfo getQuickContactShortcutInfo(long id, String lookupKey, String displayName) {
+        final ShortcutInfo.Builder builder = builderForContactShortcut(id, lookupKey, displayName);
+        addIconForContact(id, lookupKey, displayName, builder);
+        return builder.build();
+    }
+
     private void addIconForContact(Cursor cursor, ShortcutInfo.Builder builder) {
         final long id = cursor.getLong(0);
         final String lookupKey = cursor.getString(1);
         final String displayName = cursor.getString(2);
+        addIconForContact(id, lookupKey, displayName, builder);
+    }
 
+    private void addIconForContact(long id, String lookupKey, String displayName,
+            ShortcutInfo.Builder builder) {
         final Bitmap bitmap = getContactPhoto(id);
         if (bitmap != null) {
             builder.setIcon(Icon.createWithBitmap(bitmap));
diff --git a/src/com/android/contacts/quickcontact/QuickContactActivity.java b/src/com/android/contacts/quickcontact/QuickContactActivity.java
index 2007e10..e0986d5 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.ShortcutManager;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -74,6 +75,7 @@
 import android.support.v4.app.ActivityCompat;
 import android.support.v4.content.LocalBroadcastManager;
 import android.support.v4.content.res.ResourcesCompat;
+import android.support.v4.os.BuildCompat;
 import android.support.v7.graphics.Palette;
 import android.telecom.PhoneAccount;
 import android.telecom.TelecomManager;
@@ -2645,22 +2647,32 @@
 
                     @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);
-                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
-
-                        // Send a toast to give feedback to the user that a shortcut to this
-                        // contact was added to the launcher.
-                        final String displayName = shortcutIntent
-                                .getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
-                        final String toastMessage = TextUtils.isEmpty(displayName)
-                                ? getString(R.string.createContactShortcutSuccessful_NoName)
-                                : getString(R.string.createContactShortcutSuccessful, displayName);
-                        Toast.makeText(QuickContactActivity.this, toastMessage,
-                                Toast.LENGTH_SHORT).show();
+                        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 {
+                            // Broadcast the shortcutIntent to the launcher to create a
+                            // shortcut to this contact
+                            shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
+                            QuickContactActivity.this.sendBroadcast(shortcutIntent);
+                            // Send a toast to give feedback to the user that a shortcut to this
+                            // contact was added to the launcher.
+                            final String displayName = shortcutIntent
+                                    .getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
+                            final String toastMessage = TextUtils.isEmpty(displayName)
+                                    ? getString(R.string.createContactShortcutSuccessful_NoName)
+                                    : getString(R.string.createContactShortcutSuccessful,
+                                            displayName);
+                            Toast.makeText(QuickContactActivity.this, toastMessage,
+                                    Toast.LENGTH_SHORT).show();
+                        }
                     }
-
                 });
         builder.createContactShortcutIntent(mContactData.getLookupUri());
     }
@@ -2670,6 +2682,13 @@
                 mContactData.isDirectoryEntry()) {
             return false;
         }
+
+        if (BuildCompat.isAtLeastO()) {
+            final ShortcutManager manager = (ShortcutManager)
+                    getSystemService(Context.SHORTCUT_SERVICE);
+            return manager.isRequestPinShortcutSupported();
+        }
+
         final Intent createShortcutIntent = new Intent();
         createShortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
         final List<ResolveInfo> receivers = getPackageManager()