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.launcher2;
18
19import android.appwidget.AppWidgetHostView;
20import android.content.ComponentName;
21import android.content.ContentValues;
22import android.content.Context;
23
24/**
25 * Represents a widget (either instantiated or about to be) in the Launcher.
26 */
27class LauncherAppWidgetInfo extends ItemInfo {
28
29    /**
30     * Indicates that the widget hasn't been instantiated yet.
31     */
32    static final int NO_ID = -1;
33
34    /**
35     * Identifier for this widget when talking with
36     * {@link android.appwidget.AppWidgetManager} for updates.
37     */
38    int appWidgetId = NO_ID;
39
40    ComponentName providerName;
41
42    // TODO: Are these necessary here?
43    int minWidth = -1;
44    int minHeight = -1;
45
46    private boolean mHasNotifiedInitialWidgetSizeChanged;
47
48    /**
49     * View that holds this widget after it's been created.  This view isn't created
50     * until Launcher knows it's needed.
51     */
52    AppWidgetHostView hostView = null;
53
54    LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
55        itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
56        this.appWidgetId = appWidgetId;
57        this.providerName = providerName;
58
59        // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
60        // to indicate that they should be calculated based on the layout and minWidth/minHeight
61        spanX = -1;
62        spanY = -1;
63    }
64
65    @Override
66    void onAddToDatabase(Context context, ContentValues values) {
67        super.onAddToDatabase(context, values);
68        values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
69    }
70
71    /**
72     * When we bind the widget, we should notify the widget that the size has changed if we have not
73     * done so already (only really for default workspace widgets).
74     */
75    void onBindAppWidget(Launcher launcher) {
76        if (!mHasNotifiedInitialWidgetSizeChanged) {
77            notifyWidgetSizeChanged(launcher);
78        }
79    }
80
81    /**
82     * Trigger an update callback to the widget to notify it that its size has changed.
83     */
84    void notifyWidgetSizeChanged(Launcher launcher) {
85        AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
86        mHasNotifiedInitialWidgetSizeChanged = true;
87    }
88
89    @Override
90    public String toString() {
91        return "AppWidget(id=" + Integer.toString(appWidgetId) + ")";
92    }
93
94    @Override
95    void unbind() {
96        super.unbind();
97        hostView = null;
98    }
99}
100