1 /*
2  * Copyright (C) 2018 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.role.ui;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.Process;
23 import android.os.UserHandle;
24 import android.util.Log;
25 import android.view.WindowManager;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.fragment.app.Fragment;
30 import androidx.fragment.app.FragmentActivity;
31 
32 import com.android.packageinstaller.DeviceUtils;
33 import com.android.packageinstaller.role.model.Role;
34 import com.android.packageinstaller.role.model.Roles;
35 import com.android.packageinstaller.role.ui.auto.AutoDefaultAppFragment;
36 import com.android.packageinstaller.role.ui.handheld.HandheldDefaultAppFragment;
37 import com.android.permissioncontroller.R;
38 
39 /**
40  * Activity for a default app.
41  */
42 public class DefaultAppActivity extends FragmentActivity {
43 
44     private static final String LOG_TAG = DefaultAppActivity.class.getSimpleName();
45 
46     /**
47      * Create an intent for starting this activity.
48      *
49      * @param roleName the name of the role for the default app
50      * @param user the user for the default app
51      * @param context the context to create the intent
52      *
53      * @return an intent to start this activity
54      */
55     @NonNull
createIntent(@onNull String roleName, @NonNull UserHandle user, @NonNull Context context)56     public static Intent createIntent(@NonNull String roleName, @NonNull UserHandle user,
57             @NonNull Context context) {
58         return new Intent(context, DefaultAppActivity.class)
59                 .putExtra(Intent.EXTRA_ROLE_NAME, roleName)
60                 .putExtra(Intent.EXTRA_USER, user);
61     }
62 
63     @Override
onCreate(@ullable Bundle savedInstanceState)64     protected void onCreate(@Nullable Bundle savedInstanceState) {
65         if (DeviceUtils.isAuto(this)) {
66             // Automotive relies on a different theme. Apply before calling super so that
67             // fragments are restored properly on configuration changes.
68             setTheme(R.style.CarSettings);
69         }
70         super.onCreate(savedInstanceState);
71 
72         getWindow().addSystemFlags(
73                 WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
74 
75         Intent intent = getIntent();
76         String roleName = intent.getStringExtra(Intent.EXTRA_ROLE_NAME);
77         UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
78         // External callers might omit the user.
79         if (user == null) {
80             user = Process.myUserHandle();
81         }
82 
83         Role role = Roles.get(this).get(roleName);
84         if (role == null) {
85             Log.e(LOG_TAG, "Unknown role: " + roleName);
86             finish();
87             return;
88         }
89         if (!role.isAvailableAsUser(user, this)) {
90             Log.e(LOG_TAG, "Role is unavailable: " + roleName);
91             finish();
92             return;
93         }
94         if (!role.isVisibleAsUser(user, this)) {
95             Log.e(LOG_TAG, "Role is invisible: " + roleName);
96             finish();
97             return;
98         }
99 
100         if (savedInstanceState == null) {
101             Fragment fragment;
102             if (DeviceUtils.isAuto(this)) {
103                 fragment = AutoDefaultAppFragment.newInstance(roleName, user);
104             } else {
105                 fragment = HandheldDefaultAppFragment.newInstance(roleName, user);
106             }
107             getSupportFragmentManager().beginTransaction()
108                     .add(android.R.id.content, fragment)
109                     .commit();
110         }
111     }
112 }
113