blob: ca9b6714603aa4b86c244cdbc094004c24087f43 [file] [log] [blame]
Kenny Guyed131872014-04-30 03:02:21 +01001/*
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
17package com.android.launcher3.compat;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
Kenny Guyc2bd8102014-06-30 12:30:31 +010022import android.content.pm.LauncherActivityInfo;
23import android.content.pm.LauncherApps;
Kenny Guyed131872014-04-30 03:02:21 +010024import android.graphics.Rect;
25import android.os.Build;
26import android.os.Bundle;
27import android.os.UserHandle;
28
Kenny Guyed131872014-04-30 03:02:21 +010029import java.util.ArrayList;
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.List;
33import java.util.Map;
34
Kenny Guyed131872014-04-30 03:02:21 +010035public class LauncherAppsCompatVL extends LauncherAppsCompat {
36
Kenny Guyc2bd8102014-06-30 12:30:31 +010037 private LauncherApps mLauncherApps;
Kenny Guyed131872014-04-30 03:02:21 +010038
Kenny Guyc2bd8102014-06-30 12:30:31 +010039 private Map<OnAppsChangedCallbackCompat, WrappedCallback> mCallbacks
40 = new HashMap<OnAppsChangedCallbackCompat, WrappedCallback>();
Kenny Guyed131872014-04-30 03:02:21 +010041
Kenny Guyc2bd8102014-06-30 12:30:31 +010042 LauncherAppsCompatVL(Context context) {
Kenny Guyed131872014-04-30 03:02:21 +010043 super();
Kenny Guyc2bd8102014-06-30 12:30:31 +010044 mLauncherApps = (LauncherApps) context.getSystemService("launcherapps");
Kenny Guyed131872014-04-30 03:02:21 +010045 }
46
47 public List<LauncherActivityInfoCompat> getActivityList(String packageName,
48 UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +010049 List<LauncherActivityInfo> list = mLauncherApps.getActivityList(packageName,
50 user.getUser());
Kenny Guyed131872014-04-30 03:02:21 +010051 if (list.size() == 0) {
52 return Collections.EMPTY_LIST;
53 }
54 ArrayList<LauncherActivityInfoCompat> compatList =
55 new ArrayList<LauncherActivityInfoCompat>(list.size());
Kenny Guyc2bd8102014-06-30 12:30:31 +010056 for (LauncherActivityInfo info : list) {
Kenny Guyed131872014-04-30 03:02:21 +010057 compatList.add(new LauncherActivityInfoCompatVL(info));
58 }
59 return compatList;
60 }
61
62 public LauncherActivityInfoCompat resolveActivity(Intent intent, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +010063 LauncherActivityInfo activity = mLauncherApps.resolveActivity(intent, user.getUser());
Kenny Guy792dd772014-05-14 12:06:51 +010064 if (activity != null) {
65 return new LauncherActivityInfoCompatVL(activity);
66 } else {
67 return null;
68 }
Kenny Guyed131872014-04-30 03:02:21 +010069 }
70
Kenny Guyc2bd8102014-06-30 12:30:31 +010071 public void startActivityForProfile(ComponentName component, UserHandleCompat user,
72 Rect sourceBounds, Bundle opts) {
73 mLauncherApps.startActivityForProfile(component, user.getUser(), sourceBounds, opts);
Kenny Guyed131872014-04-30 03:02:21 +010074 }
75
Kenny Guyc2bd8102014-06-30 12:30:31 +010076 public void addOnAppsChangedCallback(LauncherAppsCompat.OnAppsChangedCallbackCompat callback) {
77 WrappedCallback wrappedCallback = new WrappedCallback(callback);
78 synchronized (mCallbacks) {
79 mCallbacks.put(callback, wrappedCallback);
Kenny Guyed131872014-04-30 03:02:21 +010080 }
Kenny Guyc2bd8102014-06-30 12:30:31 +010081 mLauncherApps.addOnAppsChangedCallback(wrappedCallback);
Kenny Guyed131872014-04-30 03:02:21 +010082 }
83
Kenny Guyc2bd8102014-06-30 12:30:31 +010084 public void removeOnAppsChangedCallback(
85 LauncherAppsCompat.OnAppsChangedCallbackCompat callback) {
86 WrappedCallback wrappedCallback = null;
87 synchronized (mCallbacks) {
88 wrappedCallback = mCallbacks.remove(callback);
Kenny Guyed131872014-04-30 03:02:21 +010089 }
Kenny Guyc2bd8102014-06-30 12:30:31 +010090 if (wrappedCallback != null) {
91 mLauncherApps.removeOnAppsChangedCallback(wrappedCallback);
Kenny Guyed131872014-04-30 03:02:21 +010092 }
93 }
94
95 public boolean isPackageEnabledForProfile(String packageName, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +010096 return mLauncherApps.isPackageEnabledForProfile(packageName, user.getUser());
Kenny Guyed131872014-04-30 03:02:21 +010097 }
98
99 public boolean isActivityEnabledForProfile(ComponentName component, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +0100100 return mLauncherApps.isActivityEnabledForProfile(component, user.getUser());
Kenny Guyed131872014-04-30 03:02:21 +0100101 }
102
Kenny Guyc2bd8102014-06-30 12:30:31 +0100103 private static class WrappedCallback extends LauncherApps.OnAppsChangedCallback {
104 private LauncherAppsCompat.OnAppsChangedCallbackCompat mCallback;
Kenny Guyed131872014-04-30 03:02:21 +0100105
Kenny Guyc2bd8102014-06-30 12:30:31 +0100106 public WrappedCallback(LauncherAppsCompat.OnAppsChangedCallbackCompat callback) {
107 mCallback = callback;
Kenny Guyed131872014-04-30 03:02:21 +0100108 }
109
Kenny Guyc2bd8102014-06-30 12:30:31 +0100110 public void onPackageRemoved(String packageName, UserHandle user) {
111 mCallback.onPackageRemoved(packageName, UserHandleCompat.fromUser(user));
Kenny Guyed131872014-04-30 03:02:21 +0100112 }
113
Kenny Guyc2bd8102014-06-30 12:30:31 +0100114 public void onPackageAdded(String packageName, UserHandle user) {
115 mCallback.onPackageAdded(packageName, UserHandleCompat.fromUser(user));
Kenny Guyed131872014-04-30 03:02:21 +0100116 }
117
Kenny Guyc2bd8102014-06-30 12:30:31 +0100118 public void onPackageChanged(String packageName, UserHandle user) {
119 mCallback.onPackageChanged(packageName, UserHandleCompat.fromUser(user));
Kenny Guyed131872014-04-30 03:02:21 +0100120 }
121
Kenny Guyc2bd8102014-06-30 12:30:31 +0100122 public void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing) {
123 mCallback.onPackagesAvailable(packageNames, UserHandleCompat.fromUser(user), replacing);
Kenny Guyed131872014-04-30 03:02:21 +0100124 }
125
Kenny Guyc2bd8102014-06-30 12:30:31 +0100126 public void onPackagesUnavailable(String[] packageNames, UserHandle user,
Kenny Guyed131872014-04-30 03:02:21 +0100127 boolean replacing) {
Kenny Guyc2bd8102014-06-30 12:30:31 +0100128 mCallback.onPackagesUnavailable(packageNames, UserHandleCompat.fromUser(user),
Kenny Guyed131872014-04-30 03:02:21 +0100129 replacing);
130 }
131 }
132}
133