blob: 2218767114821f6ef0dc6844d2c59eb1bd348732 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
The Android Open Source Project7376fae2009-03-11 12:11:58 -070019import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070020import android.content.ComponentName;
Sunny Goyal86df1382016-08-10 15:03:22 -070021import android.content.Intent;
Kenny Guyed131872014-04-30 03:02:21 +010022
23import com.android.launcher3.compat.UserHandleCompat;
Sunny Goyal32f3dda2016-11-11 11:45:00 -080024import com.android.launcher3.util.ContentWriter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025
26/**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070027 * Represents a widget (either instantiated or about to be) in the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028 */
Anjali Koppal7b168a12014-03-04 17:16:11 -080029public class LauncherAppWidgetInfo extends ItemInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030
Sunny Goyal651077b2014-06-30 14:15:31 -070031 public static final int RESTORE_COMPLETED = 0;
32
33 /**
34 * This is set during the package backup creation.
35 */
Sunny Goyalff572272014-07-23 13:58:07 -070036 public static final int FLAG_ID_NOT_VALID = 1;
Sunny Goyal651077b2014-06-30 14:15:31 -070037
38 /**
Sunny Goyalff572272014-07-23 13:58:07 -070039 * Indicates that the provider is not available yet.
Sunny Goyal651077b2014-06-30 14:15:31 -070040 */
Sunny Goyalff572272014-07-23 13:58:07 -070041 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;
Sunny Goyal651077b2014-06-30 14:15:31 -070047
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 /**
Sunny Goyal94485362014-09-18 16:13:58 -070049 * Indicates that the widget restore has started.
50 */
51 public static final int FLAG_RESTORE_STARTED = 8;
52
53 /**
Sunny Goyald478c832016-04-01 12:04:16 -070054 * 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 /**
Sunny Goyal86df1382016-08-10 15:03:22 -070060 * Indicates that the widget does not need to show config activity, even if it has a
61 * configuration screen. It can also optionally have some extras which are sent during bind.
62 */
63 public static final int FLAG_DIRECT_CONFIG = 32;
64
65 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070066 * Indicates that the widget hasn't been instantiated yet.
67 */
68 static final int NO_ID = -1;
69
70 /**
Adam Cohen59400422014-03-05 18:07:04 -080071 * Indicates that this is a locally defined widget and hence has no system allocated id.
72 */
73 static final int CUSTOM_WIDGET_ID = -100;
74
75 /**
Romain Guy629de3e2010-01-13 12:20:59 -080076 * Identifier for this widget when talking with
77 * {@link android.appwidget.AppWidgetManager} for updates.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070079 int appWidgetId = NO_ID;
80
Sunny Goyal6d02c7a2016-05-19 12:15:39 -070081 public ComponentName providerName;
Adam Cohended9f8d2010-11-03 13:25:16 -070082
Sunny Goyal651077b2014-06-30 14:15:31 -070083 /**
84 * Indicates the restore status of the widget.
85 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070086 public int restoreStatus;
Sunny Goyal651077b2014-06-30 14:15:31 -070087
Sunny Goyal0fc1be12014-08-11 17:05:23 -070088 /**
89 * Indicates the installation progress of the widget provider
90 */
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070091 public int installProgress = -1;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070092
Sunny Goyal86df1382016-08-10 15:03:22 -070093 /**
94 * Optional extras sent during widget bind. See {@link #FLAG_DIRECT_CONFIG}.
95 */
96 public Intent bindOptions;
97
Winson Chung211bac32012-05-15 13:43:57 -070098 private boolean mHasNotifiedInitialWidgetSizeChanged;
99
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700100 public LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
Adam Cohen59400422014-03-05 18:07:04 -0800101 if (appWidgetId == CUSTOM_WIDGET_ID) {
102 itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
103 } else {
104 itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
105 }
106
Winson Chung11a49372012-04-27 15:12:38 -0700107 this.appWidgetId = appWidgetId;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700108 this.providerName = providerName;
109
110 // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
111 // to indicate that they should be calculated based on the layout and minWidth/minHeight
112 spanX = -1;
113 spanY = -1;
Kenny Guyed131872014-04-30 03:02:21 +0100114 // We only support app widgets on current user.
115 user = UserHandleCompat.myUserHandle();
Sunny Goyal651077b2014-06-30 14:15:31 -0700116 restoreStatus = RESTORE_COMPLETED;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700117 }
118
Sunny Goyalf0ba8b72016-09-09 15:47:55 -0700119 /** Used for testing **/
120 public LauncherAppWidgetInfo() {
121 itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
122 }
123
Adam Cohen59400422014-03-05 18:07:04 -0800124 public boolean isCustomWidget() {
125 return appWidgetId == CUSTOM_WIDGET_ID;
126 }
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 @Override
Sunny Goyal32f3dda2016-11-11 11:45:00 -0800129 void onAddToDatabase(ContentWriter writer) {
130 super.onAddToDatabase(writer);
131 writer.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId)
132 .put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString())
133 .put(LauncherSettings.Favorites.RESTORED, restoreStatus)
134 .put(LauncherSettings.Favorites.INTENT, bindOptions);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 }
136
Winson Chung211bac32012-05-15 13:43:57 -0700137 /**
138 * When we bind the widget, we should notify the widget that the size has changed if we have not
139 * done so already (only really for default workspace widgets).
140 */
Sunny Goyal87af0fd2016-05-16 14:56:02 -0700141 void onBindAppWidget(Launcher launcher, AppWidgetHostView hostView) {
Adam Cohenaaa5c212012-10-05 18:14:31 -0700142 if (!mHasNotifiedInitialWidgetSizeChanged) {
Sunny Goyal25c2e3e2015-10-28 23:28:21 -0700143 AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
144 mHasNotifiedInitialWidgetSizeChanged = true;
Winson Chung211bac32012-05-15 13:43:57 -0700145 }
146 }
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 @Override
Sunny Goyal1edab712016-09-01 10:55:20 -0700149 protected String dumpProperties() {
150 return super.dumpProperties() + " appWidgetId=" + appWidgetId;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152
Sunny Goyald478c832016-04-01 12:04:16 -0700153 public final boolean isWidgetIdAllocated() {
154 return (restoreStatus & FLAG_ID_NOT_VALID) == 0 ||
155 (restoreStatus & FLAG_ID_ALLOCATED) == FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 }
157
158 public final boolean hasRestoreFlag(int flag) {
159 return (restoreStatus & flag) == flag;
Sunny Goyal651077b2014-06-30 14:15:31 -0700160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161}