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