• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.LauncherSettings.Favorites;
24 import com.android.launcher3.model.data.ItemInfo;
25 import com.android.launcher3.widget.PendingAddWidgetInfo;
26 import com.android.launcher3.widget.WidgetAddFlowHandler;
27 
28 /**
29  * Utility class to store information regarding a pending request made by launcher. This information
30  * can be saved across launcher instances.
31  */
32 public class PendingRequestArgs extends ItemInfo implements Parcelable {
33 
34     private static final int TYPE_NONE = 0;
35     private static final int TYPE_INTENT = 1;
36     private static final int TYPE_APP_WIDGET = 2;
37 
38     private final int mArg1;
39     private final int mArg2;
40     private final int mObjectType;
41     private final Parcelable mObject;
42 
PendingRequestArgs(ItemInfo info)43     public PendingRequestArgs(ItemInfo info) {
44         mArg1 = 0;
45         mArg2 = 0;
46         mObjectType = TYPE_NONE;
47         mObject = null;
48 
49         copyFrom(info);
50     }
51 
PendingRequestArgs(int arg1, int objectType, Parcelable object)52     private PendingRequestArgs(int arg1, int objectType, Parcelable object) {
53         this(arg1, 0, objectType, object);
54     }
55 
PendingRequestArgs(int arg1, int arg2, int objectType, Parcelable object)56     private PendingRequestArgs(int arg1, int arg2, int objectType, Parcelable object) {
57         mArg1 = arg1;
58         mArg2 = arg2;
59         mObjectType = objectType;
60         mObject = object;
61     }
62 
PendingRequestArgs(Parcel parcel)63     public PendingRequestArgs(Parcel parcel) {
64         readFromValues(ContentValues.CREATOR.createFromParcel(parcel));
65         user = parcel.readParcelable(null);
66 
67         mArg1 = parcel.readInt();
68         mArg2 = parcel.readInt();
69         mObjectType = parcel.readInt();
70         mObject = parcel.readParcelable(getClass().getClassLoader());
71     }
72 
73     @Override
describeContents()74     public int describeContents() {
75         return 0;
76     }
77 
78     @Override
writeToParcel(Parcel dest, int flags)79     public void writeToParcel(Parcel dest, int flags) {
80         ContentValues itemValues = new ContentValues();
81         writeToValues(new ContentWriter(itemValues, null));
82         itemValues.writeToParcel(dest, flags);
83         dest.writeParcelable(user, flags);
84 
85         dest.writeInt(mArg1);
86         dest.writeInt(mArg2);
87         dest.writeInt(mObjectType);
88         dest.writeParcelable(mObject, flags);
89     }
90 
getWidgetHandler()91     public WidgetAddFlowHandler getWidgetHandler() {
92         return mObjectType == TYPE_APP_WIDGET ? (WidgetAddFlowHandler) mObject : null;
93     }
94 
getWidgetId()95     public int getWidgetId() {
96         return mObjectType == TYPE_APP_WIDGET ? mArg1 : 0;
97     }
98 
getWidgetSourceContainer()99     public int getWidgetSourceContainer() {
100         return mObjectType == TYPE_APP_WIDGET ? mArg2 : Favorites.CONTAINER_UNKNOWN;
101     }
102 
getPendingIntent()103     public Intent getPendingIntent() {
104         return mObjectType == TYPE_INTENT ? (Intent) mObject : null;
105     }
106 
getRequestCode()107     public int getRequestCode() {
108         return mObjectType == TYPE_INTENT ? mArg1 : 0;
109     }
110 
forWidgetInfo( int appWidgetId, WidgetAddFlowHandler widgetHandler, ItemInfo info)111     public static PendingRequestArgs forWidgetInfo(
112             int appWidgetId, WidgetAddFlowHandler widgetHandler, ItemInfo info) {
113         int sourceContainer = Favorites.CONTAINER_UNKNOWN;
114         if (info instanceof PendingAddWidgetInfo) {
115             sourceContainer = ((PendingAddWidgetInfo) info).sourceContainer;
116         }
117         PendingRequestArgs args =
118                 new PendingRequestArgs(
119                         appWidgetId, sourceContainer, TYPE_APP_WIDGET, widgetHandler);
120         args.copyFrom(info);
121         return args;
122     }
123 
forIntent(int requestCode, Intent intent, ItemInfo info)124     public static PendingRequestArgs forIntent(int requestCode, Intent intent, ItemInfo info) {
125         PendingRequestArgs args = new PendingRequestArgs(requestCode, TYPE_INTENT, intent);
126         args.copyFrom(info);
127         return args;
128     }
129 
130     public static final Parcelable.Creator<PendingRequestArgs> CREATOR =
131             new Parcelable.Creator<PendingRequestArgs>() {
132                 public PendingRequestArgs createFromParcel(Parcel source) {
133                     return new PendingRequestArgs(source);
134                 }
135 
136                 public PendingRequestArgs[] newArray(int size) {
137                     return new PendingRequestArgs[size];
138                 }
139             };
140 }
141