• 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 public class ProxyActivityStarter extends Activity {
28 
29     private static final String TAG = "ProxyActivityStarter";
30 
31     public static final String EXTRA_PARAMS = "start-activity-params";
32 
33     private StartActivityParams mParams;
34 
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         setVisible(false);
39 
40         mParams = getIntent().getParcelableExtra(EXTRA_PARAMS);
41         if (mParams == null) {
42             Log.d(TAG, "Proxy activity started without params");
43             finishAndRemoveTask();
44             return;
45         }
46 
47         if (savedInstanceState != null) {
48             // Already started the activity. Just wait for the result.
49             return;
50         }
51 
52         try {
53             if (mParams.intent != null) {
54                 startActivityForResult(mParams.intent, mParams.requestCode, mParams.options);
55                 return;
56             } else if (mParams.intentSender != null) {
57                 startIntentSenderForResult(mParams.intentSender, mParams.requestCode,
58                         mParams.fillInIntent, mParams.flagsMask, mParams.flagsValues,
59                         mParams.extraFlags,
60                         mParams.options);
61                 return;
62             }
63         } catch (NullPointerException | ActivityNotFoundException | SecurityException
64                 | SendIntentException 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 }
85