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             mParams.deliverResult(this, RESULT_CANCELED, null);
65         }
66         finishAndRemoveTask();
67     }
68 
69     @Override
onActivityResult(int requestCode, int resultCode, Intent data)70     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
71         if (requestCode == mParams.requestCode) {
72             mParams.deliverResult(this, resultCode, data);
73         }
74         finishAndRemoveTask();
75     }
76 
getLaunchIntent(Context context, StartActivityParams params)77     public static Intent getLaunchIntent(Context context, StartActivityParams params) {
78         return new Intent(context, ProxyActivityStarter.class)
79                 .putExtra(EXTRA_PARAMS, params)
80                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
81                         | Intent.FLAG_ACTIVITY_CLEAR_TASK);
82     }
83 
startActivity()84     private void startActivity() throws SendIntentException {
85         if (mParams.requireActivityResult) {
86             startActivityForResult(mParams.intent, mParams.requestCode, mParams.options);
87         } else {
88             startActivity(mParams.intent, mParams.options);
89             finishAndRemoveTask();
90         }
91     }
92 
startIntentSender()93     private void startIntentSender() throws SendIntentException {
94         if (mParams.requireActivityResult) {
95             startIntentSenderForResult(mParams.intentSender, mParams.requestCode,
96                     mParams.fillInIntent, mParams.flagsMask, mParams.flagsValues,
97                     mParams.extraFlags,
98                     mParams.options);
99         } else {
100             startIntentSender(mParams.intentSender, mParams.fillInIntent, mParams.flagsMask,
101                     mParams.flagsValues, mParams.extraFlags, mParams.options);
102             finishAndRemoveTask();
103         }
104     }
105 }
106