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 static final int TYPE_APP_WIDGET_REMOVED = 3; 32 33 final int appWidgetId; 34 final int type; 35 RemoteViews views; 36 AppWidgetProviderInfo widgetInfo; 37 int viewId; 38 updateAppWidget(int appWidgetId, RemoteViews views)39 public static PendingHostUpdate updateAppWidget(int appWidgetId, RemoteViews views) { 40 PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_VIEWS_UPDATE); 41 update.views = views; 42 return update; 43 } 44 providerChanged(int appWidgetId, AppWidgetProviderInfo info)45 public static PendingHostUpdate providerChanged(int appWidgetId, AppWidgetProviderInfo info) { 46 PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_PROVIDER_CHANGED); 47 update.widgetInfo = info; 48 return update; 49 } 50 viewDataChanged(int appWidgetId, int viewId)51 public static PendingHostUpdate viewDataChanged(int appWidgetId, int viewId) { 52 PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_VIEW_DATA_CHANGED); 53 update.viewId = viewId; 54 return update; 55 } 56 57 /** 58 * IAppWidgetHost appWidgetRemoved implimentaion 59 */ appWidgetRemoved(int appWidgetId)60 public static PendingHostUpdate appWidgetRemoved(int appWidgetId) { 61 return new PendingHostUpdate(appWidgetId, TYPE_APP_WIDGET_REMOVED); 62 } 63 PendingHostUpdate(int appWidgetId, int type)64 private PendingHostUpdate(int appWidgetId, int type) { 65 this.appWidgetId = appWidgetId; 66 this.type = type; 67 } 68 PendingHostUpdate(Parcel in)69 private PendingHostUpdate(Parcel in) { 70 appWidgetId = in.readInt(); 71 type = in.readInt(); 72 73 switch (type) { 74 case TYPE_VIEWS_UPDATE: 75 if (0 != in.readInt()) { 76 views = new RemoteViews(in); 77 } 78 break; 79 case TYPE_PROVIDER_CHANGED: 80 if (0 != in.readInt()) { 81 widgetInfo = new AppWidgetProviderInfo(in); 82 } 83 break; 84 case TYPE_VIEW_DATA_CHANGED: 85 viewId = in.readInt(); 86 } 87 } 88 89 @Override describeContents()90 public int describeContents() { 91 return 0; 92 } 93 94 @Override writeToParcel(Parcel dest, int flags)95 public void writeToParcel(Parcel dest, int flags) { 96 dest.writeInt(appWidgetId); 97 dest.writeInt(type); 98 switch (type) { 99 case TYPE_VIEWS_UPDATE: 100 writeNullParcelable(views, dest, flags); 101 break; 102 case TYPE_PROVIDER_CHANGED: 103 writeNullParcelable(widgetInfo, dest, flags); 104 break; 105 case TYPE_VIEW_DATA_CHANGED: 106 dest.writeInt(viewId); 107 break; 108 } 109 } 110 writeNullParcelable(Parcelable p, Parcel dest, int flags)111 private void writeNullParcelable(Parcelable p, Parcel dest, int flags) { 112 if (p != null) { 113 dest.writeInt(1); 114 p.writeToParcel(dest, flags); 115 } else { 116 dest.writeInt(0); 117 } 118 } 119 120 /** 121 * Parcelable.Creator that instantiates PendingHostUpdate objects 122 */ 123 public static final @android.annotation.NonNull Parcelable.Creator<PendingHostUpdate> CREATOR 124 = new Parcelable.Creator<PendingHostUpdate>() { 125 public PendingHostUpdate createFromParcel(Parcel parcel) { 126 return new PendingHostUpdate(parcel); 127 } 128 129 public PendingHostUpdate[] newArray(int size) { 130 return new PendingHostUpdate[size]; 131 } 132 }; 133 } 134