PageRenderTime 85ms CodeModel.GetById 73ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/ItemID/src/com/ideal/itemid/VoiceRecorderRecordingView.java

http://eyes-free.googlecode.com/
Java | 137 lines | 105 code | 13 blank | 19 comment | 3 complexity | d1d842abea25f8383fbcad0634b2c533 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 The IDEAL Group
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.ideal.itemid;
 18
 19import android.content.Context;
 20import android.graphics.Canvas;
 21import android.graphics.Color;
 22import android.graphics.Paint;
 23import android.graphics.Typeface;
 24import android.media.MediaRecorder;
 25import android.view.MotionEvent;
 26import android.widget.TextView;
 27
 28import java.io.File;
 29import java.io.IOException;
 30
 31/**
 32 * The main View for IDEAL Item ID Label Maker. Handles the touch
 33 * events and does the recording.
 34 */
 35public class VoiceRecorderRecordingView extends TextView {
 36    public VoiceRecorderActivity parent;
 37
 38    private MediaRecorder recorder;
 39
 40    boolean screenIsBeingTouched = false;
 41
 42    public VoiceRecorderRecordingView(Context context) {
 43        super(context);
 44        parent = (VoiceRecorderActivity) context;
 45        setClickable(true);
 46        setFocusable(true);
 47        setFocusableInTouchMode(true);
 48        requestFocus();
 49        screenIsBeingTouched = false;
 50        parent.mTts
 51                .speak(
 52                        "Touch the screen, speak your message, and let go of the screen when you are done.",
 53                        0, null);
 54    }
 55
 56    @Override
 57    public boolean onTouchEvent(MotionEvent event) {
 58        int action = event.getAction();
 59        long[] pattern = {
 60                0, 1, 40, 41
 61        };
 62        switch (action) {
 63            case MotionEvent.ACTION_DOWN:
 64                screenIsBeingTouched = true;
 65                parent.mTts.playEarcon("[tock]", 0, null);
 66                parent.mVibe.vibrate(pattern, -1);
 67                startRecording();
 68                break;
 69            case MotionEvent.ACTION_UP:
 70                saveRecording();
 71                parent.mTts.playEarcon("[tock]", 0, null);
 72                parent.mVibe.vibrate(pattern, -1);
 73                parent.showConfirmationView();
 74                break;
 75            default:
 76                break;
 77        }
 78        invalidate();
 79        return true;
 80    }
 81
 82    private void startRecording() {
 83        File outputDir = new File("/sdcard/idealItemId/");
 84        outputDir.mkdirs();
 85        recorder = new MediaRecorder();
 86        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 87        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 88        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 89        recorder.setOutputFile("/sdcard/idealItemId/" + parent.FILE_TIMESTAMP + ".amr");
 90        try {
 91            recorder.prepare();
 92            recorder.start();
 93        } catch (IllegalStateException e) {
 94            e.printStackTrace();
 95        } catch (IOException e) {
 96            e.printStackTrace();
 97        }
 98    }
 99
100    private void saveRecording() {
101        recorder.stop();
102        recorder.release();
103    }
104
105    @Override
106    public void onDraw(Canvas canvas) {
107        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
108        paint.setColor(Color.WHITE);
109        paint.setTextAlign(Paint.Align.CENTER);
110        paint.setTypeface(Typeface.DEFAULT_BOLD);
111
112        int x = getWidth() / 2;
113        int y = (getHeight() / 2) - 35;
114        if (!screenIsBeingTouched) {
115            x = 5;
116            y = getHeight() - 40;
117            paint.setTextSize(20);
118            paint.setTextAlign(Paint.Align.LEFT);
119            y -= paint.ascent() / 2;
120            canvas.drawText("Touch screen to record.", x, y, paint);
121            x = 5;
122            y = getHeight() - 20;
123            paint.setTextSize(20);
124            paint.setTextAlign(Paint.Align.LEFT);
125            y -= paint.ascent() / 2;
126            canvas.drawText("Lift up when done.", x, y, paint);
127        } else {
128            x = 5;
129            y = getHeight() - 40;
130            paint.setTextSize(20);
131            paint.setTextAlign(Paint.Align.LEFT);
132            y -= paint.ascent() / 2;
133            canvas.drawText("Lift up when you are done.", x, y, paint);
134        }
135    }
136
137}