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.proxy; 18 19 import android.app.Activity; 20 import android.content.ActivityNotFoundException; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentSender.SendIntentException; 24 import android.os.Bundle; 25 import android.util.Log; 26 27 import com.android.launcher3.util.StartActivityParams; 28 29 public class ProxyActivityStarter extends Activity { 30 31 private static final String TAG = "ProxyActivityStarter"; 32 33 public static final String EXTRA_PARAMS = "start-activity-params"; 34 35 private StartActivityParams mParams; 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setVisible(false); 41 42 mParams = getIntent().getParcelableExtra(EXTRA_PARAMS); 43 if (mParams == null) { 44 Log.d(TAG, "Proxy activity started without params"); 45 finishAndRemoveTask(); 46 return; 47 } 48 49 if (savedInstanceState != null) { 50 // Already started the activity. Just wait for the result. 51 return; 52 } 53 54 try { 55 if (mParams.intent != null) { 56 startActivity(); 57 return; 58 } else if (mParams.intentSender != null) { 59 startIntentSender(); 60 return; 61 } 62 } catch (NullPointerException | ActivityNotFoundException | SecurityException 63 | SendIntentException e) { 64 Log.w(TAG, "Proxy activity starter could not start activity: ", e); 65 mParams.deliverResult(this, RESULT_CANCELED, null); 66 } 67 finishAndRemoveTask(); 68 } 69 70 @Override onActivityResult(int requestCode, int resultCode, Intent data)71 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 72 if (requestCode == mParams.requestCode) { 73 mParams.deliverResult(this, resultCode, data); 74 } 75 finishAndRemoveTask(); 76 } 77 getLaunchIntent(Context context, StartActivityParams params)78 public static Intent getLaunchIntent(Context context, StartActivityParams params) { 79 return new Intent(context, ProxyActivityStarter.class) 80 .putExtra(EXTRA_PARAMS, params) 81 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED 82 | Intent.FLAG_ACTIVITY_CLEAR_TASK); 83 } 84 startActivity()85 private void startActivity() throws SendIntentException { 86 if (mParams.requireActivityResult) { 87 startActivityForResult(mParams.intent, mParams.requestCode, mParams.options); 88 } else { 89 startActivity(mParams.intent, mParams.options); 90 finishAndRemoveTask(); 91 } 92 } 93 startIntentSender()94 private void startIntentSender() throws SendIntentException { 95 if (mParams.requireActivityResult) { 96 startIntentSenderForResult(mParams.intentSender, mParams.requestCode, 97 mParams.fillInIntent, mParams.flagsMask, mParams.flagsValues, 98 mParams.extraFlags, 99 mParams.options); 100 } else { 101 startIntentSender(mParams.intentSender, mParams.fillInIntent, mParams.flagsMask, 102 mParams.flagsValues, mParams.extraFlags, mParams.options); 103 finishAndRemoveTask(); 104 } 105 } 106 } 107