Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2014 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.launcher3.compat;
     18 
     19 import android.annotation.TargetApi;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.LauncherActivityInfo;
     24 import android.content.pm.LauncherApps;
     25 import android.graphics.Rect;
     26 import android.os.Build;
     27 import android.os.Bundle;
     28 import android.os.UserHandle;
     29 
     30 import java.util.ArrayList;
     31 import java.util.Collections;
     32 import java.util.HashMap;
     33 import java.util.List;
     34 import java.util.Map;
     35 
     36 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     37 public class LauncherAppsCompatVL extends LauncherAppsCompat {
     38 
     39     protected LauncherApps mLauncherApps;
     40 
     41     private Map<OnAppsChangedCallbackCompat, WrappedCallback> mCallbacks
     42             = new HashMap<OnAppsChangedCallbackCompat, WrappedCallback>();
     43 
     44     LauncherAppsCompatVL(Context context) {
     45         super();
     46         mLauncherApps = (LauncherApps) context.getSystemService("launcherapps");
     47     }
     48 
     49     public List<LauncherActivityInfoCompat> getActivityList(String packageName,
     50             UserHandleCompat user) {
     51         List<LauncherActivityInfo> list = mLauncherApps.getActivityList(packageName,
     52                 user.getUser());
     53         if (list.size() == 0) {
     54             return Collections.emptyList();
     55         }
     56         ArrayList<LauncherActivityInfoCompat> compatList =
     57                 new ArrayList<LauncherActivityInfoCompat>(list.size());
     58         for (LauncherActivityInfo info : list) {
     59             compatList.add(new LauncherActivityInfoCompatVL(info));
     60         }
     61         return compatList;
     62     }
     63 
     64     public LauncherActivityInfoCompat resolveActivity(Intent intent, UserHandleCompat user) {
     65         LauncherActivityInfo activity = mLauncherApps.resolveActivity(intent, user.getUser());
     66         if (activity != null) {
     67             return new LauncherActivityInfoCompatVL(activity);
     68         } else {
     69             return null;
     70         }
     71     }
     72 
     73     public void startActivityForProfile(ComponentName component, UserHandleCompat user,
     74             Rect sourceBounds, Bundle opts) {
     75         mLauncherApps.startMainActivity(component, user.getUser(), sourceBounds, opts);
     76     }
     77 
     78     public void showAppDetailsForProfile(ComponentName component, UserHandleCompat user) {
     79         mLauncherApps.startAppDetailsActivity(component, user.getUser(), null, null);
     80     }
     81 
     82     public void addOnAppsChangedCallback(LauncherAppsCompat.OnAppsChangedCallbackCompat callback) {
     83         WrappedCallback wrappedCallback = new WrappedCallback(callback);
     84         synchronized (mCallbacks) {
     85             mCallbacks.put(callback, wrappedCallback);
     86         }
     87         mLauncherApps.registerCallback(wrappedCallback);
     88     }
     89 
     90     public void removeOnAppsChangedCallback(
     91             LauncherAppsCompat.OnAppsChangedCallbackCompat callback) {
     92         WrappedCallback wrappedCallback = null;
     93         synchronized (mCallbacks) {
     94             wrappedCallback = mCallbacks.remove(callback);
     95         }
     96         if (wrappedCallback != null) {
     97             mLauncherApps.unregisterCallback(wrappedCallback);
     98         }
     99     }
    100 
    101     public boolean isPackageEnabledForProfile(String packageName, UserHandleCompat user) {
    102         return mLauncherApps.isPackageEnabled(packageName, user.getUser());
    103     }
    104 
    105     public boolean isActivityEnabledForProfile(ComponentName component, UserHandleCompat user) {
    106         return mLauncherApps.isActivityEnabled(component, user.getUser());
    107     }
    108 
    109     public boolean isPackageSuspendedForProfile(String packageName, UserHandleCompat user) {
    110         return false;
    111     }
    112 
    113     private static class WrappedCallback extends LauncherApps.Callback {
    114         private LauncherAppsCompat.OnAppsChangedCallbackCompat mCallback;
    115 
    116         public WrappedCallback(LauncherAppsCompat.OnAppsChangedCallbackCompat callback) {
    117             mCallback = callback;
    118         }
    119 
    120         public void onPackageRemoved(String packageName, UserHandle user) {
    121             mCallback.onPackageRemoved(packageName, UserHandleCompat.fromUser(user));
    122         }
    123 
    124         public void onPackageAdded(String packageName, UserHandle user) {
    125             mCallback.onPackageAdded(packageName, UserHandleCompat.fromUser(user));
    126         }
    127 
    128         public void onPackageChanged(String packageName, UserHandle user) {
    129             mCallback.onPackageChanged(packageName, UserHandleCompat.fromUser(user));
    130         }
    131 
    132         public void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing) {
    133             mCallback.onPackagesAvailable(packageNames, UserHandleCompat.fromUser(user), replacing);
    134         }
    135 
    136         public void onPackagesUnavailable(String[] packageNames, UserHandle user,
    137                 boolean replacing) {
    138             mCallback.onPackagesUnavailable(packageNames, UserHandleCompat.fromUser(user),
    139                     replacing);
    140         }
    141 
    142         public void onPackagesSuspended(String[] packageNames, UserHandle user) {
    143             mCallback.onPackagesSuspended(packageNames, UserHandleCompat.fromUser(user));
    144         }
    145 
    146         public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
    147             mCallback.onPackagesUnsuspended(packageNames, UserHandleCompat.fromUser(user));
    148         }
    149     }
    150 }
    151 
    152