Home | History | Annotate | Download | only in shortcutlauncherdemo
      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 package com.example.android.pm.shortcutlauncherdemo;
     17 
     18 import android.app.ListFragment;
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.content.pm.ApplicationInfo;
     24 import android.content.pm.LauncherApps;
     25 import android.content.pm.LauncherApps.ShortcutQuery;
     26 import android.content.pm.PackageManager;
     27 import android.content.pm.PackageManager.NameNotFoundException;
     28 import android.content.pm.ShortcutInfo;
     29 import android.os.Bundle;
     30 import android.os.UserHandle;
     31 import android.os.UserManager;
     32 import android.util.ArrayMap;
     33 import android.util.Log;
     34 import android.widget.Toast;
     35 
     36 import java.util.Arrays;
     37 import java.util.List;
     38 
     39 public abstract class MyBaseListFragment extends ListFragment {
     40     protected UserManager mUserManager;
     41     protected LauncherApps mLauncherApps;
     42 
     43     private ArrayMap<String, String> mAppNames = new ArrayMap<>();
     44 
     45     protected final ShortcutQuery mQuery = new ShortcutQuery();
     46 
     47     public final static IntentFilter sProfileFilter = new IntentFilter();
     48 
     49     static {
     50         sProfileFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
     51         sProfileFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
     52         sProfileFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
     53         sProfileFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
     54         sProfileFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
     55     }
     56 
     57     private final BroadcastReceiver mProfileReceiver = new BroadcastReceiver() {
     58         @Override
     59         public void onReceive(Context context, Intent intent) {
     60             refreshList();
     61         }
     62     };
     63 
     64     @Override
     65     public void onCreate(Bundle savedInstanceState) {
     66         super.onCreate(savedInstanceState);
     67 
     68         mUserManager = getActivity().getSystemService(UserManager.class);
     69         mLauncherApps = getActivity().getSystemService(LauncherApps.class);
     70         mLauncherApps.registerCallback(mLauncherCallback);
     71     }
     72 
     73     @Override
     74     public void onResume() {
     75         super.onResume();
     76 
     77         Log.d(Global.TAG, "Resumed");
     78 
     79         showPermissionWarningToastWhenNeeded();
     80 
     81         refreshList();
     82 
     83         getActivity().registerReceiver(mProfileReceiver, sProfileFilter);
     84     }
     85 
     86     @Override
     87     public void onPause() {
     88         getActivity().unregisterReceiver(mProfileReceiver);
     89 
     90         super.onPause();
     91     }
     92 
     93     @Override
     94     public void onDestroy() {
     95         mLauncherApps.unregisterCallback(mLauncherCallback);
     96 
     97         super.onDestroy();
     98     }
     99 
    100     protected void showPermissionWarningToastWhenNeeded() {
    101         if (!mLauncherApps.hasShortcutHostPermission()) {
    102             Toast.makeText(getActivity(), "App doesn't have the shortcut permissions",
    103                     Toast.LENGTH_SHORT).show();
    104         }
    105     }
    106 
    107     protected final String getAppLabel(String packageName) {
    108         String name = mAppNames.get(packageName);
    109         if (name != null) {
    110             return name;
    111         }
    112         PackageManager pm = getActivity().getPackageManager();
    113         try {
    114             final ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
    115             return pm.getApplicationLabel(ai).toString();
    116         } catch (NameNotFoundException e) {
    117             return packageName;
    118         }
    119     }
    120 
    121     protected abstract void refreshList();
    122 
    123     private final LauncherApps.Callback mLauncherCallback = new LauncherApps.Callback() {
    124         @Override
    125         public void onPackageRemoved(String packageName, UserHandle user) {
    126             Log.i(Global.TAG, "onPackageRemoved: package=" + packageName + " on " + user);
    127             refreshList();
    128         }
    129 
    130         @Override
    131         public void onPackageAdded(String packageName, UserHandle user) {
    132             Log.i(Global.TAG, "onPackageAdded: package=" + packageName + " on " + user);
    133             refreshList();
    134         }
    135 
    136         @Override
    137         public void onPackageChanged(String packageName, UserHandle user) {
    138             Log.i(Global.TAG, "onPackageChanged: package=" + packageName + " on " + user);
    139             refreshList();
    140         }
    141 
    142         @Override
    143         public void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing) {
    144             Log.i(Global.TAG, "onPackagesAvailable: package=" + Arrays.asList(packageNames)
    145                     + " on " + user);
    146             refreshList();
    147         }
    148 
    149         @Override
    150         public void onPackagesUnavailable(String[] packageNames, UserHandle user,
    151                 boolean replacing) {
    152             Log.i(Global.TAG, "onPackagesUnavailable: package=" + Arrays.asList(packageNames)
    153                     + " on " + user);
    154             refreshList();
    155         }
    156 
    157         @Override
    158         public void onShortcutsChanged(String packageName,
    159                 List<ShortcutInfo> shortcuts, UserHandle user) {
    160             Log.i(Global.TAG, "onShortcutsChanged: package=" + packageName + " on " + user);
    161             refreshList();
    162         }
    163     };
    164 
    165 }
    166