Home | History | Annotate | Download | only in util
      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 package com.android.launcher3.util;
     17 
     18 import android.content.ContentValues;
     19 import android.content.Intent;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 
     23 import com.android.launcher3.ItemInfo;
     24 import com.android.launcher3.LauncherAppWidgetProviderInfo;
     25 
     26 /**
     27  * Utility class to store information regarding a pending request made by launcher. This information
     28  * can be saved across launcher instances.
     29  */
     30 public class PendingRequestArgs extends ItemInfo implements Parcelable {
     31 
     32     private static final int TYPE_NONE = 0;
     33     private static final int TYPE_INTENT = 1;
     34     private static final int TYPE_APP_WIDGET = 2;
     35 
     36     private final int mArg1;
     37     private final int mObjectType;
     38     private final Parcelable mObject;
     39 
     40     public PendingRequestArgs(ItemInfo info) {
     41         mArg1 = 0;
     42         mObjectType = TYPE_NONE;
     43         mObject = null;
     44 
     45         copyFrom(info);
     46     }
     47 
     48     private PendingRequestArgs(int arg1, int objectType, Parcelable object) {
     49         mArg1 = arg1;
     50         mObjectType = objectType;
     51         mObject = object;
     52     }
     53 
     54     public PendingRequestArgs(Parcel parcel) {
     55         readFromValues(ContentValues.CREATOR.createFromParcel(parcel));
     56 
     57         mArg1 = parcel.readInt();
     58         mObjectType = parcel.readInt();
     59         if (parcel.readInt() != 0) {
     60             mObject = mObjectType == TYPE_INTENT
     61                     ? Intent.CREATOR.createFromParcel(parcel)
     62                     : new LauncherAppWidgetProviderInfo(parcel);
     63         } else {
     64             mObject = null;
     65         }
     66     }
     67 
     68     @Override
     69     public int describeContents() {
     70         return 0;
     71     }
     72 
     73     @Override
     74     public void writeToParcel(Parcel dest, int flags) {
     75         ContentValues itemValues = new ContentValues();
     76         writeToValues(itemValues);
     77         itemValues.writeToParcel(dest, flags);
     78 
     79         dest.writeInt(mArg1);
     80         dest.writeInt(mObjectType);
     81         if (mObject != null) {
     82             dest.writeInt(1);
     83             mObject.writeToParcel(dest, flags);
     84         } else {
     85             dest.writeInt(0);
     86         }
     87     }
     88 
     89     public LauncherAppWidgetProviderInfo getWidgetProvider() {
     90         return mObjectType == TYPE_APP_WIDGET ? (LauncherAppWidgetProviderInfo) mObject : null;
     91     }
     92 
     93     public int getWidgetId() {
     94         return mObjectType == TYPE_APP_WIDGET ? mArg1 : 0;
     95     }
     96 
     97     public Intent getPendingIntent() {
     98         return mObjectType == TYPE_INTENT ? (Intent) mObject : null;
     99     }
    100 
    101     public int getRequestCode() {
    102         return mObjectType == TYPE_INTENT ? mArg1 : 0;
    103     }
    104 
    105     public static PendingRequestArgs forWidgetInfo(
    106             int appWidgetId, LauncherAppWidgetProviderInfo widgetInfo, ItemInfo info) {
    107         PendingRequestArgs args = new PendingRequestArgs(appWidgetId, TYPE_APP_WIDGET, widgetInfo);
    108         args.copyFrom(info);
    109         return args;
    110     }
    111 
    112     public static PendingRequestArgs forIntent(int requestCode, Intent intent, ItemInfo info) {
    113         PendingRequestArgs args = new PendingRequestArgs(requestCode, TYPE_INTENT, intent);
    114         args.copyFrom(info);
    115         return args;
    116     }
    117 
    118     public static final Parcelable.Creator<PendingRequestArgs> CREATOR =
    119             new Parcelable.Creator<PendingRequestArgs>() {
    120                 public PendingRequestArgs createFromParcel(Parcel source) {
    121                     return new PendingRequestArgs(source);
    122                 }
    123 
    124                 public PendingRequestArgs[] newArray(int size) {
    125                     return new PendingRequestArgs[size];
    126                 }
    127             };
    128 }
    129