Fix demo launcher

- pinShortcuts() needs to be passed all pinned shortcut IDs from the target app,
but originally it was passing those from the target *activity*.

- Also LauncherApps throws IllegalStateException when work profile is off.
Catch runtime exceptions and show a message.

Change-Id: I5a97c808ed7d9aff17bd11aa4ed28ee0efc330f0
diff --git a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/AppListFragment.java b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/AppListFragment.java
index c328267..7b21424 100644
--- a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/AppListFragment.java
+++ b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/AppListFragment.java
@@ -15,17 +15,14 @@
  */
 package com.example.android.pm.shortcutlauncherdemo;
 
-import android.app.ListFragment;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.LauncherActivityInfo;
 import android.content.pm.LauncherApps;
 import android.content.pm.LauncherApps.ShortcutQuery;
-import android.content.pm.ShortcutInfo;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.UserHandle;
-import android.os.UserManager;
 import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.LayoutInflater;
@@ -36,7 +33,6 @@
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.TextView;
-import android.widget.Toast;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -159,19 +155,23 @@
                 v.setTag(ai);
 
                 v.setVisibility(View.INVISIBLE);
-                if (mLauncherApps.hasShortcutHostPermission()) {
-                    mQuery.setPackage(ai.getComponentName().getPackageName());
-                    mQuery.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC
-                            | ShortcutQuery.FLAG_MATCH_PINNED
-                            | ShortcutQuery.FLAG_MATCH_MANIFEST
-                            | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY);
-                    mQuery.setActivity(ai.getComponentName());
+                try {
+                    if (mLauncherApps.hasShortcutHostPermission()) {
+                        mQuery.setPackage(ai.getComponentName().getPackageName());
+                        mQuery.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC
+                                | ShortcutQuery.FLAG_MATCH_PINNED
+                                | ShortcutQuery.FLAG_MATCH_MANIFEST
+                                | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY);
+                        mQuery.setActivity(ai.getComponentName());
 
-                    if (mLauncherApps.getShortcuts(mQuery, ai.getUser()).size() > 0) {
-                        v.setOnClickListener(this);
-                        v.setVisibility(View.VISIBLE);
-                        v.setText("Shortcuts");
+                        if (mLauncherApps.getShortcuts(mQuery, ai.getUser()).size() > 0) {
+                            v.setOnClickListener(this);
+                            v.setVisibility(View.VISIBLE);
+                            v.setText("Shortcuts");
+                        }
                     }
+                } catch (Exception e) {
+                    Log.w(Global.TAG, "Caught exception", e);
                 }
             }
 
@@ -191,8 +191,12 @@
             final LauncherActivityInfo ai = (LauncherActivityInfo) v.getTag();
             switch (v.getId()) {
                 case R.id.launch:
-                    mLauncherApps.startMainActivity(ai.getComponentName(), ai.getUser(),
-                            null, null);
+                    try {
+                        mLauncherApps.startMainActivity(ai.getComponentName(), ai.getUser(),
+                                null, null);
+                    } catch (Exception e) {
+                        Global.showToast(getContext(), e.getMessage());
+                    }
                     return;
                 case R.id.action2:
                     showShortcutsForPackage(ai);
diff --git a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/Global.java b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/Global.java
index 63a469f..809b9a2 100644
--- a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/Global.java
+++ b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/Global.java
@@ -15,6 +15,13 @@
  */
 package com.example.android.pm.shortcutlauncherdemo;
 
+import android.content.Context;
+import android.widget.Toast;
+
 public class Global {
     public static final String TAG = "ShortcutLauncherDemo";
+
+    public static void showToast(Context context, String message) {
+        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
+    }
 }
diff --git a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutLauncherMain.java b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutLauncherMain.java
index 993fe4f..3364221 100644
--- a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutLauncherMain.java
+++ b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutLauncherMain.java
@@ -30,7 +30,6 @@
 import android.view.ViewGroup;
 
 public class ShortcutLauncherMain extends Activity {
-    private LauncherApps mLauncherApps;
     private ViewPager mPager;
     private PagerAdapter mPagerAdapter;
 
@@ -40,8 +39,6 @@
 
         setContentView(R.layout.main);
 
-        mLauncherApps = getSystemService(LauncherApps.class);
-
         mPager = (ViewPager) findViewById(R.id.pager);
         mPager.setOffscreenPageLimit(2);
         mPagerAdapter = new MyPagerAdapter(getFragmentManager());
diff --git a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutListFragment.java b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutListFragment.java
index 9e08a57..379c574 100644
--- a/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutListFragment.java
+++ b/samples/ShortcutDemo/launcher/src/com/example/android/pm/shortcutlauncherdemo/ShortcutListFragment.java
@@ -24,6 +24,7 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.text.TextUtils;
+import android.util.Log;
 import android.widget.Toast;
 
 import com.example.android.pm.shortcutdemo.ShortcutAdapter;
@@ -93,49 +94,62 @@
     private void togglePin(ShortcutInfo selected) {
         final String packageName = selected.getPackage();
 
-        final List<String> pinned = new ArrayList<>();
-        for (ShortcutInfo si : mAdapter.getShortcuts()) {
-            if (si.isPinned()
-                    && si.getPackage().equals(packageName)
-                    && si.getUserHandle().equals(selected.getUserHandle())) {
+        try {
+            final ShortcutQuery q = new ShortcutQuery()
+                    .setPackage(packageName)
+                    .setQueryFlags(ShortcutQuery.FLAG_MATCH_PINNED)
+                    ;
+
+            final List<String> pinned = new ArrayList<>();
+            for (ShortcutInfo si : mLauncherApps.getShortcuts(q, android.os.Process.myUserHandle())) {
                 pinned.add(si.getId());
             }
+            if (selected.isPinned()) {
+                pinned.remove(selected.getId());
+            } else {
+                pinned.add(selected.getId());
+            }
+            mLauncherApps.pinShortcuts(packageName, pinned, selected.getUserHandle());
+        } catch (Exception e) {
+            Global.showToast(getContext(), e.getMessage());
         }
-        if (selected.isPinned()) {
-            pinned.remove(selected.getId());
-        } else {
-            pinned.add(selected.getId());
-        }
-        mLauncherApps.pinShortcuts(packageName, pinned, selected.getUserHandle());
     }
 
     private void launch(ShortcutInfo si) {
-        mLauncherApps.startShortcut(si.getPackage(), si.getId(), null, null,
-                si.getUserHandle());
+        try {
+            mLauncherApps.startShortcut(si.getPackage(), si.getId(), null, null,
+                    si.getUserHandle());
+        } catch (Exception e) {
+            Global.showToast(getContext(), e.getMessage());
+        }
     }
 
     @Override
     protected void refreshList() {
-        if (!mLauncherApps.hasShortcutHostPermission()) {
-            return;
+        try {
+            if (!mLauncherApps.hasShortcutHostPermission()) {
+                return;
+            }
+
+            final List<ShortcutInfo> list = new ArrayList<>();
+
+            for (UserHandle user : getTargetUsers()) {
+                final Bundle b = getArguments();
+                mQuery.setQueryFlags(
+                        (b.getBoolean(ARG_INCLUDE_DYNAMIC) ? ShortcutQuery.FLAG_MATCH_DYNAMIC : 0) |
+                        (b.getBoolean(ARG_INCLUDE_MANIFEST) ? ShortcutQuery.FLAG_MATCH_MANIFEST : 0) |
+                        (b.getBoolean(ARG_INCLUDE_PINNED) ? ShortcutQuery.FLAG_MATCH_PINNED : 0));
+                mQuery.setPackage(b.getString(ARG_TARGET_PACKAGE));
+                mQuery.setActivity(b.getParcelable(ARG_TARGET_ACTIVITY));
+
+                list.addAll(mLauncherApps.getShortcuts(mQuery, user));
+            }
+            Collections.sort(list, mShortcutComparator);
+
+            mAdapter.setShortcuts(list);
+        } catch (Exception e) {
+            Log.w(Global.TAG, "Caught exception", e);
         }
-
-        final List<ShortcutInfo> list = new ArrayList<>();
-
-        for (UserHandle user : getTargetUsers()) {
-            final Bundle b = getArguments();
-            mQuery.setQueryFlags(
-                    (b.getBoolean(ARG_INCLUDE_DYNAMIC) ? ShortcutQuery.FLAG_MATCH_DYNAMIC : 0) |
-                    (b.getBoolean(ARG_INCLUDE_MANIFEST) ? ShortcutQuery.FLAG_MATCH_MANIFEST : 0) |
-                    (b.getBoolean(ARG_INCLUDE_PINNED) ? ShortcutQuery.FLAG_MATCH_PINNED : 0));
-            mQuery.setPackage(b.getString(ARG_TARGET_PACKAGE));
-            mQuery.setActivity(b.getParcelable(ARG_TARGET_ACTIVITY));
-
-            list.addAll(mLauncherApps.getShortcuts(mQuery, user));
-        }
-        Collections.sort(list, mShortcutComparator);
-
-        mAdapter.setShortcuts(list);
     }
 
     private final Comparator<ShortcutInfo> mShortcutComparator =