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 17325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3; 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; 22ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport android.content.Context; 23ed13187a745866483139e2878037e1f8427ce567Kenny Guy 24ed13187a745866483139e2878037e1f8427ce567Kenny Guyimport com.android.launcher3.compat.UserHandleCompat; 2531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project 2631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/** 276569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy * Represents a widget (either instantiated or about to be) in the Launcher. 2831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */ 297b168a1bb94044d9ae11004bae18beba9eed46fdAnjali Koppalpublic class LauncherAppWidgetInfo extends ItemInfo { 3031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project 31651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal public static final int RESTORE_COMPLETED = 0; 32651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal 33651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal /** 34651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal * This is set during the package backup creation. 35651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal */ 36ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal public static final int FLAG_ID_NOT_VALID = 1; 37651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal 38651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal /** 39ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal * Indicates that the provider is not available yet. 40651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal */ 41ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal public static final int FLAG_PROVIDER_NOT_READY = 2; 42ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal 43ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal /** 44ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal * Indicates that the widget UI is not yet ready, and user needs to set it up again. 45ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal */ 46ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal public static final int FLAG_UI_NOT_READY = 4; 47651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal 4831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project /** 499448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal * Indicates that the widget restore has started. 509448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal */ 519448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal public static final int FLAG_RESTORE_STARTED = 8; 529448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal 539448536b113afa6ceefce26604a1b44618d1d4f2Sunny Goyal /** 546569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy * Indicates that the widget hasn't been instantiated yet. 556569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy */ 566569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy static final int NO_ID = -1; 576569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy 586569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy /** 59629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy * Identifier for this widget when talking with 60629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy * {@link android.appwidget.AppWidgetManager} for updates. 6131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */ 626569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy int appWidgetId = NO_ID; 636569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy 64af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka ComponentName providerName; 65ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen 666569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy // TODO: Are these necessary here? 676569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy int minWidth = -1; 686569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy int minHeight = -1; 696569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy 70651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal /** 71651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal * Indicates the restore status of the widget. 72651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal */ 73651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal int restoreStatus; 74651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal 750fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal /** 760fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal * Indicates the installation progress of the widget provider 770fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal */ 78e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal int installProgress = -1; 790fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal 80211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung private boolean mHasNotifiedInitialWidgetSizeChanged; 81211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung 8231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project /** 837376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project * View that holds this widget after it's been created. This view isn't created 8431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * until Launcher knows it's needed. 8531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */ 867376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project AppWidgetHostView hostView = null; 8731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project 8811a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) { 896569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET; 9011a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung this.appWidgetId = appWidgetId; 916569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy this.providerName = providerName; 926569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy 936569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy // Since the widget isn't instantiated yet, we don't know these values. Set them to -1 946569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy // to indicate that they should be calculated based on the layout and minWidth/minHeight 956569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy spanX = -1; 966569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy spanY = -1; 97ed13187a745866483139e2878037e1f8427ce567Kenny Guy // We only support app widgets on current user. 98ed13187a745866483139e2878037e1f8427ce567Kenny Guy user = UserHandleCompat.myUserHandle(); 99651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal restoreStatus = RESTORE_COMPLETED; 1006569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy } 1016569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy 10231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project @Override 103ed13187a745866483139e2878037e1f8427ce567Kenny Guy void onAddToDatabase(Context context, ContentValues values) { 104ed13187a745866483139e2878037e1f8427ce567Kenny Guy super.onAddToDatabase(context, values); 1057376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); 106d5e66bfca1dc4fdbab7513d88c6568bc4d17bca8Chris Wren values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString()); 107ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal values.put(LauncherSettings.Favorites.RESTORED, restoreStatus); 10831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project } 10931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project 110211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung /** 111211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung * When we bind the widget, we should notify the widget that the size has changed if we have not 112211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung * done so already (only really for default workspace widgets). 113211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung */ 114211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung void onBindAppWidget(Launcher launcher) { 115aaa5c21ee2e03def644fb6ba8eadbb59873ddd45Adam Cohen if (!mHasNotifiedInitialWidgetSizeChanged) { 116211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung notifyWidgetSizeChanged(launcher); 117211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung } 118211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung } 119211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung 120211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung /** 121211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung * Trigger an update callback to the widget to notify it that its size has changed. 122211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung */ 123211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung void notifyWidgetSizeChanged(Launcher launcher) { 124211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY); 125211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung mHasNotifiedInitialWidgetSizeChanged = true; 126211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung } 127211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung 12831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project @Override 12931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project public String toString() { 1308802e960495e61803c18ea3dda2e30ef0a611d8fDaniel Sandler return "AppWidget(id=" + Integer.toString(appWidgetId) + ")"; 13131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project } 1329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato 1339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato @Override 1349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato void unbind() { 1359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato super.unbind(); 1369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato hostView = null; 1379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato } 138651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal 139651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal public final boolean isWidgetIdValid() { 140ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal return (restoreStatus & FLAG_ID_NOT_VALID) == 0; 141ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal } 142ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal 143ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal public final boolean hasRestoreFlag(int flag) { 144ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal return (restoreStatus & flag) == flag; 145651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal } 14631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project} 147