1 /*
2 * Copyright (C) 2023 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.mediadrm;
18 
19 import android.content.Context;
20 import android.media.MediaDrm;
21 import android.sysprop.WidevineProperties;
22 import android.util.Log;
23 
24 import androidx.preference.Preference;
25 
26 import java.util.UUID;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.settings.R;
30 import com.android.settings.core.TogglePreferenceController;
31 import com.android.settingslib.development.DevelopmentSettingsEnabler;
32 import com.android.settings.media_drm.Flags;
33 
34 /**
35  * The controller (in the Media Drm settings) enforces software secure crypto.
36 */
37 public class ForceSwSecureCryptoFallbackPreferenceController extends TogglePreferenceController {
38     private static final String TAG = "ForceSwSecureCryptoFallbackPreferenceController";
39     private static final UUID WIDEVINE_UUID =
40         new UUID(0xEDEF8BA979D64ACEL, 0xA3C827DCD51D21EDL);
41 
ForceSwSecureCryptoFallbackPreferenceController(Context context, String preferenceKey)42     public ForceSwSecureCryptoFallbackPreferenceController(Context context, String preferenceKey) {
43         super(context, preferenceKey);
44     }
45 
46     @Override
isChecked()47     public boolean isChecked() {
48         return WidevineProperties.forcel3_enabled().orElse(false);
49     }
50 
51     @Override
setChecked(boolean isChecked)52     public boolean setChecked(boolean isChecked) {
53         WidevineProperties.forcel3_enabled(isChecked);
54         return true;
55     }
56 
57     @Override
updateState(Preference preference)58     public void updateState(Preference preference) {
59         boolean isEnable = false;
60         if (Flags.forceL3Enabled()) {
61             try (MediaDrm drm = new MediaDrm(WIDEVINE_UUID)) {
62                 String version = drm.getPropertyString(MediaDrm.PROPERTY_VERSION);
63                 if (Integer.parseInt(version.split("\\.", 2)[0]) >= 19) {
64                     isEnable = true;
65                 }
66             } catch (Exception ex) {
67                 Log.e(TAG, "An exception occurred:", ex);
68             }
69         }
70 
71         preference.setEnabled(isEnable);
72         if (!isEnable) {
73             // In case of flag rollback, the controller should be unchecked.
74             WidevineProperties.forcel3_enabled(false);
75         }
76         Log.i(TAG, "Force software crypto is " + isEnable);
77         super.updateState(preference);
78     }
79 
80     @Override
getAvailabilityStatus()81     public int getAvailabilityStatus() {
82         if (DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(mContext)) {
83             return AVAILABLE;
84         } else {
85             return CONDITIONALLY_UNAVAILABLE;
86         }
87     }
88 
89     @Override
getSliceHighlightMenuRes()90     public int getSliceHighlightMenuRes() {
91         return R.string.menu_key_system;
92     }
93 }