/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. package com.google.marvin.remindme;
  17. import java.io.File;
  18. import android.content.Context;
  19. import android.graphics.Canvas;
  20. import android.graphics.Color;
  21. import android.graphics.Paint;
  22. import android.graphics.Typeface;
  23. import android.media.MediaRecorder;
  24. import android.os.Vibrator;
  25. import android.view.MotionEvent;
  26. import android.widget.TextView;
  27. /**
  28. * Implements the user interface for doing slide dialing.
  29. *
  30. * @author clchen@google.com (Charles L. Chen) Created 8-2-2008
  31. */
  32. public class RecordReminderView extends TextView {
  33. public RemindMe parent;
  34. private Vibrator vibe;
  35. private MediaRecorder recorder;
  36. boolean screenIsBeingTouched = false;
  37. public RecordReminderView(Context context) {
  38. super(context);
  39. parent = (RemindMe) context;
  40. vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  41. setClickable(true);
  42. setFocusable(true);
  43. setFocusableInTouchMode(true);
  44. requestFocus();
  45. screenIsBeingTouched = false;
  46. }
  47. @Override
  48. public boolean onTouchEvent(MotionEvent event) {
  49. int action = event.getAction();
  50. long[] pattern = {0, 1, 40, 41};
  51. switch (action) {
  52. case MotionEvent.ACTION_DOWN:
  53. screenIsBeingTouched = true;
  54. startRecording();
  55. parent.tts.playEarcon("[tock]", 0, null);
  56. vibe.vibrate(pattern, -1);
  57. break;
  58. case MotionEvent.ACTION_UP:
  59. saveRecording();
  60. parent.tts.playEarcon("[tock]", 0, null);
  61. vibe.vibrate(pattern, -1);
  62. parent.showConfirmationView();
  63. break;
  64. default:
  65. break;
  66. }
  67. invalidate();
  68. return true;
  69. }
  70. private void startRecording(){
  71. File outputDir = new File("/sdcard/remindme/");
  72. outputDir.mkdirs();
  73. recorder = new MediaRecorder();
  74. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  75. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  76. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  77. recorder.setOutputFile("/sdcard/remindme/note00.amr");
  78. recorder.prepare();
  79. recorder.start();
  80. }
  81. private void saveRecording(){
  82. recorder.stop();
  83. recorder.release();
  84. }
  85. @Override
  86. public void onDraw(Canvas canvas) {
  87. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  88. paint.setColor(Color.WHITE);
  89. paint.setTextAlign(Paint.Align.CENTER);
  90. paint.setTypeface(Typeface.DEFAULT_BOLD);
  91. int x = getWidth() / 2;
  92. int y = (getHeight() / 2) - 35;
  93. if (!screenIsBeingTouched) {
  94. x = 5;
  95. y = getHeight() - 40;
  96. paint.setTextSize(20);
  97. paint.setTextAlign(Paint.Align.LEFT);
  98. y -= paint.ascent() / 2;
  99. canvas.drawText("Touch screen to record.", x, y, paint);
  100. x = 5;
  101. y = getHeight() - 20;
  102. paint.setTextSize(20);
  103. paint.setTextAlign(Paint.Align.LEFT);
  104. y -= paint.ascent() / 2;
  105. canvas.drawText("Lift up when done.", x, y, paint);
  106. } else {
  107. x = 5;
  108. y = getHeight() - 40;
  109. paint.setTextSize(20);
  110. paint.setTextAlign(Paint.Align.LEFT);
  111. y -= paint.ascent() / 2;
  112. canvas.drawText("Lift up when you are done.", x, y, paint);
  113. }
  114. }
  115. }