1 /*
2  * Copyright 2017 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.packageinstaller;
18 
19 import static android.content.Intent.CATEGORY_LAUNCHER;
20 
21 import static com.android.packageinstaller.PackageInstallerActivity.EXTRA_STAGED_SESSION_ID;
22 
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.os.Bundle;
26 
27 import androidx.annotation.Nullable;
28 
29 /**
30  * Trampoline activity. Calls PackageInstallerActivity and deletes staged install file onResult.
31  */
32 public class DeleteStagedFileOnResult extends Activity {
33     @Override
onCreate(@ullable Bundle savedInstanceState)34     protected void onCreate(@Nullable Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36 
37         if (savedInstanceState == null) {
38             Intent installIntent = new Intent(getIntent());
39             installIntent.setClass(this, PackageInstallerActivity.class);
40 
41             installIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
42             startActivityForResult(installIntent, 0);
43         }
44     }
45 
46     @Override
onActivityResult(int requestCode, int resultCode, Intent data)47     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
48         setResult(resultCode, data);
49         finish();
50         if (data != null && data.hasCategory(CATEGORY_LAUNCHER)) {
51             startActivity(data);
52         }
53     }
54 
55     @Override
onDestroy()56     protected void onDestroy() {
57         super.onDestroy();
58 
59         if (isFinishing()) {
60             // While we expect PIA/InstallStaging to abandon/commit the session, still there
61             // might be cases when the session becomes orphan.
62             int sessionId = getIntent().getIntExtra(EXTRA_STAGED_SESSION_ID, 0);
63             try {
64                 getPackageManager().getPackageInstaller().abandonSession(sessionId);
65             } catch (SecurityException ignored) {
66             }
67         }
68     }
69 }
70