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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
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.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import com.DeviceTest.WifiTestActivity.MyBroadcastReceiver;
import com.DeviceTest.helper.Recorder;
import com.DeviceTest.helper.SystemUtil;
import com.DeviceTest.helper.VUMeter;
import com.DeviceTest.view.GsensorBall;
import com.DeviceTest.view.KeyTestView;
import com.DeviceTest.view.TestView;
 
import android.app.Activity;
import android.app.Dialog;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.os.storage.StorageEventListener;
import android.os.SystemProperties;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
 
import android.os.ServiceManager;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
 
public class FirstRun extends Activity implements SurfaceHolder.Callback{
   private static final String TAG = "FirstRun";
   
   public enum TEST_STATUS{
       WAITING,
       TESTING,
       FAILED,
       SUCCEED,
   }    
   private TEST_STATUS wifi_test_status = TEST_STATUS.WAITING;
   private TEST_STATUS sdcard_test_status = TEST_STATUS.WAITING;
   private TEST_STATUS otg_test_status = TEST_STATUS.WAITING;
 
   private View v = null;
   private WindowManager wm = null;
   KeyguardLock kl = null;
   
   private Resources mRes;
   private StorageManager mStorageManager = null;
   
   private int testszie = 21;
   private TextView productname = null;
   private TextView productversion = null;
   private TextView nandstorage = null;
   private TextView availablenandstorage = null;    
 
   private TestView mSDcardTestView = null;
   private TestView mUsbHostTestView = null;
   private TestView mWifiTestView = null;
    private WifiManager mWifiManager;
    private BroadcastReceiver mWifiReceiver;
    private Handler mWifiHandler;
    private boolean mReadyToTest = false;
    private final static int WIFI_MSG_SCAN = 0;
    
    private SensorManager sensorManager;
   private SensorEventListener lsn = null;
   private GsensorBall mGsensorBall;
   private TextView gsensortext = null;
   
   private Camera mCameraDevice;
   private View nocamera;
   private boolean hasCamera = false;
   private ContentProviderClient mMediaProviderClient;
   private SurfaceView mSurfaceView;
   private Button mSwitchBut;
   private SurfaceHolder mSurfaceHolder = null;
   private int mNumberOfCameras = 0;
   private int mCurrentCameraId = 0;
   
   private final static int MAXIMUM_BRIGHTNESS = 255;
   private final static int MINIMUM_BRIGHTNESS = 2;
   private final static int ONE_STAGE = 5;
   
   private ProgressBar progressBar;
   private Button brightnessbut;
   private boolean isTestBrightness = false;
   private BrightnessHandler mBrightnessHandler = new BrightnessHandler();
   private static final int BRIGHTNESS_MSG = 0;
   
   private VUMeter mVUMeter;
   private Button recordtestbutton;
   private TextView recordtext;
   private Recorder mRecorder;
   private AudioManager mAudioManager;
   private int mOldVolume;
   private boolean mSpeakerOn = false;
   private RecordHandler mRecordHandler = new RecordHandler();
   private final static int RECORD_TIME = 5;    
 
   private static final String CURRENT_PATH = "/sys/class/power_supply/battery/capacity";
   PowerBroadcastReceiver mPowerBroadcastReceiver = new PowerBroadcastReceiver();
   private TextView mChargeStatus;
   private TextView mVoltage;
   private TextView mCurrent;
   private TextView mCapacity;
   private TextView mPlug;
   
   private KeyTestView mKeyTestView;
   private String mKeyNames[] = {    "HOME", 
                                   "MENU", 
                                   "VOLUME+", 
                                   "VOLUME-", 
                                   "BACK",
                                   "SEARCH"};
   private int mKeyCodes[] = {        KeyEvent.KEYCODE_HOME,
                                   KeyEvent.KEYCODE_MENU,
                                   KeyEvent.KEYCODE_VOLUME_UP, 
                                   KeyEvent.KEYCODE_VOLUME_DOWN,
                                   KeyEvent.KEYCODE_BACK,
                                   KeyEvent.KEYCODE_SEARCH};
   
   private Button mSingletestBut;
   private Button mClosefirstrunBut;
   
   public static String flash_path = null;
   public static String sdcard_path = null;
   public static String usb_path = null;
   private int flash_pit = 0; 
   private int sdcard_pit = 1; 
   private int usb_pit = 2; 
   private StorageVolume[] storageVolumes = null;
   @Override
   protected void onCreate(Bundle savedInstanceState) {        
       super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
       setContentView(R.layout.firstrun_layout);
       initView();
       InitStorage();
       flash_path = getStoragePath(this, false);//storageVolumes[flash_pit].getPath();
       sdcard_path = getStoragePath(this, true);
       GetDevieInformation();
   }
   private void initView(){
       v = new View(FirstRun.this);
       wm = (WindowManager)FirstRun.this.getSystemService(WINDOW_SERVICE);    
       
        KeyguardManager km= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        kl = km.newKeyguardLock("unLock");
       
       mRes = getResources();
       productname = (TextView)findViewById(R.id.product_name);
       productversion = (TextView)findViewById(R.id.product_version);
       nandstorage = (TextView)findViewById(R.id.nand_storage);
       availablenandstorage = (TextView)findViewById(R.id.available_nand_storage);
       productname.setTextSize(testszie + 2);
       productversion.setTextSize(testszie);
       nandstorage.setTextSize(testszie);
       availablenandstorage.setTextSize(testszie);
       
       mWifiTestView = (TestView)findViewById(R.id.wifitestview);
       mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
       mWifiReceiver = new MyBroadcastReceiver();        
       mWifiHandler = new WifiHandler();
       
       mSDcardTestView = (TestView)findViewById(R.id.sdcardtestview);
       mUsbHostTestView = (TestView)findViewById(R.id.usbhosttestview);
 
       sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
       mGsensorBall = (GsensorBall)findViewById(R.id.gsensorball);
       gsensortext = (TextView)findViewById(R.id.gsensortext);
       lsn = new SensorEventListener() {
           public void onAccuracyChanged(Sensor sensor, int accuracy) {
           }
           public void onSensorChanged(SensorEvent e) {
               float x = e.values[0];
               float y = e.values[1];
               float z = e.values[2];
               mGsensorBall.setXYZ(x, y, z);
               gsensortext.setText(mRes.getString(R.string.GsensorTest) + "\nX: " + x + "\nY: " + y + "\nZ: " + z);
           }
       };
       
       nocamera = findViewById(R.id.nocamera);
       mSurfaceView = (SurfaceView)findViewById(R.id.camera_preview);
       mSwitchBut = (Button)findViewById(R.id.camera_switch_btu);
       mSwitchBut.setOnClickListener(mOnClickListener);
       mNumberOfCameras = Camera.getNumberOfCameras();
       if(mNumberOfCameras <= 1)
           mSwitchBut.setVisibility(View.GONE);
       
       progressBar = (ProgressBar) findViewById(R.id.brightnessBar);
       progressBar.setClickable(false);
       progressBar.setMax(MAXIMUM_BRIGHTNESS);
       brightnessbut = (Button)findViewById(R.id.brightnesstestbut);
       brightnessbut.setOnClickListener(mOnClickListener);
       
       mVUMeter = (VUMeter) findViewById(R.id.uvMeter);
       mRecorder = new Recorder();
       mVUMeter.setRecorder(mRecorder);
       mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
       mOldVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
       recordtestbutton = (Button)findViewById(R.id.recordtestbutton);
       recordtestbutton.setOnClickListener(mOnClickListener);
       recordtext = (TextView)findViewById(R.id.recordtext);        
 
       mChargeStatus = (TextView) findViewById(R.id.chargeStatusText);
       mVoltage = (TextView) findViewById(R.id.voltageText);
       mCurrent = (TextView) findViewById(R.id.currentText);
       mCapacity = (TextView) findViewById(R.id.capacityText);
       mPlug = (TextView) findViewById(R.id.plugText);
       
       mKeyTestView = (KeyTestView)findViewById(R.id.keytestview);
       if(mKeyNames.length == mKeyCodes.length){
           for(int i = 0; i < mKeyNames.length; i ++){
               mKeyTestView.addKey(mKeyNames[i], mKeyCodes[i]);
           }
       }
       mSingletestBut = (Button)findViewById(R.id.singletest);
       mSingletestBut.setOnClickListener(mOnClickListener);
       mClosefirstrunBut = (Button)findViewById(R.id.closefirstrun);
       mClosefirstrunBut.setOnClickListener(mOnClickListener);
   }
   @Override
   protected void onResume() {
       super.onResume();    
        updateMemoryStatus(flash_path);
        kl.disableKeyguard(); 
//        addWindow();
       IntentFilter localIntentFilter = new IntentFilter();
       localIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
       localIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
       localIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
       registerReceiver(mWifiReceiver, localIntentFilter);
       Log.i("Jeffy-------", "try to enable wifi");
       mWifiTestView.setStatus(TEST_STATUS.TESTING);
       mWifiManager.setWifiEnabled(true);
       
       mSDcardTestView.setStatus(TEST_STATUS.TESTING);
       if(testSdcard()){
           mSDcardTestView.setStatus(TEST_STATUS.SUCCEED);
       }else{
           mSDcardTestView.setStatus(TEST_STATUS.FAILED);
       }
 
       mUsbHostTestView.setStatus(TEST_STATUS.TESTING);
       if(testUSBHost()){
           mUsbHostTestView.setStatus(TEST_STATUS.SUCCEED);
       }else{
           mUsbHostTestView.setStatus(TEST_STATUS.FAILED);
       }        
 
       Sensor sensors = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
       sensorManager.registerListener(lsn, sensors, SensorManager.SENSOR_DELAY_NORMAL);
       
       SurfaceHolder holder = mSurfaceView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       
       registerReceiver(mPowerBroadcastReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
   }
   @Override
   protected void onPause() {
       super.onPause();
   //    removeWindow();
        kl.reenableKeyguard();
       unregisterReceiver(mWifiReceiver);
       unregisterReceiver(mPowerBroadcastReceiver);
       sensorManager.unregisterListener(lsn);
       mWifiHandler.removeMessages(WIFI_MSG_SCAN);
       Log.d(TAG, " __________________---------- oldBrightness = " + oldBrightness);
       
       switch (mRecorder.state()) {
           case Recorder.IDLE_STATE:
               mRecorder.delete();
               break;
           case Recorder.PLAYING_STATE:
               mRecorder.stop();
               mRecorder.delete();
               break;
           case Recorder.RECORDING_STATE:
               mRecorder.stop();
               mRecorder.clear();
               break;
       }
       mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mOldVolume, 0);
       if (mSpeakerOn) {
           mAudioManager.setSpeakerphoneOn(false);
       }
//        unregisterReceiver(mPowerBroadcastReceiver);
   }
   
   @Override
   protected void onDestroy() {
        if (mStorageManager != null && mStorageListener != null) {
            mStorageManager.unregisterListener(mStorageListener);
        }
        if(mCameraDevice != null)
            mCameraDevice.release();
       super.onDestroy();
   }
   
   /******************************************************************
    * about DeviceInfo
    */
   private void GetDevieInformation(){
       productname.setText(Build.DEVICE);
       productversion.setText(mRes.getString(R.string.Firmwareversion) + Build.DISPLAY);
   }
 
   /******************************************************************
    * about DeviceStorage()
    */
   private void InitStorage(){
       if (mStorageManager == null) {
            mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
            mStorageManager.registerListener(mStorageListener);
            storageVolumes = mStorageManager.getVolumeList();
            if(storageVolumes.length >= 3){
                flash_path = storageVolumes[flash_pit].getPath();
                sdcard_path = storageVolumes[sdcard_pit].getPath();
                usb_path = storageVolumes[usb_pit].getPath();
                Log.d(TAG, " _____ " + flash_path + "   " + sdcard_path + "   " + usb_path);
            }else{
                flash_path = getStoragePath(this, false);
           sdcard_path = getStoragePath(this, true);
       usb_path= "/mnt/usb_storage";
       
       }
 
            /*flash_path = "/mnt/internal_sd";
            sdcard_path = "/mnt/external_sd";
            usb_path = "/mnt/usb_storage";*/
        }
   }    
   private void updateMemoryStatus(String path) {
        String status = mStorageManager.getVolumeState(path);
        if (path.equals(flash_path)) {
            status = mStorageManager.getVolumeState(flash_path);
        }
        String readOnly = "";
        if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
            status = Environment.MEDIA_MOUNTED;
            readOnly = mRes.getString(R.string.read_only);
        }
 
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            try {
                //File path = Environment.getExternalStorageDirectory();
                StatFs stat = new StatFs(path);
                long blockSize = stat.getBlockSize();
                long totalBlocks = stat.getBlockCount();
                long availableBlocks = stat.getAvailableBlocks();
 
                if (path.equals(flash_path)) {
                    nandstorage.setText(mRes.getString(R.string.total_space) + " : " + formatSize(totalBlocks * blockSize));
                    availablenandstorage.setText(mRes.getString(R.string.available_space) + " : " + formatSize(availableBlocks * blockSize) + readOnly);
                }
            } catch (IllegalArgumentException e) {
                status = Environment.MEDIA_REMOVED;
            }            
        } else {
            if (path.equals(flash_path)) {
                nandstorage.setText(mRes.getString(R.string.nand_unavailable));
                availablenandstorage.setText(mRes.getString(R.string.nand_unavailable));
            }
        }
    }
   private String formatSize(long size) {
        return Formatter.formatFileSize(this, size);
    }
   StorageEventListener mStorageListener = new StorageEventListener() {
        @Override
        public void onStorageStateChanged(String path, String oldState, String newState) {
            Log.d(TAG, "--------------->>>>> Received storage state changed, path = " +
                    path + ",      changed state from " + oldState +
                    " to " + newState);
            if (path.equals(flash_path) 
                             && !newState.equals(Environment.MEDIA_MOUNTED)) {
            } else {
                updateMemoryStatus(flash_path);
            }
            if(path.equals(sdcard_path)){
                if(testSdcard()){
                    mSDcardTestView.setStatus(TEST_STATUS.SUCCEED);
                }else{
                    mSDcardTestView.setStatus(TEST_STATUS.FAILED);
                }
            }else if(path.equals(usb_path)){
                if(testUSBHost()){
                    mUsbHostTestView.setStatus(TEST_STATUS.SUCCEED);
                }else{
                    mUsbHostTestView.setStatus(TEST_STATUS.FAILED);
                }
            }
        }
    };
 
    
   /******************************************************************
    * about WiFitest
    */    
    class WifiHandler extends Handler {
       public void handleMessage(Message msg) {
           switch(msg.what){
           case WIFI_MSG_SCAN:
               removeMessages(WIFI_MSG_SCAN);
               mWifiManager.startScan();
               break;
           }
       }
    }
    class MyBroadcastReceiver extends BroadcastReceiver {
       public void onReceive(Context context, Intent intent) {
           String action = intent.getAction();
           Log.i("Jeffy", "action:" + action);
           if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
               int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                       WifiManager.WIFI_STATE_UNKNOWN);
               if (state == WifiManager.WIFI_STATE_ENABLED) {
                   mWifiHandler.sendEmptyMessage(WIFI_MSG_SCAN);
               }
           }
 
           if (WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION.equals(action)) {
               boolean connected = intent.getBooleanExtra(
                       WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
               if (connected && mReadyToTest) {
                   Log.d("Jeffy===", "already connect to:" + mWifiManager.getConnectionInfo().getSSID());
//                    mHandler.sendEmptyMessageDelayed(MSG_FINISH_TEST, 1000);
                   mWifiTestView.setStatus(TEST_STATUS.SUCCEED);
               }
           }
           if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
               List<ScanResult> resultList = mWifiManager.getScanResults();
               if ((resultList != null) && (!resultList.isEmpty())) {
                   mReadyToTest = true;
                   Log.d("Jeffy===", " ____________-------- resultList.size() = " + resultList.size());
                   mWifiTestView.setStatus(TEST_STATUS.SUCCEED);
               }
           }
 
       }
   }    
 
   /******************************************************************
    * about SDcard
    */
    private static final String TEST_STRING = "Rockchip UsbHostTest File";
    public boolean testSdcard() {
        try {
            String externalVolumeState = mStorageManager.getVolumeState(sdcard_path);
 
            Log.d(TAG, " __________----------- testSdcard(),    externalVolumeState = " + externalVolumeState);
            if (!externalVolumeState.equals(Environment.MEDIA_MOUNTED)) {
                return false;
            }
        } catch (Exception rex) {
            rex.printStackTrace();
            //test sdcard fail
            return false;
        }
        Log.d(TAG, " __________----------- testSdcard() __ begin test read and write");
        return testReadAndWrite(sdcard_path + "/test");
    }
    public boolean testReadAndWrite(String directoryName) {
       return dotestReadAndWrite(directoryName);
    }
    private boolean dotestReadAndWrite(String directoryName) {
        File directory = new File(directoryName);
        Log.d(TAG, " _______-------- dotestReadAndWrite()0, directoryName = " + directoryName);
        if (!directory.isDirectory()) {
            if (!directory.mkdirs()) {
                Log.d(TAG, " _______-------- dotestReadAndWrite()0 1, directoryName = " + directoryName);
                return false;
            }
        }
        File f = new File(directoryName, "storagetest.txt");
        try {
            if (f.exists()) {
                f.delete();
            }
            if (!f.createNewFile()) {
                return false;
            } else {
                doWriteFile(f.getAbsoluteFile().toString());
                if (!doReadFile(f.getAbsoluteFile().toString()).equals(TEST_STRING)) {
                    return false;
                }
            }
            f.delete();
            return true;
        } catch (IOException ex) {
            Log.e(TAG, "isWritable : false (IOException)!");
            return false;
        }
    }
 
    public void doWriteFile(String filename) {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filename));
            osw.write(TEST_STRING, 0, TEST_STRING.length());
            osw.flush();
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String doReadFile(String filename) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader
                    (new FileInputStream(filename)));
            String data = null;
            StringBuilder temp = new StringBuilder();
            while ((data = br.readLine()) != null) {
                temp.append(data);
            }
            br.close();
            Log.e(TAG, "Readfile " + temp.toString());
            return temp.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
   /******************************************************************
    * about USBHost
    */
    public boolean testUSBHost() {
        try {
            String externalVolumeState = mStorageManager.getVolumeState(usb_path).toString();
 
            Log.d(TAG, " __________----------- testUSBHost(),    externalVolumeState = " + externalVolumeState);
            if (!externalVolumeState.equals(Environment.MEDIA_MOUNTED) && !externalVolumeState.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
                return false;
            }
        } catch (Exception rex) {
            rex.printStackTrace();
            return false;
        }
        Log.d(TAG, " __________----------- testUSBHost() __ begin test read and write");
        return testReadAndWrite(usb_path + "/test");
    }
    
   /******************************************************************
    * about Camera
    */
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {    
        Log.d(TAG, "---->>>>>>>>>> surfaceChanged()");
       if (holder.getSurface() == null) {
            Log.d(TAG, "---- surfaceChanged(),  holder.getSurface() == null");
            return;
        }
       mSurfaceHolder = holder;
       try{
           hasCamera = true;
           nocamera.setVisibility(View.GONE);
           mCameraDevice = Camera.open(mCurrentCameraId);
       }catch(Exception e){
           hasCamera = false;
           nocamera.setVisibility(View.VISIBLE);
           mSwitchBut.setVisibility(View.GONE);
           Log.e(TAG, " ____________- camera error");
           return;
       }
       try{
           mCameraDevice.setPreviewDisplay(mSurfaceHolder);
       }catch(IOException e){
           e.printStackTrace();
       }
       mCameraDevice.startPreview();
   }
   public void surfaceCreated(SurfaceHolder holder) {
   }
   public void surfaceDestroyed(SurfaceHolder holder) {
       if(mCameraDevice != null){
           mCameraDevice.stopPreview();
           mCameraDevice.release();
       }
       mSurfaceHolder = null;
   }
   OnClickListener mOnClickListener = new OnClickListener(){
       public void onClick(View arg0) {
           switch(arg0.getId()){
           case R.id.camera_switch_btu:
               mCameraDevice.stopPreview();
               mCameraDevice.release();
               mCameraDevice = null;
               mCurrentCameraId = (mCurrentCameraId + 1) % mNumberOfCameras;
               mCameraDevice = Camera.open(mCurrentCameraId);
               mSurfaceHolder = null;
               mSurfaceView.setVisibility(View.GONE);
               mSurfaceView.setVisibility(View.VISIBLE);
               break;
           case R.id.brightnesstestbut:
               if(isTestBrightness){
                   mBrightnessHandler.removeMessages(0);
                   setBrightness(oldBrightness);
                   brightnessbut.setText(R.string.BrightnessTitle);
               }else{
                   mBrightnessHandler.sendEmptyMessage(0);
                   brightnessbut.setText(R.string.BrightnessTitleStop);
               }
               isTestBrightness = !isTestBrightness;
               break;
           case R.id.recordtestbutton:
               mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 
                       mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
               mSpeakerOn = mAudioManager.isSpeakerphoneOn();
               if (!mSpeakerOn) {
                   mAudioManager.setSpeakerphoneOn(true);
               }
               mRecordHandler.sendEmptyMessage(MSG_TEST_MIC_START);
               recordtestbutton.setEnabled(false);
               break;
           case R.id.singletest:
               Intent tmp = new Intent(FirstRun.this, DeviceTest.class);
               FirstRun.this.startActivity(tmp);
               break;
           case R.id.closefirstrun:
               FirstRun.this.finish();
               break;
           default:
               break;
           }
       }        
   };    
 
   /******************************************************************
    * about brightness
    */
   int mBrightness = 30;
   int oldBrightness = 200;
   boolean increase = true;
   private void setBrightness(int paramInt) {
       WindowManager.LayoutParams lp = getWindow().getAttributes();
       float brightness = (float) paramInt / MAXIMUM_BRIGHTNESS;
       lp.screenBrightness = brightness;
       getWindow().setAttributes(lp);
   }    
   class BrightnessHandler extends Handler {
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
           int delay = 25;
           if (increase) {
               mBrightness += ONE_STAGE;
               if (mBrightness >= MAXIMUM_BRIGHTNESS) {
                   mBrightness = MAXIMUM_BRIGHTNESS;
                   increase = false;
                   delay = 200;
               }
           } else {
               mBrightness -= ONE_STAGE;
               if (mBrightness <= MINIMUM_BRIGHTNESS) {
                   mBrightness = MINIMUM_BRIGHTNESS;
                   increase = true;
                   delay = 200;
               }
           }
           progressBar.setProgress(mBrightness);
           setBrightness(mBrightness);            
           sendEmptyMessageDelayed(0, delay);
       }
   }
   
   /******************************************************************
    * about mic
    */
   int mTimes;
   private static final int MSG_TEST_MIC_ING = 0;
   private static final int MSG_TEST_MIC_OVER = 1;
   private static final int MSG_TEST_MIC_START = 2;
   class RecordHandler extends Handler {
       @Override
       public void handleMessage(Message msg) {
           switch (msg.what) {
           default:
           case MSG_TEST_MIC_START:
               removeMessages(MSG_TEST_MIC_START);
               mTimes = RECORD_TIME;
               recordtext.setText("  "+mTimes+" ");
               mRecorder.startRecording(3, ".amr");
               sendEmptyMessageDelayed(MSG_TEST_MIC_ING, 1000L);
               break;
           case MSG_TEST_MIC_ING:
               if (mTimes > 0) {
                   recordtext.setText("  "+mTimes+" ");
                   mTimes--;
                   Log.i(TAG, "mTimes=" + mTimes);
                   sendEmptyMessageDelayed(MSG_TEST_MIC_ING, 1000L);
               } else {
                   removeMessages(MSG_TEST_MIC_ING);
                   sendEmptyMessage(MSG_TEST_MIC_OVER);
 
               }
               break;
           case MSG_TEST_MIC_OVER:
               removeMessages(MSG_TEST_MIC_OVER);
               mRecorder.stopRecording();
               if (mRecorder.sampleLength() > 0) {
                   recordtext.setText(R.string.HeadsetRecodrSuccess);
                   mRecorder.startPlayback();
               } else {
                   recordtext.setText(R.string.RecordError);
               }
               recordtestbutton.setEnabled(true);
               break;
           }            
           mVUMeter.invalidate();
       }
   }
 
   /******************************************************************
    * about Power
    */
   class PowerBroadcastReceiver extends BroadcastReceiver {
       public void onReceive(Context paramContext, Intent intent) {
           String action = intent.getAction();
           if (!Intent.ACTION_BATTERY_CHANGED.equals(action)) {
               return;
           }
           int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
           int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
           int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
           int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
           int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
           Log.e("Jeffy", "plugged:" + plugged);
           int current = -1;
           try {
               String currentStr = SystemUtil.execScriptCmd("cat "
                       + CURRENT_PATH, DeviceTest.TEMP_FILE_PATH, true);
               if (currentStr.length() > 0) {
                   current = Integer.parseInt(currentStr);
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
           String statusString = "";
           switch (status) {
           case BatteryManager.BATTERY_STATUS_UNKNOWN:
               statusString = "Unknown";
               break;
           case BatteryManager.BATTERY_STATUS_CHARGING:
               statusString = "Charging";
               break;
           case BatteryManager.BATTERY_STATUS_DISCHARGING:
               statusString = "Discharging";
               break;
           case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
               statusString = "Not Charging";
               break;
           case BatteryManager.BATTERY_STATUS_FULL:
               statusString = "Full";
               break;
           }
           mChargeStatus.setText(getString(R.string.ChargeState) + statusString);
           mVoltage.setText(getString(R.string.Voltage) + voltage + "mV");
           if (current != -1) {
               mCurrent.setText("Current:" + (current / 1000) + "mA");
           } else {
               mCurrent.setVisibility(View.GONE);
           }
           mCapacity.setText(getString(R.string.Capacity) + (level * 100 / scale) + "%");
           boolean acPlugin = false;
           String pluggedStr = "";
           switch (plugged) {
           case BatteryManager.BATTERY_PLUGGED_AC:
               acPlugin = true;
               pluggedStr = "AC";
               break;
           case BatteryManager.BATTERY_PLUGGED_USB:
               pluggedStr = "USB";
               break;
           default:
               pluggedStr = "Unplugged";
               break;
           }
           mPlug.setText(getString(R.string.Plug) + pluggedStr);
       }
   }
 
 
   /******************************************************************
    * about Key
    */
   @Override
   public boolean dispatchKeyEvent(KeyEvent event) {
       Log.d(TAG, " _____________---- dispatchKeyEvent(),   " + event.getKeyCode());
       switch(event.getAction()){
       case KeyEvent.ACTION_DOWN:
           mKeyTestView.setKeyDown(event.getKeyCode());
           break;
       case KeyEvent.ACTION_UP:
           mKeyTestView.setKeyUp(event.getKeyCode());
           mKeyTestView.requestFocus();
           mKeyTestView.requestFocusFromTouch();
           if(event.getKeyCode()== KeyEvent.KEYCODE_HOME){
                Log.d(TAG,getWindow().getAttributes().type+ " _____________---- onKeyEEEE(),   " + event.getKeyCode());
                return true;
               }else if(event.getKeyCode()== KeyEvent.KEYCODE_BACK){
                   Log.d(TAG,getWindow().getAttributes().type+ " _____________---- onKeyEEEE(),   " + event.getKeyCode());
                   return true;
               }else if(event.getKeyCode()== KeyEvent.KEYCODE_MENU){
                   Log.d(TAG,getWindow().getAttributes().type+ " _____________---- onKeyEEEE(),   " + event.getKeyCode());
                   mKeyTestView.setFocusable(true);
                   mKeyTestView.setFocusableInTouchMode(true);
                 mKeyTestView.requestFocus();
 
                   return true;
               }
           break;
       }
       return super.dispatchKeyEvent(event);
 
   }
   @Override
   public void onAttachedToWindow() {
       Log.d(TAG, "onAttachedToWindow");
       Log.d(TAG, "____________________________ ____________ onAttachedToWindow type: "  + getWindow().getAttributes().type);
//        getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
////        getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
//        getWindow().addFlags(FLAG_KEEP_SCREEN_ON);
       super.onAttachedToWindow();
   }
   private void addWindow(){
       WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_KEYGUARD;
//         params.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
//        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
//                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
//         params.flags = WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
//                 |  WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
       params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
               | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
       params.width = 0;//WindowManager.LayoutParams.FILL_PARENT;
       params.height = 0;//WindowManager.LayoutParams.FILL_PARENT;
       params.format=PixelFormat.TRANSLUCENT;                
       params.gravity=Gravity.LEFT|Gravity.TOP;
      //����Ļ���Ͻ�Ϊԭ�㣬����x��y��ʼֵ
       params.x = 0;
       params.y = 0;
       
       wm.addView(v, params);
       v.requestFocus();
       v.setOnKeyListener(new OnKeyListener(){
           public boolean onKey(View v, int keyCode, KeyEvent event) {
               switch(event.getAction()){
               case KeyEvent.ACTION_DOWN:
                   mKeyTestView.setKeyDown(event.getKeyCode());
                   break;
               case KeyEvent.ACTION_UP:
                   mKeyTestView.setKeyUp(event.getKeyCode());
                   if(keyCode== KeyEvent.KEYCODE_HOME){
                     Log.d(TAG,getWindow().getAttributes().type+ " _____________---- onKeyEEEE(),   " + event.getKeyCode());
                     return true;
                    }
                   
                   break;
               
                }
               return false;
           }
       });        
   }    
   private void removeWindow(){
       wm.removeView(v);
   }
    private static String getStoragePath(Context mContext, boolean is_removale) {  
 
          StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
            Class<?> storageVolumeClazz = null;
            try {
                storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
                Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
                Method getPath = storageVolumeClazz.getMethod("getPath");
                Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
                Object result = getVolumeList.invoke(mStorageManager);
                final int length = Array.getLength(result);
                for (int i = 0; i < length; i++) {
                    Object storageVolumeElement = Array.get(result, i);
                    String path = (String) getPath.invoke(storageVolumeElement);
                    boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
                    if (is_removale == removable) {
                        return path;
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return null;
    }
}