Home | History | Annotate | Download | only in compat
      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 import android.support.annotation.Nullable;
     26 
     27 import com.android.launcher3.LauncherAppWidgetInfo;
     28 import com.android.launcher3.LauncherAppWidgetProviderInfo;
     29 import com.android.launcher3.Utilities;
     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.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 
     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 
     59     AppWidgetManagerCompat(Context context) {
     60         mContext = context;
     61         mAppWidgetManager = AppWidgetManager.getInstance(context);
     62     }
     63 
     64     public LauncherAppWidgetProviderInfo getLauncherAppWidgetInfo(int appWidgetId) {
     65         if (FeatureFlags.ENABLE_CUSTOM_WIDGETS
     66                 && appWidgetId <= LauncherAppWidgetInfo.CUSTOM_WIDGET_ID) {
     67             return CustomWidgetParser.getWidgetProvider(mContext, appWidgetId);
     68         }
     69 
     70         AppWidgetProviderInfo info = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
     71         return info == null ? null : LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
     72     }
     73 
     74     public abstract List<AppWidgetProviderInfo> getAllProviders(
     75             @Nullable PackageUserKey packageUser);
     76 
     77     public abstract boolean bindAppWidgetIdIfAllowed(
     78             int appWidgetId, AppWidgetProviderInfo info, Bundle options);
     79 
     80     public abstract LauncherAppWidgetProviderInfo findProvider(
     81             ComponentName provider, UserHandle user);
     82 
     83     public abstract HashMap<ComponentKey, AppWidgetProviderInfo> getAllProvidersMap();
     84 }
     85