Home | History | Annotate | Download | only in display
      1 /*
      2  * Copyright (C) 2016 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.display;
     18 
     19 import com.android.settings.R;
     20 
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.PackageManager;
     24 import android.content.pm.ResolveInfo;
     25 import android.graphics.drawable.Drawable;
     26 import android.util.AttributeSet;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.ArrayAdapter;
     30 import android.widget.GridView;
     31 import android.widget.ImageView;
     32 
     33 import java.util.ArrayList;
     34 import java.util.Collections;
     35 import java.util.List;
     36 
     37 public class AppGridView extends GridView {
     38     public AppGridView(Context context) {
     39         this(context, null);
     40     }
     41 
     42     public AppGridView(Context context, AttributeSet attrs) {
     43         this(context, attrs, 0);
     44     }
     45 
     46     public AppGridView(Context context, AttributeSet attrs, int defStyleAttr) {
     47         this(context, attrs, defStyleAttr, 0);
     48     }
     49 
     50     public AppGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleResId) {
     51         super(context, attrs, defStyleAttr, defStyleResId);
     52 
     53         setNumColumns(AUTO_FIT);
     54 
     55         final int columnWidth = getResources().getDimensionPixelSize(
     56                 R.dimen.screen_zoom_preview_app_icon_width);
     57         setColumnWidth(columnWidth);
     58 
     59         setAdapter(new AppsAdapter(context, R.layout.screen_zoom_preview_app_icon,
     60                 android.R.id.text1, android.R.id.icon1));
     61     }
     62 
     63     /**
     64      * Loads application labels and icons.
     65      */
     66     private static class AppsAdapter extends ArrayAdapter<ActivityEntry> {
     67         private final PackageManager mPackageManager;
     68         private final int mIconResId;
     69 
     70         public AppsAdapter(Context context, int layout, int textResId, int iconResId) {
     71             super(context, layout, textResId);
     72 
     73             mIconResId = iconResId;
     74             mPackageManager = context.getPackageManager();
     75 
     76             loadAllApps();
     77         }
     78 
     79         @Override
     80         public View getView(int position, View convertView, ViewGroup parent) {
     81             final View view = super.getView(position, convertView, parent);
     82             final ActivityEntry entry = getItem(position);
     83             final ImageView iconView = (ImageView) view.findViewById(mIconResId);
     84             final Drawable icon = entry.info.loadIcon(mPackageManager);
     85             iconView.setImageDrawable(icon);
     86             return view;
     87         }
     88 
     89         @Override
     90         public boolean hasStableIds() {
     91             return true;
     92         }
     93 
     94         @Override
     95         public long getItemId(int position) {
     96             return position;
     97         }
     98 
     99         @Override
    100         public boolean isEnabled(int position) {
    101             return false;
    102         }
    103 
    104         private void loadAllApps() {
    105             final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    106             mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    107 
    108             final PackageManager pm = mPackageManager;
    109             final ArrayList<ActivityEntry> results = new ArrayList<>();
    110             final List<ResolveInfo> infos = pm.queryIntentActivities(mainIntent, 0);
    111             for (ResolveInfo info : infos) {
    112                 final CharSequence label = info.loadLabel(pm);
    113                 if (label != null) {
    114                     results.add(new ActivityEntry(info, label.toString()));
    115                 }
    116             }
    117 
    118             Collections.sort(results);
    119 
    120             addAll(results);
    121         }
    122     }
    123 
    124     /**
    125      * Class used for caching the activity label and icon.
    126      */
    127     private static class ActivityEntry implements Comparable<ActivityEntry> {
    128         public final ResolveInfo info;
    129         public final String label;
    130 
    131         public ActivityEntry(ResolveInfo info, String label) {
    132             this.info = info;
    133             this.label = label;
    134         }
    135 
    136         @Override
    137         public int compareTo(ActivityEntry entry) {
    138             return label.compareToIgnoreCase(entry.label);
    139         }
    140 
    141         @Override
    142         public String toString() {
    143             return label;
    144         }
    145     }
    146 }
    147