Home | History | Annotate | Download | only in settings
      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;
     18 
     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;
     35 
     36 import com.android.settings.Settings.TetherSettingsActivity;
     37 
     38 import java.util.List;
     39 
     40 public 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(activityInfo.icon));
     63         }
     64         setResult(RESULT_OK, intent);
     65         finish();
     66     }
     67 
     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);
     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