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.Context;
21 import android.content.Intent;
22 import android.content.IntentSender.SendIntentException;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 public class ProxyActivityStarter extends Activity {
27 
28     private static final String TAG = "ProxyActivityStarter";
29 
30     public static final String EXTRA_PARAMS = "start-activity-params";
31 
32     private StartActivityParams mParams;
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37         setVisible(false);
38 
39         mParams = getIntent().getParcelableExtra(EXTRA_PARAMS);
40         if (mParams == null) {
41             Log.d(TAG, "Proxy activity started without params");
42             finishAndRemoveTask();
43             return;
44         }
45 
46         if (savedInstanceState != null) {
47             // Already started the activity. Just wait for the result.
48             return;
49         }
50 
51         if (mParams.intent != null) {
52             startActivityForResult(mParams.intent, mParams.requestCode, mParams.options);
53             return;
54         } else if (mParams.intentSender != null) {
55             try {
56                 startIntentSenderForResult(mParams.intentSender, mParams.requestCode,
57                         mParams.fillInIntent, mParams.flagsMask, mParams.flagsValues,
58                         mParams.extraFlags,
59                         mParams.options);
60                 return;
61             } catch (SendIntentException e) {
62                 mParams.deliverResult(this, RESULT_CANCELED, null);
63             }
64         }
65         finishAndRemoveTask();
66     }
67 
68     @Override
onActivityResult(int requestCode, int resultCode, Intent data)69     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
70         if (requestCode == mParams.requestCode) {
71             mParams.deliverResult(this, resultCode, data);
72         }
73         finishAndRemoveTask();
74     }
75 
getLaunchIntent(Context context, StartActivityParams params)76     public static Intent getLaunchIntent(Context context, StartActivityParams params) {
77         return new Intent(context, ProxyActivityStarter.class)
78                 .putExtra(EXTRA_PARAMS, params)
79                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
80                         | Intent.FLAG_ACTIVITY_CLEAR_TASK);
81     }
82 }
83