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