• 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         return mAppWidgetManager.getInstalledProvidersForPackage(
82                 packageUser.mPackageName, packageUser.mUser);
83     }
84 
85     /**
86      * @see AppWidgetManager#bindAppWidgetIdIfAllowed(int, UserHandle, ComponentName, Bundle)
87      */
bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, Bundle options)88     public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info,
89             Bundle options) {
90         if (WidgetsModel.GO_DISABLE_WIDGETS) {
91             return false;
92         }
93         if (appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
94             return true;
95         }
96         return mAppWidgetManager.bindAppWidgetIdIfAllowed(
97                 appWidgetId, info.getProfile(), info.provider, options);
98     }
99 
findProvider(ComponentName provider, UserHandle user)100     public LauncherAppWidgetProviderInfo findProvider(ComponentName provider, UserHandle user) {
101         if (WidgetsModel.GO_DISABLE_WIDGETS) {
102             return null;
103         }
104         for (AppWidgetProviderInfo info :
105                 getAllProviders(new PackageUserKey(provider.getPackageName(), user))) {
106             if (info.provider.equals(provider)) {
107                 return LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
108             }
109         }
110         return null;
111     }
112 
113     /**
114      * Returns if a AppWidgetProvider has marked a widget restored
115      */
isAppWidgetRestored(int appWidgetId)116     public boolean isAppWidgetRestored(int appWidgetId) {
117         return !WidgetsModel.GO_DISABLE_WIDGETS && mAppWidgetManager.getAppWidgetOptions(
118                 appWidgetId).getBoolean(WIDGET_OPTION_RESTORE_COMPLETED);
119     }
120 
allWidgetsSteam(Context context)121     private static Stream<AppWidgetProviderInfo> allWidgetsSteam(Context context) {
122         AppWidgetManager awm = context.getSystemService(AppWidgetManager.class);
123         return Stream.concat(
124                 UserCache.INSTANCE.get(context)
125                         .getUserProfiles()
126                         .stream()
127                         .flatMap(u -> awm.getInstalledProvidersForProfile(u).stream()),
128                 CustomWidgetManager.INSTANCE.get(context).stream());
129     }
130 }
131