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