1 /* 2 * Copyright (C) 2019 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 com.android.launcher3.util; 18 19 import static android.app.PendingIntent.FLAG_MUTABLE; 20 import static android.app.PendingIntent.FLAG_ONE_SHOT; 21 import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; 22 23 import static com.android.launcher3.Utilities.allowBGLaunch; 24 25 import android.app.Activity; 26 import android.app.ActivityOptions; 27 import android.app.PendingIntent; 28 import android.app.PendingIntent.CanceledException; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.IntentSender; 32 import android.os.Bundle; 33 import android.os.Parcel; 34 import android.os.Parcelable; 35 import android.util.Log; 36 37 /** 38 * Wrapper class for parameters to start an activity. 39 */ 40 public class StartActivityParams implements Parcelable { 41 42 private static final String TAG = "StartActivityParams"; 43 44 private final PendingIntent mPICallback; 45 public final int requestCode; 46 47 public Intent intent; 48 49 public IntentSender intentSender; 50 public Intent fillInIntent; 51 public int flagsMask; 52 public int flagsValues; 53 public int extraFlags; 54 public Bundle options; 55 public boolean requireActivityResult = true; 56 StartActivityParams(Activity activity, int requestCode)57 public StartActivityParams(Activity activity, int requestCode) { 58 this(activity.createPendingResult(requestCode, new Intent(), 59 FLAG_ONE_SHOT | FLAG_UPDATE_CURRENT | FLAG_MUTABLE), requestCode); 60 } 61 StartActivityParams(PendingIntent pendingIntent, int requestCode)62 public StartActivityParams(PendingIntent pendingIntent, int requestCode) { 63 this.mPICallback = pendingIntent; 64 this.requestCode = requestCode; 65 } 66 StartActivityParams(Parcel parcel)67 private StartActivityParams(Parcel parcel) { 68 mPICallback = parcel.readTypedObject(PendingIntent.CREATOR); 69 requestCode = parcel.readInt(); 70 intent = parcel.readTypedObject(Intent.CREATOR); 71 72 intentSender = parcel.readTypedObject(IntentSender.CREATOR); 73 fillInIntent = parcel.readTypedObject(Intent.CREATOR); 74 flagsMask = parcel.readInt(); 75 flagsValues = parcel.readInt(); 76 extraFlags = parcel.readInt(); 77 options = parcel.readBundle(); 78 requireActivityResult = parcel.readInt() != 0; 79 } 80 81 82 @Override describeContents()83 public int describeContents() { 84 return 0; 85 } 86 87 @Override writeToParcel(Parcel parcel, int flags)88 public void writeToParcel(Parcel parcel, int flags) { 89 parcel.writeTypedObject(mPICallback, flags); 90 parcel.writeInt(requestCode); 91 parcel.writeTypedObject(intent, flags); 92 93 parcel.writeTypedObject(intentSender, flags); 94 parcel.writeTypedObject(fillInIntent, flags); 95 parcel.writeInt(flagsMask); 96 parcel.writeInt(flagsValues); 97 parcel.writeInt(extraFlags); 98 parcel.writeBundle(options); 99 parcel.writeInt(requireActivityResult ? 1 : 0); 100 } 101 102 /** Perform the operation on the pendingIntent. */ deliverResult(Context context, int resultCode, Intent data)103 public void deliverResult(Context context, int resultCode, Intent data) { 104 ActivityOptions options = allowBGLaunch(ActivityOptions.makeBasic()); 105 try { 106 if (mPICallback != null) { 107 mPICallback.send(context, resultCode, data, null, null, null, options.toBundle()); 108 } 109 } catch (CanceledException e) { 110 Log.e(TAG, "Unable to send back result", e); 111 } 112 } 113 114 public static final Parcelable.Creator<StartActivityParams> CREATOR = 115 new Parcelable.Creator<>() { 116 public StartActivityParams createFromParcel(Parcel source) { 117 return new StartActivityParams(source); 118 } 119 120 public StartActivityParams[] newArray(int size) { 121 return new StartActivityParams[size]; 122 } 123 }; 124 } 125