PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/remindme/src/com/google/marvin/remindme/RecordReminderView.java

http://eyes-free.googlecode.com/
Java | 131 lines | 95 code | 16 blank | 20 comment | 3 complexity | 5239ed66a0ff8622a23fe25ee30df286 MD5 | raw file
  1/*
  2 * Copyright (C) 2008 Google Inc.
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * 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, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.google.marvin.remindme;
 18
 19
 20import java.io.File;
 21
 22import android.content.Context;
 23import android.graphics.Canvas;
 24import android.graphics.Color;
 25import android.graphics.Paint;
 26import android.graphics.Typeface;
 27import android.media.MediaRecorder;
 28import android.os.Vibrator;
 29import android.view.MotionEvent;
 30import android.widget.TextView;
 31
 32/**
 33 * Implements the user interface for doing slide dialing.
 34 * 
 35 * @author clchen@google.com (Charles L. Chen) Created 8-2-2008
 36 */
 37public class RecordReminderView extends TextView {
 38  public RemindMe parent;
 39  private Vibrator vibe;
 40  private MediaRecorder recorder;
 41
 42
 43  boolean screenIsBeingTouched = false;
 44
 45  public RecordReminderView(Context context) {
 46    super(context);
 47    parent = (RemindMe) context;
 48    vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
 49    setClickable(true);
 50    setFocusable(true);
 51    setFocusableInTouchMode(true);
 52    requestFocus();
 53    screenIsBeingTouched = false;
 54  }
 55
 56
 57  @Override
 58  public boolean onTouchEvent(MotionEvent event) {
 59    int action = event.getAction();
 60    long[] pattern = {0, 1, 40, 41};
 61    switch (action) {
 62      case MotionEvent.ACTION_DOWN:
 63        screenIsBeingTouched = true;
 64        startRecording();
 65        parent.tts.playEarcon("[tock]", 0, null);
 66        vibe.vibrate(pattern, -1);
 67        break;
 68      case MotionEvent.ACTION_UP:
 69        saveRecording();
 70        parent.tts.playEarcon("[tock]", 0, null);
 71        vibe.vibrate(pattern, -1);
 72        parent.showConfirmationView();
 73        break;
 74      default:
 75        break;
 76    }
 77    invalidate();
 78    return true;
 79  }
 80  
 81  private void startRecording(){    
 82    File outputDir = new File("/sdcard/remindme/");
 83    outputDir.mkdirs();
 84    recorder = new MediaRecorder();
 85    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 86    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 87    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 88    recorder.setOutputFile("/sdcard/remindme/note00.amr");
 89    recorder.prepare();
 90    recorder.start();
 91  }
 92  
 93  private void saveRecording(){
 94    recorder.stop();
 95    recorder.release();
 96  }
 97
 98
 99  @Override
100  public void onDraw(Canvas canvas) {
101    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
102    paint.setColor(Color.WHITE);
103    paint.setTextAlign(Paint.Align.CENTER);
104    paint.setTypeface(Typeface.DEFAULT_BOLD);
105
106    int x = getWidth() / 2;
107    int y = (getHeight() / 2) - 35;
108    if (!screenIsBeingTouched) {
109      x = 5;
110      y = getHeight() - 40;
111      paint.setTextSize(20);
112      paint.setTextAlign(Paint.Align.LEFT);
113      y -= paint.ascent() / 2;
114      canvas.drawText("Touch screen to record.", x, y, paint);
115      x = 5;
116      y = getHeight() - 20;
117      paint.setTextSize(20);
118      paint.setTextAlign(Paint.Align.LEFT);
119      y -= paint.ascent() / 2;
120      canvas.drawText("Lift up when done.", x, y, paint);
121    } else {
122      x = 5;
123      y = getHeight() - 40;
124      paint.setTextSize(20);
125      paint.setTextAlign(Paint.Align.LEFT);
126      y -= paint.ascent() / 2;
127      canvas.drawText("Lift up when you are done.", x, y, paint);
128    }
129  }
130
131}