• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.launcher3;
18 
19 import android.appwidget.AppWidgetHostView;
20 import android.content.ComponentName;
21 import android.content.ContentValues;
22 
23 /**
24  * Represents a widget (either instantiated or about to be) in the Launcher.
25  */
26 class LauncherAppWidgetInfo extends ItemInfo {
27 
28     /**
29      * Indicates that the widget hasn't been instantiated yet.
30      */
31     static final int NO_ID = -1;
32 
33     /**
34      * Identifier for this widget when talking with
35      * {@link android.appwidget.AppWidgetManager} for updates.
36      */
37     int appWidgetId = NO_ID;
38 
39     ComponentName providerName;
40 
41     // TODO: Are these necessary here?
42     int minWidth = -1;
43     int minHeight = -1;
44 
45     private boolean mHasNotifiedInitialWidgetSizeChanged;
46 
47     /**
48      * View that holds this widget after it's been created.  This view isn't created
49      * until Launcher knows it's needed.
50      */
51     AppWidgetHostView hostView = null;
52 
LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName)53     LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
54         itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
55         this.appWidgetId = appWidgetId;
56         this.providerName = providerName;
57 
58         // Since the widget isn't instantiated yet, we don't know these values. Set them to -1
59         // to indicate that they should be calculated based on the layout and minWidth/minHeight
60         spanX = -1;
61         spanY = -1;
62     }
63 
64     @Override
onAddToDatabase(ContentValues values)65     void onAddToDatabase(ContentValues values) {
66         super.onAddToDatabase(values);
67         values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
68         values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString());
69     }
70 
71     /**
72      * When we bind the widget, we should notify the widget that the size has changed if we have not
73      * done so already (only really for default workspace widgets).
74      */
onBindAppWidget(Launcher launcher)75     void onBindAppWidget(Launcher launcher) {
76         if (!mHasNotifiedInitialWidgetSizeChanged) {
77             notifyWidgetSizeChanged(launcher);
78         }
79     }
80 
81     /**
82      * Trigger an update callback to the widget to notify it that its size has changed.
83      */
notifyWidgetSizeChanged(Launcher launcher)84     void notifyWidgetSizeChanged(Launcher launcher) {
85         AppWidgetResizeFrame.updateWidgetSizeRanges(hostView, launcher, spanX, spanY);
86         mHasNotifiedInitialWidgetSizeChanged = true;
87     }
88 
89     @Override
toString()90     public String toString() {
91         return "AppWidget(id=" + Integer.toString(appWidgetId) + ")";
92     }
93 
94     @Override
unbind()95     void unbind() {
96         super.unbind();
97         hostView = null;
98     }
99 }
100