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.annotation.TargetApi;
20 import android.app.Activity;
21 import android.appwidget.AppWidgetHost;
22 import android.appwidget.AppWidgetManager;
23 import android.appwidget.AppWidgetProviderInfo;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.graphics.Bitmap;
28 import android.graphics.drawable.Drawable;
29 import android.os.Build;
30 import android.os.Bundle;
31 
32 import com.android.launcher3.IconCache;
33 import com.android.launcher3.LauncherAppWidgetProviderInfo;
34 import com.android.launcher3.Utilities;
35 import com.android.launcher3.util.ComponentKey;
36 
37 import java.util.HashMap;
38 import java.util.List;
39 
40 class AppWidgetManagerCompatV16 extends AppWidgetManagerCompat {
41 
AppWidgetManagerCompatV16(Context context)42     AppWidgetManagerCompatV16(Context context) {
43         super(context);
44     }
45 
46     @Override
getAllProviders()47     public List<AppWidgetProviderInfo> getAllProviders() {
48         return mAppWidgetManager.getInstalledProviders();
49     }
50 
51     @Override
loadLabel(LauncherAppWidgetProviderInfo info)52     public String loadLabel(LauncherAppWidgetProviderInfo info) {
53         return Utilities.trim(info.label);
54     }
55 
56     @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
57     @Override
bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, Bundle options)58     public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info,
59             Bundle options) {
60         if (Utilities.ATLEAST_JB_MR1) {
61             return mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.provider, options);
62         } else {
63             return mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.provider);
64         }
65     }
66 
67     @Override
getUser(LauncherAppWidgetProviderInfo info)68     public UserHandleCompat getUser(LauncherAppWidgetProviderInfo info) {
69         return UserHandleCompat.myUserHandle();
70     }
71 
72     @Override
startConfigActivity(AppWidgetProviderInfo info, int widgetId, Activity activity, AppWidgetHost host, int requestCode)73     public void startConfigActivity(AppWidgetProviderInfo info, int widgetId, Activity activity,
74             AppWidgetHost host, int requestCode) {
75         Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
76         intent.setComponent(info.configure);
77         intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
78         Utilities.startActivityForResultSafely(activity, intent, requestCode);
79     }
80 
81     @Override
loadPreview(AppWidgetProviderInfo info)82     public Drawable loadPreview(AppWidgetProviderInfo info) {
83         return mContext.getPackageManager().getDrawable(
84                 info.provider.getPackageName(), info.previewImage, null);
85     }
86 
87     @Override
loadIcon(LauncherAppWidgetProviderInfo info, IconCache cache)88     public Drawable loadIcon(LauncherAppWidgetProviderInfo info, IconCache cache) {
89         return cache.getFullResIcon(info.provider.getPackageName(), info.icon);
90     }
91 
92     @Override
getBadgeBitmap(LauncherAppWidgetProviderInfo info, Bitmap bitmap, int imageWidth, int imageHeight)93     public Bitmap getBadgeBitmap(LauncherAppWidgetProviderInfo info, Bitmap bitmap,
94             int imageWidth, int imageHeight) {
95         return bitmap;
96     }
97 
98     @Override
findProvider( ComponentName provider, UserHandleCompat user)99     public LauncherAppWidgetProviderInfo findProvider(
100             ComponentName provider, UserHandleCompat user) {
101         for (AppWidgetProviderInfo info : mAppWidgetManager.getInstalledProviders()) {
102             if (info.provider.equals(provider)) {
103                 return LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
104             }
105         }
106         return null;
107     }
108 
109     @Override
getAllProvidersMap()110     public HashMap<ComponentKey, AppWidgetProviderInfo> getAllProvidersMap() {
111         HashMap<ComponentKey, AppWidgetProviderInfo> result = new HashMap<>();
112         UserHandleCompat user = UserHandleCompat.myUserHandle();
113         for (AppWidgetProviderInfo info : mAppWidgetManager.getInstalledProviders()) {
114             result.put(new ComponentKey(info.provider, user), info);
115         }
116         return result;
117     }
118 }
119