1 /*
2  * Copyright (C) 2019 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.auto;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.preference.Preference;
27 import androidx.preference.TwoStatePreference;
28 
29 import com.android.permissioncontroller.R;
30 import com.android.permissioncontroller.auto.AutoSettingsFrameFragment;
31 import com.android.permissioncontroller.role.model.Role;
32 import com.android.permissioncontroller.role.ui.DefaultAppChildFragment;
33 
34 /** Screen to pick a default app for a particular {@link Role}. */
35 public class AutoDefaultAppFragment extends AutoSettingsFrameFragment implements
36         DefaultAppChildFragment.Parent {
37 
38     private String mRoleName;
39 
40     private UserHandle mUser;
41 
42     /**
43      * Create a new instance of this fragment.
44      *
45      * @param roleName the name of the role for the default app
46      * @param user     the user for the default app
47      * @return a new instance of this fragment
48      */
49     @NonNull
newInstance(@onNull String roleName, @NonNull UserHandle user)50     public static AutoDefaultAppFragment newInstance(@NonNull String roleName,
51             @NonNull UserHandle user) {
52         AutoDefaultAppFragment fragment = new AutoDefaultAppFragment();
53         Bundle arguments = new Bundle();
54         arguments.putString(Intent.EXTRA_ROLE_NAME, roleName);
55         arguments.putParcelable(Intent.EXTRA_USER, user);
56         fragment.setArguments(arguments);
57         return fragment;
58     }
59 
60     @Override
onCreate(@ullable Bundle savedInstanceState)61     public void onCreate(@Nullable Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63 
64         Bundle arguments = getArguments();
65         mRoleName = arguments.getString(Intent.EXTRA_ROLE_NAME);
66         mUser = arguments.getParcelable(Intent.EXTRA_USER);
67     }
68 
69     @Override
onCreatePreferences(Bundle bundle, String s)70     public void onCreatePreferences(Bundle bundle, String s) {
71         // Preferences will be added via shared logic in {@link DefaultAppChildFragment}.
72     }
73 
74     @Override
onActivityCreated(@ullable Bundle savedInstanceState)75     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
76         super.onActivityCreated(savedInstanceState);
77 
78         if (savedInstanceState == null) {
79             DefaultAppChildFragment fragment = DefaultAppChildFragment.newInstance(mRoleName,
80                     mUser);
81             getChildFragmentManager().beginTransaction()
82                     .add(fragment, null)
83                     .commit();
84         }
85     }
86 
87     @Override
setTitle(@onNull CharSequence title)88     public void setTitle(@NonNull CharSequence title) {
89         setHeaderLabel(title);
90     }
91 
92     @NonNull
93     @Override
createApplicationPreference(@onNull Context context)94     public TwoStatePreference createApplicationPreference(@NonNull Context context) {
95         return new AutoDefaultAppPreference(context);
96     }
97 
98     @NonNull
99     @Override
createFooterPreference(@onNull Context context)100     public Preference createFooterPreference(@NonNull Context context) {
101         Preference preference = new Preference(context);
102         preference.setIcon(R.drawable.ic_info_outline);
103         preference.setSelectable(false);
104         return preference;
105     }
106 
107     @Override
onPreferenceScreenChanged()108     public void onPreferenceScreenChanged() {
109     }
110 }
111