1 /*
2  * Copyright (C) 2020 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 package com.android.launcher3.util;
17 
18 import android.appwidget.AppWidgetProviderInfo;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.pm.ActivityInfo;
22 import android.content.pm.ApplicationInfo;
23 import android.os.Bundle;
24 
25 import com.android.launcher3.LauncherSettings;
26 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
27 import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
28 import com.android.launcher3.widget.LauncherWidgetHolder;
29 import com.android.launcher3.widget.PendingAddWidgetInfo;
30 import com.android.launcher3.widget.WidgetManagerHelper;
31 
32 /**
33  * Common method for widget binding
34  */
35 public class WidgetUtils {
36 
37     /**
38      * Creates a LauncherAppWidgetInfo corresponding to {@param info}
39      *
40      * @param bindWidget if true the info is bound and a valid widgetId is assigned to
41      *                   the LauncherAppWidgetInfo
42      */
createWidgetInfo( LauncherAppWidgetProviderInfo info, Context targetContext, boolean bindWidget)43     public static LauncherAppWidgetInfo createWidgetInfo(
44             LauncherAppWidgetProviderInfo info, Context targetContext, boolean bindWidget) {
45         LauncherAppWidgetInfo item = new LauncherAppWidgetInfo(
46                 LauncherAppWidgetInfo.NO_ID, info.provider);
47         item.spanX = info.minSpanX;
48         item.spanY = info.minSpanY;
49         item.minSpanX = info.minSpanX;
50         item.minSpanY = info.minSpanY;
51         item.user = info.getProfile();
52         item.cellX = 0;
53         item.cellY = 1;
54         item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
55 
56         if (bindWidget) {
57             PendingAddWidgetInfo pendingInfo =
58                     new PendingAddWidgetInfo(
59                             info, LauncherSettings.Favorites.CONTAINER_WIDGETS_TRAY);
60             pendingInfo.spanX = item.spanX;
61             pendingInfo.spanY = item.spanY;
62             pendingInfo.minSpanX = item.minSpanX;
63             pendingInfo.minSpanY = item.minSpanY;
64             Bundle options = pendingInfo.getDefaultSizeOptions(targetContext);
65 
66             LauncherWidgetHolder holder = LauncherWidgetHolder.newInstance(targetContext);
67             try {
68                 int widgetId = holder.allocateAppWidgetId();
69                 if (!new WidgetManagerHelper(targetContext)
70                         .bindAppWidgetIdIfAllowed(widgetId, info, options)) {
71                     holder.deleteAppWidgetId(widgetId);
72                     throw new IllegalArgumentException("Unable to bind widget id");
73                 }
74                 item.appWidgetId = widgetId;
75             } finally {
76                 // Necessary to destroy the holder to free up possible activity context
77                 holder.destroy();
78             }
79         }
80         return item;
81     }
82 
83     /**
84      * Creates a {@link AppWidgetProviderInfo} for the provided component name
85      */
createAppWidgetProviderInfo(ComponentName cn)86     public static AppWidgetProviderInfo createAppWidgetProviderInfo(ComponentName cn) {
87         ActivityInfo activityInfo = new ActivityInfo();
88         activityInfo.applicationInfo = new ApplicationInfo();
89         AppWidgetProviderInfo info = new AppWidgetProviderInfo();
90         info.providerInfo = activityInfo;
91         info.provider = cn;
92         return info;
93     }
94 }
95