1 /*
2  * Copyright (C) 2015 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.widget;
17 
18 import android.appwidget.AppWidgetHostView;
19 import android.content.Context;
20 import android.os.Bundle;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 
25 import com.android.launcher3.LauncherSettings;
26 import com.android.launcher3.PendingAddItemInfo;
27 import com.android.launcher3.logger.LauncherAtom;
28 import com.android.launcher3.model.data.CollectionInfo;
29 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
30 import com.android.launcher3.widget.picker.WidgetRecommendationCategory;
31 import com.android.launcher3.widget.util.WidgetSizes;
32 
33 /**
34  * Meta data used for late binding of {@link LauncherAppWidgetProviderInfo}.
35  *
36  * @see {@link PendingAddItemInfo}
37  */
38 public class PendingAddWidgetInfo extends PendingAddItemInfo {
39     public int previewImage;
40     public int icon;
41     public LauncherAppWidgetProviderInfo info;
42     public AppWidgetHostView boundWidget;
43     public Bundle bindOptions = null;
44     public int sourceContainer;
45 
46     public WidgetRecommendationCategory recommendationCategory = null;
47 
PendingAddWidgetInfo( LauncherAppWidgetProviderInfo i, int container, WidgetRecommendationCategory recommendationCategory)48     public PendingAddWidgetInfo(
49             LauncherAppWidgetProviderInfo i,
50             int container,
51             WidgetRecommendationCategory recommendationCategory) {
52         this(i, container);
53         this.recommendationCategory = recommendationCategory;
54     }
55 
PendingAddWidgetInfo(LauncherAppWidgetProviderInfo i, int container)56     public PendingAddWidgetInfo(LauncherAppWidgetProviderInfo i, int container) {
57         if (i.isCustomWidget()) {
58             itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
59         } else {
60             itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
61         }
62         this.info = i;
63         user = i.getProfile();
64         componentName = i.provider;
65         previewImage = i.previewImage;
66         icon = i.icon;
67 
68         spanX = i.spanX;
69         spanY = i.spanY;
70         minSpanX = i.minSpanX;
71         minSpanY = i.minSpanY;
72         this.sourceContainer = this.container = container;
73     }
74 
getHandler()75     public WidgetAddFlowHandler getHandler() {
76         return new WidgetAddFlowHandler(info);
77     }
78 
getDefaultSizeOptions(Context context)79     public Bundle getDefaultSizeOptions(Context context) {
80         return WidgetSizes.getWidgetSizeOptions(context, componentName, spanX, spanY);
81     }
82 
83     @NonNull
84     @Override
buildProto(@ullable CollectionInfo collectionInfo)85     public LauncherAtom.ItemInfo buildProto(@Nullable CollectionInfo collectionInfo) {
86         LauncherAtom.ItemInfo info = super.buildProto(collectionInfo);
87         return info.toBuilder()
88                 .addItemAttributes(LauncherAppWidgetInfo.getAttribute(sourceContainer))
89                 .build();
90     }
91 }
92