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.SystemProperties;
21 import android.text.TextUtils;
22 import android.view.ThreadedRenderer;
23 
24 import androidx.preference.ListPreference;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
29 import com.android.settingslib.development.SystemPropPoker;
30 
31 public class ProfileGpuRenderingPreferenceController extends DeveloperOptionsPreferenceController
32         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
33 
34     private static final String TRACK_FRAME_TIME_KEY = "track_frame_time";
35 
36     private final String[] mListValues;
37     private final String[] mListSummaries;
38 
ProfileGpuRenderingPreferenceController(Context context)39     public ProfileGpuRenderingPreferenceController(Context context) {
40         super(context);
41 
42         mListValues = context.getResources()
43                 .getStringArray(com.android.settingslib.R.array.track_frame_time_values);
44         mListSummaries = context.getResources()
45                 .getStringArray(com.android.settingslib.R.array.track_frame_time_entries);
46     }
47 
48     @Override
getPreferenceKey()49     public String getPreferenceKey() {
50         return TRACK_FRAME_TIME_KEY;
51     }
52 
53     @Override
onPreferenceChange(Preference preference, Object newValue)54     public boolean onPreferenceChange(Preference preference, Object newValue) {
55         writeTrackFrameTimeOptions(newValue);
56         updateTrackFrameTimeOptions();
57         return true;
58     }
59 
60     @Override
updateState(Preference preference)61     public void updateState(Preference preference) {
62         updateTrackFrameTimeOptions();
63     }
64 
writeTrackFrameTimeOptions(Object newValue)65     private void writeTrackFrameTimeOptions(Object newValue) {
66         SystemProperties.set(ThreadedRenderer.PROFILE_PROPERTY,
67                 newValue == null ? "" : newValue.toString());
68         SystemPropPoker.getInstance().poke();
69     }
70 
updateTrackFrameTimeOptions()71     private void updateTrackFrameTimeOptions() {
72         final String value = SystemProperties.get(
73                 ThreadedRenderer.PROFILE_PROPERTY, "" /* default */);
74         int index = 0; // default
75         for (int i = 0; i < mListValues.length; i++) {
76             if (TextUtils.equals(value, mListValues[i])) {
77                 index = i;
78                 break;
79             }
80         }
81         final ListPreference listPreference = (ListPreference) mPreference;
82         listPreference.setValue(mListValues[index]);
83         listPreference.setSummary(mListSummaries[index]);
84     }
85 }
86