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.common;
17 
18 import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID;
19 import static com.android.launcher3.widget.WidgetHostViewLoader.getDefaultOptionsForWidget;
20 
21 import android.appwidget.AppWidgetHost;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.os.Bundle;
25 
26 import com.android.launcher3.LauncherAppWidgetHost;
27 import com.android.launcher3.LauncherAppWidgetProviderInfo;
28 import com.android.launcher3.LauncherSettings;
29 import com.android.launcher3.model.data.ItemInfo;
30 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
31 import com.android.launcher3.util.ContentWriter;
32 import com.android.launcher3.widget.PendingAddWidgetInfo;
33 import com.android.launcher3.widget.WidgetManagerHelper;
34 
35 /**
36  * Common method for widget binding
37  */
38 public class WidgetUtils {
39 
40     /**
41      * Creates a LauncherAppWidgetInfo corresponding to {@param info}
42      *
43      * @param bindWidget if true the info is bound and a valid widgetId is assigned to
44      *                   the LauncherAppWidgetInfo
45      */
createWidgetInfo( LauncherAppWidgetProviderInfo info, Context targetContext, boolean bindWidget)46     public static LauncherAppWidgetInfo createWidgetInfo(
47             LauncherAppWidgetProviderInfo info, Context targetContext, boolean bindWidget) {
48         LauncherAppWidgetInfo item = new LauncherAppWidgetInfo(
49                 LauncherAppWidgetInfo.NO_ID, info.provider);
50         item.spanX = info.minSpanX;
51         item.spanY = info.minSpanY;
52         item.minSpanX = info.minSpanX;
53         item.minSpanY = info.minSpanY;
54         item.user = info.getProfile();
55         item.cellX = 0;
56         item.cellY = 1;
57         item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
58 
59         if (bindWidget) {
60             PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(info);
61             pendingInfo.spanX = item.spanX;
62             pendingInfo.spanY = item.spanY;
63             pendingInfo.minSpanX = item.minSpanX;
64             pendingInfo.minSpanY = item.minSpanY;
65             Bundle options = getDefaultOptionsForWidget(targetContext, pendingInfo);
66 
67             AppWidgetHost host = new LauncherAppWidgetHost(targetContext);
68             int widgetId = host.allocateAppWidgetId();
69             if (!new WidgetManagerHelper(targetContext)
70                     .bindAppWidgetIdIfAllowed(widgetId, info, options)) {
71                 host.deleteAppWidgetId(widgetId);
72                 throw new IllegalArgumentException("Unable to bind widget id");
73             }
74             item.appWidgetId = widgetId;
75         }
76         return item;
77     }
78 
79     /**
80      * Adds {@param item} on the homescreen on the 0th screen
81      */
addItemToScreen(ItemInfo item, Context targetContext)82     public static void addItemToScreen(ItemInfo item, Context targetContext) {
83         ContentResolver resolver = targetContext.getContentResolver();
84         int screenId = FIRST_SCREEN_ID;
85         // Update the screen id counter for the provider.
86         LauncherSettings.Settings.call(resolver,
87                 LauncherSettings.Settings.METHOD_NEW_SCREEN_ID);
88 
89         if (screenId > FIRST_SCREEN_ID) {
90             screenId = FIRST_SCREEN_ID;
91         }
92 
93         // Insert the item
94         ContentWriter writer = new ContentWriter(targetContext);
95         item.id = LauncherSettings.Settings.call(
96                 resolver, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
97                 .getInt(LauncherSettings.Settings.EXTRA_VALUE);
98         item.screenId = screenId;
99         item.onAddToDatabase(writer);
100         writer.put(LauncherSettings.Favorites._ID, item.id);
101         resolver.insert(LauncherSettings.Favorites.CONTENT_URI,
102                 writer.getValues(targetContext));
103     }
104 }
105