LauncherAppsCompat.java revision 3ff9047221820a01c6510503466f8c78b43fdc6d
1ed13187a745866483139e2878037e1f8427ce567Kenny Guy/*
2ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Copyright (C) 2014 The Android Open Source Project
3ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
4ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Licensed under the Apache License, Version 2.0 (the "License");
5ed13187a745866483139e2878037e1f8427ce567Kenny Guy * you may not use this file except in compliance with the License.
6ed13187a745866483139e2878037e1f8427ce567Kenny Guy * You may obtain a copy of the License at
7ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
8ed13187a745866483139e2878037e1f8427ce567Kenny Guy *      http://www.apache.org/licenses/LICENSE-2.0
9ed13187a745866483139e2878037e1f8427ce567Kenny Guy *
10ed13187a745866483139e2878037e1f8427ce567Kenny Guy * Unless required by applicable law or agreed to in writing, software
11ed13187a745866483139e2878037e1f8427ce567Kenny Guy * distributed under the License is distributed on an "AS IS" BASIS,
12ed13187a745866483139e2878037e1f8427ce567Kenny Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ed13187a745866483139e2878037e1f8427ce567Kenny Guy * See the License for the specific language governing permissions and
14ed13187a745866483139e2878037e1f8427ce567Kenny Guy * limitations under the License.
15ed13187a745866483139e2878037e1f8427ce567Kenny Guy */
16ed13187a745866483139e2878037e1f8427ce567Kenny Guy
17ed13187a745866483139e2878037e1f8427ce567Kenny Guypackage com.android.launcher3.compat;
18ed13187a745866483139e2878037e1f8427ce567Kenny Guy
19ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.content.ComponentName;
20ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.content.Context;
21ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.content.Intent;
223ff9047221820a01c6510503466f8c78b43fdc6dCharles Heimport android.content.pm.ApplicationInfo;
233e9be43b6ea75c8b82b57aa58508a0c3e8e1d721Sunny Goyalimport android.content.pm.LauncherActivityInfo;
24ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.graphics.Rect;
25ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.os.Bundle;
267c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyalimport android.os.UserHandle;
27dec3a908bfa395095e80e4a532cff98612b624deSunny Goyalimport android.support.annotation.Nullable;
28ed13187a745866483139e2878037e1f8427ce567Kenny Guy
29dec3a908bfa395095e80e4a532cff98612b624deSunny Goyalimport com.android.launcher3.LauncherAppState;
30dec3a908bfa395095e80e4a532cff98612b624deSunny Goyalimport com.android.launcher3.ShortcutInfo;
31782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyalimport com.android.launcher3.Utilities;
32dec3a908bfa395095e80e4a532cff98612b624deSunny Goyalimport com.android.launcher3.graphics.LauncherIcons;
33bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickhamimport com.android.launcher3.shortcuts.ShortcutInfoCompat;
34d794a3f46521b972fa02826d379d1efa112793d2Kenny Guy
35ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport java.util.List;
36ed13187a745866483139e2878037e1f8427ce567Kenny Guy
37ed13187a745866483139e2878037e1f8427ce567Kenny Guypublic abstract class LauncherAppsCompat {
38ed13187a745866483139e2878037e1f8427ce567Kenny Guy
39c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy    public interface OnAppsChangedCallbackCompat {
407c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        void onPackageRemoved(String packageName, UserHandle user);
417c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        void onPackageAdded(String packageName, UserHandle user);
427c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        void onPackageChanged(String packageName, UserHandle user);
437c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing);
447c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        void onPackagesUnavailable(String[] packageNames, UserHandle user, boolean replacing);
457c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        void onPackagesSuspended(String[] packageNames, UserHandle user);
467c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal        void onPackagesUnsuspended(String[] packageNames, UserHandle user);
47bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        void onShortcutsChanged(String packageName, List<ShortcutInfoCompat> shortcuts,
487c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal                UserHandle user);
49ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
50ed13187a745866483139e2878037e1f8427ce567Kenny Guy
51ed13187a745866483139e2878037e1f8427ce567Kenny Guy    protected LauncherAppsCompat() {
52ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
53ed13187a745866483139e2878037e1f8427ce567Kenny Guy
54c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy    private static LauncherAppsCompat sInstance;
55c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy    private static Object sInstanceLock = new Object();
56c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy
57ed13187a745866483139e2878037e1f8427ce567Kenny Guy    public static LauncherAppsCompat getInstance(Context context) {
58c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy        synchronized (sInstanceLock) {
59c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            if (sInstance == null) {
60782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                if (Utilities.isAtLeastO()) {
61782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                    sInstance = new LauncherAppsCompatVO(context.getApplicationContext());
62782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                } else {
63782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                    sInstance = new LauncherAppsCompatVL(context.getApplicationContext());
64782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal                }
65ed13187a745866483139e2878037e1f8427ce567Kenny Guy            }
66c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            return sInstance;
67ed13187a745866483139e2878037e1f8427ce567Kenny Guy        }
68ed13187a745866483139e2878037e1f8427ce567Kenny Guy    }
69ed13187a745866483139e2878037e1f8427ce567Kenny Guy
703e9be43b6ea75c8b82b57aa58508a0c3e8e1d721Sunny Goyal    public abstract List<LauncherActivityInfo> getActivityList(String packageName,
717c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal            UserHandle user);
723e9be43b6ea75c8b82b57aa58508a0c3e8e1d721Sunny Goyal    public abstract LauncherActivityInfo resolveActivity(Intent intent,
737c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal            UserHandle user);
747c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal    public abstract void startActivityForProfile(ComponentName component, UserHandle user,
75c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy            Rect sourceBounds, Bundle opts);
763ff9047221820a01c6510503466f8c78b43fdc6dCharles He    public abstract ApplicationInfo getApplicationInfo(String packageName, UserHandle user);
777c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal    public abstract void showAppDetailsForProfile(ComponentName component, UserHandle user);
78c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy    public abstract void addOnAppsChangedCallback(OnAppsChangedCallbackCompat listener);
79c2bd8101b8e26b9ebb2c079ae6867229dad3f196Kenny Guy    public abstract void removeOnAppsChangedCallback(OnAppsChangedCallbackCompat listener);
807c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal    public abstract boolean isPackageEnabledForProfile(String packageName, UserHandle user);
81ed13187a745866483139e2878037e1f8427ce567Kenny Guy    public abstract boolean isActivityEnabledForProfile(ComponentName component,
827c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal            UserHandle user);
83782f0c9a896db58aeaa60d15f291831b8d7b4c93Sunny Goyal    public abstract List<ShortcutConfigActivityInfo> getCustomShortcutActivityList();
84dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal
85dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    /**
86dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * request.accept() will initiate the following flow:
87dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     *      -> go-to-system-process for actual processing (a)
88dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     *      -> callback-to-launcher on UI thread (b)
89dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     *      -> post callback on the worker thread (c)
90dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     *      -> Update model and unpin (in system) any shortcut not in out model. (d)
91dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     *
92dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * Note that (b) will take at-least one frame as it involves posting callback from binder
93dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * thread to UI thread.
94dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * If (d) happens before we add this shortcut to our model, we will end up unpinning
95dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * the shortcut in the system.
96dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * Here its the caller's responsibility to add the newly created ShortcutInfo immediately
97dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * to the model (which may involves a single post-to-worker-thread). That will guarantee
98dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     * that (d) happens after model is updated.
99dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal     */
100dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    @Nullable
101dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    public static ShortcutInfo createShortcutInfoFromPinItemRequest(
102dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            Context context, PinItemRequestCompat request) {
103dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal        if (request != null &&
104dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal                request.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT &&
105dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal                request.isValid() && request.accept()) {
106dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            ShortcutInfoCompat compat = new ShortcutInfoCompat(request.getShortcutInfo());
107dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            ShortcutInfo info = new ShortcutInfo(compat, context);
108dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            // Apply the unbadged icon and fetch the actual icon asynchronously.
109dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            info.iconBitmap = LauncherIcons
110dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal                    .createShortcutIcon(compat, context, false /* badged */);
111dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            LauncherAppState.getInstance(context).getModel()
112dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal                    .updateAndBindShortcutInfo(info, compat);
113dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            return info;
114dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal        } else {
115dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal            return null;
116dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal        }
117dec3a908bfa395095e80e4a532cff98612b624deSunny Goyal    }
11844cba696386b44f9115cad13ec9ecf67a0ac9119Kenny Guy}
119