1 /*
2  * Copyright (C) 2020 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.cts.sessioninspector;
18 
19 import static android.content.Intent.EXTRA_RESULT_RECEIVER;
20 import static android.content.pm.PackageInstaller.EXTRA_SESSION;
21 import static android.content.pm.PackageInstaller.EXTRA_SESSION_ID;
22 import static android.content.pm.PackageInstaller.SessionInfo;
23 import static android.content.pm.PackageInstaller.SessionParams;
24 import static android.content.pm.PackageInstaller.SessionParams.MODE_FULL_INSTALL;
25 
26 import static com.android.cts.sessioninspector.Constants.ACTION_ABANDON_SESSION;
27 import static com.android.cts.sessioninspector.Constants.ACTION_CREATE_SESSION;
28 import static com.android.cts.sessioninspector.Constants.ACTION_GET_SESSION;
29 import static com.android.cts.sessioninspector.Constants.REFERRER_URI;
30 
31 import android.app.Activity;
32 import android.os.Bundle;
33 import android.os.RemoteCallback;
34 
35 public class SessionInspectorActivity extends Activity {
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         RemoteCallback remoteCallback = getIntent().getParcelableExtra(EXTRA_RESULT_RECEIVER);
41         final Bundle result = new Bundle();
42         String action = getIntent().getAction();
43         try {
44             switch (action) {
45                 case ACTION_CREATE_SESSION:
46                     SessionParams params = new SessionParams(MODE_FULL_INSTALL);
47                     params.setReferrerUri(REFERRER_URI);
48                     final int session =
49                             getPackageManager().getPackageInstaller().createSession(params);
50                     result.putInt(EXTRA_SESSION_ID, session);
51                     break;
52                 case ACTION_GET_SESSION: {
53                     final int sessionId = getIntent().getIntExtra(EXTRA_SESSION_ID, 0);
54                     final SessionInfo sessionInfo =
55                             getPackageManager().getPackageInstaller().getSessionInfo(sessionId);
56                     result.putParcelable(EXTRA_SESSION, sessionInfo);
57                     break;
58                 }
59                 case ACTION_ABANDON_SESSION: {
60                     final int sessionId = getIntent().getIntExtra(EXTRA_SESSION_ID, 0);
61                     getPackageManager().getPackageInstaller().abandonSession(sessionId);
62                     break;
63                 }
64                 default:
65                     throw new IllegalArgumentException("Unrecognized action: " + action);
66             }
67         } catch (Exception e) {
68             result.putSerializable("error", e);
69         }
70         remoteCallback.sendResult(result);
71         finish();
72     }
73 }
74