blob: b3ac12b3728764e2ee068aa665c60d29ea82d189 [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 */
36 public static final int RESTORE_REMAP_PENDING = 1;
37
38 /**
39 * Widget provider is not yet installed.
40 */
41 public static final int RESTORE_PROVIDER_PENDING = 2;
42
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043 /**
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070044 * Indicates that the widget hasn't been instantiated yet.
45 */
46 static final int NO_ID = -1;
47
48 /**
Romain Guy629de3e2010-01-13 12:20:59 -080049 * Identifier for this widget when talking with
50 * {@link android.appwidget.AppWidgetManager} for updates.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 */
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070052 int appWidgetId = NO_ID;
53
Michael Jurkaaf442092010-06-10 17:01:57 -070054 ComponentName providerName;
Adam Cohended9f8d2010-11-03 13:25:16 -070055
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070056 // TODO: Are these necessary here?
57 int minWidth = -1;
58 int minHeight = -1;
59
Sunny Goyal651077b2014-06-30 14:15:31 -070060 /**
61 * Indicates the restore status of the widget.
62 */
63 int restoreStatus;
64
Winson Chung211bac32012-05-15 13:43:57 -070065 private boolean mHasNotifiedInitialWidgetSizeChanged;
66
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -070068 * 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 -080069 * until Launcher knows it's needed.
70 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -070071 AppWidgetHostView hostView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Winson Chung11a49372012-04-27 15:12:38 -070073 LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070074 itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
Winson Chung11a49372012-04-27 15:12:38 -070075 this.appWidgetId = appWidgetId;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070076 this.providerName = providerName;
77
78 // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
79 // to indicate that they should be calculated based on the layout and minWidth/minHeight
80 spanX = -1;
81 spanY = -1;
Kenny Guyed131872014-04-30 03:02:21 +010082 // We only support app widgets on current user.
83 user = UserHandleCompat.myUserHandle();
Sunny Goyal651077b2014-06-30 14:15:31 -070084 restoreStatus = RESTORE_COMPLETED;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085 }
86
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 @Override
Kenny Guyed131872014-04-30 03:02:21 +010088 void onAddToDatabase(Context context, ContentValues values) {
89 super.onAddToDatabase(context, values);
The Android Open Source Project7376fae2009-03-11 12:11:58 -070090 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -040091 values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 }
93
Winson Chung211bac32012-05-15 13:43:57 -070094 /**
95 * When we bind the widget, we should notify the widget that the size has changed if we have not
96 * done so already (only really for default workspace widgets).
97 */
98 void onBindAppWidget(Launcher launcher) {
Adam Cohenaaa5c212012-10-05 18:14:31 -070099 if (!mHasNotifiedInitialWidgetSizeChanged) {
Winson Chung211bac32012-05-15 13:43:57 -0700100 notifyWidgetSizeChanged(launcher);
101 }
102 }
103
104 /**
105 * Trigger an update callback to the widget to notify it that its size has changed.
106 */
107 void notifyWidgetSizeChanged(Launcher launcher) {
108 AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
109 mHasNotifiedInitialWidgetSizeChanged = true;
110 }
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 @Override
113 public String toString() {
Daniel Sandler8802e962010-05-26 16:28:16 -0400114 return "AppWidget(id=" + Integer.toString(appWidgetId) + ")";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 @Override
118 void unbind() {
119 super.unbind();
120 hostView = null;
121 }
Sunny Goyal651077b2014-06-30 14:15:31 -0700122
123 public final boolean isWidgetIdValid() {
124 return restoreStatus != RESTORE_REMAP_PENDING;
125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126}