Home | History | Annotate | Download | only in launcher3
      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 import android.content.Context;
     23 
     24 import com.android.launcher3.compat.UserHandleCompat;
     25 
     26 /**
     27  * Represents a widget (either instantiated or about to be) in the Launcher.
     28  */
     29 public class LauncherAppWidgetInfo extends ItemInfo {
     30 
     31     public static final int RESTORE_COMPLETED = 0;
     32 
     33     /**
     34      * This is set during the package backup creation.
     35      */
     36     public static final int FLAG_ID_NOT_VALID = 1;
     37 
     38     /**
     39      * Indicates that the provider is not available yet.
     40      */
     41     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;
     47 
     48     /**
     49      * Indicates that the widget restore has started.
     50      */
     51     public static final int FLAG_RESTORE_STARTED = 8;
     52 
     53     /**
     54      * Indicates that the widget hasn't been instantiated yet.
     55      */
     56     static final int NO_ID = -1;
     57 
     58     /**
     59      * 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     /**
     64      * Identifier for this widget when talking with
     65      * {@link android.appwidget.AppWidgetManager} for updates.
     66      */
     67     int appWidgetId = NO_ID;
     68 
     69     ComponentName providerName;
     70 
     71     // TODO: Are these necessary here?
     72     int minWidth = -1;
     73     int minHeight = -1;
     74 
     75     /**
     76      * Indicates the restore status of the widget.
     77      */
     78     int restoreStatus;
     79 
     80     /**
     81      * Indicates the installation progress of the widget provider
     82      */
     83     int installProgress = -1;
     84 
     85     private boolean mHasNotifiedInitialWidgetSizeChanged;
     86 
     87     /**
     88      * View that holds this widget after it's been created.  This view isn't created
     89      * until Launcher knows it's needed.
     90      */
     91     AppWidgetHostView hostView = null;
     92 
     93     LauncherAppWidgetInfo(int appWidgetId, ComponentName providerName) {
     94         if (appWidgetId == CUSTOM_WIDGET_ID) {
     95             itemType = LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
     96         } else {
     97             itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET;
     98         }
     99 
    100         this.appWidgetId = appWidgetId;
    101         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;
    107         // We only support app widgets on current user.
    108         user = UserHandleCompat.myUserHandle();
    109         restoreStatus = RESTORE_COMPLETED;
    110     }
    111 
    112     public boolean isCustomWidget() {
    113         return appWidgetId == CUSTOM_WIDGET_ID;
    114     }
    115 
    116     @Override
    117     void onAddToDatabase(Context context, ContentValues values) {
    118         super.onAddToDatabase(context, values);
    119         values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
    120         values.put(LauncherSettings.Favorites.APPWIDGET_PROVIDER, providerName.flattenToString());
    121         values.put(LauncherSettings.Favorites.RESTORED, restoreStatus);
    122     }
    123 
    124     /**
    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) {
    129         if (!mHasNotifiedInitialWidgetSizeChanged) {
    130             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 
    142     @Override
    143     public String toString() {
    144         return "AppWidget(id=" + Integer.toString(appWidgetId) + ")";
    145     }
    146 
    147     @Override
    148     void unbind() {
    149         super.unbind();
    150         hostView = null;
    151     }
    152 
    153     public final boolean isWidgetIdValid() {
    154         return (restoreStatus & FLAG_ID_NOT_VALID) == 0;
    155     }
    156 
    157     public final boolean hasRestoreFlag(int flag) {
    158         return (restoreStatus & flag) == flag;
    159     }
    160 }
    161