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 */
16package com.android.launcher3.util;
17
18import android.content.ContentValues;
19import android.content.Intent;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import com.android.launcher3.ItemInfo;
24import com.android.launcher3.widget.WidgetAddFlowHandler;
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 */
30public 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        user = parcel.readParcelable(null);
57
58        mArg1 = parcel.readInt();
59        mObjectType = parcel.readInt();
60        mObject = parcel.readParcelable(null);
61    }
62
63    @Override
64    public int describeContents() {
65        return 0;
66    }
67
68    @Override
69    public void writeToParcel(Parcel dest, int flags) {
70        ContentValues itemValues = new ContentValues();
71        writeToValues(new ContentWriter(itemValues, null));
72        itemValues.writeToParcel(dest, flags);
73        dest.writeParcelable(user, flags);
74
75        dest.writeInt(mArg1);
76        dest.writeInt(mObjectType);
77        dest.writeParcelable(mObject, flags);
78    }
79
80    public WidgetAddFlowHandler getWidgetHandler() {
81        return mObjectType == TYPE_APP_WIDGET ? (WidgetAddFlowHandler) mObject : null;
82    }
83
84    public int getWidgetId() {
85        return mObjectType == TYPE_APP_WIDGET ? mArg1 : 0;
86    }
87
88    public Intent getPendingIntent() {
89        return mObjectType == TYPE_INTENT ? (Intent) mObject : null;
90    }
91
92    public int getRequestCode() {
93        return mObjectType == TYPE_INTENT ? mArg1 : 0;
94    }
95
96    public static PendingRequestArgs forWidgetInfo(
97            int appWidgetId, WidgetAddFlowHandler widgetHandler, ItemInfo info) {
98        PendingRequestArgs args =
99                new PendingRequestArgs(appWidgetId, TYPE_APP_WIDGET, widgetHandler);
100        args.copyFrom(info);
101        return args;
102    }
103
104    public static PendingRequestArgs forIntent(int requestCode, Intent intent, ItemInfo info) {
105        PendingRequestArgs args = new PendingRequestArgs(requestCode, TYPE_INTENT, intent);
106        args.copyFrom(info);
107        return args;
108    }
109
110    public static final Parcelable.Creator<PendingRequestArgs> CREATOR =
111            new Parcelable.Creator<PendingRequestArgs>() {
112                public PendingRequestArgs createFromParcel(Parcel source) {
113                    return new PendingRequestArgs(source);
114                }
115
116                public PendingRequestArgs[] newArray(int size) {
117                    return new PendingRequestArgs[size];
118                }
119            };
120}
121