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.view.ContextThemeWrapper;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.View.MeasureSpec;
32import android.widget.ImageView;
33import android.widget.ListView;
34
35import com.android.settings.Settings.TetherSettingsActivity;
36import com.android.settings.dashboard.DashboardCategory;
37import com.android.settings.dashboard.DashboardTile;
38import com.android.settingslib.TetherUtil;
39
40import java.util.ArrayList;
41import java.util.List;
42
43public class CreateShortcut extends LauncherActivity {
44
45    private static final String TOP_LEVEL_HEADER = "com.android.settings.TOP_LEVEL_HEADER_ID";
46
47    @Override
48    protected Intent getTargetIntent() {
49        Intent targetIntent = new Intent(Intent.ACTION_MAIN, null);
50        targetIntent.addCategory("com.android.settings.SHORTCUT");
51        targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
52        return targetIntent;
53    }
54
55    @Override
56    protected void onListItemClick(ListView l, View v, int position, long id) {
57        Intent shortcutIntent = intentForPosition(position);
58        shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
59        Intent intent = new Intent();
60        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
61                Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_settings));
62        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
63        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, itemForPosition(position).label);
64        ResolveInfo resolveInfo = itemForPosition(position).resolveInfo;
65        ActivityInfo activityInfo = resolveInfo.activityInfo;
66        if (activityInfo.metaData != null && activityInfo.metaData.containsKey(TOP_LEVEL_HEADER)) {
67            int topLevelId = activityInfo.metaData.getInt(TOP_LEVEL_HEADER);
68            int resourceId = getDrawableResource(topLevelId);
69            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, createIcon(resourceId));
70        }
71        setResult(RESULT_OK, intent);
72        finish();
73    }
74
75    private Bitmap createIcon(int resource) {
76        Context context = new ContextThemeWrapper(this, android.R.style.Theme_Material);
77        View view = LayoutInflater.from(context).inflate(R.layout.shortcut_badge, null);
78        ((ImageView) view.findViewById(android.R.id.icon)).setImageResource(resource);
79
80        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
81        view.measure(spec, spec);
82        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
83                Config.ARGB_8888);
84        Canvas canvas = new Canvas(bitmap);
85        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
86        view.draw(canvas);
87        return bitmap;
88    }
89
90    private int getDrawableResource(int topLevelId) {
91        ArrayList<DashboardCategory> categories = new ArrayList<>();
92        SettingsActivity.loadCategoriesFromResource(R.xml.dashboard_categories, categories, this);
93        for (DashboardCategory category : categories) {
94            for (DashboardTile tile : category.tiles) {
95                if (tile.id == topLevelId) {
96                    return tile.iconRes;
97                }
98            }
99        }
100        return 0;
101    }
102
103    @Override
104    protected boolean onEvaluateShowIcons() {
105        return false;
106    }
107
108    /**
109     * Perform query on package manager for list items.  The default
110     * implementation queries for activities.
111     */
112    protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) {
113        List<ResolveInfo> activities = getPackageManager().queryIntentActivities(queryIntent,
114                PackageManager.GET_META_DATA);
115        if (activities == null) return null;
116        for (int i = activities.size() - 1; i >= 0; i--) {
117            ResolveInfo info = activities.get(i);
118            if (info.activityInfo.name.endsWith(TetherSettingsActivity.class.getSimpleName())) {
119                if (!TetherUtil.isTetheringSupported(this)) {
120                    activities.remove(i);
121                }
122            }
123        }
124        return activities;
125    }
126}
127