CreateShortcut.java revision 79ec6df771b0c05609a611a939f08fcd51f45b74
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 17package com.android.settings; 18 19import android.app.LauncherActivity; 20import android.content.Context; 21import android.content.Intent; 22import android.content.pm.ActivityInfo; 23import android.content.pm.PackageManager; 24import android.content.pm.ResolveInfo; 25import android.graphics.Bitmap; 26import android.graphics.Bitmap.Config; 27import android.graphics.Canvas; 28import android.net.ConnectivityManager; 29import android.view.ContextThemeWrapper; 30import android.view.LayoutInflater; 31import android.view.View; 32import android.view.View.MeasureSpec; 33import android.widget.ImageView; 34import android.widget.ListView; 35 36import com.android.settings.Settings.TetherSettingsActivity; 37 38import java.util.List; 39 40public class CreateShortcut extends LauncherActivity { 41 42 @Override 43 protected Intent getTargetIntent() { 44 Intent targetIntent = new Intent(Intent.ACTION_MAIN, null); 45 targetIntent.addCategory("com.android.settings.SHORTCUT"); 46 targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 47 return targetIntent; 48 } 49 50 @Override 51 protected void onListItemClick(ListView l, View v, int position, long id) { 52 Intent shortcutIntent = intentForPosition(position); 53 shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 54 Intent intent = new Intent(); 55 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 56 Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_settings)); 57 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 58 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, itemForPosition(position).label); 59 ResolveInfo resolveInfo = itemForPosition(position).resolveInfo; 60 ActivityInfo activityInfo = resolveInfo.activityInfo; 61 if (activityInfo.icon != 0) { 62 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(this, activityInfo.icon)); 63 } 64 setResult(RESULT_OK, intent); 65 finish(); 66 } 67 68 static Bitmap createIcon(Context ctx, int resource) { 69 Context context = new ContextThemeWrapper(ctx, android.R.style.Theme_Material); 70 View view = LayoutInflater.from(context).inflate(R.layout.shortcut_badge, null); 71 ((ImageView) view.findViewById(android.R.id.icon)).setImageResource(resource); 72 73 int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 74 view.measure(spec, spec); 75 Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), 76 Config.ARGB_8888); 77 Canvas canvas = new Canvas(bitmap); 78 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 79 view.draw(canvas); 80 return bitmap; 81 } 82 83 @Override 84 protected boolean onEvaluateShowIcons() { 85 return false; 86 } 87 88 /** 89 * Perform query on package manager for list items. The default 90 * implementation queries for activities. 91 */ 92 protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) { 93 List<ResolveInfo> activities = getPackageManager().queryIntentActivities(queryIntent, 94 PackageManager.GET_META_DATA); 95 final ConnectivityManager cm = 96 (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 97 if (activities == null) return null; 98 for (int i = activities.size() - 1; i >= 0; i--) { 99 ResolveInfo info = activities.get(i); 100 if (info.activityInfo.name.endsWith(TetherSettingsActivity.class.getSimpleName())) { 101 if (!cm.isTetheringSupported()) { 102 activities.remove(i); 103 } 104 } 105 } 106 return activities; 107 } 108} 109