The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 19 | import android.appwidget.AppWidgetHostView; |
Michael Jurka | af44209 | 2010-06-10 17:01:57 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 22 | import android.os.Process; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 23 | |
Jon Miranda | 2b823f4 | 2017-05-02 18:36:18 -0700 | [diff] [blame] | 24 | import com.android.launcher3.model.PackageItemInfo; |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 25 | import com.android.launcher3.util.ContentWriter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 28 | * Represents a widget (either instantiated or about to be) in the Launcher. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | */ |
Anjali Koppal | 7b168a1 | 2014-03-04 17:16:11 -0800 | [diff] [blame] | 30 | public class LauncherAppWidgetInfo extends ItemInfo { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 32 | public static final int RESTORE_COMPLETED = 0; |
| 33 | |
| 34 | /** |
| 35 | * This is set during the package backup creation. |
| 36 | */ |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 37 | public static final int FLAG_ID_NOT_VALID = 1; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 40 | * Indicates that the provider is not available yet. |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 41 | */ |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 42 | public static final int FLAG_PROVIDER_NOT_READY = 2; |
| 43 | |
| 44 | /** |
| 45 | * Indicates that the widget UI is not yet ready, and user needs to set it up again. |
| 46 | */ |
| 47 | public static final int FLAG_UI_NOT_READY = 4; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | /** |
Sunny Goyal | 9448536 | 2014-09-18 16:13:58 -0700 | [diff] [blame] | 50 | * Indicates that the widget restore has started. |
| 51 | */ |
| 52 | public static final int FLAG_RESTORE_STARTED = 8; |
| 53 | |
| 54 | /** |
Sunny Goyal | d478c83 | 2016-04-01 12:04:16 -0700 | [diff] [blame] | 55 | * Indicates that the widget has been allocated an Id. The id is still not valid, as it has |
| 56 | * not been bound yet. |
| 57 | */ |
| 58 | public static final int FLAG_ID_ALLOCATED = 16; |
| 59 | |
| 60 | /** |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 61 | * Indicates that the widget does not need to show config activity, even if it has a |
| 62 | * configuration screen. It can also optionally have some extras which are sent during bind. |
| 63 | */ |
| 64 | public static final int FLAG_DIRECT_CONFIG = 32; |
| 65 | |
| 66 | /** |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 67 | * Indicates that the widget hasn't been instantiated yet. |
| 68 | */ |
Sunny Goyal | 658058b | 2017-01-21 01:33:02 -0800 | [diff] [blame] | 69 | public static final int NO_ID = -1; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 72 | * Indicates that this is a locally defined widget and hence has no system allocated id. |
| 73 | */ |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 74 | public static final int CUSTOM_WIDGET_ID = -100; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
Romain Guy | 629de3e | 2010-01-13 12:20:59 -0800 | [diff] [blame] | 77 | * Identifier for this widget when talking with |
| 78 | * {@link android.appwidget.AppWidgetManager} for updates. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 79 | */ |
Sunny Goyal | 04a324a | 2017-01-20 21:08:59 -0800 | [diff] [blame] | 80 | public int appWidgetId = NO_ID; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 81 | |
Sunny Goyal | 6d02c7a | 2016-05-19 12:15:39 -0700 | [diff] [blame] | 82 | public ComponentName providerName; |
Adam Cohen | ded9f8d | 2010-11-03 13:25:16 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Indicates the restore status of the widget. |
| 86 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 87 | public int restoreStatus; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 88 | |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Indicates the installation progress of the widget provider |
| 91 | */ |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 92 | public int installProgress = -1; |
Sunny Goyal | 0fc1be1 | 2014-08-11 17:05:23 -0700 | [diff] [blame] | 93 | |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Optional extras sent during widget bind. See {@link #FLAG_DIRECT_CONFIG}. |
| 96 | */ |
| 97 | public Intent bindOptions; |
| 98 | |
Jon Miranda | 2b823f4 | 2017-05-02 18:36:18 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Nonnull for pending widgets. We use this to get the icon and title for the widget. |
| 101 | */ |
| 102 | public PackageItemInfo pendingItemInfo; |
| 103 | |
Winson Chung | 211bac3 | 2012-05-15 13:43:57 -0700 | [diff] [blame] | 104 | private boolean mHasNotifiedInitialWidgetSizeChanged; |
| 105 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 106 | public LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) { |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 107 | this.appWidgetId = appWidgetId; |
| 108 | this.providerName = providerName; |
| 109 | |
| 110 | if (isCustomWidget()) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 111 | itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET; |
| 112 | } else { |
| 113 | itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET; |
| 114 | } |
| 115 | |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 116 | // Since the widget isn't instantiated yet, we don't know these values. Set them to -1 |
| 117 | // to indicate that they should be calculated based on the layout and minWidth/minHeight |
| 118 | spanX = -1; |
| 119 | spanY = -1; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 120 | // We only support app widgets on current user. |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 121 | user = Process.myUserHandle(); |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 122 | restoreStatus = RESTORE_COMPLETED; |
Patrick Dubroy | 6569f2c | 2010-07-12 14:25:18 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 125 | /** Used for testing **/ |
| 126 | public LauncherAppWidgetInfo() { |
| 127 | itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET; |
| 128 | } |
| 129 | |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 130 | public boolean isCustomWidget() { |
Sunny Goyal | 952e63d | 2017-08-16 04:59:08 -0700 | [diff] [blame] | 131 | return appWidgetId <= CUSTOM_WIDGET_ID; |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 132 | } |
| 133 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | @Override |
Sunny Goyal | 658058b | 2017-01-21 01:33:02 -0800 | [diff] [blame] | 135 | public void onAddToDatabase(ContentWriter writer) { |
Sunny Goyal | 32f3dda | 2016-11-11 11:45:00 -0800 | [diff] [blame] | 136 | super.onAddToDatabase(writer); |
| 137 | writer.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId) |
| 138 | .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString()) |
| 139 | .put(LauncherSettings.Favorites.RESTORED, restoreStatus) |
| 140 | .put(LauncherSettings.Favorites.INTENT, bindOptions); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Winson Chung | 211bac3 | 2012-05-15 13:43:57 -0700 | [diff] [blame] | 143 | /** |
| 144 | * When we bind the widget, we should notify the widget that the size has changed if we have not |
| 145 | * done so already (only really for default workspace widgets). |
| 146 | */ |
Sunny Goyal | 87af0fd | 2016-05-16 14:56:02 -0700 | [diff] [blame] | 147 | void onBindAppWidget(Launcher launcher, AppWidgetHostView hostView) { |
Adam Cohen | aaa5c21 | 2012-10-05 18:14:31 -0700 | [diff] [blame] | 148 | if (!mHasNotifiedInitialWidgetSizeChanged) { |
Sunny Goyal | 25c2e3e | 2015-10-28 23:28:21 -0700 | [diff] [blame] | 149 | AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY); |
| 150 | mHasNotifiedInitialWidgetSizeChanged = true; |
Winson Chung | 211bac3 | 2012-05-15 13:43:57 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 154 | @Override |
Sunny Goyal | 1edab71 | 2016-09-01 10:55:20 -0700 | [diff] [blame] | 155 | protected String dumpProperties() { |
| 156 | return super.dumpProperties() + " appWidgetId=" + appWidgetId; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 157 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 158 | |
Sunny Goyal | d478c83 | 2016-04-01 12:04:16 -0700 | [diff] [blame] | 159 | public final boolean isWidgetIdAllocated() { |
| 160 | return (restoreStatus & FLAG_ID_NOT_VALID) == 0 || |
| 161 | (restoreStatus & FLAG_ID_ALLOCATED) == FLAG_ID_ALLOCATED; |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | public final boolean hasRestoreFlag(int flag) { |
| 165 | return (restoreStatus & flag) == flag; |
Sunny Goyal | 651077b | 2014-06-30 14:15:31 -0700 | [diff] [blame] | 166 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 167 | } |