1 /*
2  * Copyright (C) 2017 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.settings.development;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.os.SystemProperties;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.ListPreference;
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
31 import com.android.settingslib.development.SystemPropPoker;
32 
33 public class HdcpCheckingPreferenceController extends DeveloperOptionsPreferenceController
34         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
35 
36     private static final String HDCP_CHECKING_KEY = "hdcp_checking";
37 
38     @VisibleForTesting
39     static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
40     @VisibleForTesting
41     static final String USER_BUILD_TYPE = "user";
42 
43     private final String[] mListValues;
44     private final String[] mListSummaries;
45 
HdcpCheckingPreferenceController(Context context)46     public HdcpCheckingPreferenceController(Context context) {
47         super(context);
48 
49         mListValues = mContext.getResources().getStringArray(R.array.hdcp_checking_values);
50         mListSummaries = mContext.getResources().getStringArray(R.array.hdcp_checking_summaries);
51     }
52 
53     @Override
isAvailable()54     public boolean isAvailable() {
55         return !TextUtils.equals(USER_BUILD_TYPE, getBuildType());
56     }
57 
58     @Override
getPreferenceKey()59     public String getPreferenceKey() {
60         return HDCP_CHECKING_KEY;
61     }
62 
63     @Override
onPreferenceChange(Preference preference, Object newValue)64     public boolean onPreferenceChange(Preference preference, Object newValue) {
65         SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
66         updateHdcpValues((ListPreference) mPreference);
67         SystemPropPoker.getInstance().poke();
68         return true;
69     }
70 
71     @Override
updateState(Preference preference)72     public void updateState(Preference preference) {
73         updateHdcpValues((ListPreference) mPreference);
74     }
75 
updateHdcpValues(ListPreference preference)76     private void updateHdcpValues(ListPreference preference) {
77         final String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
78         int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
79         for (int i = 0; i < mListValues.length; i++) {
80             if (TextUtils.equals(currentValue, mListValues[i])) {
81                 index = i;
82                 break;
83             }
84         }
85         preference.setValue(mListValues[index]);
86         preference.setSummary(mListSummaries[index]);
87     }
88 
89     @VisibleForTesting
getBuildType()90     public String getBuildType() {
91         return Build.TYPE;
92     }
93 }
94