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
package com.DeviceTest;
 
import java.io.File;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StatFs;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
import com.DeviceTest.helper.ControlButtonUtil;
import com.DeviceTest.helper.Recorder;
import com.DeviceTest.helper.VUMeter;
 
public class PhoneMicTestActivity extends Activity implements OnClickListener{
   private static final String TAG = PhoneMicTestActivity.class
           .getSimpleName();
   
   private final static String ERRMSG = "Record error";
   private final static int RECORD_TIME = 3;
   private static final int MSG_TEST_MIC_ING = 8738;
   private static final int MSG_TEST_MIC_OVER = 13107;
   private static final int MSG_TEST_MIC_START = 4369;
   private boolean isSDcardTestOk = false;
   private AudioManager mAudioManager;
   private Handler mHandler;
   private int mOldVolume;
   private Recorder mRecorder;
   private TextView mResult;
   boolean mSpeakerOn = false;
   private TextView mText;
   int mTimes;
   TextView mTitle;
   private Button mBtnRetest;
   private VUMeter mVUMeter;
 
   public PhoneMicTestActivity() {
       this.mHandler = new MyHandler();
   }
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
 
       super.onCreate(savedInstanceState);
 
       requestWindowFeature(Window.FEATURE_NO_TITLE);
 
       getWindow().addFlags(1152);
       setContentView(R.layout.phonemictest);
 
       mVUMeter = (VUMeter) findViewById(R.id.uvMeter);
       this.mResult = (TextView) findViewById(R.id.phoneresultText);
       this.mResult.setVisibility(View.VISIBLE);
       this.mResult.setGravity(17);
       ControlButtonUtil.initControlButtonView(this);
       mBtnRetest = (Button)findViewById(R.id.btn_retest);
       mBtnRetest.setOnClickListener(this);
       mBtnRetest.setEnabled(false);
       this.mRecorder = new Recorder();
       this.mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
       mVUMeter.setRecorder(mRecorder);
   }
 
   @Override
   protected void onResume() {
 
       super.onResume();
 
       this.isSDcardTestOk = false;
       if (!Environment.getExternalStorageState().equals(
               Environment.MEDIA_MOUNTED)) {
           this.mResult.setText(R.string.InsertSdCard);
           return;
       }
 
       if (!isSDcardHasSpace()) {
           this.mResult.setText(R.string.SdCardNospace);
           stopMediaPlayBack();
           return;
       }
       stopMediaPlayBack();
       this.isSDcardTestOk = true;
 
       this.mOldVolume = this.mAudioManager
               .getStreamVolume(AudioManager.STREAM_MUSIC);
       int maxVolume = this.mAudioManager
               .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
       this.mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
               maxVolume, 0);
 
       this.mSpeakerOn = mAudioManager.isSpeakerphoneOn();
 
       if (!this.mSpeakerOn) {
           this.mAudioManager.setSpeakerphoneOn(true);
       }
       this.mHandler.sendEmptyMessage(MSG_TEST_MIC_START);
 
   }
 
   @Override
   protected void onPause() {
 
       super.onPause();
 
       if (this.isSDcardTestOk) {
 
           switch (this.mRecorder.state()) {
 
           case Recorder.IDLE_STATE:
               this.mRecorder.delete();
               break;
           case Recorder.PLAYING_STATE:
               this.mRecorder.stop();
               this.mRecorder.delete();
               break;
           case Recorder.RECORDING_STATE:
               this.mRecorder.stop();
               this.mRecorder.clear();
               break;
           }
 
           
           mAudioManager.setStreamVolume(3, mOldVolume, 0);
             
           if (mSpeakerOn) {
               mAudioManager.setSpeakerphoneOn(false);
 
           }
       }
 
   }
 
   public void stopMediaPlayBack() {
       Intent localIntent = new Intent("com.android.music.musicservicecommand");
       localIntent.putExtra("command", "pause");
       sendBroadcast(localIntent);
   }
 
   public boolean isSDcardHasSpace() {
       File pathFile = android.os.Environment.getExternalStorageDirectory();
 
       StatFs statfs = new StatFs(pathFile.getPath());
 
       if (statfs.getAvailableBlocks() > 1) {
 
           return true;
 
       }
 
       return false;
 
   }
 
   class MyHandler extends Handler {
       MyHandler() {
       }
 
       @Override
       public void handleMessage(Message msg) {
 
           
           switch (msg.what) {
           default:
           case MSG_TEST_MIC_START:
 
               removeMessages(MSG_TEST_MIC_START);
               mTimes = RECORD_TIME;
 
               mResult.setText("  "+mTimes+" ");
               mRecorder.startRecording(3, ".amr");
               sendEmptyMessageDelayed(MSG_TEST_MIC_ING, 1000L);
               break;
           case MSG_TEST_MIC_ING:
 
               
 
               if (mTimes > 0) {
 
                   mResult.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) {
                   mResult.setText(R.string.HeadsetRecodrSuccess);
                   mRecorder.startPlayback();
               } else {
                   mResult.setText(R.string.RecordError);
               }
               mBtnRetest.setEnabled(true);
               break;
           }
           
           mVUMeter.invalidate();
       }
 
   }
 
    public void onClick(View v) {
        switch (this.mRecorder.state()) {
 
            case Recorder.IDLE_STATE:
                this.mRecorder.delete();
                break;
            case Recorder.PLAYING_STATE:
                this.mRecorder.stop();
                this.mRecorder.delete();
                break;
        }
        mRecorder.stopPlayback();
        mBtnRetest.setEnabled(false);
        this.mHandler.sendEmptyMessage(MSG_TEST_MIC_START);
        
    }
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
}