• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.UserHandle;
27 
28 import androidx.annotation.Nullable;
29 
30 import com.android.launcher3.model.WidgetsModel;
31 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
32 import com.android.launcher3.pm.UserCache;
33 import com.android.launcher3.util.PackageUserKey;
34 import com.android.launcher3.widget.custom.CustomWidgetManager;
35 
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.stream.Collectors;
39 import java.util.stream.Stream;
40 
41 /**
42  * Utility class to working with {@link AppWidgetManager}
43  */
44 public class WidgetManagerHelper {
45 
46     //TODO: replace this with OPTION_APPWIDGET_RESTORE_COMPLETED b/63667276
47     public static final String WIDGET_OPTION_RESTORE_COMPLETED = "appWidgetRestoreCompleted";
48 
49     final AppWidgetManager mAppWidgetManager;
50     final Context mContext;
51 
WidgetManagerHelper(Context context)52     public WidgetManagerHelper(Context context) {
53         mContext = context;
54         mAppWidgetManager = AppWidgetManager.getInstance(context);
55     }
56 
57     /**
58      * @see AppWidgetManager#getAppWidgetInfo(int)
59      */
getLauncherAppWidgetInfo(int appWidgetId)60     public LauncherAppWidgetProviderInfo getLauncherAppWidgetInfo(int appWidgetId) {
61         if (appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
62             return CustomWidgetManager.INSTANCE.get(mContext).getWidgetProvider(appWidgetId);
63         }
64         AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
65         return info == null ? null : LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
66     }
67 
68     /**
69      * @see AppWidgetManager#getInstalledProvidersForPackage(String, UserHandle)
70      */
71     @TargetApi(Build.VERSION_CODES.O)
getAllProviders(@ullable PackageUserKey packageUser)72     public List<AppWidgetProviderInfo> getAllProviders(@Nullable PackageUserKey packageUser) {
73         if (WidgetsModel.GO_DISABLE_WIDGETS) {
74             return Collections.emptyList();
75         }
76 
77         if (packageUser == null) {
78             return allWidgetsSteam(mContext).collect(Collectors.toList());
79         }
80 
81         try {
82             return mAppWidgetManager.getInstalledProvidersForPackage(
83                     packageUser.mPackageName, packageUser.mUser);
84         } catch (IllegalStateException e) {
85             // b/277189566: Launcher will load the widget when it gets the user-unlock event.
86             // If exception is thrown because of device is locked, it means a race condition occurs
87             // that the user got locked again while launcher is processing the event. In this case
88             // we should return empty list.
89             return Collections.emptyList();
90         }
91     }
92 
93     /**
94      * @see AppWidgetManager#bindAppWidgetIdIfAllowed(int, UserHandle, ComponentName, Bundle)
95      */
bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, Bundle options)96     public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info,
97             Bundle options) {
98         if (WidgetsModel.GO_DISABLE_WIDGETS) {
99             return false;
100         }
101         if (appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
102             return true;
103         }
104         return mAppWidgetManager.bindAppWidgetIdIfAllowed(
105                 appWidgetId, info.getProfile(), info.provider, options);
106     }
107 
findProvider(ComponentName provider, UserHandle user)108     public LauncherAppWidgetProviderInfo findProvider(ComponentName provider, UserHandle user) {
109         if (WidgetsModel.GO_DISABLE_WIDGETS) {
110             return null;
111         }
112         for (AppWidgetProviderInfo info :
113                 getAllProviders(new PackageUserKey(provider.getPackageName(), user))) {
114             if (info.provider.equals(provider)) {
115                 return LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
116             }
117         }
118         return null;
119     }
120 
121     /**
122      * Returns if a AppWidgetProvider has marked a widget restored
123      */
isAppWidgetRestored(int appWidgetId)124     public boolean isAppWidgetRestored(int appWidgetId) {
125         return !WidgetsModel.GO_DISABLE_WIDGETS && mAppWidgetManager.getAppWidgetOptions(
126                 appWidgetId).getBoolean(WIDGET_OPTION_RESTORE_COMPLETED);
127     }
128 
allWidgetsSteam(Context context)129     private static Stream<AppWidgetProviderInfo> allWidgetsSteam(Context context) {
130         AppWidgetManager awm = context.getSystemService(AppWidgetManager.class);
131         return Stream.concat(
132                 UserCache.INSTANCE.get(context)
133                         .getUserProfiles()
134                         .stream()
135                         .flatMap(u -> awm.getInstalledProvidersForProfile(u).stream()),
136                 CustomWidgetManager.INSTANCE.get(context).stream());
137     }
138 }
139