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.Context; 19 import android.content.Intent; 20 import android.content.pm.LauncherActivityInfo; 21 import android.content.pm.LauncherApps; 22 import android.content.pm.LauncherApps.ShortcutQuery; 23 import android.graphics.drawable.Drawable; 24 import android.os.Bundle; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.util.DisplayMetrics; 28 import android.util.Log; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.View.OnClickListener; 32 import android.view.ViewGroup; 33 import android.widget.BaseAdapter; 34 import android.widget.Button; 35 import android.widget.ImageView; 36 import android.widget.TextView; 37 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.Comparator; 41 import java.util.List; 42 43 public class AppListFragment extends MyBaseListFragment { 44 private AppAdapter mAdapter; 45 46 @Override 47 public void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 50 mAdapter = new AppAdapter(getActivity()); 51 setListAdapter(mAdapter); 52 } 53 54 @Override 55 protected void refreshList() { 56 Log.d(Global.TAG, "Loading apps and shortcuts..."); 57 58 final List<LauncherActivityInfo> apps = new ArrayList<>(); 59 60 for (UserHandle user : mUserManager.getUserProfiles()) { 61 apps.addAll(mLauncherApps.getActivityList(null, user)); 62 } 63 Collections.sort(apps, sLauncherIconComparator); 64 65 Log.d(Global.TAG, "Apps and shortcuts loaded."); 66 67 mAdapter.setList(apps); 68 } 69 70 private static final Comparator<LauncherActivityInfo> sLauncherIconComparator = 71 (LauncherActivityInfo l1, LauncherActivityInfo l2) -> { 72 int ret = 0; 73 ret = l1.getLabel().toString().compareTo(l2.getLabel().toString()); 74 if (ret != 0) return ret; 75 76 // TODO Don't rely on hashCode being the user-id. 77 ret = l1.getUser().hashCode() - l2.getUser().hashCode(); 78 if (ret != 0) return ret; 79 80 return 0; 81 }; 82 83 public class AppAdapter extends BaseAdapter implements OnClickListener { 84 private final Context mContext; 85 private final LayoutInflater mInflater; 86 private final UserManager mUserManager; 87 private final LauncherApps mLauncherApps; 88 private List<LauncherActivityInfo> mList; 89 90 public AppAdapter(Context context) { 91 mContext = context; 92 mInflater = mContext.getSystemService(LayoutInflater.class); 93 mUserManager = mContext.getSystemService(UserManager.class); 94 mLauncherApps = mContext.getSystemService(LauncherApps.class); 95 } 96 97 public void setList(List<LauncherActivityInfo> list) { 98 mList = list; 99 notifyDataSetChanged(); 100 } 101 102 @Override 103 public int getCount() { 104 return mList == null ? 0 : mList.size(); 105 } 106 107 @Override 108 public LauncherActivityInfo getItem(int position) { 109 return mList == null ? null : mList.get(position); 110 } 111 112 @Override 113 public long getItemId(int position) { 114 return position; 115 } 116 117 @Override 118 public boolean hasStableIds() { 119 return false; 120 } 121 122 @Override 123 public boolean areAllItemsEnabled() { 124 return true; 125 } 126 127 @Override 128 public boolean isEnabled(int position) { 129 return true; 130 } 131 132 @Override 133 public View getView(int position, View convertView, ViewGroup parent) { 134 final View view; 135 if (convertView != null) { 136 view = convertView; 137 } else { 138 view = mInflater.inflate(R.layout.list_item, null); 139 } 140 141 bindView(view, getItem(position)); 142 143 return view; 144 } 145 146 public void bindView(View view, LauncherActivityInfo ai) { 147 { 148 final View v = view.findViewById(R.id.launch); 149 150 v.setTag(ai); 151 152 v.setOnClickListener(this); 153 v.setVisibility(View.VISIBLE); 154 } 155 { 156 final Button v = (Button) view.findViewById(R.id.action2); 157 158 v.setTag(ai); 159 160 v.setVisibility(View.INVISIBLE); 161 try { 162 if (mUserManager.isUserUnlocked(ai.getUser()) 163 && mLauncherApps.hasShortcutHostPermission()) { 164 mQuery.setPackage(ai.getComponentName().getPackageName()); 165 mQuery.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC 166 | ShortcutQuery.FLAG_MATCH_PINNED 167 | ShortcutQuery.FLAG_MATCH_MANIFEST 168 | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY); 169 mQuery.setActivity(ai.getComponentName()); 170 171 if (mLauncherApps.getShortcuts(mQuery, ai.getUser()).size() > 0) { 172 v.setOnClickListener(this); 173 v.setVisibility(View.VISIBLE); 174 v.setText("Shortcuts"); 175 } 176 } 177 } catch (Exception e) { 178 Log.w(Global.TAG, "Caught exception", e); 179 } 180 } 181 182 final TextView line1 = (TextView) view.findViewById(R.id.line1); 183 final TextView line2 = (TextView) view.findViewById(R.id.line2); 184 185 line1.setText(ai.getLabel()); 186 187 // TODO Do it on worker thread 188 final Drawable icon = ai.getBadgedIcon(DisplayMetrics.DENSITY_DEFAULT); 189 final ImageView image = (ImageView) view.findViewById(R.id.image); 190 image.setImageDrawable(icon); 191 } 192 193 @Override 194 public void onClick (View v){ 195 final LauncherActivityInfo ai = (LauncherActivityInfo) v.getTag(); 196 switch (v.getId()) { 197 case R.id.launch: 198 try { 199 mLauncherApps.startMainActivity(ai.getComponentName(), ai.getUser(), 200 null, null); 201 } catch (Exception e) { 202 Global.showToast(getContext(), e.getMessage()); 203 } 204 return; 205 case R.id.action2: 206 showShortcutsForPackage(ai); 207 return; 208 } 209 } 210 } 211 212 private void showShortcutsForPackage(LauncherActivityInfo ai) { 213 final Intent i = PackageShortcutActivity.getLaunchIntent( 214 getActivity(), 215 ai.getComponentName().getPackageName(), 216 ai.getComponentName(), 217 ai.getUser(), 218 ai.getLabel()); 219 getActivity().startActivity(i); 220 } 221 }