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.tv.settings.device.apps.specialaccess;
18 
19 import android.app.ActivityManager;
20 import android.app.tvsettings.TvSettingsEnums;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.os.Bundle;
24 
25 import androidx.annotation.Keep;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 
29 import com.android.tv.settings.R;
30 import com.android.tv.settings.SettingsPreferenceFragment;
31 
32 /**
33  * Screen for Special App Access items
34  */
35 @Keep
36 public class SpecialAppAccess extends SettingsPreferenceFragment {
37 
38     @VisibleForTesting
39     static final String KEY_FEATURE_PIP = "picture_in_picture";
40     static final String KEY_FEATURE_NOTIFICATION_ACCESS = "notification_access";
41     private static final String[] DISABLED_FEATURES_LOW_RAM_TV =
42             new String[]{KEY_FEATURE_PIP, KEY_FEATURE_NOTIFICATION_ACCESS};
43 
44     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)45     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
46         setPreferencesFromResource(R.xml.special_app_access, null);
47 
48         updatePreferenceStates();
49     }
50 
51     @VisibleForTesting
updatePreferenceStates()52     void updatePreferenceStates() {
53         ActivityManager activityManager = (ActivityManager) getContext()
54                 .getSystemService(Context.ACTIVITY_SERVICE);
55         if (activityManager.isLowRamDevice()) {
56             for (String disabledFeature : DISABLED_FEATURES_LOW_RAM_TV) {
57                 removePreference(disabledFeature);
58             }
59         }
60         PackageManager packageManager = getActivity().getPackageManager();
61         if (!packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
62             removePreference(KEY_FEATURE_PIP);
63         }
64     }
65 
66     @VisibleForTesting
removePreference(String key)67     void removePreference(String key) {
68         final Preference preference = findPreference(key);
69         if (preference != null) {
70             getPreferenceScreen().removePreference(preference);
71         }
72     }
73 
74     @Override
getPageId()75     protected int getPageId() {
76         return TvSettingsEnums.APPS_SPECIAL_APP_ACCESS;
77     }
78 }
79