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.AppWidgetManager; 20 import android.appwidget.AppWidgetProviderInfo; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.os.UserHandle; 25 26 import androidx.annotation.Nullable; 27 28 import com.android.launcher3.LauncherAppWidgetInfo; 29 import com.android.launcher3.LauncherAppWidgetProviderInfo; 30 import com.android.launcher3.Utilities; 31 import com.android.launcher3.util.ComponentKey; 32 import com.android.launcher3.util.PackageUserKey; 33 import com.android.launcher3.widget.custom.CustomWidgetManager; 34 35 import java.util.HashMap; 36 import java.util.List; 37 38 public abstract class AppWidgetManagerCompat { 39 40 private static final Object sInstanceLock = new Object(); 41 private static AppWidgetManagerCompat sInstance; 42 getInstance(Context context)43 public static AppWidgetManagerCompat getInstance(Context context) { 44 synchronized (sInstanceLock) { 45 if (sInstance == null) { 46 if (Utilities.ATLEAST_OREO) { 47 sInstance = new AppWidgetManagerCompatVO(context.getApplicationContext()); 48 } else { 49 sInstance = new AppWidgetManagerCompatVL(context.getApplicationContext()); 50 } 51 } 52 return sInstance; 53 } 54 } 55 56 final AppWidgetManager mAppWidgetManager; 57 final Context mContext; 58 AppWidgetManagerCompat(Context context)59 AppWidgetManagerCompat(Context context) { 60 mContext = context; 61 mAppWidgetManager = AppWidgetManager.getInstance(context); 62 } 63 getLauncherAppWidgetInfo(int appWidgetId)64 public LauncherAppWidgetProviderInfo getLauncherAppWidgetInfo(int appWidgetId) { 65 if (appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) { 66 return CustomWidgetManager.INSTANCE.get(mContext).getWidgetProvider(appWidgetId); 67 } 68 AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId); 69 return info == null ? null : LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info); 70 } 71 getAllProviders( @ullable PackageUserKey packageUser)72 public abstract List<AppWidgetProviderInfo> getAllProviders( 73 @Nullable PackageUserKey packageUser); 74 bindAppWidgetIdIfAllowed( int appWidgetId, AppWidgetProviderInfo info, Bundle options)75 public abstract boolean bindAppWidgetIdIfAllowed( 76 int appWidgetId, AppWidgetProviderInfo info, Bundle options); 77 findProvider( ComponentName provider, UserHandle user)78 public abstract LauncherAppWidgetProviderInfo findProvider( 79 ComponentName provider, UserHandle user); 80 getAllProvidersMap()81 public abstract HashMap<ComponentKey, AppWidgetProviderInfo> getAllProvidersMap(); 82 } 83