131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2009 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
197376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Projectimport android.appwidget.AppWidgetHostView;
20af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport android.content.ComponentName;
2131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ContentValues;
22374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasaniimport android.content.Context;
2331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
256569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy * Represents a widget (either instantiated or about to be) in the Launcher.
2631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
277376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Projectclass LauncherAppWidgetInfo extends ItemInfo {
2831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
306569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     * Indicates that the widget hasn't been instantiated yet.
316569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     */
326569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    static final int NO_ID = -1;
336569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
346569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    /**
35629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy     * Identifier for this widget when talking with
36629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy     * {@link android.appwidget.AppWidgetManager} for updates.
3731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
386569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    int appWidgetId = NO_ID;
396569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
40af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka    ComponentName providerName;
41ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
426569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    // TODO: Are these necessary here?
436569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    int minWidth = -1;
446569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    int minHeight = -1;
456569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
46211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung    private boolean mHasNotifiedInitialWidgetSizeChanged;
47211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung
4831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
497376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project     * View that holds this widget after it's been created.  This view isn't created
5031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * until Launcher knows it's needed.
5131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
527376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    AppWidgetHostView hostView = null;
5331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
5411a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung    LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
556569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
5611a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung        this.appWidgetId = appWidgetId;
576569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        this.providerName = providerName;
586569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
596569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
606569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        // to indicate that they should be calculated based on the layout and minWidth/minHeight
616569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        spanX = -1;
626569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        spanY = -1;
636569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    }
646569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy
6531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
66374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani    void onAddToDatabase(Context context, ContentValues values) {
67374753cabf05cde1ad669d07bde47e34fdcbe499Amith Yamasani        super.onAddToDatabase(context, values);
687376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
6931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
7031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
71211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung    /**
72211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung     * When we bind the widget, we should notify the widget that the size has changed if we have not
73211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung     * done so already (only really for default workspace widgets).
74211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung     */
75211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung    void onBindAppWidget(Launcher launcher) {
76aaa5c21ee2e03def644fb6ba8eadbb59873ddd45Adam Cohen        if (!mHasNotifiedInitialWidgetSizeChanged) {
77211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung            notifyWidgetSizeChanged(launcher);
78211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung        }
79211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung    }
80211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung
81211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung    /**
82211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung     * Trigger an update callback to the widget to notify it that its size has changed.
83211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung     */
84211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung    void notifyWidgetSizeChanged(Launcher launcher) {
85211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung        AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
86211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung        mHasNotifiedInitialWidgetSizeChanged = true;
87211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung    }
88211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung
8931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
9031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public String toString() {
918802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler        return "AppWidget(id=" + Integer.toString(appWidgetId) + ")";
9231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    @Override
959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    void unbind() {
969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        super.unbind();
979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        hostView = null;
989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
9931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
100