LauncherAppWidgetInfo.java revision d478c83fd6abebe6ce3f066d392a28f8ba9be100
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
17package com.android.launcher3;
18
19import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.ContentValues;
22import android.content.Context;
23
24import com.android.launcher3.compat.UserHandleCompat;
25
26/**
27 * Represents a widget (either instantiated or about to be) in the Launcher.
28 */
29public 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 hasn't been instantiated yet.
61     */
62    static final int NO_ID = -1;
63
64    /**
65     * Indicates that this is a locally defined widget and hence has no system allocated id.
66     */
67    static final int CUSTOM_WIDGET_ID = -100;
68
69    /**
70     * Identifier for this widget when talking with
71     * {@link android.appwidget.AppWidgetManager} for updates.
72     */
73    int appWidgetId = NO_ID;
74
75    ComponentName providerName;
76
77    /**
78     * Indicates the restore status of the widget.
79     */
80    int restoreStatus;
81
82    /**
83     * Indicates the installation progress of the widget provider
84     */
85    int installProgress = -1;
86
87    private boolean mHasNotifiedInitialWidgetSizeChanged;
88
89    LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
90        if (appWidgetId == CUSTOM_WIDGET_ID) {
91            itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
92        } else {
93            itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
94        }
95
96        this.appWidgetId = appWidgetId;
97        this.providerName = providerName;
98
99        // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
100        // to indicate that they should be calculated based on the layout and minWidth/minHeight
101        spanX = -1;
102        spanY = -1;
103        // We only support app widgets on current user.
104        user = UserHandleCompat.myUserHandle();
105        restoreStatus = RESTORE_COMPLETED;
106    }
107
108    public boolean isCustomWidget() {
109        return appWidgetId == CUSTOM_WIDGET_ID;
110    }
111
112    @Override
113    void onAddToDatabase(Context context, ContentValues values) {
114        super.onAddToDatabase(context, values);
115        values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
116        values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString());
117        values.put(LauncherSettings.Favorites.RESTORED, restoreStatus);
118    }
119
120    /**
121     * When we bind the widget, we should notify the widget that the size has changed if we have not
122     * done so already (only really for default workspace widgets).
123     */
124    void onBindAppWidget(Launcher launcher, AppWidgetHostView hostView) {
125        if (!mHasNotifiedInitialWidgetSizeChanged) {
126            AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
127            mHasNotifiedInitialWidgetSizeChanged = true;
128        }
129    }
130
131    @Override
132    public String toString() {
133        return "AppWidget(id=" + Integer.toString(appWidgetId) + ")";
134    }
135
136    public final boolean isWidgetIdAllocated() {
137        return (restoreStatus & FLAG_ID_NOT_VALID) == 0 ||
138                (restoreStatus & FLAG_ID_ALLOCATED) == FLAG_ID_ALLOCATED;
139    }
140
141    public final boolean hasRestoreFlag(int flag) {
142        return (restoreStatus & flag) == flag;
143    }
144}
145