1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.pm.shortcutlauncherdemo; 17 18 import android.content.Intent; 19 import android.content.pm.LauncherActivityInfo; 20 import android.content.pm.LauncherApps.ShortcutQuery; 21 import android.os.UserHandle; 22 import android.util.Log; 23 import android.view.View; 24 import android.view.View.OnClickListener; 25 import android.widget.Button; 26 27 import java.util.List; 28 29 public class AppListFragment extends BaseActivityListFragment { 30 31 @Override 32 protected List<LauncherActivityInfo> getActivities(UserHandle user) { 33 return mLauncherApps.getActivityList(null, user); 34 } 35 36 @Override 37 protected void onBindAction2(Button v, LauncherActivityInfo ai, OnClickListener listener) { 38 try { 39 if (mUserManager.isUserUnlocked(ai.getUser()) 40 && mLauncherApps.hasShortcutHostPermission()) { 41 mQuery.setPackage(ai.getComponentName().getPackageName()); 42 mQuery.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC 43 | ShortcutQuery.FLAG_MATCH_PINNED 44 | ShortcutQuery.FLAG_MATCH_MANIFEST 45 | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY); 46 mQuery.setActivity(ai.getComponentName()); 47 48 if (mLauncherApps.getShortcuts(mQuery, ai.getUser()).size() > 0) { 49 v.setOnClickListener(listener); 50 v.setVisibility(View.VISIBLE); 51 v.setText("Shortcuts"); 52 } 53 } 54 } catch (Exception e) { 55 Log.w(Global.TAG, "Caught exception", e); 56 } 57 } 58 59 @Override 60 protected void onLaunch(LauncherActivityInfo ai) { 61 mLauncherApps.startMainActivity(ai.getComponentName(), ai.getUser(), null, null); 62 } 63 64 @Override 65 protected void onAction2(LauncherActivityInfo ai) { 66 final Intent i = PackageShortcutActivity.getLaunchIntent( 67 getActivity(), 68 ai.getComponentName().getPackageName(), 69 ai.getComponentName(), 70 ai.getUser(), 71 ai.getLabel()); 72 getActivity().startActivity(i); 73 }}