blob: aad18b578e1a61b65b83690dde012a19ebbc8c34 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.ContentValues;
Kenny Guyed131872014-04-30 03:02:21 +010022import android.content.Context;
23
24import com.android.launcher3.compat.UserHandleCompat;
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 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070054 * Indicates that the widget hasn't been instantiated yet.
55 */
56 static final int NO_ID = -1;
57
58 /**
Adam Cohen59400422014-03-05 18:07:04 -080059 * Indicates that this is a locally defined widget and hence has no system allocated id.
60 */
61 static final int CUSTOM_WIDGET_ID = -100;
62
63 /**
Romain Guy629de3e2010-01-13 12:20:59 -080064 * Identifier for this widget when talking with
65 * {@link android.appwidget.AppWidgetManager} for updates.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070067 int appWidgetId = NO_ID;
68
Michael Jurkaaf442092010-06-10 17:01:57 -070069 ComponentName providerName;
Adam Cohended9f8d2010-11-03 13:25:16 -070070
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070071 // TODO: Are these necessary here?
72 int minWidth = -1;
73 int minHeight = -1;
74
Sunny Goyal651077b2014-06-30 14:15:31 -070075 /**
76 * Indicates the restore status of the widget.
77 */
78 int restoreStatus;
79
Sunny Goyal0fc1be12014-08-11 17:05:23 -070080 /**
81 * Indicates the installation progress of the widget provider
82 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -070083 int installProgress = -1;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070084
Winson Chung211bac32012-05-15 13:43:57 -070085 private boolean mHasNotifiedInitialWidgetSizeChanged;
86
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -070088 * View that holds this widget after it's been created. This view isn't created
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 * until Launcher knows it's needed.
90 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -070091 AppWidgetHostView hostView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Winson Chung11a49372012-04-27 15:12:38 -070093 LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
Adam Cohen59400422014-03-05 18:07:04 -080094 if (appWidgetId == CUSTOM_WIDGET_ID) {
95 itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
96 } else {
97 itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
98 }
99
Winson Chung11a49372012-04-27 15:12:38 -0700100 this.appWidgetId = appWidgetId;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700101 this.providerName = providerName;
102
103 // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
104 // to indicate that they should be calculated based on the layout and minWidth/minHeight
105 spanX = -1;
106 spanY = -1;
Kenny Guyed131872014-04-30 03:02:21 +0100107 // We only support app widgets on current user.
108 user = UserHandleCompat.myUserHandle();
Sunny Goyal651077b2014-06-30 14:15:31 -0700109 restoreStatus = RESTORE_COMPLETED;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700110 }
111
Adam Cohen59400422014-03-05 18:07:04 -0800112 public boolean isCustomWidget() {
113 return appWidgetId == CUSTOM_WIDGET_ID;
114 }
115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 @Override
Kenny Guyed131872014-04-30 03:02:21 +0100117 void onAddToDatabase(Context context, ContentValues values) {
118 super.onAddToDatabase(context, values);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -0400120 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString());
Sunny Goyalff572272014-07-23 13:58:07 -0700121 values.put(LauncherSettings.Favorites.RESTORED, restoreStatus);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 }
123
Winson Chung211bac32012-05-15 13:43:57 -0700124 /**
125 * When we bind the widget, we should notify the widget that the size has changed if we have not
126 * done so already (only really for default workspace widgets).
127 */
128 void onBindAppWidget(Launcher launcher) {
Adam Cohenaaa5c212012-10-05 18:14:31 -0700129 if (!mHasNotifiedInitialWidgetSizeChanged) {
Winson Chung211bac32012-05-15 13:43:57 -0700130 notifyWidgetSizeChanged(launcher);
131 }
132 }
133
134 /**
135 * Trigger an update callback to the widget to notify it that its size has changed.
136 */
137 void notifyWidgetSizeChanged(Launcher launcher) {
138 AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
139 mHasNotifiedInitialWidgetSizeChanged = true;
140 }
141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 @Override
143 public String toString() {
Daniel Sandler8802e962010-05-26 16:28:16 -0400144 return "AppWidget(id=" + Integer.toString(appWidgetId) + ")";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400146
Joe Onorato9c1289c2009-08-17 11:03:03 -0400147 @Override
148 void unbind() {
149 super.unbind();
150 hostView = null;
151 }
Sunny Goyal651077b2014-06-30 14:15:31 -0700152
153 public final boolean isWidgetIdValid() {
Sunny Goyalff572272014-07-23 13:58:07 -0700154 return (restoreStatus & FLAG_ID_NOT_VALID) == 0;
155 }
156
157 public final boolean hasRestoreFlag(int flag) {
158 return (restoreStatus & flag) == flag;
Sunny Goyal651077b2014-06-30 14:15:31 -0700159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160}