1 /*
2  * Copyright (C) 2020 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.shortcuts;
18 
19 import static com.android.launcher3.model.WidgetsModel.GO_DISABLE_WIDGETS;
20 
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.pm.LauncherApps;
24 import android.content.pm.LauncherApps.ShortcutQuery;
25 import android.content.pm.ShortcutInfo;
26 import android.os.UserHandle;
27 import android.util.Log;
28 
29 import androidx.annotation.Nullable;
30 
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35 
36 /**
37  * Utility class to streamline Shortcut query
38  */
39 public class ShortcutRequest {
40 
41     private static final String TAG = "ShortcutRequest";
42 
43     public static final int ALL = ShortcutQuery.FLAG_MATCH_DYNAMIC
44             | ShortcutQuery.FLAG_MATCH_MANIFEST | ShortcutQuery.FLAG_MATCH_PINNED;
45     public static final int PUBLISHED = ShortcutQuery.FLAG_MATCH_DYNAMIC
46             | ShortcutQuery.FLAG_MATCH_MANIFEST;
47     public static final int PINNED = ShortcutQuery.FLAG_MATCH_PINNED;
48 
49     private final ShortcutQuery mQuery = GO_DISABLE_WIDGETS ? null : new ShortcutQuery();
50 
51     private final Context mContext;
52     private final UserHandle mUserHandle;
53 
54     boolean mFailed = false;
55 
ShortcutRequest(Context context, UserHandle userHandle)56     public ShortcutRequest(Context context, UserHandle userHandle) {
57         mContext = context;
58         mUserHandle = userHandle;
59     }
60 
61     /** @see #forPackage(String, List) */
forPackage(String packageName)62     public ShortcutRequest forPackage(String packageName) {
63         return forPackage(packageName, (List<String>) null);
64     }
65 
66     /** @see #forPackage(String, List) */
forPackage(String packageName, String... shortcutIds)67     public ShortcutRequest forPackage(String packageName, String... shortcutIds) {
68         return forPackage(packageName, Arrays.asList(shortcutIds));
69     }
70 
71     /**
72      * @param shortcutIds If null, match all shortcuts, otherwise only match the given id's.
73      * @return A list of ShortcutInfo's associated with the given package.
74      */
forPackage(String packageName, @Nullable List<String> shortcutIds)75     public ShortcutRequest forPackage(String packageName, @Nullable List<String> shortcutIds) {
76         if (!GO_DISABLE_WIDGETS && packageName != null) {
77             mQuery.setPackage(packageName);
78             mQuery.setShortcutIds(shortcutIds);
79         }
80         return this;
81     }
82 
withContainer(@ullable ComponentName activity)83     public ShortcutRequest withContainer(@Nullable ComponentName activity) {
84         if (!GO_DISABLE_WIDGETS) {
85             if (activity == null) {
86                 mFailed = true;
87             } else {
88                 mQuery.setActivity(activity);
89             }
90         }
91         return this;
92     }
93 
query(int flags)94     public QueryResult query(int flags) {
95         if (GO_DISABLE_WIDGETS || mFailed) {
96             return QueryResult.DEFAULT;
97         }
98         mQuery.setQueryFlags(flags);
99 
100         try {
101             return new QueryResult(mContext.getSystemService(LauncherApps.class)
102                     .getShortcuts(mQuery, mUserHandle));
103         } catch (SecurityException | IllegalStateException e) {
104             Log.e(TAG, "Failed to query for shortcuts", e);
105             return QueryResult.DEFAULT;
106         }
107     }
108 
109     public static class QueryResult extends ArrayList<ShortcutInfo> {
110 
111         static final QueryResult DEFAULT = new QueryResult(GO_DISABLE_WIDGETS);
112 
113         private final boolean mWasSuccess;
114 
QueryResult(List<ShortcutInfo> result)115         QueryResult(List<ShortcutInfo> result) {
116             super(result == null ? Collections.emptyList() : result);
117             mWasSuccess = true;
118         }
119 
QueryResult(boolean wasSuccess)120         QueryResult(boolean wasSuccess) {
121             mWasSuccess = wasSuccess;
122         }
123 
124 
wasSuccess()125         public boolean wasSuccess() {
126             return mWasSuccess;
127         }
128     }
129 }
130