Home | History | Annotate | Download | only in appwidget
      1 /*
      2  * Copyright (C) 2016 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 android.appwidget;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 import android.widget.RemoteViews;
     22 
     23 /**
     24  * @hide
     25  */
     26 public class PendingHostUpdate implements Parcelable {
     27 
     28     static final int TYPE_VIEWS_UPDATE = 0;
     29     static final int TYPE_PROVIDER_CHANGED = 1;
     30     static final int TYPE_VIEW_DATA_CHANGED = 2;
     31 
     32     final int appWidgetId;
     33     final int type;
     34     RemoteViews views;
     35     AppWidgetProviderInfo widgetInfo;
     36     int viewId;
     37 
     38     public static PendingHostUpdate updateAppWidget(int appWidgetId, RemoteViews views) {
     39         PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_VIEWS_UPDATE);
     40         update.views = views;
     41         return update;
     42     }
     43 
     44     public static PendingHostUpdate providerChanged(int appWidgetId, AppWidgetProviderInfo info) {
     45         PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_PROVIDER_CHANGED);
     46         update.widgetInfo = info;
     47         return update;
     48     }
     49 
     50     public static PendingHostUpdate viewDataChanged(int appWidgetId, int viewId) {
     51         PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_VIEW_DATA_CHANGED);
     52         update.viewId = viewId;
     53         return update;
     54     }
     55 
     56     private PendingHostUpdate(int appWidgetId, int type) {
     57         this.appWidgetId = appWidgetId;
     58         this.type = type;
     59     }
     60 
     61     private PendingHostUpdate(Parcel in) {
     62         appWidgetId = in.readInt();
     63         type = in.readInt();
     64 
     65         switch (type) {
     66             case TYPE_VIEWS_UPDATE:
     67                 if (0 != in.readInt()) {
     68                     views = new RemoteViews(in);
     69                 }
     70                 break;
     71             case TYPE_PROVIDER_CHANGED:
     72                 if (0 != in.readInt()) {
     73                     widgetInfo = new AppWidgetProviderInfo(in);
     74                 }
     75                 break;
     76             case TYPE_VIEW_DATA_CHANGED:
     77                 viewId = in.readInt();
     78         }
     79     }
     80 
     81     @Override
     82     public int describeContents() {
     83         return 0;
     84     }
     85 
     86     @Override
     87     public void writeToParcel(Parcel dest, int flags) {
     88         dest.writeInt(appWidgetId);
     89         dest.writeInt(type);
     90         switch (type) {
     91             case TYPE_VIEWS_UPDATE:
     92                 writeNullParcelable(views, dest, flags);
     93                 break;
     94             case TYPE_PROVIDER_CHANGED:
     95                 writeNullParcelable(widgetInfo, dest, flags);
     96                 break;
     97             case TYPE_VIEW_DATA_CHANGED:
     98                 dest.writeInt(viewId);
     99                 break;
    100         }
    101     }
    102 
    103     private void writeNullParcelable(Parcelable p, Parcel dest, int flags) {
    104         if (p != null) {
    105             dest.writeInt(1);
    106             p.writeToParcel(dest, flags);
    107         } else {
    108             dest.writeInt(0);
    109         }
    110     }
    111 
    112     /**
    113      * Parcelable.Creator that instantiates PendingHostUpdate objects
    114      */
    115     public static final Parcelable.Creator<PendingHostUpdate> CREATOR
    116             = new Parcelable.Creator<PendingHostUpdate>() {
    117         public PendingHostUpdate createFromParcel(Parcel parcel) {
    118             return new PendingHostUpdate(parcel);
    119         }
    120 
    121         public PendingHostUpdate[] newArray(int size) {
    122             return new PendingHostUpdate[size];
    123         }
    124     };
    125 }
    126