• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 startActivityForResult(mParams.intent, mParams.requestCode, mParams.options);
57                 return;
58             } else if (mParams.intentSender != null) {
59                 startIntentSenderForResult(mParams.intentSender, mParams.requestCode,
60                         mParams.fillInIntent, mParams.flagsMask, mParams.flagsValues,
61                         mParams.extraFlags,
62                         mParams.options);
63                 return;
64             }
65         } catch (NullPointerException | ActivityNotFoundException | SecurityException
66                 | SendIntentException e) {
67             mParams.deliverResult(this, RESULT_CANCELED, null);
68         }
69         finishAndRemoveTask();
70     }
71 
72     @Override
onActivityResult(int requestCode, int resultCode, Intent data)73     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
74         if (requestCode == mParams.requestCode) {
75             mParams.deliverResult(this, resultCode, data);
76         }
77         finishAndRemoveTask();
78     }
79 
getLaunchIntent(Context context, StartActivityParams params)80     public static Intent getLaunchIntent(Context context, StartActivityParams params) {
81         return new Intent(context, ProxyActivityStarter.class)
82                 .putExtra(EXTRA_PARAMS, params)
83                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
84                         | Intent.FLAG_ACTIVITY_CLEAR_TASK);
85     }
86 }
87