Home | History | Annotate | Download | only in shortcutlauncherdemo
      1 /*
      2  * Copyright (C) 2017 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 package com.example.android.pm.shortcutlauncherdemo;
     17 
     18 import android.content.Context;
     19 import android.content.pm.LauncherActivityInfo;
     20 import android.content.pm.LauncherApps;
     21 import android.graphics.drawable.Drawable;
     22 import android.os.Bundle;
     23 import android.os.UserHandle;
     24 import android.os.UserManager;
     25 import android.util.DisplayMetrics;
     26 import android.util.Log;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.View.OnClickListener;
     30 import android.view.ViewGroup;
     31 import android.widget.BaseAdapter;
     32 import android.widget.Button;
     33 import android.widget.ImageView;
     34 import android.widget.TextView;
     35 
     36 import java.util.ArrayList;
     37 import java.util.Collections;
     38 import java.util.Comparator;
     39 import java.util.List;
     40 
     41 public abstract class BaseActivityListFragment extends MyBaseListFragment {
     42     private AppAdapter mAdapter;
     43 
     44     @Override
     45     public void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47 
     48         mAdapter = new AppAdapter(getActivity());
     49         setListAdapter(mAdapter);
     50     }
     51 
     52     @Override
     53     protected void refreshList() {
     54         Log.d(Global.TAG, "Loading apps and shortcuts...");
     55 
     56         final List<LauncherActivityInfo> apps = new ArrayList<>();
     57 
     58         try {
     59             for (UserHandle user : Compat.getProfiles(getActivity())) {
     60                 if (mUserManager.isUserUnlocked(user)) {
     61                     apps.addAll(getActivities(user));
     62                 }
     63             }
     64             Collections.sort(apps, sLauncherIconComparator);
     65         } catch (Exception e) {
     66             Global.showToast(getContext(), e.getMessage());
     67         }
     68 
     69         Log.d(Global.TAG, "Apps and shortcuts loaded. (count=" + apps.size() + ")");
     70 
     71         mAdapter.setList(apps);
     72     }
     73 
     74     protected abstract List<LauncherActivityInfo> getActivities(UserHandle user);
     75 
     76     private static final Comparator<LauncherActivityInfo> sLauncherIconComparator =
     77             (LauncherActivityInfo l1, LauncherActivityInfo l2) -> {
     78                 int ret = 0;
     79                 ret = l1.getLabel().toString().compareTo(l2.getLabel().toString());
     80                 if (ret != 0) return ret;
     81 
     82                 // TODO Don't rely on hashCode being the user-id.
     83                 ret = l1.getUser().hashCode() - l2.getUser().hashCode();
     84                 if (ret != 0) return ret;
     85 
     86                 return 0;
     87             };
     88 
     89     private class AppAdapter extends BaseAdapter implements OnClickListener {
     90         private final Context mContext;
     91         private final LayoutInflater mInflater;
     92         private List<LauncherActivityInfo> mList;
     93 
     94         public AppAdapter(Context context) {
     95             mContext = context;
     96             mInflater = mContext.getSystemService(LayoutInflater.class);
     97             mUserManager = mContext.getSystemService(UserManager.class);
     98             mLauncherApps = mContext.getSystemService(LauncherApps.class);
     99         }
    100 
    101         public void setList(List<LauncherActivityInfo> list) {
    102             mList = list;
    103             notifyDataSetChanged();
    104         }
    105 
    106         @Override
    107         public int getCount() {
    108             return mList == null ? 0 : mList.size();
    109         }
    110 
    111         @Override
    112         public LauncherActivityInfo getItem(int position) {
    113             return mList == null ? null : mList.get(position);
    114         }
    115 
    116         @Override
    117         public long getItemId(int position) {
    118             return position;
    119         }
    120 
    121         @Override
    122         public boolean hasStableIds() {
    123             return false;
    124         }
    125 
    126         @Override
    127         public boolean areAllItemsEnabled() {
    128             return true;
    129         }
    130 
    131         @Override
    132         public boolean isEnabled(int position) {
    133             return true;
    134         }
    135 
    136         @Override
    137         public View getView(int position, View convertView, ViewGroup parent) {
    138             final View view;
    139             if (convertView != null) {
    140                 view = convertView;
    141             } else {
    142                 view = mInflater.inflate(R.layout.list_item, null);
    143             }
    144 
    145             bindView(view, getItem(position));
    146 
    147             return view;
    148         }
    149 
    150         public void bindView(View view, LauncherActivityInfo ai) {
    151             {
    152                 final View v = view.findViewById(R.id.launch);
    153 
    154                 v.setTag(ai);
    155 
    156                 v.setOnClickListener(this);
    157                 v.setVisibility(View.VISIBLE);
    158             }
    159             {
    160                 final Button v = (Button) view.findViewById(R.id.action2);
    161                 v.setTag(ai);
    162                 v.setVisibility(View.INVISIBLE);
    163 
    164                 try {
    165                     onBindAction2(v, ai, this);
    166                 } catch (Exception e) {
    167                     Log.w(Global.TAG, "Caught exception", e);
    168                 }
    169             }
    170 
    171             final TextView line1 = (TextView) view.findViewById(R.id.line1);
    172             final TextView line2 = (TextView) view.findViewById(R.id.line2);
    173 
    174             line1.setText(ai.getLabel());
    175 
    176             // TODO Do it on worker thread
    177             final Drawable icon = ai.getBadgedIcon(DisplayMetrics.DENSITY_DEFAULT);
    178             final ImageView image = (ImageView) view.findViewById(R.id.image);
    179             image.setImageDrawable(icon);
    180         }
    181 
    182         @Override
    183         public void onClick(View v) {
    184             final LauncherActivityInfo ai = (LauncherActivityInfo) v.getTag();
    185             switch (v.getId()) {
    186                 case R.id.launch:
    187                     try {
    188                         onLaunch(ai);
    189                     } catch (Exception e) {
    190                         Global.showToast(getContext(), e.getMessage());
    191                     }
    192                     return;
    193                 case R.id.action2:
    194                     try {
    195                         onAction2(ai);
    196                     } catch (Exception e) {
    197                         Global.showToast(getContext(), e.getMessage());
    198                     }
    199                     return;
    200             }
    201         }
    202     }
    203 
    204     protected void onBindAction2(Button v, LauncherActivityInfo ai,
    205             OnClickListener listener) {
    206     }
    207 
    208     protected abstract void onLaunch(LauncherActivityInfo ai);
    209 
    210     protected void onAction2(LauncherActivityInfo ai) {
    211     }
    212 }