1 /*
2  * Copyright (C) 2015 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.tv.settings.device.apps;
18 
19 import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;
20 
21 import android.app.Activity;
22 import android.app.Application;
23 import android.app.tvsettings.TvSettingsEnums;
24 import android.content.Context;
25 import android.os.Bundle;
26 import android.text.TextUtils;
27 
28 import androidx.annotation.Keep;
29 import androidx.annotation.Nullable;
30 import androidx.preference.Preference;
31 
32 import com.android.settingslib.core.AbstractPreferenceController;
33 import com.android.tv.settings.PreferenceControllerFragment;
34 import com.android.tv.settings.R;
35 import com.android.tv.settings.overlay.FlavorUtils;
36 import com.android.tv.settings.util.SliceUtils;
37 import com.android.tv.twopanelsettings.slices.SlicePreference;
38 
39 import java.util.ArrayList;
40 import java.util.List;
41 
42 /**
43  * Fragment for managing recent apps, and apps permissions.
44  */
45 @Keep
46 public class AppsFragment extends PreferenceControllerFragment {
47 
48     private static final String KEY_PERMISSIONS = "Permissions";
49     private static final String KEY_SECURITY = "security";
50     private static final String KEY_OVERLAY_SECURITY = "overlay_security";
51     private static final String KEY_UPDATE = "update";
52 
prepareArgs(Bundle b, String volumeUuid, String volumeName)53     public static void prepareArgs(Bundle b, String volumeUuid, String volumeName) {
54         b.putString(AppsActivity.EXTRA_VOLUME_UUID, volumeUuid);
55         b.putString(AppsActivity.EXTRA_VOLUME_NAME, volumeName);
56     }
57 
newInstance(String volumeUuid, String volumeName)58     public static AppsFragment newInstance(String volumeUuid, String volumeName) {
59         final Bundle b = new Bundle(2);
60         prepareArgs(b, volumeUuid, volumeName);
61         final AppsFragment f = new AppsFragment();
62         f.setArguments(b);
63         return f;
64     }
65 
66     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)67     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
68         super.onCreatePreferences(savedInstanceState, rootKey);
69 
70         final Preference permissionPreference = findPreference(KEY_PERMISSIONS);
71         final String volumeUuid = getArguments().getString(AppsActivity.EXTRA_VOLUME_UUID);
72         permissionPreference.setVisible(TextUtils.isEmpty(volumeUuid));
73         permissionPreference.setOnPreferenceClickListener(
74                 preference -> {
75                     logEntrySelected(TvSettingsEnums.APPS_APP_PERMISSIONS);
76                     return false;
77                 }
78         );
79         final Preference securityPreference = findPreference(KEY_SECURITY);
80         final Preference overlaySecuritySlicePreference = findPreference(KEY_OVERLAY_SECURITY);
81         final Preference updateSlicePreference = findPreference(KEY_UPDATE);
82         if (FlavorUtils.getFeatureFactory(getContext()).getBasicModeFeatureProvider()
83                 .isBasicMode(getContext())) {
84             showSecurityPreference(securityPreference, overlaySecuritySlicePreference);
85             if (updateSlicePreference != null) {
86                 updateSlicePreference.setVisible(false);
87             }
88         } else {
89             if (isOverlaySecuritySlicePreferenceEnabled(overlaySecuritySlicePreference)) {
90                 showOverlaySecuritySlicePreference(
91                         overlaySecuritySlicePreference, securityPreference);
92             } else {
93                 showSecurityPreference(securityPreference, overlaySecuritySlicePreference);
94             }
95             if (updateSlicePreference != null) {
96                 updateSlicePreference.setVisible(
97                         isUpdateSlicePreferenceEnabled(updateSlicePreference));
98             }
99         }
100     }
101 
isOverlaySecuritySlicePreferenceEnabled( @ullable Preference overlaySecuritySlicePreference)102     private boolean isOverlaySecuritySlicePreferenceEnabled(
103             @Nullable Preference overlaySecuritySlicePreference) {
104         return overlaySecuritySlicePreference instanceof SlicePreference
105                 && SliceUtils.isSettingsSliceEnabled(
106                         getContext(), ((SlicePreference) overlaySecuritySlicePreference).getUri());
107     }
108 
showOverlaySecuritySlicePreference( @ullable Preference overlaySecuritySlicePreference, @Nullable Preference securityPreference)109     private void showOverlaySecuritySlicePreference(
110             @Nullable Preference overlaySecuritySlicePreference,
111             @Nullable Preference securityPreference) {
112         if (overlaySecuritySlicePreference != null) {
113             overlaySecuritySlicePreference.setVisible(true);
114         }
115         if (securityPreference != null) {
116             securityPreference.setVisible(false);
117         }
118     }
119 
showSecurityPreference( @ullable Preference securityPreference, @Nullable Preference overlaySecuritySlicePreference)120     private void showSecurityPreference(
121             @Nullable Preference securityPreference,
122             @Nullable Preference overlaySecuritySlicePreference) {
123         if (securityPreference != null) {
124             securityPreference.setVisible(true);
125         }
126         if (overlaySecuritySlicePreference != null) {
127             overlaySecuritySlicePreference.setVisible(false);
128         }
129     }
130 
isUpdateSlicePreferenceEnabled( @ullable Preference updateSlicePreference)131     private boolean isUpdateSlicePreferenceEnabled(
132             @Nullable Preference updateSlicePreference) {
133         return updateSlicePreference instanceof SlicePreference
134                 && SliceUtils.isSettingsSliceEnabled(
135                         getContext(), ((SlicePreference) updateSlicePreference).getUri());
136     }
137 
138     @Override
getPreferenceScreenResId()139     protected int getPreferenceScreenResId() {
140         switch (FlavorUtils.getFlavor(getContext())) {
141             case FlavorUtils.FLAVOR_X:
142             case FlavorUtils.FLAVOR_VENDOR:
143                 return R.xml.apps_x;
144             default:
145                 return R.xml.apps;
146         }
147     }
148 
149     @Override
onCreatePreferenceControllers(Context context)150     protected List<AbstractPreferenceController> onCreatePreferenceControllers(Context context) {
151         final Activity activity = getActivity();
152         final Application app = activity != null ? activity.getApplication() : null;
153         List<AbstractPreferenceController> controllers = new ArrayList<>();
154         controllers.add(new RecentAppsPreferenceController(getContext(), app));
155         return controllers;
156     }
157 
158     @Override
getPageId()159     protected int getPageId() {
160         return TvSettingsEnums.APPS;
161     }
162 }
163