huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Timer;
import java.util.TimerTask;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.SystemProperties;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
 
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.view.LcdTestView;
import android.os.UEventObserver;
 
public class HdmiTestActivity extends Activity {
    private final static String TAG = "HDMITEST";
 
    private final static int CHANGE_COLOR = 1;
    private final static int HDMI_SCAN = 2;
    private int[] TestColor = {Color.RED, Color.GREEN, Color.BLUE };
    private LcdTestView mTestView;
    private TextView mTitle;
    private TextView mResult;
    private TextView mShowTime;
    private int mTestNo;
    private boolean isStart = false;
    private File HdmiFile = null;
    private File HdmiState = null;
    private File HdmiDisplayEnable=null;
    private File HdmiDisplayMode=null;
    private File HdmiDisplayConnect=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.hdmitest);
 
        mTestView = (LcdTestView) findViewById(R.id.lcdtestview);
        mResult = (TextView) findViewById(R.id.result);
        mShowTime = (TextView) findViewById(R.id.TimeShow);
        mTestNo = 0;
        
        HdmiFile = new File("/sys/class/hdmi/hdmi-0/enable");
        HdmiState = new File("/sys/devices/platform/display-subsystem/drm/card0/card0-HDMI-A-1/status");
        HdmiDisplayEnable=new File("/sys/class/display/HDMI/enable");
        //HdmiDisplayMode=new File("/sys/class/display/HDMI/mode");
        HdmiDisplayConnect=new File("sys/class/display/HDMI/connect");
        ControlButtonUtil.initControlButtonView(this);
        ((Button) findViewById(R.id.btn_Pass)).setVisibility(View.INVISIBLE);
    }
 
    @Override
    public void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(HDMI_SCAN, 500);
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mHandler.removeMessages(HDMI_SCAN);
        mHandler.removeMessages(CHANGE_COLOR);
    }
    
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case CHANGE_COLOR:
                    if (mTestNo > TestColor.length - 1) {
                        finishHdmiTest();
                        return;
                    }
                    ControlButtonUtil.Hide();
                    mShowTime.setVisibility(View.VISIBLE);
                    mTestView.setVisibility(View.VISIBLE);
                    mResult.setText(R.string.HdmiStart);
                    mTestView.setBackgroundColor(TestColor[mTestNo++]);
                    sendEmptyMessageDelayed(CHANGE_COLOR, 1500);
                    break;
                case HDMI_SCAN:
                    this.removeMessages(HDMI_SCAN);
                    if (startHdmiTest()) {
                        mResult.setText(R.string.HdmiPrepare);
                        //setHdmiConfig(HdmiFile, true);
                        mTestNo = 0;
                        sendEmptyMessageDelayed(CHANGE_COLOR, 4000);
                    }else{
                        sendEmptyMessageDelayed(HDMI_SCAN, 500);
                    }
                    break;
                default:
                    break;
            }
        }
    };
    
    public boolean startHdmiTest() {
        if (!isStart && isHdmiConnected()) {
            mResult.setText(R.string.HdmiPrepare);
            setHdmiConfig(HdmiFile, true);
            mTestNo = 0;
            isStart = true;
            return true;
        }
        mResult.setText(R.string.HdmiNoInsert);
        Log.i(TAG, "Hdmi no insert");
        return false;
    }
    
    public void finishHdmiTest() {
        ((Button) findViewById(R.id.btn_Pass)).setVisibility(View.VISIBLE);
        ControlButtonUtil.Show();
        isStart = false;
        mShowTime.setVisibility(View.GONE);
        mTestView.setVisibility(View.GONE);
        mResult.setText(R.string.HdmiResult);
//        setHdmiConfig(HdmiFile, false);
    }
 
    protected boolean isHdmiConnected() {
        File file = HdmiState;
        boolean isConnected = false;
        if (file.exists()) {
            try {
                FileReader fread = new FileReader(file);
                BufferedReader buffer = new BufferedReader(fread);
                String strPlug = "plug=1";
                String str = null;
 
                while ((str = buffer.readLine()) != null) {
                    int length = str.length();
                    //if ((length == 6) && (str.equals(strPlug))) {
                    if(str.equals("connected")){
                        isConnected = true;
                        break;
                    } else {
                        isConnected = false;
                    }
                }
            } catch (IOException e) {
                Log.e(TAG, "IO Exception");
            }
        } else {
            Log.e(TAG, file + "isHdmiConnected : file no exist");
        }
        return isConnected;
    }
 
    protected void setHdmiConfig(File file, boolean enable) {
        if (SystemProperties.get("ro.board.platform", "none").equals("rk29xx")){
            if (file.exists()) {
                try {
                    String strDouble = "2";
                    String strChecked = "1";
                    String strUnChecked = "0";
                    RandomAccessFile rdf = null;
                    rdf = new RandomAccessFile(file, "rw");
 
                    if (enable) {
                        rdf.writeBytes(strChecked);
                    } else {
                        rdf.writeBytes(strUnChecked);
 
                    }
 
                } catch (IOException re) {
                    Log.e(TAG, "IO Exception");
                }
            } else {
                Log.i(TAG, "The File " + file + " is not exists");
            }
        } else{
            if (file.exists()) {
                try {
                    Log.d(TAG, "setHdmiConfig");
                    String strChecked = "1";
                    String strUnChecked = "0";
 
                    RandomAccessFile rdf = null;
                    rdf = new RandomAccessFile(file, "rw");
                    if (enable) {
                        rdf.writeBytes(strChecked);
                    } else {
                        rdf.writeBytes(strUnChecked);
                    }
                } catch (IOException re) {
                    Log.e(TAG, "IO Exception");
                    re.printStackTrace();
                }
            } else {
                Log.i(TAG, "The File " + file + " is not exists");
            }
        }
    }
 
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.dispatchKeyEvent(event);
    }
    
    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        if (paramMotionEvent.getAction() == MotionEvent.ACTION_DOWN && !isStart) {
            mHandler.sendEmptyMessageDelayed(HDMI_SCAN, 500);
        }
        return super.onTouchEvent(paramMotionEvent);
    }
    
 
}