1: package com.proxia.securitydemo;
2:
3: import android.app.Activity;
4: import android.widget.LinearLayout;
5: import android.os.Bundle;
6: import android.os.Environment;
7: import android.view.Menu;
8: import android.view.MenuInflater;
9: import android.view.MenuItem;
10: import android.view.ViewGroup;
11: import android.widget.Button;
12: import android.view.View;
13: import android.view.View.OnClickListener;
14: import android.content.Context;
15: import android.content.Intent;
16: import android.util.Log;
17: import android.media.MediaRecorder;
18: import android.media.MediaPlayer;
19: import java.io.IOException;
20:
21: public class AudioRecordTest extends Activity {
22: private static final String LOG_TAG = "AudioRecordTest";
23: private static String mFileName = null;
24: private RecordButton mRecordButton = null;
25: private MediaRecorder mRecorder = null;
26: private PlayButton mPlayButton = null;
27: private MediaPlayer mPlayer = null;
28:
29: private void onRecord(boolean start) {
30: if (start) {
31: startRecording();
32: } else {
33: stopRecording();
34: }
35: }
36:
37: private void onPlay(boolean start) {
38: if (start) {
39: startPlaying();
40: } else {
41: stopPlaying();
42: }
43: }
44:
45: private void startPlaying() {
46: mPlayer = new MediaPlayer();
47: try {
48: mPlayer.setDataSource(mFileName);
49: mPlayer.prepare();
50: mPlayer.start();
51: } catch (IOException e) {
52: Log.e(LOG_TAG, "prepare() failed");
53: }
54: }
55:
56: private void stopPlaying() {
57: mPlayer.release();
58: mPlayer = null;
59: }
60:
61: private void startRecording() {
62: mRecorder = new MediaRecorder();
63: mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
64: mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
65: mRecorder.setOutputFile(mFileName);
66: mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
67: try {
68: mRecorder.prepare();
69: } catch (IOException e) {
70: Log.e(LOG_TAG, "prepare() failed");
71: }
72: mRecorder.start();
73: }
74:
75: private void stopRecording() {
76: mRecorder.stop();
77: mRecorder.release();
78: mRecorder = null;
79: }
80:
81: class RecordButton extends Button {
82: boolean mStartRecording = true;
83: OnClickListener clicker = new OnClickListener() {
84: public void onClick(View v) {
85: onRecord(mStartRecording);
86: if (mStartRecording) {
87: setText("Stop recording");
88: } else {
89: setText("Start recording");
90: }
91: mStartRecording = !mStartRecording;
92: }
93: };
94:
95: public RecordButton(Context ctx) {
96: super(ctx);
97: setText("Start recording");
98: setOnClickListener(clicker);
99: }
100: }
101:
102: class PlayButton extends Button {
103: boolean mStartPlaying = true;
104: OnClickListener clicker = new OnClickListener() {
105: public void onClick(View v) {
106: onPlay(mStartPlaying);
107: if (mStartPlaying) {
108: setText("Stop playing");
109: } else {
110: setText("Start playing");
111: }
112: mStartPlaying = !mStartPlaying;
113: }
114: };
115:
116: public PlayButton(Context ctx) {
117: super(ctx);
118: setText("Start playing");
119: setOnClickListener(clicker);
120: }
121: }
122:
123: public AudioRecordTest() {
124: mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
125: mFileName += "/audiorecordtest.3gp";
126: }
127:
128: @Override
129: public void onCreate(Bundle icicle) {
130: super.onCreate(icicle);
131: LinearLayout ll = new LinearLayout(this);
132: mRecordButton = new RecordButton(this);
133: ll.addView(mRecordButton,
134: new LinearLayout.LayoutParams(
135: ViewGroup.LayoutParams.WRAP_CONTENT,
136: ViewGroup.LayoutParams.WRAP_CONTENT,
137: 0));
138: mPlayButton = new PlayButton(this);
139: ll.addView(mPlayButton,
140: new LinearLayout.LayoutParams(
141: ViewGroup.LayoutParams.WRAP_CONTENT,
142: ViewGroup.LayoutParams.WRAP_CONTENT,
143: 0));
144: setContentView(ll);
145: }
146:
147: @Override
148: public void onPause() {
149: super.onPause();
150: if (mRecorder != null) {
151: mRecorder.release();
152: mRecorder = null;
153: }
154: if (mPlayer != null) {
155: mPlayer.release();
156: mPlayer = null;
157: }
158: }
159:
160: public boolean onPrepareOptionsMenu(Menu menu)
161: {
162: super.onPrepareOptionsMenu(menu);
163:
164: return true;
165: }
166:
167: // ------------------------------------------------------
168: // ------------------------------------------------------
169: @Override
170: public boolean onCreateOptionsMenu(Menu menu)
171: {
172: super.onCreateOptionsMenu(menu);
173:
174: try
175: {
176: MenuInflater inflater = getMenuInflater();
177:
178: inflater.inflate(R.menu.securitydemo, menu);
179:
180: inflater = null;
181: }
182: catch (Exception e)
183: {
184: }
185:
186: return true;
187: }
188:
189: // ------------------------------------------------------
190: // ------------------------------------------------------
191: @Override
192: public boolean onOptionsItemSelected(MenuItem item)
193: {
194: switch (item.getItemId())
195: {
196: case R.id.menu_mail:
197: {
198: Intent intent = new Intent(this, SendEmailDemo.class);
199: this.startActivity(intent);
200: finish();
201: return true;
202: }
203:
204: case R.id.menu_sd:
205: {
206: Intent intent = new Intent(this, WriteSDDemo.class);
207: this.startActivity(intent);
208: finish();
209: return true;
210: }
211:
212: case R.id.menu_sms:
213: {
214: Intent intent = new Intent(this, SecurityDemo.class);
215: this.startActivity(intent);
216: finish();
217: return true;
218: }
219:
220: case R.id.menu_picture:
221: {
222: Intent intent = new Intent(this, CameraDemo.class);
223: this.startActivity(intent);
224: finish();
225: return true;
226: }
227:
228: case R.id.menu_audio:
229: {
230: return true;
231: }
232: }
233:
234: return false;
235: }
236:
237:
238: }