2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.LauncherActivity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ActivityInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.graphics.Bitmap;
26 import android.graphics.Bitmap.Config;
27 import android.graphics.Canvas;
28 import android.net.ConnectivityManager;
29 import android.view.ContextThemeWrapper;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.View.MeasureSpec;
33 import android.widget.ImageView;
34 import android.widget.ListView;
36 import com.android.settings.Settings.TetherSettingsActivity;
38 import java.util.List;
40 public class CreateShortcut extends LauncherActivity {
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);
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(activityInfo.icon));
64 setResult(RESULT_OK, intent);
68 private Bitmap createIcon(int resource) {
69 Context context = new ContextThemeWrapper(this, 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);
73 int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
74 view.measure(spec, spec);
75 Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
77 Canvas canvas = new Canvas(bitmap);
78 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
84 protected boolean onEvaluateShowIcons() {
89 * Perform query on package manager for list items. The default
90 * implementation queries for activities.
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);