1 /*
2  * Copyright (C) 2009 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;
18 
19 import android.appwidget.AppWidgetHostView;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.os.Process;
23 
24 import com.android.launcher3.util.ContentWriter;
25 
26 /**
27  * Represents a widget (either instantiated or about to be) in the Launcher.
28  */
29 public class LauncherAppWidgetInfo extends ItemInfo {
30 
31     public static final int RESTORE_COMPLETED = 0;
32 
33     /**
34      * This is set during the package backup creation.
35      */
36     public static final int FLAG_ID_NOT_VALID = 1;
37 
38     /**
39      * Indicates that the provider is not available yet.
40      */
41     public static final int FLAG_PROVIDER_NOT_READY = 2;
42 
43     /**
44      * Indicates that the widget UI is not yet ready, and user needs to set it up again.
45      */
46     public static final int FLAG_UI_NOT_READY = 4;
47 
48     /**
49      * Indicates that the widget restore has started.
50      */
51     public static final int FLAG_RESTORE_STARTED = 8;
52 
53     /**
54      * Indicates that the widget has been allocated an Id. The id is still not valid, as it has
55      * not been bound yet.
56      */
57     public static final int FLAG_ID_ALLOCATED = 16;
58 
59     /**
60      * Indicates that the widget does not need to show config activity, even if it has a
61      * configuration screen. It can also optionally have some extras which are sent during bind.
62      */
63     public static final int FLAG_DIRECT_CONFIG = 32;
64 
65     /**
66      * Indicates that the widget hasn't been instantiated yet.
67      */
68     public static final int NO_ID = -1;
69 
70     /**
71      * Indicates that this is a locally defined widget and hence has no system allocated id.
72      */
73     static final int CUSTOM_WIDGET_ID = -100;
74 
75     /**
76      * Identifier for this widget when talking with
77      * {@link android.appwidget.AppWidgetManager} for updates.
78      */
79     public int appWidgetId = NO_ID;
80 
81     public ComponentName providerName;
82 
83     /**
84      * Indicates the restore status of the widget.
85      */
86     public int restoreStatus;
87 
88     /**
89      * Indicates the installation progress of the widget provider
90      */
91     public int installProgress = -1;
92 
93     /**
94      * Optional extras sent during widget bind. See {@link #FLAG_DIRECT_CONFIG}.
95      */
96     public Intent bindOptions;
97 
98     private boolean mHasNotifiedInitialWidgetSizeChanged;
99 
LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName)100     public LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
101         if (appWidgetId == CUSTOM_WIDGET_ID) {
102             itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
103         } else {
104             itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
105         }
106 
107         this.appWidgetId = appWidgetId;
108         this.providerName = providerName;
109 
110         // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
111         // to indicate that they should be calculated based on the layout and minWidth/minHeight
112         spanX = -1;
113         spanY = -1;
114         // We only support app widgets on current user.
115         user = Process.myUserHandle();
116         restoreStatus = RESTORE_COMPLETED;
117     }
118 
119     /** Used for testing **/
LauncherAppWidgetInfo()120     public LauncherAppWidgetInfo() {
121         itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
122     }
123 
isCustomWidget()124     public boolean isCustomWidget() {
125         return appWidgetId == CUSTOM_WIDGET_ID;
126     }
127 
128     @Override
onAddToDatabase(ContentWriter writer)129     public void onAddToDatabase(ContentWriter writer) {
130         super.onAddToDatabase(writer);
131         writer.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId)
132                 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString())
133                 .put(LauncherSettings.Favorites.RESTORED, restoreStatus)
134                 .put(LauncherSettings.Favorites.INTENT, bindOptions);
135     }
136 
137     /**
138      * When we bind the widget, we should notify the widget that the size has changed if we have not
139      * done so already (only really for default workspace widgets).
140      */
onBindAppWidget(Launcher launcher, AppWidgetHostView hostView)141     void onBindAppWidget(Launcher launcher, AppWidgetHostView hostView) {
142         if (!mHasNotifiedInitialWidgetSizeChanged) {
143             AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
144             mHasNotifiedInitialWidgetSizeChanged = true;
145         }
146     }
147 
148     @Override
dumpProperties()149     protected String dumpProperties() {
150         return super.dumpProperties() + " appWidgetId=" + appWidgetId;
151     }
152 
isWidgetIdAllocated()153     public final boolean isWidgetIdAllocated() {
154         return (restoreStatus & FLAG_ID_NOT_VALID) == 0 ||
155                 (restoreStatus & FLAG_ID_ALLOCATED) == FLAG_ID_ALLOCATED;
156     }
157 
hasRestoreFlag(int flag)158     public final boolean hasRestoreFlag(int flag) {
159         return (restoreStatus & flag) == flag;
160     }
161 }
162