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