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.widget;
18 
19 import android.annotation.TargetApi;
20 import android.appwidget.AppWidgetManager;
21 import android.appwidget.AppWidgetProviderInfo;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.os.Process;
27 import android.os.UserHandle;
28 
29 import androidx.annotation.Nullable;
30 
31 import com.android.launcher3.LauncherAppWidgetProviderInfo;
32 import com.android.launcher3.Utilities;
33 import com.android.launcher3.model.WidgetsModel;
34 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
35 import com.android.launcher3.pm.UserCache;
36 import com.android.launcher3.util.ComponentKey;
37 import com.android.launcher3.util.PackageUserKey;
38 import com.android.launcher3.widget.custom.CustomWidgetManager;
39 
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.function.Function;
44 import java.util.stream.Collectors;
45 import java.util.stream.Stream;
46 
47 /**
48  * Utility class to working with {@link AppWidgetManager}
49  */
50 public class WidgetManagerHelper {
51 
52     //TODO: replace this with OPTION_APPWIDGET_RESTORE_COMPLETED b/63667276
53     public static final String WIDGET_OPTION_RESTORE_COMPLETED = "appWidgetRestoreCompleted";
54 
55     final AppWidgetManager mAppWidgetManager;
56     final Context mContext;
57 
WidgetManagerHelper(Context context)58     public WidgetManagerHelper(Context context) {
59         mContext = context;
60         mAppWidgetManager = AppWidgetManager.getInstance(context);
61     }
62 
63     /**
64      * @see AppWidgetManager#getAppWidgetInfo(int)
65      */
getLauncherAppWidgetInfo(int appWidgetId)66     public LauncherAppWidgetProviderInfo getLauncherAppWidgetInfo(int appWidgetId) {
67         if (appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
68             return CustomWidgetManager.INSTANCE.get(mContext).getWidgetProvider(appWidgetId);
69         }
70         AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
71         return info == null ? null : LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
72     }
73 
74     /**
75      * @see AppWidgetManager#getInstalledProvidersForPackage(String, UserHandle)
76      */
77     @TargetApi(Build.VERSION_CODES.O)
getAllProviders(@ullable PackageUserKey packageUser)78     public List<AppWidgetProviderInfo> getAllProviders(@Nullable PackageUserKey packageUser) {
79         if (WidgetsModel.GO_DISABLE_WIDGETS) {
80             return Collections.emptyList();
81         }
82 
83         if (packageUser == null) {
84             return allWidgetsSteam(mContext).collect(Collectors.toList());
85         }
86 
87         if (Utilities.ATLEAST_OREO) {
88             return mAppWidgetManager.getInstalledProvidersForPackage(
89                     packageUser.mPackageName, packageUser.mUser);
90         }
91 
92         String pkg = packageUser.mPackageName;
93         return Stream.concat(
94                 // Only get providers for the given package/user.
95                 mAppWidgetManager.getInstalledProvidersForProfile(packageUser.mUser)
96                         .stream()
97                         .filter(w -> w.provider.equals(pkg)),
98                 Process.myUserHandle().equals(packageUser.mUser)
99                         && mContext.getPackageName().equals(pkg)
100                         ? CustomWidgetManager.INSTANCE.get(mContext).stream()
101                         : Stream.empty())
102                 .collect(Collectors.toList());
103     }
104 
105     /**
106      * @see AppWidgetManager#bindAppWidgetIdIfAllowed(int, UserHandle, ComponentName, Bundle)
107      */
bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, Bundle options)108     public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info,
109             Bundle options) {
110         if (WidgetsModel.GO_DISABLE_WIDGETS) {
111             return false;
112         }
113         if (appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
114             return true;
115         }
116         return mAppWidgetManager.bindAppWidgetIdIfAllowed(
117                 appWidgetId, info.getProfile(), info.provider, options);
118     }
119 
findProvider(ComponentName provider, UserHandle user)120     public LauncherAppWidgetProviderInfo findProvider(ComponentName provider, UserHandle user) {
121         if (WidgetsModel.GO_DISABLE_WIDGETS) {
122             return null;
123         }
124         for (AppWidgetProviderInfo info :
125                 getAllProviders(new PackageUserKey(provider.getPackageName(), user))) {
126             if (info.provider.equals(provider)) {
127                 return LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
128             }
129         }
130         return null;
131     }
132 
133     /**
134      * Returns if a AppWidgetProvider has marked a widget restored
135      */
isAppWidgetRestored(int appWidgetId)136     public boolean isAppWidgetRestored(int appWidgetId) {
137         return !WidgetsModel.GO_DISABLE_WIDGETS && mAppWidgetManager.getAppWidgetOptions(
138                 appWidgetId).getBoolean(WIDGET_OPTION_RESTORE_COMPLETED);
139     }
140 
getAllProvidersMap(Context context)141     public static Map<ComponentKey, AppWidgetProviderInfo> getAllProvidersMap(Context context) {
142         if (WidgetsModel.GO_DISABLE_WIDGETS) {
143             return Collections.emptyMap();
144         }
145         return allWidgetsSteam(context).collect(
146                         Collectors.toMap(info -> new ComponentKey(info.provider, info.getProfile()),
147                         Function.identity()));
148     }
149 
allWidgetsSteam(Context context)150     private static Stream<AppWidgetProviderInfo> allWidgetsSteam(Context context) {
151         AppWidgetManager awm = context.getSystemService(AppWidgetManager.class);
152         return Stream.concat(
153                 UserCache.INSTANCE.get(context)
154                         .getUserProfiles()
155                         .stream()
156                         .flatMap(u -> awm.getInstalledProvidersForProfile(u).stream()),
157                 CustomWidgetManager.INSTANCE.get(context).stream());
158     }
159 }
160