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.app.Activity; 20 import android.appwidget.AppWidgetHost; 21 import android.appwidget.AppWidgetProviderInfo; 22 import android.content.ActivityNotFoundException; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.support.annotation.Nullable; 29 import android.widget.Toast; 30 31 import com.android.launcher3.LauncherAppWidgetProviderInfo; 32 import com.android.launcher3.R; 33 import com.android.launcher3.util.ComponentKey; 34 import com.android.launcher3.util.PackageUserKey; 35 36 import java.util.ArrayList; 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 AppWidgetManagerCompatVL(Context context)45 AppWidgetManagerCompatVL(Context context) { 46 super(context); 47 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 48 } 49 50 @Override getAllProviders(@ullable PackageUserKey packageUser)51 public List<AppWidgetProviderInfo> getAllProviders(@Nullable PackageUserKey packageUser) { 52 if (packageUser == null) { 53 ArrayList<AppWidgetProviderInfo> providers = new ArrayList<AppWidgetProviderInfo>(); 54 for (UserHandle user : mUserManager.getUserProfiles()) { 55 providers.addAll(mAppWidgetManager.getInstalledProvidersForProfile(user)); 56 } 57 return providers; 58 } 59 // Only get providers for the given package/user. 60 List<AppWidgetProviderInfo> providers = new ArrayList<>(mAppWidgetManager 61 .getInstalledProvidersForProfile(packageUser.mUser)); 62 Iterator<AppWidgetProviderInfo> iterator = providers.iterator(); 63 while (iterator.hasNext()) { 64 if (!iterator.next().provider.getPackageName().equals(packageUser.mPackageName)) { 65 iterator.remove(); 66 } 67 } 68 return providers; 69 } 70 71 @Override bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, Bundle options)72 public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, 73 Bundle options) { 74 return mAppWidgetManager.bindAppWidgetIdIfAllowed( 75 appWidgetId, info.getProfile(), info.provider, options); 76 } 77 78 @Override startConfigActivity(AppWidgetProviderInfo info, int widgetId, Activity activity, AppWidgetHost host, int requestCode)79 public void startConfigActivity(AppWidgetProviderInfo info, int widgetId, Activity activity, 80 AppWidgetHost host, int requestCode) { 81 try { 82 host.startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null); 83 } catch (ActivityNotFoundException | SecurityException e) { 84 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); 85 } 86 } 87 88 @Override findProvider(ComponentName provider, UserHandle user)89 public LauncherAppWidgetProviderInfo findProvider(ComponentName provider, UserHandle user) { 90 for (AppWidgetProviderInfo info : mAppWidgetManager 91 .getInstalledProvidersForProfile(user)) { 92 if (info.provider.equals(provider)) { 93 return LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info); 94 } 95 } 96 return null; 97 } 98 99 @Override getAllProvidersMap()100 public HashMap<ComponentKey, AppWidgetProviderInfo> getAllProvidersMap() { 101 HashMap<ComponentKey, AppWidgetProviderInfo> result = new HashMap<>(); 102 for (UserHandle user : mUserManager.getUserProfiles()) { 103 for (AppWidgetProviderInfo info : 104 mAppWidgetManager.getInstalledProvidersForProfile(user)) { 105 result.put(new ComponentKey(info.provider, user), info); 106 } 107 } 108 return result; 109 } 110 } 111