• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
17 package com.android.settings.shortcut;
18 
19 import android.app.LauncherActivity;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ActivityInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.content.pm.ShortcutInfo;
27 import android.content.pm.ShortcutManager;
28 import android.graphics.Bitmap;
29 import android.graphics.Bitmap.Config;
30 import android.graphics.Canvas;
31 import android.graphics.drawable.Drawable;
32 import android.graphics.drawable.Icon;
33 import android.graphics.drawable.LayerDrawable;
34 import android.net.ConnectivityManager;
35 import android.os.AsyncTask;
36 import android.support.annotation.VisibleForTesting;
37 import android.view.ContextThemeWrapper;
38 import android.view.LayoutInflater;
39 import android.view.View;
40 import android.view.View.MeasureSpec;
41 import android.widget.ImageView;
42 import android.widget.ListView;
43 
44 import com.android.internal.logging.nano.MetricsProto;
45 import com.android.settings.R;
46 import com.android.settings.Settings.TetherSettingsActivity;
47 import com.android.settings.overlay.FeatureFactory;
48 
49 import java.util.ArrayList;
50 import java.util.List;
51 
52 public class CreateShortcut extends LauncherActivity {
53 
54     @VisibleForTesting
55     static final String SHORTCUT_ID_PREFIX = "component-shortcut-";
56 
57     @Override
getTargetIntent()58     protected Intent getTargetIntent() {
59         return getBaseIntent().addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60     }
61 
62     @Override
onListItemClick(ListView l, View v, int position, long id)63     protected void onListItemClick(ListView l, View v, int position, long id) {
64         final ListItem item = itemForPosition(position);
65         logCreateShortcut(item.resolveInfo);
66         setResult(RESULT_OK, createResultIntent(intentForPosition(position),
67                 item.resolveInfo, item.label));
68         finish();
69     }
70 
71     @VisibleForTesting
createResultIntent(Intent shortcutIntent, ResolveInfo resolveInfo, CharSequence label)72     Intent createResultIntent(Intent shortcutIntent, ResolveInfo resolveInfo,
73             CharSequence label) {
74         shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
75         ShortcutManager sm = getSystemService(ShortcutManager.class);
76         ActivityInfo activityInfo = resolveInfo.activityInfo;
77 
78         Icon maskableIcon = activityInfo.icon != 0 ? Icon.createWithAdaptiveBitmap(
79                 createIcon(activityInfo.icon,
80                         R.layout.shortcut_badge_maskable,
81                         getResources().getDimensionPixelSize(R.dimen.shortcut_size_maskable))) :
82                 Icon.createWithResource(this, R.drawable.ic_launcher_settings);
83         String shortcutId = SHORTCUT_ID_PREFIX +
84                 shortcutIntent.getComponent().flattenToShortString();
85         ShortcutInfo info = new ShortcutInfo.Builder(this, shortcutId)
86                 .setShortLabel(label)
87                 .setIntent(shortcutIntent)
88                 .setIcon(maskableIcon)
89                 .build();
90         Intent intent = sm.createShortcutResultIntent(info);
91         if (intent == null) {
92             intent = new Intent();
93         }
94         intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
95                 Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_settings));
96         intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
97         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
98 
99         if (activityInfo.icon != 0) {
100             intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(activityInfo.icon,
101                     R.layout.shortcut_badge,
102                     getResources().getDimensionPixelSize(R.dimen.shortcut_size)));
103         }
104         return intent;
105     }
106 
logCreateShortcut(ResolveInfo info)107     private void logCreateShortcut(ResolveInfo info) {
108         if (info == null || info.activityInfo == null) {
109             return;
110         }
111         FeatureFactory.getFactory(this).getMetricsFeatureProvider().action(
112                 this, MetricsProto.MetricsEvent.ACTION_SETTINGS_CREATE_SHORTCUT,
113                 info.activityInfo.name);
114     }
115 
createIcon(int resource, int layoutRes, int size)116     private Bitmap createIcon(int resource, int layoutRes, int size) {
117         Context context = new ContextThemeWrapper(this, android.R.style.Theme_Material);
118         View view = LayoutInflater.from(context).inflate(layoutRes, null);
119         Drawable iconDrawable = getDrawable(resource);
120         if (iconDrawable instanceof LayerDrawable) {
121             iconDrawable = ((LayerDrawable) iconDrawable).getDrawable(1);
122         }
123         ((ImageView) view.findViewById(android.R.id.icon)).setImageDrawable(iconDrawable);
124 
125         int spec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
126         view.measure(spec, spec);
127         Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
128                 Config.ARGB_8888);
129         Canvas canvas = new Canvas(bitmap);
130         view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
131         view.draw(canvas);
132         return bitmap;
133     }
134 
135     @Override
onEvaluateShowIcons()136     protected boolean onEvaluateShowIcons() {
137         return false;
138     }
139 
140     @Override
onSetContentView()141     protected void onSetContentView() {
142         setContentView(R.layout.activity_list);
143     }
144 
145     /**
146      * Perform query on package manager for list items.  The default
147      * implementation queries for activities.
148      */
onQueryPackageManager(Intent queryIntent)149     protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) {
150         List<ResolveInfo> activities = getPackageManager().queryIntentActivities(queryIntent,
151                 PackageManager.GET_META_DATA);
152         final ConnectivityManager cm =
153                 (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
154         if (activities == null) return null;
155         for (int i = activities.size() - 1; i >= 0; i--) {
156             ResolveInfo info = activities.get(i);
157             if (info.activityInfo.name.endsWith(TetherSettingsActivity.class.getSimpleName())) {
158                 if (!cm.isTetheringSupported()) {
159                     activities.remove(i);
160                 }
161             }
162         }
163         return activities;
164     }
165 
166     @VisibleForTesting
getBaseIntent()167     static Intent getBaseIntent() {
168         return new Intent(Intent.ACTION_MAIN).addCategory("com.android.settings.SHORTCUT");
169     }
170 
171     public static class ShortcutsUpdateTask extends AsyncTask<Void, Void, Void> {
172 
173         private final Context mContext;
174 
ShortcutsUpdateTask(Context context)175         public ShortcutsUpdateTask(Context context) {
176             mContext = context;
177         }
178 
179         @Override
doInBackground(Void... params)180         public Void doInBackground(Void... params) {
181             ShortcutManager sm = mContext.getSystemService(ShortcutManager.class);
182             PackageManager pm = mContext.getPackageManager();
183 
184             List<ShortcutInfo> updates = new ArrayList<>();
185             for (ShortcutInfo info : sm.getPinnedShortcuts()) {
186                 if (!info.getId().startsWith(SHORTCUT_ID_PREFIX)) {
187                     continue;
188                 }
189                 ComponentName cn = ComponentName.unflattenFromString(
190                         info.getId().substring(SHORTCUT_ID_PREFIX.length()));
191                 ResolveInfo ri = pm.resolveActivity(getBaseIntent().setComponent(cn), 0);
192                 if (ri == null) {
193                     continue;
194                 }
195                 updates.add(new ShortcutInfo.Builder(mContext, info.getId())
196                         .setShortLabel(ri.loadLabel(pm)).build());
197             }
198             if (!updates.isEmpty()) {
199                 sm.updateShortcuts(updates);
200             }
201             return null;
202         }
203     }
204 }
205