ronnie
2022-10-23 eadd9db01b24ccde96a129dafa989d4ec436cdfd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.android.settings.display;
 
import android.content.Context;
import androidx.preference.SwitchPreference;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
 
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.AbstractPreferenceController;
 
import java.io.File;
 
import com.softwinner.AWDisplay;
import com.android.settings.R;
 
public class AwDisplayPreferenceController extends AbstractPreferenceController implements
        PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
 
    private static final String HDMI_STATE = "/sys/class/extcon/hdmi/state";
   private static final String CVBS_STATE = "/sys/class/extcon/cvbs/state";
    private static final String KEY_HDMI_SETTING = "second_screen_setting";
    private static final String KEY_HDMI_OUTPUT_MODE = "second_screen_output_mode";
    private static final String KEY_HDMI_FULLSCREEN = "hdmi_fullscreen";
    private static final String KEY_HDMI_WIDTH_SCALE = "second_screen_width_scale";
    private static final String KEY_HDMI_HEIGHT_SCALE = "second_screen_height_scale";
    private static final int HDMI_SCALE_MIN = 80;
    private static final int HDMI_SCALE_MAX = 100;
    private static final int NONE_DISPLAY = 0;
    private static final int HDMI_PLUG_IN = 1;
    private static final int CVBS_PLUG_IN = 2;
    private int display_type = NONE_DISPLAY;
    private ListPreference mOutputMode;
    private SwitchPreference mFullscreen;
    private SeekBarPreference mWidthScale;
    private SeekBarPreference mHeightScale;
    private Context mContext;
 
    public AwDisplayPreferenceController(Context context) {
        super(context);
        mContext = context;
    }
 
    @Override
    public void displayPreference(PreferenceScreen screen) {
        if (isAvailable()) {
            setVisible(screen, getPreferenceKey(), true);
            mOutputMode = (ListPreference) screen.findPreference(KEY_HDMI_OUTPUT_MODE);
            mOutputMode.setOnPreferenceChangeListener(this);
            mFullscreen = (SwitchPreference) screen.findPreference(KEY_HDMI_FULLSCREEN);
            mFullscreen.setOnPreferenceChangeListener(this);
            mWidthScale = (SeekBarPreference) screen.findPreference(KEY_HDMI_WIDTH_SCALE);
            mWidthScale.setOnPreferenceChangeListener(this);
            mHeightScale = (SeekBarPreference) screen.findPreference(KEY_HDMI_HEIGHT_SCALE);
            mHeightScale.setOnPreferenceChangeListener(this);
            mWidthScale.setMax(HDMI_SCALE_MAX - HDMI_SCALE_MIN);
            mHeightScale.setMax(HDMI_SCALE_MAX - HDMI_SCALE_MIN);
            if (display_type == CVBS_PLUG_IN) {
                //CVBS dont need full screen mode
                PreferenceCategory screen_settings = (PreferenceCategory)(screen.findPreference(KEY_HDMI_SETTING));
                screen_settings.removePreference(mFullscreen);
                //OutputMode default ues hdmi mode entries and values, so if display type is cvbs, should change entries and values
                mOutputMode.setEntries(R.array.cvbs_output_mode_entries);
                mOutputMode.setEntryValues(R.array.cvbs_output_mode_values);
            }
        } else {
            setVisible(screen, getPreferenceKey(), false);
        }
    }
 
    //support HDMI and CVBS
    private boolean checkHDMIState() {
        if (new File(HDMI_STATE).exists()) {
            display_type = HDMI_PLUG_IN;
            return true;
        }
        return false;
    }
 
    private boolean checkCVBSState() {
        if (new File(CVBS_STATE).exists()) {
            display_type = CVBS_PLUG_IN;
            return true;
        }
        return false;
    }
 
    @Override
    public boolean isAvailable() {
        //second display only support HDMI or CVBS, can not support them as the same time
        return mContext.getResources().getBoolean(R.bool.aw_display_enabled) && (checkHDMIState() || checkCVBSState());
    }
 
    @Override
    public String getPreferenceKey() {
        return KEY_HDMI_SETTING;
    }
 
    @Override
    public void updateState(Preference preference) {
        if (mOutputMode != null) {
            int mode = AWDisplay.getHdmiMode();
            mOutputMode.setValue("" + mode);
        }
        if (mFullscreen != null) {
            boolean full = AWDisplay.getHdmiFullscreen();
            mFullscreen.setChecked(full);
        }
        if (mWidthScale != null) {
            int value = AWDisplay.getMarginWidth();
            mWidthScale.setProgress(value - HDMI_SCALE_MIN);
        }
        if (mHeightScale != null) {
            int value = AWDisplay.getMarginHeight();
            mHeightScale.setProgress(value - HDMI_SCALE_MIN);
        }
    }
 
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (mOutputMode == preference) {
            int mode = Integer.parseInt((String) newValue);
            AWDisplay.setHdmiMode(mode);
        } else if (mFullscreen == preference) {
            boolean full = (Boolean) newValue;
            AWDisplay.setHdmiFullscreen(full);
        } else if (mWidthScale == preference) {
            int value = (Integer) newValue;
            AWDisplay.setMarginWidth(value + HDMI_SCALE_MIN);
        } else if (mHeightScale == preference) {
            int value = (Integer) newValue;
            AWDisplay.setMarginHeight(value + HDMI_SCALE_MIN);
        }
        return true;
    }
}