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.compat; 18 19 import android.appwidget.AppWidgetProviderInfo; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.os.Process; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 27 import com.android.launcher3.LauncherAppWidgetInfo; 28 import com.android.launcher3.LauncherAppWidgetProviderInfo; 29 import com.android.launcher3.config.FeatureFlags; 30 import com.android.launcher3.util.ComponentKey; 31 import com.android.launcher3.util.PackageUserKey; 32 import com.android.launcher3.widget.custom.CustomWidgetParser; 33 34 import java.util.ArrayList; 35 import java.util.Collections; 36 import java.util.HashMap; 37 import java.util.Iterator; 38 import java.util.List; 39 40 import androidx.annotation.Nullable; 41 42 class AppWidgetManagerCompatVL extends AppWidgetManagerCompat { 43 44 private final UserManager mUserManager; 45 AppWidgetManagerCompatVL(Context context)46 AppWidgetManagerCompatVL(Context context) { 47 super(context); 48 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 49 } 50 51 @Override getAllProviders(@ullable PackageUserKey packageUser)52 public List<AppWidgetProviderInfo> getAllProviders(@Nullable PackageUserKey packageUser) { 53 if (FeatureFlags.GO_DISABLE_WIDGETS) { 54 return Collections.emptyList(); 55 } 56 if (packageUser == null) { 57 ArrayList<AppWidgetProviderInfo> providers = new ArrayList<AppWidgetProviderInfo>(); 58 for (UserHandle user : mUserManager.getUserProfiles()) { 59 providers.addAll(mAppWidgetManager.getInstalledProvidersForProfile(user)); 60 } 61 62 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS) { 63 providers.addAll(CustomWidgetParser.getCustomWidgets(mContext)); 64 } 65 return providers; 66 } 67 // Only get providers for the given package/user. 68 List<AppWidgetProviderInfo> providers = new ArrayList<>(mAppWidgetManager 69 .getInstalledProvidersForProfile(packageUser.mUser)); 70 Iterator<AppWidgetProviderInfo> iterator = providers.iterator(); 71 while (iterator.hasNext()) { 72 if (!iterator.next().provider.getPackageName().equals(packageUser.mPackageName)) { 73 iterator.remove(); 74 } 75 } 76 77 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS && Process.myUserHandle().equals(packageUser.mUser) 78 && mContext.getPackageName().equals(packageUser.mPackageName)) { 79 providers.addAll(CustomWidgetParser.getCustomWidgets(mContext)); 80 } 81 return providers; 82 } 83 84 @Override bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, Bundle options)85 public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, 86 Bundle options) { 87 if (FeatureFlags.GO_DISABLE_WIDGETS) { 88 return false; 89 } 90 91 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS 92 && appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) { 93 return true; 94 } 95 return mAppWidgetManager.bindAppWidgetIdIfAllowed( 96 appWidgetId, info.getProfile(), info.provider, options); 97 } 98 99 @Override findProvider(ComponentName provider, UserHandle user)100 public LauncherAppWidgetProviderInfo findProvider(ComponentName provider, UserHandle user) { 101 if (FeatureFlags.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 111 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS && Process.myUserHandle().equals(user)) { 112 for (LauncherAppWidgetProviderInfo info : 113 CustomWidgetParser.getCustomWidgets(mContext)) { 114 if (info.provider.equals(provider)) { 115 return info; 116 } 117 } 118 } 119 return null; 120 } 121 122 @Override getAllProvidersMap()123 public HashMap<ComponentKey, AppWidgetProviderInfo> getAllProvidersMap() { 124 HashMap<ComponentKey, AppWidgetProviderInfo> result = new HashMap<>(); 125 if (FeatureFlags.GO_DISABLE_WIDGETS) { 126 return result; 127 } 128 for (UserHandle user : mUserManager.getUserProfiles()) { 129 for (AppWidgetProviderInfo info : 130 mAppWidgetManager.getInstalledProvidersForProfile(user)) { 131 result.put(new ComponentKey(info.provider, user), info); 132 } 133 } 134 135 if (FeatureFlags.ENABLE_CUSTOM_WIDGETS) { 136 for (LauncherAppWidgetProviderInfo info : 137 CustomWidgetParser.getCustomWidgets(mContext)) { 138 result.put(new ComponentKey(info.provider, info.getProfile()), info); 139 } 140 } 141 return result; 142 } 143 } 144