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