blob: 66d895726b73f3e792c627dd99b5e9c04946cee9 [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;
Sunny Goyal86df1382016-08-10 15:03:22 -070023import android.content.Intent;
Kenny Guyed131872014-04-30 03:02:21 +010024
25import com.android.launcher3.compat.UserHandleCompat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026
27/**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070028 * Represents a widget (either instantiated or about to be) in the Launcher.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029 */
Anjali Koppal7b168a12014-03-04 17:16:11 -080030public class LauncherAppWidgetInfo extends ItemInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031
Sunny Goyal651077b2014-06-30 14:15:31 -070032 public static final int RESTORE_COMPLETED = 0;
33
34 /**
35 * This is set during the package backup creation.
36 */
Sunny Goyalff572272014-07-23 13:58:07 -070037 public static final int FLAG_ID_NOT_VALID = 1;
Sunny Goyal651077b2014-06-30 14:15:31 -070038
39 /**
Sunny Goyalff572272014-07-23 13:58:07 -070040 * Indicates that the provider is not available yet.
Sunny Goyal651077b2014-06-30 14:15:31 -070041 */
Sunny Goyalff572272014-07-23 13:58:07 -070042 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 Goyal651077b2014-06-30 14:15:31 -070048
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 /**
Sunny Goyal94485362014-09-18 16:13:58 -070050 * Indicates that the widget restore has started.
51 */
52 public static final int FLAG_RESTORE_STARTED = 8;
53
54 /**
Sunny Goyald478c832016-04-01 12:04:16 -070055 * 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 Goyal86df1382016-08-10 15:03:22 -070061 * 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 Dubroy6569f2c2010-07-12 14:25:18 -070067 * Indicates that the widget hasn't been instantiated yet.
68 */
69 static final int NO_ID = -1;
70
71 /**
Adam Cohen59400422014-03-05 18:07:04 -080072 * Indicates that this is a locally defined widget and hence has no system allocated id.
73 */
74 static final int CUSTOM_WIDGET_ID = -100;
75
76 /**
Romain Guy629de3e2010-01-13 12:20:59 -080077 * Identifier for this widget when talking with
78 * {@link android.appwidget.AppWidgetManager} for updates.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070080 int appWidgetId = NO_ID;
81
Sunny Goyal6d02c7a2016-05-19 12:15:39 -070082 public ComponentName providerName;
Adam Cohended9f8d2010-11-03 13:25:16 -070083
Sunny Goyal651077b2014-06-30 14:15:31 -070084 /**
85 * Indicates the restore status of the widget.
86 */
87 int restoreStatus;
88
Sunny Goyal0fc1be12014-08-11 17:05:23 -070089 /**
90 * Indicates the installation progress of the widget provider
91 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -070092 int installProgress = -1;
Sunny Goyal0fc1be12014-08-11 17:05:23 -070093
Sunny Goyal86df1382016-08-10 15:03:22 -070094 /**
95 * Optional extras sent during widget bind. See {@link #FLAG_DIRECT_CONFIG}.
96 */
97 public Intent bindOptions;
98
Winson Chung211bac32012-05-15 13:43:57 -070099 private boolean mHasNotifiedInitialWidgetSizeChanged;
100
Winson Chung11a49372012-04-27 15:12:38 -0700101 LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
Adam Cohen59400422014-03-05 18:07:04 -0800102 if (appWidgetId == CUSTOM_WIDGET_ID) {
103 itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
104 } else {
105 itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
106 }
107
Winson Chung11a49372012-04-27 15:12:38 -0700108 this.appWidgetId = appWidgetId;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700109 this.providerName = providerName;
110
111 // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
112 // to indicate that they should be calculated based on the layout and minWidth/minHeight
113 spanX = -1;
114 spanY = -1;
Kenny Guyed131872014-04-30 03:02:21 +0100115 // We only support app widgets on current user.
116 user = UserHandleCompat.myUserHandle();
Sunny Goyal651077b2014-06-30 14:15:31 -0700117 restoreStatus = RESTORE_COMPLETED;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700118 }
119
Adam Cohen59400422014-03-05 18:07:04 -0800120 public boolean isCustomWidget() {
121 return appWidgetId == CUSTOM_WIDGET_ID;
122 }
123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 @Override
Kenny Guyed131872014-04-30 03:02:21 +0100125 void onAddToDatabase(Context context, ContentValues values) {
126 super.onAddToDatabase(context, values);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -0400128 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString());
Sunny Goyalff572272014-07-23 13:58:07 -0700129 values.put(LauncherSettings.Favorites.RESTORED, restoreStatus);
Sunny Goyal86df1382016-08-10 15:03:22 -0700130 values.put(LauncherSettings.Favorites.INTENT,
131 bindOptions == null ? null : bindOptions.toUri(0));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133
Winson Chung211bac32012-05-15 13:43:57 -0700134 /**
135 * When we bind the widget, we should notify the widget that the size has changed if we have not
136 * done so already (only really for default workspace widgets).
137 */
Sunny Goyal87af0fd2016-05-16 14:56:02 -0700138 void onBindAppWidget(Launcher launcher, AppWidgetHostView hostView) {
Adam Cohenaaa5c212012-10-05 18:14:31 -0700139 if (!mHasNotifiedInitialWidgetSizeChanged) {
Sunny Goyal25c2e3e2015-10-28 23:28:21 -0700140 AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
141 mHasNotifiedInitialWidgetSizeChanged = true;
Winson Chung211bac32012-05-15 13:43:57 -0700142 }
143 }
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 @Override
Sunny Goyal1edab712016-09-01 10:55:20 -0700146 protected String dumpProperties() {
147 return super.dumpProperties() + " appWidgetId=" + appWidgetId;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149
Sunny Goyald478c832016-04-01 12:04:16 -0700150 public final boolean isWidgetIdAllocated() {
151 return (restoreStatus & FLAG_ID_NOT_VALID) == 0 ||
152 (restoreStatus & FLAG_ID_ALLOCATED) == FLAG_ID_ALLOCATED;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 }
154
155 public final boolean hasRestoreFlag(int flag) {
156 return (restoreStatus & flag) == flag;
Sunny Goyal651077b2014-06-30 14:15:31 -0700157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158}