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