• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.shortcutdemo;
17 
18 import android.content.Context;
19 import android.content.pm.LauncherApps;
20 import android.content.pm.PackageManager;
21 import android.content.pm.PackageManager.NameNotFoundException;
22 import android.content.pm.ShortcutInfo;
23 import android.content.res.Resources;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.os.ParcelFileDescriptor;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.View.OnClickListener;
33 import android.view.ViewGroup;
34 import android.widget.BaseAdapter;
35 import android.widget.Button;
36 import android.widget.ImageView;
37 import android.widget.TextView;
38 
39 import java.io.IOException;
40 import java.util.List;
41 
42 public abstract class ShortcutAdapter extends BaseAdapter implements OnClickListener {
43     public static final String TAG = "ShortcutDemo";
44 
45     private final Context mContext;
46     private final LayoutInflater mInflater;
47     private LauncherApps mLauncherApps;
48     private final AppLabelCache mAppLabelCache;
49     private List<ShortcutInfo> mShortcuts;
50 
ShortcutAdapter(Context context)51     public ShortcutAdapter(Context context) {
52         mContext = context;
53         mAppLabelCache = new AppLabelCache(mContext);
54         mInflater = mContext.getSystemService(LayoutInflater.class);
55         mLauncherApps = mContext.getSystemService(LauncherApps.class);
56     }
57 
getLayoutId()58     protected abstract int getLayoutId();
getText1Id()59     protected abstract int getText1Id();
getText2Id()60     protected abstract int getText2Id();
getImageId()61     protected abstract int getImageId();
getLaunchId()62     protected abstract int getLaunchId();
getAction2Id()63     protected abstract int getAction2Id();
64 
showLine2()65     protected boolean showLine2() {
66         return true;
67     }
68 
showLaunch(ShortcutInfo si)69     protected boolean showLaunch(ShortcutInfo si) {
70         return false;
71     }
72 
showAction2(ShortcutInfo si)73     protected boolean showAction2(ShortcutInfo si) {
74         return false;
75     }
76 
getAction2Text(ShortcutInfo si)77     protected String getAction2Text(ShortcutInfo si) {
78         return "Action2";
79     }
80 
onLaunchClicked(ShortcutInfo si)81     protected void onLaunchClicked(ShortcutInfo si) {
82     }
83 
onAction2Clicked(ShortcutInfo si)84     protected void onAction2Clicked(ShortcutInfo si) {
85     }
86 
setShortcuts(List<ShortcutInfo> shortcuts)87     public void setShortcuts(List<ShortcutInfo> shortcuts) {
88         mShortcuts = shortcuts;
89         notifyDataSetChanged();
90     }
91 
getShortcuts()92     public List<ShortcutInfo> getShortcuts() {
93         return mShortcuts;
94     }
95 
96     @Override
getCount()97     public int getCount() {
98         return mShortcuts == null ? 0 : mShortcuts.size();
99     }
100 
101     @Override
getItem(int position)102     public Object getItem(int position) {
103         return mShortcuts.get(position);
104     }
105 
106     @Override
getItemId(int position)107     public long getItemId(int position) {
108         return position;
109     }
110 
111     @Override
hasStableIds()112     public boolean hasStableIds() {
113         return false;
114     }
115 
116     @Override
areAllItemsEnabled()117     public boolean areAllItemsEnabled() {
118         return true;
119     }
120 
121     @Override
isEnabled(int position)122     public boolean isEnabled(int position) {
123         return true;
124     }
125 
126     @Override
getView(int position, View convertView, ViewGroup parent)127     public View getView(int position, View convertView, ViewGroup parent) {
128         final View view;
129         if (convertView != null) {
130             view = convertView;
131         } else {
132             view = mInflater.inflate(getLayoutId(), null);
133         }
134 
135         bindView(view, position, mShortcuts.get(position));
136 
137         return view;
138     }
139 
bindView(View view, int position, ShortcutInfo si)140     public void bindView(View view, int position, ShortcutInfo si) {
141         {
142             final View v = view.findViewById(getLaunchId());
143             v.setVisibility(View.GONE);
144             if (showLaunch(si)) {
145                 v.setOnClickListener(this);
146                 v.setVisibility(View.VISIBLE);
147             }
148         }
149         {
150             final Button v = (Button) view.findViewById(getAction2Id());
151             v.setVisibility(View.GONE);
152             if (showAction2(si)) {
153                 v.setOnClickListener(this);
154                 v.setVisibility(View.VISIBLE);
155                 v.setText(getAction2Text(si));
156             }
157         }
158 
159         final TextView line1 = (TextView) view.findViewById(getText1Id());
160         final TextView line2 = (TextView) view.findViewById(getText2Id());
161 
162         view.setTag(si);
163 
164         line1.setText(si.getShortLabel());
165         if (showLine2()) {
166             line2.setText(
167                     si.getId() + (si.isDynamic() ? " [dynamic]" : "")
168                             + (si.isDeclaredInManifest() ? " [manifest]" : "")
169                             + (si.isPinned() ? " [pinned]" : "") + "\n"
170                             + (si.isEnabled() ? "" : si.getDisabledMessage()) + "\n"
171                             + "Long label: " + si.getLongLabel() + "\n"
172                             + "App: " + mAppLabelCache.getAppLabel(si.getPackage()));
173             line2.setVisibility(View.VISIBLE);
174         } else {
175             line2.setVisibility(View.GONE);
176         }
177 
178         // view.setBackgroundColor(si.isPinned() ? Color.rgb(255, 255, 192) : Color.WHITE);
179 
180         // TODO Do it on worker thread
181         final ImageView image = (ImageView) view.findViewById(getImageId());
182         if (!mLauncherApps.hasShortcutHostPermission()) {
183             image.setVisibility(View.GONE);
184         } else {
185             image.setVisibility(View.VISIBLE);
186             image.setImageDrawable(mLauncherApps.getShortcutBadgedIconDrawable(si,
187                     mContext.getResources().getDisplayMetrics().densityDpi));
188         }
189     }
190 
191     @Override
onClick(View v)192     public void onClick(View v) {
193         final ShortcutInfo si = (ShortcutInfo)(((View) v.getParent()).getTag());
194         if (v.getId() == getLaunchId()) {
195             onLaunchClicked(si);
196         } else if (v.getId() == getAction2Id()) {
197             onAction2Clicked(si);
198         }
199     }
200 }
201