The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 7 | * |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.camera; |
| 18 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Wu-cheng Li | 46fc7ae | 2009-06-19 19:28:47 +0800 | [diff] [blame] | 20 | import android.content.ActivityNotFoundException; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.ContentValues; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.content.SharedPreferences; |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 28 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 29 | import android.content.res.Resources; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 32 | import android.hardware.Camera.Parameters; |
Wei-Ta Chen | 7e4655b | 2009-09-25 17:23:22 -0700 | [diff] [blame] | 33 | import android.hardware.Camera.Size; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 34 | import android.media.MediaRecorder; |
Ray Chen | 9f1480b | 2009-08-27 17:59:29 -0700 | [diff] [blame] | 35 | import android.media.ThumbnailUtil; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 36 | import android.net.Uri; |
Owen Lin | f6ef7b9 | 2009-09-14 18:48:12 +0800 | [diff] [blame] | 37 | import android.os.Build; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 38 | import android.os.Bundle; |
| 39 | import android.os.Environment; |
| 40 | import android.os.Handler; |
| 41 | import android.os.Message; |
| 42 | import android.os.StatFs; |
| 43 | import android.os.SystemClock; |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 44 | import android.os.SystemProperties; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 45 | import android.preference.PreferenceManager; |
Wei-Ta Chen | 7e4655b | 2009-09-25 17:23:22 -0700 | [diff] [blame] | 46 | import android.preference.PreferenceScreen; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 47 | import android.provider.MediaStore; |
| 48 | import android.provider.MediaStore.Video; |
| 49 | import android.text.format.DateFormat; |
| 50 | import android.util.Log; |
| 51 | import android.view.KeyEvent; |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 52 | import android.view.LayoutInflater; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 53 | import android.view.Menu; |
| 54 | import android.view.MenuItem; |
Owen Lin | c1f2e30 | 2009-09-14 19:04:37 +0800 | [diff] [blame] | 55 | import android.view.MotionEvent; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 56 | import android.view.SurfaceHolder; |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 57 | import android.view.SurfaceView; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 58 | import android.view.View; |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 59 | import android.view.ViewGroup; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 60 | import android.view.Window; |
| 61 | import android.view.WindowManager; |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 62 | import android.view.MenuItem.OnMenuItemClickListener; |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 63 | import android.view.animation.AlphaAnimation; |
| 64 | import android.view.animation.Animation; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 65 | import android.widget.ImageView; |
| 66 | import android.widget.TextView; |
| 67 | import android.widget.Toast; |
| 68 | |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 69 | import com.android.camera.PreviewFrameLayout.OnSizeChangedListener; |
Owen Lin | 2e768c1 | 2009-07-01 15:51:45 -0700 | [diff] [blame] | 70 | import com.android.camera.gallery.IImage; |
| 71 | import com.android.camera.gallery.IImageList; |
| 72 | |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 73 | import java.io.File; |
| 74 | import java.io.FileDescriptor; |
| 75 | import java.io.IOException; |
| 76 | import java.text.SimpleDateFormat; |
| 77 | import java.util.ArrayList; |
| 78 | import java.util.Date; |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 79 | import java.util.HashMap; |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 80 | |
Ray Chen | 23c51b7 | 2009-04-10 03:41:00 -0700 | [diff] [blame] | 81 | /** |
| 82 | * The Camcorder activity. |
| 83 | */ |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 84 | public class VideoCamera extends Activity implements View.OnClickListener, |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 85 | ShutterButton.OnShutterButtonListener, SurfaceHolder.Callback, |
Owen Lin | e239acc | 2009-06-23 22:18:20 -0700 | [diff] [blame] | 86 | MediaRecorder.OnErrorListener, MediaRecorder.OnInfoListener, |
Owen Lin | 3e737c2 | 2009-09-11 11:31:59 +0800 | [diff] [blame] | 87 | Switcher.OnSwitchListener, OnSharedPreferenceChangeListener, |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 88 | OnScreenSettings.OnVisibilityChangedListener, |
| 89 | PreviewFrameLayout.OnSizeChangedListener { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 90 | |
| 91 | private static final String TAG = "videocamera"; |
| 92 | |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 93 | private static final int INIT_RECORDER = 3; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 94 | private static final int CLEAR_SCREEN_DELAY = 4; |
| 95 | private static final int UPDATE_RECORD_TIME = 5; |
| 96 | |
| 97 | private static final int SCREEN_DELAY = 2 * 60 * 1000; |
| 98 | |
| 99 | private static final long NO_STORAGE_ERROR = -1L; |
| 100 | private static final long CANNOT_STAT_ERROR = -2L; |
| 101 | private static final long LOW_STORAGE_THRESHOLD = 512L * 1024L; |
| 102 | |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 103 | private static final int STORAGE_STATUS_OK = 0; |
| 104 | private static final int STORAGE_STATUS_LOW = 1; |
| 105 | private static final int STORAGE_STATUS_NONE = 2; |
| 106 | |
Owen Lin | e239acc | 2009-06-23 22:18:20 -0700 | [diff] [blame] | 107 | private static final boolean SWITCH_CAMERA = true; |
| 108 | private static final boolean SWITCH_VIDEO = false; |
| 109 | |
Chih-Chung Chang | 522e836 | 2009-08-26 16:12:34 +0800 | [diff] [blame] | 110 | private SharedPreferences mPreferences; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 111 | |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 112 | private PreviewFrameLayout mPreviewFrameLayout; |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 113 | private SurfaceView mVideoPreview; |
Chih-Chung Chang | 522e836 | 2009-08-26 16:12:34 +0800 | [diff] [blame] | 114 | private SurfaceHolder mSurfaceHolder = null; |
| 115 | private ImageView mVideoFrame; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 116 | |
| 117 | private boolean mIsVideoCaptureIntent; |
| 118 | // mLastPictureButton and mThumbController |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 119 | // are non-null only if mIsVideoCaptureIntent is true. |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 120 | private ImageView mLastPictureButton; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 121 | private ThumbnailController mThumbController; |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 122 | private boolean mStartPreviewFail = false; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 123 | |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 124 | private int mStorageStatus = STORAGE_STATUS_OK; |
| 125 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 126 | private MediaRecorder mMediaRecorder; |
| 127 | private boolean mMediaRecorderRecording = false; |
| 128 | private long mRecordingStartTime; |
| 129 | // The video file that the hardware camera is about to record into |
| 130 | // (or is recording into.) |
| 131 | private String mCameraVideoFilename; |
| 132 | private FileDescriptor mCameraVideoFileDescriptor; |
| 133 | |
| 134 | // The video file that has already been recorded, and that is being |
| 135 | // examined by the user. |
| 136 | private String mCurrentVideoFilename; |
| 137 | private Uri mCurrentVideoUri; |
| 138 | private ContentValues mCurrentVideoValues; |
| 139 | |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 140 | private MediaRecorderProfile mProfile; |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 141 | |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 142 | // The video duration limit. 0 menas no limit. |
Chih-Chung Chang | 82ded20 | 2009-05-20 14:24:25 +0800 | [diff] [blame] | 143 | private int mMaxVideoDurationInMs; |
| 144 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 145 | boolean mPausing = false; |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 146 | boolean mPreviewing = false; // True if preview is started. |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 147 | |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 148 | private ContentResolver mContentResolver; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 149 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 150 | private ShutterButton mShutterButton; |
| 151 | private TextView mRecordingTimeView; |
Chih-Chung Chang | fae0daa | 2009-09-21 05:36:24 +0800 | [diff] [blame] | 152 | private View mGripper; |
Owen Lin | 2e768c1 | 2009-07-01 15:51:45 -0700 | [diff] [blame] | 153 | private Switcher mSwitcher; |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 154 | private boolean mRecordingTimeCountsDown = false; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 155 | |
Wei-Ta Chen | 7e4655b | 2009-09-25 17:23:22 -0700 | [diff] [blame] | 156 | private final ArrayList<MenuItem> mGalleryItems = new ArrayList<MenuItem>(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 157 | |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 158 | private final Handler mHandler = new MainHandler(); |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 159 | private Parameters mParameters; |
| 160 | private OnScreenSettings mSettings; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 161 | |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 162 | // This Handler is used to post message back onto the main thread of the |
| 163 | // application |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 164 | private class MainHandler extends Handler { |
| 165 | @Override |
| 166 | public void handleMessage(Message msg) { |
| 167 | switch (msg.what) { |
| 168 | |
| 169 | case CLEAR_SCREEN_DELAY: { |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 170 | getWindow().clearFlags( |
| 171 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | |
| 175 | case UPDATE_RECORD_TIME: { |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 176 | updateRecordingTime(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 180 | case INIT_RECORDER: { |
| 181 | initializeRecorder(); |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 185 | default: |
| 186 | Log.v(TAG, "Unhandled message: " + msg.what); |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 187 | break; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 190 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 191 | |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 192 | private BroadcastReceiver mReceiver = null; |
| 193 | |
| 194 | private class MyBroadcastReceiver extends BroadcastReceiver { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 195 | @Override |
| 196 | public void onReceive(Context context, Intent intent) { |
| 197 | String action = intent.getAction(); |
| 198 | if (action.equals(Intent.ACTION_MEDIA_EJECT)) { |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 199 | updateAndShowStorageHint(false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 200 | stopVideoRecording(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 201 | } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 202 | updateAndShowStorageHint(true); |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 203 | initializeRecorder(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 204 | } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) { |
| 205 | // SD card unavailable |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 206 | // handled in ACTION_MEDIA_EJECT |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 207 | } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 208 | Toast.makeText(VideoCamera.this, |
| 209 | getResources().getString(R.string.wait), 5000); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 210 | } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) { |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 211 | updateAndShowStorageHint(true); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 214 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 215 | |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 216 | private static String createName(long dateTaken) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 217 | return DateFormat.format("yyyy-MM-dd kk.mm.ss", dateTaken).toString(); |
| 218 | } |
| 219 | |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 220 | private void showCameraBusyAndFinish() { |
| 221 | Resources ress = getResources(); |
| 222 | Util.showFatalErrorAndFinish(VideoCamera.this, |
| 223 | ress.getString(R.string.camera_error_title), |
| 224 | ress.getString(R.string.cannot_connect_camera)); |
| 225 | } |
| 226 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 227 | /** Called with the activity is first created. */ |
| 228 | @Override |
| 229 | public void onCreate(Bundle icicle) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 230 | super.onCreate(icicle); |
| 231 | |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 232 | mPreferences = PreferenceManager.getDefaultSharedPreferences(this); |
Chih-Chung Chang | 6f2c530 | 2009-08-31 20:02:55 +0800 | [diff] [blame] | 233 | CameraSettings.upgradePreferences(mPreferences); |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 234 | |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 235 | readVideoPreferences(); |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 236 | |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 237 | /* |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 238 | * To reduce startup time, we start the preview in another thread. |
| 239 | * We make sure the preview is started at the end of onCreate. |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 240 | */ |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 241 | Thread startPreviewThread = new Thread(new Runnable() { |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 242 | public void run() { |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 243 | try { |
| 244 | mStartPreviewFail = false; |
| 245 | startPreview(); |
| 246 | } catch (CameraHardwareException e) { |
Owen Lin | f6ef7b9 | 2009-09-14 18:48:12 +0800 | [diff] [blame] | 247 | // In eng build, we throw the exception so that test tool |
| 248 | // can detect it and report it |
| 249 | if ("eng".equals(Build.TYPE)) { |
| 250 | throw new RuntimeException(e); |
| 251 | } |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 252 | mStartPreviewFail = true; |
| 253 | } |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 254 | } |
| 255 | }); |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 256 | startPreviewThread.start(); |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 257 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 258 | mContentResolver = getContentResolver(); |
| 259 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 260 | requestWindowFeature(Window.FEATURE_PROGRESS); |
| 261 | setContentView(R.layout.video_camera); |
| 262 | |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 263 | mPreviewFrameLayout = (PreviewFrameLayout) |
| 264 | findViewById(R.id.frame_layout); |
| 265 | mPreviewFrameLayout.setOnSizeChangedListener(this); |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 266 | resizeForPreviewAspectRatio(); |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 267 | |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 268 | mVideoPreview = (SurfaceView) findViewById(R.id.camera_preview); |
Chih-Chung Chang | 54dd1d3 | 2009-09-01 13:26:14 +0800 | [diff] [blame] | 269 | mVideoFrame = (ImageView) findViewById(R.id.video_frame); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 270 | |
| 271 | // don't set mSurfaceHolder here. We have it set ONLY within |
| 272 | // surfaceCreated / surfaceDestroyed, other parts of the code |
| 273 | // assume that when it is set, the surface is also set. |
| 274 | SurfaceHolder holder = mVideoPreview.getHolder(); |
| 275 | holder.addCallback(this); |
| 276 | holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
| 277 | |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 278 | mIsVideoCaptureIntent = isVideoCaptureIntent(); |
| 279 | mRecordingTimeView = (TextView) findViewById(R.id.recording_time); |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 280 | |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 281 | ViewGroup rootView = (ViewGroup) findViewById(R.id.video_camera); |
| 282 | LayoutInflater inflater = this.getLayoutInflater(); |
| 283 | if (!mIsVideoCaptureIntent) { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 284 | View controlBar = inflater.inflate( |
| 285 | R.layout.camera_control, rootView); |
| 286 | mLastPictureButton = |
| 287 | (ImageView) controlBar.findViewById(R.id.review_thumbnail); |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 288 | mThumbController = new ThumbnailController( |
| 289 | getResources(), mLastPictureButton, mContentResolver); |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 290 | mLastPictureButton.setOnClickListener(this); |
| 291 | mThumbController.loadData(ImageManager.getLastVideoThumbPath()); |
Owen Lin | 2e768c1 | 2009-07-01 15:51:45 -0700 | [diff] [blame] | 292 | mSwitcher = ((Switcher) findViewById(R.id.camera_switch)); |
| 293 | mSwitcher.setOnSwitchListener(this); |
Chih-Chung Chang | 781f55b | 2009-08-10 17:00:52 +0800 | [diff] [blame] | 294 | mSwitcher.addTouchView(findViewById(R.id.camera_switch_set)); |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 295 | } else { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 296 | View controlBar = inflater.inflate( |
| 297 | R.layout.attach_camera_control, rootView); |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 298 | controlBar.findViewById(R.id.btn_cancel).setOnClickListener(this); |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 299 | ImageView retake = |
| 300 | (ImageView) controlBar.findViewById(R.id.btn_retake); |
Owen Lin | 1af86ab | 2009-06-22 17:19:56 -0700 | [diff] [blame] | 301 | retake.setOnClickListener(this); |
| 302 | retake.setImageResource(R.drawable.btn_ic_review_retake_video); |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 303 | controlBar.findViewById(R.id.btn_play).setOnClickListener(this); |
| 304 | controlBar.findViewById(R.id.btn_done).setOnClickListener(this); |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Owen Lin | dad4b18 | 2009-06-11 16:02:29 -0700 | [diff] [blame] | 307 | mShutterButton = (ShutterButton) findViewById(R.id.shutter_button); |
| 308 | mShutterButton.setImageResource(R.drawable.btn_ic_video_record); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 309 | mShutterButton.setOnShutterButtonListener(this); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 310 | mShutterButton.requestFocus(); |
Chih-Chung Chang | fae0daa | 2009-09-21 05:36:24 +0800 | [diff] [blame] | 311 | mGripper = findViewById(R.id.btn_gripper); |
| 312 | mGripper.setOnTouchListener(new GripperTouchListener()); |
Owen Lin | 059daa3 | 2009-05-18 15:31:17 -0700 | [diff] [blame] | 313 | |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 314 | // Make sure preview is started. |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 315 | try { |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 316 | startPreviewThread.join(); |
Owen Lin | f6ef7b9 | 2009-09-14 18:48:12 +0800 | [diff] [blame] | 317 | if (mStartPreviewFail) { |
| 318 | showCameraBusyAndFinish(); |
| 319 | return; |
| 320 | } |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 321 | } catch (InterruptedException ex) { |
| 322 | // ignore |
| 323 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Owen Lin | 2e768c1 | 2009-07-01 15:51:45 -0700 | [diff] [blame] | 326 | @Override |
| 327 | protected void onStart() { |
| 328 | super.onStart(); |
| 329 | if (!mIsVideoCaptureIntent) { |
| 330 | mSwitcher.setSwitch(SWITCH_VIDEO); |
| 331 | } |
| 332 | } |
| 333 | |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 334 | private void startShareVideoActivity() { |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 335 | Intent intent = new Intent(); |
| 336 | intent.setAction(Intent.ACTION_SEND); |
| 337 | intent.setType("video/3gpp"); |
| 338 | intent.putExtra(Intent.EXTRA_STREAM, mCurrentVideoUri); |
| 339 | try { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 340 | startActivity(Intent.createChooser(intent, |
| 341 | getText(R.string.sendVideo))); |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 342 | } catch (android.content.ActivityNotFoundException ex) { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 343 | Toast.makeText(VideoCamera.this, R.string.no_way_to_share_video, |
| 344 | Toast.LENGTH_SHORT).show(); |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 348 | private void startPlayVideoActivity() { |
| 349 | Intent intent = new Intent(Intent.ACTION_VIEW, mCurrentVideoUri); |
| 350 | try { |
| 351 | startActivity(intent); |
| 352 | } catch (android.content.ActivityNotFoundException ex) { |
| 353 | Log.e(TAG, "Couldn't view video " + mCurrentVideoUri, ex); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 358 | public void onClick(View v) { |
| 359 | switch (v.getId()) { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 360 | case R.id.btn_retake: |
| 361 | discardCurrentVideoAndInitRecorder(); |
| 362 | break; |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 363 | case R.id.btn_play: |
| 364 | startPlayVideoActivity(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 365 | break; |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 366 | case R.id.btn_done: |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 367 | doReturnToCaller(true); |
| 368 | break; |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 369 | case R.id.btn_cancel: |
| 370 | stopVideoRecording(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 371 | doReturnToCaller(false); |
| 372 | break; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 373 | case R.id.discard: { |
Ray Chen | 7638a54 | 2009-03-24 20:37:45 -0700 | [diff] [blame] | 374 | Runnable deleteCallback = new Runnable() { |
| 375 | public void run() { |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 376 | discardCurrentVideoAndInitRecorder(); |
Ray Chen | 7638a54 | 2009-03-24 20:37:45 -0700 | [diff] [blame] | 377 | } |
| 378 | }; |
| 379 | MenuHelper.deleteVideo(this, deleteCallback); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 380 | break; |
| 381 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 382 | case R.id.share: { |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 383 | startShareVideoActivity(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 384 | break; |
| 385 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 386 | case R.id.play: { |
| 387 | doPlayCurrentVideo(); |
| 388 | break; |
| 389 | } |
Owen Lin | dad4b18 | 2009-06-11 16:02:29 -0700 | [diff] [blame] | 390 | case R.id.review_thumbnail: { |
Wu-cheng Li | 46fc7ae | 2009-06-19 19:28:47 +0800 | [diff] [blame] | 391 | stopVideoRecordingAndShowReview(); |
Chih-Chung Chang | 22b5c55 | 2009-10-02 15:27:55 -0700 | [diff] [blame] | 392 | initializeRecorder(); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 393 | break; |
| 394 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | public void onShutterButtonFocus(ShutterButton button, boolean pressed) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 399 | // Do nothing (everything happens in onShutterButtonClick). |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | public void onShutterButtonClick(ShutterButton button) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 403 | switch (button.getId()) { |
Owen Lin | dad4b18 | 2009-06-11 16:02:29 -0700 | [diff] [blame] | 404 | case R.id.shutter_button: |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 405 | if (mMediaRecorderRecording) { |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 406 | if (mIsVideoCaptureIntent) { |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 407 | stopVideoRecordingAndShowAlert(); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 408 | } else { |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 409 | stopVideoRecordingAndGetThumbnail(); |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 410 | initializeRecorder(); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 411 | } |
Owen Lin | 0dd2341 | 2009-09-11 16:03:42 +0800 | [diff] [blame] | 412 | } else if (mMediaRecorder != null) { |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 413 | // If the click comes before recorder initialization, it is |
| 414 | // ignored. If users click the button during initialization, |
| 415 | // the event is put in the queue and record will be started |
| 416 | // eventually. |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 417 | startVideoRecording(); |
| 418 | } |
| 419 | break; |
| 420 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 421 | } |
| 422 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 423 | private void doPlayCurrentVideo() { |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 424 | Log.v(TAG, "Playing current video: " + mCurrentVideoUri); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 425 | Intent intent = new Intent(Intent.ACTION_VIEW, mCurrentVideoUri); |
| 426 | try { |
| 427 | startActivity(intent); |
| 428 | } catch (android.content.ActivityNotFoundException ex) { |
| 429 | Log.e(TAG, "Couldn't view video " + mCurrentVideoUri, ex); |
| 430 | } |
| 431 | } |
| 432 | |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 433 | private void discardCurrentVideoAndInitRecorder() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 434 | deleteCurrentVideo(); |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 435 | hideAlertAndInitializeRecorder(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | private OnScreenHint mStorageHint; |
| 439 | |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 440 | private void updateAndShowStorageHint(boolean mayHaveSd) { |
| 441 | mStorageStatus = getStorageStatus(mayHaveSd); |
| 442 | showStorageHint(); |
| 443 | } |
| 444 | |
| 445 | private void showStorageHint() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 446 | String errorMessage = null; |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 447 | switch (mStorageStatus) { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 448 | case STORAGE_STATUS_NONE: |
| 449 | errorMessage = getString(R.string.no_storage); |
| 450 | break; |
| 451 | case STORAGE_STATUS_LOW: |
| 452 | errorMessage = getString(R.string.spaceIsLow_content); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 453 | } |
| 454 | if (errorMessage != null) { |
| 455 | if (mStorageHint == null) { |
| 456 | mStorageHint = OnScreenHint.makeText(this, errorMessage); |
| 457 | } else { |
| 458 | mStorageHint.setText(errorMessage); |
| 459 | } |
| 460 | mStorageHint.show(); |
| 461 | } else if (mStorageHint != null) { |
| 462 | mStorageHint.cancel(); |
| 463 | mStorageHint = null; |
| 464 | } |
| 465 | } |
| 466 | |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 467 | private int getStorageStatus(boolean mayHaveSd) { |
| 468 | long remaining = mayHaveSd ? getAvailableStorage() : NO_STORAGE_ERROR; |
| 469 | if (remaining == NO_STORAGE_ERROR) { |
| 470 | return STORAGE_STATUS_NONE; |
| 471 | } |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 472 | return remaining < LOW_STORAGE_THRESHOLD |
| 473 | ? STORAGE_STATUS_LOW |
| 474 | : STORAGE_STATUS_OK; |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 477 | private void readVideoPreferences() { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 478 | boolean videoQualityHigh = |
| 479 | getBooleanPreference(CameraSettings.KEY_VIDEO_QUALITY, |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 480 | CameraSettings.DEFAULT_VIDEO_QUALITY_VALUE); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 481 | |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 482 | // Set video quality. |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 483 | Intent intent = getIntent(); |
| 484 | if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 485 | int extraVideoQuality = |
| 486 | intent.getIntExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 487 | videoQualityHigh = (extraVideoQuality > 0); |
| 488 | } |
| 489 | |
Chih-Chung Chang | 232b70a | 2009-08-25 13:42:04 +0800 | [diff] [blame] | 490 | // Set video duration limit. The limit is read from the preference, |
| 491 | // unless it is specified in the intent. |
| 492 | if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) { |
Chih-Chung Chang | 522e836 | 2009-08-26 16:12:34 +0800 | [diff] [blame] | 493 | int seconds = |
| 494 | intent.getIntExtra(MediaStore.EXTRA_DURATION_LIMIT, 0); |
Chih-Chung Chang | 232b70a | 2009-08-25 13:42:04 +0800 | [diff] [blame] | 495 | mMaxVideoDurationInMs = 1000 * seconds; |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 496 | } else { |
Chih-Chung Chang | 232b70a | 2009-08-25 13:42:04 +0800 | [diff] [blame] | 497 | int minutes = getIntPreference(CameraSettings.KEY_VIDEO_DURATION, |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 498 | CameraSettings.DEFAULT_VIDEO_DURATION_VALUE); |
Chih-Chung Chang | 6f2c530 | 2009-08-31 20:02:55 +0800 | [diff] [blame] | 499 | if (minutes == -1) { |
| 500 | // This is a special case: the value -1 means we want to use the |
Chih-Chung Chang | 232b70a | 2009-08-25 13:42:04 +0800 | [diff] [blame] | 501 | // device-dependent duration for MMS messages. The value is |
| 502 | // represented in seconds. |
Chih-Chung Chang | 9b93bcb | 2009-09-04 19:46:14 +0800 | [diff] [blame] | 503 | mMaxVideoDurationInMs = |
| 504 | 1000 * CameraSettings.MMS_VIDEO_DURATION; |
Chih-Chung Chang | 232b70a | 2009-08-25 13:42:04 +0800 | [diff] [blame] | 505 | } else { |
| 506 | // 1 minute = 60000ms |
| 507 | mMaxVideoDurationInMs = 60000 * minutes; |
| 508 | } |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 509 | } |
| 510 | |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 511 | mProfile = new MediaRecorderProfile(videoQualityHigh); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 512 | } |
| 513 | |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 514 | private void resizeForPreviewAspectRatio() { |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 515 | mPreviewFrameLayout.setAspectRatio( |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 516 | (double) mProfile.mVideoWidth / mProfile.mVideoHeight); |
Chih-Chung Chang | 54dd1d3 | 2009-09-01 13:26:14 +0800 | [diff] [blame] | 517 | } |
| 518 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 519 | @Override |
| 520 | public void onResume() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 521 | super.onResume(); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 522 | mPausing = false; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 523 | |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 524 | readVideoPreferences(); |
Cheng-Ru Lin | ffcca74 | 2009-09-28 03:21:25 +0800 | [diff] [blame] | 525 | resizeForPreviewAspectRatio(); |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 526 | if (!mPreviewing && !mStartPreviewFail) { |
| 527 | try { |
| 528 | startPreview(); |
| 529 | } catch (CameraHardwareException e) { |
| 530 | showCameraBusyAndFinish(); |
| 531 | return; |
| 532 | } |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 533 | } |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 534 | keepScreenOnAwhile(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 535 | |
| 536 | // install an intent filter to receive SD card related events. |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 537 | IntentFilter intentFilter = |
| 538 | new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 539 | intentFilter.addAction(Intent.ACTION_MEDIA_EJECT); |
| 540 | intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); |
| 541 | intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); |
| 542 | intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); |
| 543 | intentFilter.addDataScheme("file"); |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 544 | mReceiver = new MyBroadcastReceiver(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 545 | registerReceiver(mReceiver, intentFilter); |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 546 | mStorageStatus = getStorageStatus(true); |
| 547 | |
| 548 | mHandler.postDelayed(new Runnable() { |
| 549 | public void run() { |
| 550 | showStorageHint(); |
| 551 | } |
| 552 | }, 200); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 553 | |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 554 | if (mSurfaceHolder != null) { |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 555 | mHandler.sendEmptyMessage(INIT_RECORDER); |
| 556 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 559 | private void setPreviewDisplay(SurfaceHolder holder) { |
| 560 | try { |
| 561 | mCameraDevice.setPreviewDisplay(holder); |
| 562 | } catch (Throwable ex) { |
| 563 | closeCamera(); |
| 564 | throw new RuntimeException("setPreviewDisplay failed", ex); |
| 565 | } |
| 566 | } |
| 567 | |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 568 | private void startPreview() throws CameraHardwareException { |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 569 | Log.v(TAG, "startPreview"); |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 570 | if (mPreviewing) { |
Chih-Chung Chang | 1fb8ac4 | 2009-06-05 17:32:54 +0800 | [diff] [blame] | 571 | // After recording a video, preview is not stopped. So just return. |
Wu-cheng Li | fa1dfa2 | 2009-06-10 14:44:43 +0800 | [diff] [blame] | 572 | return; |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 573 | } |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 574 | |
| 575 | if (mCameraDevice == null) { |
| 576 | // If the activity is paused and resumed, camera device has been |
| 577 | // released and we need to open the camera. |
Chih-Chung Chang | cd65be3 | 2009-06-09 13:51:29 +0800 | [diff] [blame] | 578 | mCameraDevice = CameraHolder.instance().open(); |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 579 | } |
| 580 | |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 581 | mCameraDevice.lock(); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 582 | setCameraParameters(); |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 583 | setPreviewDisplay(mSurfaceHolder); |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 584 | |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 585 | try { |
| 586 | mCameraDevice.startPreview(); |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 587 | mPreviewing = true; |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 588 | } catch (Throwable ex) { |
Wu-cheng Li | fa1dfa2 | 2009-06-10 14:44:43 +0800 | [diff] [blame] | 589 | closeCamera(); |
| 590 | throw new RuntimeException("startPreview failed", ex); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 591 | } |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 592 | |
| 593 | // If setPreviewDisplay has been set with a valid surface, unlock now. |
| 594 | // If surface is null, unlock later. Otherwise, setPreviewDisplay in |
| 595 | // surfaceChanged will fail. |
| 596 | if (mSurfaceHolder != null) { |
| 597 | mCameraDevice.unlock(); |
| 598 | } |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 599 | } |
| 600 | |
Chih-Chung Chang | d55a5e9 | 2009-05-14 11:49:34 +0800 | [diff] [blame] | 601 | private void closeCamera() { |
| 602 | Log.v(TAG, "closeCamera"); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 603 | if (mCameraDevice == null) { |
| 604 | Log.d(TAG, "already stopped."); |
| 605 | return; |
| 606 | } |
Wu-cheng Li | fbdbae6 | 2009-06-02 15:46:20 +0800 | [diff] [blame] | 607 | // If we don't lock the camera, release() will fail. |
| 608 | mCameraDevice.lock(); |
Chih-Chung Chang | cd65be3 | 2009-06-09 13:51:29 +0800 | [diff] [blame] | 609 | CameraHolder.instance().release(); |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 610 | mCameraDevice = null; |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 611 | mPreviewing = false; |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 612 | } |
| 613 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 614 | @Override |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 615 | protected void onPause() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 616 | super.onPause(); |
| 617 | |
Wu-cheng Li | fbdbae6 | 2009-06-02 15:46:20 +0800 | [diff] [blame] | 618 | mPausing = true; |
| 619 | |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 620 | if (mSettings != null && mSettings.isVisible()) { |
| 621 | mSettings.setVisible(false); |
| 622 | } |
| 623 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 624 | // This is similar to what mShutterButton.performClick() does, |
| 625 | // but not quite the same. |
| 626 | if (mMediaRecorderRecording) { |
| 627 | if (mIsVideoCaptureIntent) { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 628 | stopVideoRecording(); |
| 629 | showAlert(); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 630 | } else { |
| 631 | stopVideoRecordingAndGetThumbnail(); |
| 632 | } |
| 633 | } else { |
| 634 | stopVideoRecording(); |
| 635 | } |
Wu-cheng Li | fbdbae6 | 2009-06-02 15:46:20 +0800 | [diff] [blame] | 636 | closeCamera(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 637 | |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 638 | if (mReceiver != null) { |
| 639 | unregisterReceiver(mReceiver); |
| 640 | mReceiver = null; |
| 641 | } |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 642 | resetScreenOn(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 643 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 644 | if (!mIsVideoCaptureIntent) { |
| 645 | mThumbController.storeData(ImageManager.getLastVideoThumbPath()); |
| 646 | } |
Owen Lin | 095afa1 | 2009-03-24 23:41:27 -0700 | [diff] [blame] | 647 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 648 | if (mStorageHint != null) { |
| 649 | mStorageHint.cancel(); |
| 650 | mStorageHint = null; |
| 651 | } |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 652 | |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 653 | mHandler.removeMessages(INIT_RECORDER); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | @Override |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 657 | public void onUserInteraction() { |
| 658 | super.onUserInteraction(); |
| 659 | if (!mMediaRecorderRecording) keepScreenOnAwhile(); |
| 660 | } |
| 661 | |
| 662 | @Override |
Chih-Chung Chang | 0b8028e | 2009-09-25 15:12:14 -0700 | [diff] [blame] | 663 | public void onBackPressed() { |
| 664 | if (mPausing) return; |
| 665 | if (mMediaRecorderRecording) { |
| 666 | mShutterButton.performClick(); |
| 667 | return; |
| 668 | } |
| 669 | super.onBackPressed(); |
| 670 | } |
| 671 | |
| 672 | @Override |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 673 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Wu-cheng Li | fbdbae6 | 2009-06-02 15:46:20 +0800 | [diff] [blame] | 674 | // Do not handle any key if the activity is paused. |
| 675 | if (mPausing) { |
| 676 | return true; |
| 677 | } |
| 678 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 679 | switch (keyCode) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 680 | case KeyEvent.KEYCODE_CAMERA: |
| 681 | if (event.getRepeatCount() == 0) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 682 | mShutterButton.performClick(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 683 | return true; |
| 684 | } |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 685 | break; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 686 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 687 | if (event.getRepeatCount() == 0) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 688 | mShutterButton.performClick(); |
| 689 | return true; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 690 | } |
| 691 | break; |
| 692 | case KeyEvent.KEYCODE_MENU: |
| 693 | if (mMediaRecorderRecording) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 694 | mShutterButton.performClick(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 695 | return true; |
| 696 | } |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | return super.onKeyDown(keyCode, event); |
| 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 705 | switch (keyCode) { |
| 706 | case KeyEvent.KEYCODE_CAMERA: |
| 707 | mShutterButton.setPressed(false); |
| 708 | return true; |
Wei-Ta Chen | bf45b96 | 2009-09-27 16:20:23 -0700 | [diff] [blame] | 709 | case KeyEvent.KEYCODE_MENU: |
| 710 | if (this.mIsVideoCaptureIntent) { |
| 711 | showOnScreenSettings(); |
| 712 | return true; |
| 713 | } |
| 714 | break; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 715 | } |
| 716 | return super.onKeyUp(keyCode, event); |
| 717 | } |
| 718 | |
| 719 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
Wu-cheng Li | 37dcfbb | 2009-06-23 15:34:19 +0800 | [diff] [blame] | 720 | // Make sure we have a surface in the holder before proceeding. |
| 721 | if (holder.getSurface() == null) { |
| 722 | Log.d(TAG, "holder.getSurface() == null"); |
| 723 | return; |
| 724 | } |
| 725 | |
Andreas Huber | 314f9a7 | 2009-03-24 20:12:48 -0700 | [diff] [blame] | 726 | if (mPausing) { |
| 727 | // We're pausing, the screen is off and we already stopped |
| 728 | // video recording. We don't want to start the camera again |
| 729 | // in this case in order to conserve power. |
| 730 | // The fact that surfaceChanged is called _after_ an onPause appears |
| 731 | // to be legitimate since in that case the lockscreen always returns |
| 732 | // to portrait orientation possibly triggering the notification. |
| 733 | return; |
| 734 | } |
| 735 | |
Owen Lin | 3665f92 | 2009-09-02 18:03:42 +0800 | [diff] [blame] | 736 | // The mCameraDevice will be null if it is fail to connect to the |
| 737 | // camera hardware. In this case we will show a dialog and then |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 738 | // finish the activity, so it's OK to ignore it. |
| 739 | if (mCameraDevice == null) return; |
| 740 | |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 741 | if (mMediaRecorderRecording) { |
| 742 | stopVideoRecording(); |
| 743 | } |
| 744 | |
Wu-cheng Li | 48f6cdf | 2009-07-15 11:13:13 +0800 | [diff] [blame] | 745 | // Set preview display if the surface is being created. Preview was |
| 746 | // already started. |
| 747 | if (holder.isCreating()) { |
| 748 | setPreviewDisplay(holder); |
| 749 | mCameraDevice.unlock(); |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 750 | mHandler.sendEmptyMessage(INIT_RECORDER); |
| 751 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | public void surfaceCreated(SurfaceHolder holder) { |
| 755 | mSurfaceHolder = holder; |
| 756 | } |
| 757 | |
| 758 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 759 | mSurfaceHolder = null; |
| 760 | } |
| 761 | |
Chih-Chung Chang | 0a475e1 | 2009-04-16 11:42:12 +0800 | [diff] [blame] | 762 | private void gotoGallery() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 763 | MenuHelper.gotoCameraVideoGallery(this); |
| 764 | } |
| 765 | |
| 766 | @Override |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 767 | public boolean onCreateOptionsMenu(Menu menu) { |
| 768 | super.onCreateOptionsMenu(menu); |
| 769 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 770 | if (mIsVideoCaptureIntent) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 771 | // No options menu for attach mode. |
| 772 | return false; |
| 773 | } else { |
| 774 | addBaseMenuItems(menu); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 775 | } |
| 776 | return true; |
| 777 | } |
| 778 | |
| 779 | private boolean isVideoCaptureIntent() { |
| 780 | String action = getIntent().getAction(); |
| 781 | return (MediaStore.ACTION_VIDEO_CAPTURE.equals(action)); |
| 782 | } |
| 783 | |
| 784 | private void doReturnToCaller(boolean success) { |
| 785 | Intent resultIntent = new Intent(); |
| 786 | int resultCode; |
| 787 | if (success) { |
| 788 | resultCode = RESULT_OK; |
| 789 | resultIntent.setData(mCurrentVideoUri); |
| 790 | } else { |
| 791 | resultCode = RESULT_CANCELED; |
| 792 | } |
| 793 | setResult(resultCode, resultIntent); |
| 794 | finish(); |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Returns |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 799 | * |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 800 | * @return number of bytes available, or an ERROR code. |
| 801 | */ |
| 802 | private static long getAvailableStorage() { |
| 803 | try { |
| 804 | if (!ImageManager.hasStorage()) { |
| 805 | return NO_STORAGE_ERROR; |
| 806 | } else { |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 807 | String storageDirectory = |
| 808 | Environment.getExternalStorageDirectory().toString(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 809 | StatFs stat = new StatFs(storageDirectory); |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 810 | return (long) stat.getAvailableBlocks() |
| 811 | * (long) stat.getBlockSize(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 812 | } |
Ray Chen | 23c51b7 | 2009-04-10 03:41:00 -0700 | [diff] [blame] | 813 | } catch (RuntimeException ex) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 814 | // if we can't stat the filesystem then we don't know how many |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 815 | // free bytes exist. It might be zero but just leave it |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 816 | // blank since we really don't know. |
| 817 | return CANNOT_STAT_ERROR; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | private void cleanupEmptyFile() { |
| 822 | if (mCameraVideoFilename != null) { |
| 823 | File f = new File(mCameraVideoFilename); |
| 824 | if (f.length() == 0 && f.delete()) { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 825 | Log.v(TAG, "Empty video file deleted: " + mCameraVideoFilename); |
| 826 | mCameraVideoFilename = null; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 831 | private android.hardware.Camera mCameraDevice; |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 832 | |
Wu-cheng Li | 80b7c5b | 2009-09-27 18:03:50 -0700 | [diff] [blame] | 833 | // Prepares media recorder. |
| 834 | private void initializeRecorder() { |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 835 | Log.v(TAG, "initializeRecorder"); |
Wu-cheng Li | 80b7c5b | 2009-09-27 18:03:50 -0700 | [diff] [blame] | 836 | if (mMediaRecorder != null) return; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 837 | |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 838 | // We will call initializeRecorder() again when the alert is hidden. |
Owen Lin | 3f3c857 | 2009-08-18 13:33:49 +0800 | [diff] [blame] | 839 | // If the mCameraDevice is null, then this activity is going to finish |
Wu-cheng Li | 80b7c5b | 2009-09-27 18:03:50 -0700 | [diff] [blame] | 840 | if (isAlertVisible() || mCameraDevice == null) return; |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 841 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 842 | Intent intent = getIntent(); |
| 843 | Bundle myExtras = intent.getExtras(); |
| 844 | |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 845 | long requestedSizeLimit = 0; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 846 | if (mIsVideoCaptureIntent && myExtras != null) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 847 | Uri saveUri = (Uri) myExtras.getParcelable(MediaStore.EXTRA_OUTPUT); |
| 848 | if (saveUri != null) { |
| 849 | try { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 850 | mCameraVideoFileDescriptor = |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 851 | mContentResolver.openFileDescriptor(saveUri, "rw") |
| 852 | .getFileDescriptor(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 853 | mCurrentVideoUri = saveUri; |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 854 | } catch (java.io.FileNotFoundException ex) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 855 | // invalid uri |
| 856 | Log.e(TAG, ex.toString()); |
| 857 | } |
| 858 | } |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 859 | requestedSizeLimit = myExtras.getLong(MediaStore.EXTRA_SIZE_LIMIT); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 860 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 861 | mMediaRecorder = new MediaRecorder(); |
| 862 | |
Chih-Chung Chang | 845accc | 2009-04-23 14:14:37 +0800 | [diff] [blame] | 863 | mMediaRecorder.setCamera(mCameraDevice); |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 864 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 865 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 866 | mMediaRecorder.setOutputFormat(mProfile.mOutputFormat); |
Chih-Chung Chang | 82ded20 | 2009-05-20 14:24:25 +0800 | [diff] [blame] | 867 | mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs); |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 868 | |
Wu-cheng Li | 80b7c5b | 2009-09-27 18:03:50 -0700 | [diff] [blame] | 869 | // Set output file. |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 870 | if (mStorageStatus != STORAGE_STATUS_OK) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 871 | mMediaRecorder.setOutputFile("/dev/null"); |
| 872 | } else { |
Wu-cheng Li | 80b7c5b | 2009-09-27 18:03:50 -0700 | [diff] [blame] | 873 | // Try Uri in the intent first. If it doesn't exist, use our own |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 874 | // instead. |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 875 | if (mCameraVideoFileDescriptor != null) { |
| 876 | mMediaRecorder.setOutputFile(mCameraVideoFileDescriptor); |
| 877 | } else { |
| 878 | createVideoPath(); |
| 879 | mMediaRecorder.setOutputFile(mCameraVideoFilename); |
| 880 | } |
| 881 | } |
| 882 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 883 | // Use the same frame rate for both, since internally |
| 884 | // if the frame rate is too large, it can cause camera to become |
| 885 | // unstable. We need to fix the MediaRecorder to disable the support |
| 886 | // of setting frame rate for now. |
Chih-Chung Chang | 947455a | 2009-07-29 14:50:07 +0800 | [diff] [blame] | 887 | mMediaRecorder.setVideoFrameRate(mProfile.mVideoFps); |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 888 | mMediaRecorder.setVideoSize( |
| 889 | mProfile.mVideoWidth, mProfile.mVideoHeight); |
Chih-Chung Chang | 947455a | 2009-07-29 14:50:07 +0800 | [diff] [blame] | 890 | mMediaRecorder.setParameters(String.format( |
| 891 | "video-param-encoding-bitrate=%d", mProfile.mVideoBitrate)); |
| 892 | mMediaRecorder.setParameters(String.format( |
| 893 | "audio-param-encoding-bitrate=%d", mProfile.mAudioBitrate)); |
| 894 | mMediaRecorder.setParameters(String.format( |
| 895 | "audio-param-number-of-channels=%d", mProfile.mAudioChannels)); |
| 896 | mMediaRecorder.setParameters(String.format( |
| 897 | "audio-param-sampling-rate=%d", mProfile.mAudioSamplingRate)); |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 898 | mMediaRecorder.setVideoEncoder(mProfile.mVideoEncoder); |
| 899 | mMediaRecorder.setAudioEncoder(mProfile.mAudioEncoder); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 900 | mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 901 | |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 902 | // Set maximum file size. |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 903 | // remaining >= LOW_STORAGE_THRESHOLD at this point, reserve a quarter |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 904 | // of that to make it more likely that recording can complete |
| 905 | // successfully. |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 906 | long maxFileSize = getAvailableStorage() - LOW_STORAGE_THRESHOLD / 4; |
| 907 | if (requestedSizeLimit > 0 && requestedSizeLimit < maxFileSize) { |
| 908 | maxFileSize = requestedSizeLimit; |
| 909 | } |
| 910 | |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 911 | try { |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 912 | mMediaRecorder.setMaxFileSize(maxFileSize); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 913 | } catch (RuntimeException exception) { |
| 914 | // We are going to ignore failure of setMaxFileSize here, as |
| 915 | // a) The composer selected may simply not support it, or |
| 916 | // b) The underlying media framework may not handle 64-bit range |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 917 | // on the size restriction. |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 918 | } |
| 919 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 920 | try { |
| 921 | mMediaRecorder.prepare(); |
Wu-cheng Li | 80b7c5b | 2009-09-27 18:03:50 -0700 | [diff] [blame] | 922 | } catch (IOException e) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 923 | Log.e(TAG, "prepare failed for " + mCameraVideoFilename); |
| 924 | releaseMediaRecorder(); |
Wu-cheng Li | 80b7c5b | 2009-09-27 18:03:50 -0700 | [diff] [blame] | 925 | throw new RuntimeException(e); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 926 | } |
| 927 | mMediaRecorderRecording = false; |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 928 | |
Wu-cheng Li | ac3a8c8 | 2009-05-12 13:00:10 +0800 | [diff] [blame] | 929 | // Update the last video thumbnail. |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 930 | if (!mIsVideoCaptureIntent) { |
| 931 | if (!mThumbController.isUriValid()) { |
| 932 | updateLastVideo(); |
| 933 | } |
| 934 | mThumbController.updateDisplayIfNeeded(); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 935 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | private void releaseMediaRecorder() { |
| 939 | Log.v(TAG, "Releasing media recorder."); |
| 940 | if (mMediaRecorder != null) { |
| 941 | cleanupEmptyFile(); |
| 942 | mMediaRecorder.reset(); |
| 943 | mMediaRecorder.release(); |
| 944 | mMediaRecorder = null; |
| 945 | } |
| 946 | } |
| 947 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 948 | private int getIntPreference(String key, int defaultValue) { |
| 949 | String s = mPreferences.getString(key, ""); |
| 950 | int result = defaultValue; |
| 951 | try { |
| 952 | result = Integer.parseInt(s); |
| 953 | } catch (NumberFormatException e) { |
| 954 | // Ignore, result is already the default value. |
| 955 | } |
| 956 | return result; |
| 957 | } |
| 958 | |
| 959 | private boolean getBooleanPreference(String key, boolean defaultValue) { |
| 960 | return getIntPreference(key, defaultValue ? 1 : 0) != 0; |
| 961 | } |
| 962 | |
| 963 | private void createVideoPath() { |
| 964 | long dateTaken = System.currentTimeMillis(); |
| 965 | String title = createName(dateTaken); |
| 966 | String displayName = title + ".3gp"; // Used when emailing. |
| 967 | String cameraDirPath = ImageManager.CAMERA_IMAGE_BUCKET_NAME; |
| 968 | File cameraDir = new File(cameraDirPath); |
| 969 | cameraDir.mkdirs(); |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 970 | SimpleDateFormat dateFormat = new SimpleDateFormat( |
| 971 | getString(R.string.video_file_name_format)); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 972 | Date date = new Date(dateTaken); |
| 973 | String filepart = dateFormat.format(date); |
| 974 | String filename = cameraDirPath + "/" + filepart + ".3gp"; |
| 975 | ContentValues values = new ContentValues(7); |
| 976 | values.put(Video.Media.TITLE, title); |
| 977 | values.put(Video.Media.DISPLAY_NAME, displayName); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 978 | values.put(Video.Media.DATE_TAKEN, dateTaken); |
| 979 | values.put(Video.Media.MIME_TYPE, "video/3gpp"); |
| 980 | values.put(Video.Media.DATA, filename); |
| 981 | mCameraVideoFilename = filename; |
| 982 | Log.v(TAG, "Current camera video filename: " + mCameraVideoFilename); |
| 983 | mCurrentVideoValues = values; |
| 984 | } |
| 985 | |
| 986 | private void registerVideo() { |
| 987 | if (mCameraVideoFileDescriptor == null) { |
| 988 | Uri videoTable = Uri.parse("content://media/external/video/media"); |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 989 | mCurrentVideoValues.put(Video.Media.SIZE, |
| 990 | new File(mCurrentVideoFilename).length()); |
| 991 | mCurrentVideoUri = mContentResolver.insert(videoTable, |
| 992 | mCurrentVideoValues); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 993 | Log.v(TAG, "Current video URI: " + mCurrentVideoUri); |
| 994 | } |
| 995 | mCurrentVideoValues = null; |
| 996 | } |
| 997 | |
| 998 | private void deleteCurrentVideo() { |
| 999 | if (mCurrentVideoFilename != null) { |
| 1000 | deleteVideoFile(mCurrentVideoFilename); |
| 1001 | mCurrentVideoFilename = null; |
| 1002 | } |
| 1003 | if (mCurrentVideoUri != null) { |
| 1004 | mContentResolver.delete(mCurrentVideoUri, null, null); |
| 1005 | mCurrentVideoUri = null; |
| 1006 | } |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1007 | updateAndShowStorageHint(true); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | private void deleteVideoFile(String fileName) { |
| 1011 | Log.v(TAG, "Deleting video " + fileName); |
| 1012 | File f = new File(fileName); |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1013 | if (!f.delete()) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1014 | Log.v(TAG, "Could not delete " + fileName); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | private void addBaseMenuItems(Menu menu) { |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 1019 | MenuItem gallery = menu.add(Menu.NONE, Menu.NONE, |
| 1020 | MenuHelper.POSITION_GOTO_GALLERY, |
Chih-Chung Chang | b594654 | 2009-09-07 20:17:12 +0800 | [diff] [blame] | 1021 | R.string.camera_gallery_photos_text) |
| 1022 | .setOnMenuItemClickListener( |
| 1023 | new OnMenuItemClickListener() { |
| 1024 | public boolean onMenuItemClick(MenuItem item) { |
| 1025 | gotoGallery(); |
| 1026 | return true; |
| 1027 | } |
| 1028 | }); |
| 1029 | gallery.setIcon(android.R.drawable.ic_menu_gallery); |
| 1030 | mGalleryItems.add(gallery); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1031 | |
Chih-Chung Chang | d189083 | 2009-09-08 13:32:52 +0800 | [diff] [blame] | 1032 | MenuItem item = menu.add(Menu.NONE, Menu.NONE, |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1033 | MenuHelper.POSITION_CAMERA_SETTING, R.string.settings) |
| 1034 | .setOnMenuItemClickListener(new OnMenuItemClickListener() { |
| 1035 | public boolean onMenuItemClick(MenuItem item) { |
Owen Lin | c1f2e30 | 2009-09-14 19:04:37 +0800 | [diff] [blame] | 1036 | showOnScreenSettings(); |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1037 | return true; |
| 1038 | }}); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1039 | item.setIcon(android.R.drawable.ic_menu_preferences); |
| 1040 | } |
| 1041 | |
Owen Lin | c1f2e30 | 2009-09-14 19:04:37 +0800 | [diff] [blame] | 1042 | private void showOnScreenSettings() { |
| 1043 | if (mSettings == null) { |
| 1044 | mSettings = new OnScreenSettings( |
| 1045 | findViewById(R.id.camera_preview)); |
| 1046 | CameraSettings helper = new CameraSettings(this, mParameters); |
Wei-Ta Chen | 7e4655b | 2009-09-25 17:23:22 -0700 | [diff] [blame] | 1047 | PreferenceScreen screen = helper |
| 1048 | .getPreferenceScreen(R.xml.video_preferences); |
| 1049 | if (mIsVideoCaptureIntent) { |
| 1050 | screen = filterPreferenceScreenByIntent(screen); |
| 1051 | } |
| 1052 | |
| 1053 | mSettings.setPreferenceScreen(screen); |
Owen Lin | c1f2e30 | 2009-09-14 19:04:37 +0800 | [diff] [blame] | 1054 | mSettings.setOnVisibilityChangedListener(this); |
| 1055 | } |
Cheng-Ru Lin | abdd8a1 | 2009-09-30 15:35:43 +0800 | [diff] [blame] | 1056 | mSettings.setVisible(true); |
Owen Lin | c1f2e30 | 2009-09-14 19:04:37 +0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Wei-Ta Chen | 7e4655b | 2009-09-25 17:23:22 -0700 | [diff] [blame] | 1059 | private PreferenceScreen filterPreferenceScreenByIntent( |
| 1060 | PreferenceScreen screen) { |
| 1061 | Intent intent = getIntent(); |
| 1062 | if (intent.hasExtra(MediaStore.EXTRA_VIDEO_QUALITY)) { |
| 1063 | CameraSettings.removePreferenceFromScreen(screen, |
| 1064 | CameraSettings.KEY_VIDEO_QUALITY); |
| 1065 | } |
| 1066 | |
| 1067 | if (intent.hasExtra(MediaStore.EXTRA_DURATION_LIMIT)) { |
| 1068 | CameraSettings.removePreferenceFromScreen(screen, |
| 1069 | CameraSettings.KEY_VIDEO_DURATION); |
| 1070 | } |
| 1071 | return screen; |
| 1072 | } |
| 1073 | |
Owen Lin | c1f2e30 | 2009-09-14 19:04:37 +0800 | [diff] [blame] | 1074 | private class GripperTouchListener implements View.OnTouchListener { |
| 1075 | public boolean onTouch(View view, MotionEvent event) { |
| 1076 | switch (event.getAction()) { |
| 1077 | case MotionEvent.ACTION_DOWN: |
| 1078 | return true; |
| 1079 | case MotionEvent.ACTION_UP: |
| 1080 | showOnScreenSettings(); |
| 1081 | return true; |
| 1082 | } |
| 1083 | return false; |
| 1084 | } |
| 1085 | } |
| 1086 | |
Owen Lin | 3e737c2 | 2009-09-11 11:31:59 +0800 | [diff] [blame] | 1087 | public void onVisibilityChanged(boolean visible) { |
Chih-Chung Chang | fae0daa | 2009-09-21 05:36:24 +0800 | [diff] [blame] | 1088 | // At this point, we are not recording. |
| 1089 | mGripper.setVisibility(visible ? View.INVISIBLE : View.VISIBLE); |
Owen Lin | 3e737c2 | 2009-09-11 11:31:59 +0800 | [diff] [blame] | 1090 | if (visible) { |
| 1091 | releaseMediaRecorder(); |
Wei-Ta Chen | d18f380 | 2009-09-30 20:27:42 -0700 | [diff] [blame] | 1092 | mPreferences.registerOnSharedPreferenceChangeListener(this); |
Owen Lin | 3e737c2 | 2009-09-11 11:31:59 +0800 | [diff] [blame] | 1093 | } else { |
| 1094 | initializeRecorder(); |
Wei-Ta Chen | d18f380 | 2009-09-30 20:27:42 -0700 | [diff] [blame] | 1095 | mPreferences.unregisterOnSharedPreferenceChangeListener(this); |
Owen Lin | 3e737c2 | 2009-09-11 11:31:59 +0800 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1099 | // from MediaRecorder.OnErrorListener |
| 1100 | public void onError(MediaRecorder mr, int what, int extra) { |
| 1101 | if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) { |
| 1102 | // We may have run out of space on the sdcard. |
| 1103 | stopVideoRecording(); |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 1104 | updateAndShowStorageHint(true); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1108 | // from MediaRecorder.OnInfoListener |
| 1109 | public void onInfo(MediaRecorder mr, int what, int extra) { |
| 1110 | if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1111 | mShutterButton.performClick(); |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 1112 | } else if (what |
| 1113 | == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1114 | mShutterButton.performClick(); |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 1115 | // Show the toast. |
| 1116 | Toast.makeText(VideoCamera.this, R.string.video_reach_size_limit, |
| 1117 | Toast.LENGTH_LONG).show(); |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
The Android Open Source Project | 9c9be2e | 2009-03-05 14:34:37 -0800 | [diff] [blame] | 1121 | /* |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1122 | * Make sure we're not recording music playing in the background, ask the |
| 1123 | * MediaPlaybackService to pause playback. |
The Android Open Source Project | 9c9be2e | 2009-03-05 14:34:37 -0800 | [diff] [blame] | 1124 | */ |
| 1125 | private void pauseAudioPlayback() { |
| 1126 | // Shamelessly copied from MediaPlaybackService.java, which |
| 1127 | // should be public, but isn't. |
| 1128 | Intent i = new Intent("com.android.music.musicservicecommand"); |
| 1129 | i.putExtra("command", "pause"); |
| 1130 | |
| 1131 | sendBroadcast(i); |
| 1132 | } |
| 1133 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1134 | private void startVideoRecording() { |
| 1135 | Log.v(TAG, "startVideoRecording"); |
| 1136 | if (!mMediaRecorderRecording) { |
| 1137 | |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 1138 | if (mStorageStatus != STORAGE_STATUS_OK) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1139 | Log.v(TAG, "Storage issue, ignore the start request"); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
| 1143 | // Check mMediaRecorder to see whether it is initialized or not. |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 1144 | if (mMediaRecorder == null) { |
| 1145 | Log.e(TAG, "MediaRecorder is not initialized."); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1146 | return; |
| 1147 | } |
| 1148 | |
The Android Open Source Project | 9c9be2e | 2009-03-05 14:34:37 -0800 | [diff] [blame] | 1149 | pauseAudioPlayback(); |
| 1150 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1151 | try { |
| 1152 | mMediaRecorder.setOnErrorListener(this); |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1153 | mMediaRecorder.setOnInfoListener(this); |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1154 | mMediaRecorder.start(); // Recording is now started |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1155 | } catch (RuntimeException e) { |
| 1156 | Log.e(TAG, "Could not start media recorder. ", e); |
| 1157 | return; |
| 1158 | } |
| 1159 | mMediaRecorderRecording = true; |
| 1160 | mRecordingStartTime = SystemClock.uptimeMillis(); |
Wu-cheng Li | 46fc7ae | 2009-06-19 19:28:47 +0800 | [diff] [blame] | 1161 | updateRecordingIndicator(false); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1162 | mRecordingTimeView.setText(""); |
| 1163 | mRecordingTimeView.setVisibility(View.VISIBLE); |
Wu-cheng Li | 5f3fe87 | 2009-09-29 20:29:49 -0700 | [diff] [blame] | 1164 | updateRecordingTime(); |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1165 | keepScreenOn(); |
Chih-Chung Chang | fae0daa | 2009-09-21 05:36:24 +0800 | [diff] [blame] | 1166 | mGripper.setVisibility(View.INVISIBLE); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | private void updateRecordingIndicator(boolean showRecording) { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1171 | int drawableId = |
| 1172 | showRecording ? R.drawable.btn_ic_video_record |
| 1173 | : R.drawable.btn_ic_video_record_stop; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1174 | Drawable drawable = getResources().getDrawable(drawableId); |
| 1175 | mShutterButton.setImageDrawable(drawable); |
| 1176 | } |
| 1177 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 1178 | private void stopVideoRecordingAndGetThumbnail() { |
| 1179 | stopVideoRecording(); |
| 1180 | acquireVideoThumb(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1183 | private void stopVideoRecordingAndShowAlert() { |
| 1184 | stopVideoRecording(); |
| 1185 | showAlert(); |
| 1186 | } |
| 1187 | |
| 1188 | private void showAlert() { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1189 | fadeOut(findViewById(R.id.shutter_button)); |
| 1190 | if (mCurrentVideoFilename != null) { |
| 1191 | mVideoFrame.setImageBitmap( |
Ray Chen | 9f1480b | 2009-08-27 17:59:29 -0700 | [diff] [blame] | 1192 | ThumbnailUtil.createVideoThumbnail(mCurrentVideoFilename)); |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1193 | mVideoFrame.setVisibility(View.VISIBLE); |
| 1194 | } |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1195 | int[] pickIds = {R.id.btn_retake, R.id.btn_done, R.id.btn_play}; |
| 1196 | for (int id : pickIds) { |
| 1197 | View button = findViewById(id); |
| 1198 | fadeIn(((View) button.getParent())); |
| 1199 | } |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | private void hideAlert() { |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1203 | mVideoFrame.setVisibility(View.INVISIBLE); |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1204 | fadeIn(findViewById(R.id.shutter_button)); |
| 1205 | int[] pickIds = {R.id.btn_retake, R.id.btn_done, R.id.btn_play}; |
| 1206 | for (int id : pickIds) { |
| 1207 | View button = findViewById(id); |
| 1208 | fadeOut(((View) button.getParent())); |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1212 | private static void fadeIn(View view) { |
| 1213 | view.setVisibility(View.VISIBLE); |
| 1214 | Animation animation = new AlphaAnimation(0F, 1F); |
| 1215 | animation.setDuration(500); |
| 1216 | view.startAnimation(animation); |
| 1217 | } |
| 1218 | |
| 1219 | private static void fadeOut(View view) { |
| 1220 | view.setVisibility(View.INVISIBLE); |
| 1221 | Animation animation = new AlphaAnimation(1F, 0F); |
| 1222 | animation.setDuration(500); |
| 1223 | view.startAnimation(animation); |
| 1224 | } |
| 1225 | |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1226 | private boolean isAlertVisible() { |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1227 | return this.mVideoFrame.getVisibility() == View.VISIBLE; |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Wu-cheng Li | 46fc7ae | 2009-06-19 19:28:47 +0800 | [diff] [blame] | 1230 | private void stopVideoRecordingAndShowReview() { |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1231 | stopVideoRecording(); |
Wu-cheng Li | 46fc7ae | 2009-06-19 19:28:47 +0800 | [diff] [blame] | 1232 | if (mThumbController.isUriValid()) { |
| 1233 | Uri targetUri = mThumbController.getUri(); |
| 1234 | Intent intent = new Intent(this, ReviewImage.class); |
| 1235 | intent.setData(targetUri); |
| 1236 | intent.putExtra(MediaStore.EXTRA_FULL_SCREEN, true); |
| 1237 | intent.putExtra(MediaStore.EXTRA_SHOW_ACTION_ICONS, true); |
| 1238 | intent.putExtra("com.android.camera.ReviewMode", true); |
| 1239 | try { |
| 1240 | startActivity(intent); |
| 1241 | } catch (ActivityNotFoundException ex) { |
| 1242 | Log.e(TAG, "review video fail", ex); |
| 1243 | } |
| 1244 | } else { |
| 1245 | Log.e(TAG, "Can't view last video."); |
| 1246 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | private void stopVideoRecording() { |
| 1250 | Log.v(TAG, "stopVideoRecording"); |
| 1251 | boolean needToRegisterRecording = false; |
| 1252 | if (mMediaRecorderRecording || mMediaRecorder != null) { |
| 1253 | if (mMediaRecorderRecording && mMediaRecorder != null) { |
| 1254 | try { |
| 1255 | mMediaRecorder.setOnErrorListener(null); |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1256 | mMediaRecorder.setOnInfoListener(null); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1257 | mMediaRecorder.stop(); |
| 1258 | } catch (RuntimeException e) { |
| 1259 | Log.e(TAG, "stop fail: " + e.getMessage()); |
| 1260 | } |
The Android Open Source Project | 8d0dd0e | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 1261 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1262 | mCurrentVideoFilename = mCameraVideoFilename; |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 1263 | Log.v(TAG, "Setting current video filename: " |
| 1264 | + mCurrentVideoFilename); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1265 | needToRegisterRecording = true; |
| 1266 | mMediaRecorderRecording = false; |
| 1267 | } |
| 1268 | releaseMediaRecorder(); |
Wu-cheng Li | 46fc7ae | 2009-06-19 19:28:47 +0800 | [diff] [blame] | 1269 | updateRecordingIndicator(true); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1270 | mRecordingTimeView.setVisibility(View.GONE); |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1271 | keepScreenOnAwhile(); |
Chih-Chung Chang | fae0daa | 2009-09-21 05:36:24 +0800 | [diff] [blame] | 1272 | mGripper.setVisibility(View.VISIBLE); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1273 | } |
The Android Open Source Project | 9a379bd | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 1274 | if (needToRegisterRecording && mStorageStatus == STORAGE_STATUS_OK) { |
| 1275 | registerVideo(); |
| 1276 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1277 | |
| 1278 | mCameraVideoFilename = null; |
| 1279 | mCameraVideoFileDescriptor = null; |
| 1280 | } |
| 1281 | |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1282 | private void resetScreenOn() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1283 | mHandler.removeMessages(CLEAR_SCREEN_DELAY); |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1284 | getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1287 | private void keepScreenOnAwhile() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1288 | mHandler.removeMessages(CLEAR_SCREEN_DELAY); |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1289 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1290 | mHandler.sendEmptyMessageDelayed(CLEAR_SCREEN_DELAY, SCREEN_DELAY); |
| 1291 | } |
| 1292 | |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1293 | private void keepScreenOn() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1294 | mHandler.removeMessages(CLEAR_SCREEN_DELAY); |
Owen Lin | cc8d756 | 2009-09-16 14:43:09 +0800 | [diff] [blame] | 1295 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 1298 | private void hideAlertAndInitializeRecorder() { |
Wu-cheng Li | 84f9646 | 2009-06-19 20:11:44 +0800 | [diff] [blame] | 1299 | hideAlert(); |
Wu-cheng Li | b128446 | 2009-06-05 14:35:42 +0800 | [diff] [blame] | 1300 | mHandler.sendEmptyMessage(INIT_RECORDER); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 1303 | private void acquireVideoThumb() { |
Ray Chen | 9f1480b | 2009-08-27 17:59:29 -0700 | [diff] [blame] | 1304 | Bitmap videoFrame = ThumbnailUtil.createVideoThumbnail(mCurrentVideoFilename); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 1305 | mThumbController.setData(mCurrentVideoUri, videoFrame); |
The Android Open Source Project | de365d8 | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 1308 | private static ImageManager.DataLocation dataLocation() { |
| 1309 | return ImageManager.DataLocation.EXTERNAL; |
| 1310 | } |
| 1311 | |
| 1312 | private void updateLastVideo() { |
Chih-Chung Chang | f5bf8ca | 2009-08-25 18:28:29 +0800 | [diff] [blame] | 1313 | IImageList list = ImageManager.makeImageList( |
| 1314 | mContentResolver, |
| 1315 | dataLocation(), |
Chih-Chung Chang | 91acfc9 | 2009-07-06 15:37:24 +0800 | [diff] [blame] | 1316 | ImageManager.INCLUDE_VIDEOS, |
| 1317 | ImageManager.SORT_ASCENDING, |
Owen Lin | 61b9831 | 2009-06-19 00:21:10 -0700 | [diff] [blame] | 1318 | ImageManager.CAMERA_IMAGE_BUCKET_ID); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 1319 | int count = list.getCount(); |
| 1320 | if (count > 0) { |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1321 | IImage image = list.getImageAt(count - 1); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 1322 | Uri uri = image.fullSizeImageUri(); |
| 1323 | mThumbController.setData(uri, image.miniThumbBitmap()); |
| 1324 | } else { |
| 1325 | mThumbController.setData(null, null); |
| 1326 | } |
Chih-Chung Chang | f5bf8ca | 2009-08-25 18:28:29 +0800 | [diff] [blame] | 1327 | list.close(); |
Chih-Chung Chang | 9bc8d1b | 2009-03-24 19:22:29 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1330 | private void updateRecordingTime() { |
Ray Chen | 23c51b7 | 2009-04-10 03:41:00 -0700 | [diff] [blame] | 1331 | if (!mMediaRecorderRecording) { |
| 1332 | return; |
| 1333 | } |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1334 | long now = SystemClock.uptimeMillis(); |
| 1335 | long delta = now - mRecordingStartTime; |
| 1336 | |
| 1337 | // Starting a minute before reaching the max duration |
| 1338 | // limit, we'll countdown the remaining time instead. |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1339 | boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 |
Wu-cheng Li | b6ed73e | 2009-07-08 13:55:06 +0800 | [diff] [blame] | 1340 | && delta >= mMaxVideoDurationInMs - 60000); |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1341 | |
Wu-cheng Li | 5f3fe87 | 2009-09-29 20:29:49 -0700 | [diff] [blame] | 1342 | long next_update_delay = 1000 - (delta % 1000); |
| 1343 | long seconds; |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1344 | if (countdownRemainingTime) { |
Chih-Chung Chang | 82ded20 | 2009-05-20 14:24:25 +0800 | [diff] [blame] | 1345 | delta = Math.max(0, mMaxVideoDurationInMs - delta); |
Wu-cheng Li | 5f3fe87 | 2009-09-29 20:29:49 -0700 | [diff] [blame] | 1346 | seconds = (delta + 999) / 1000; |
| 1347 | } else { |
| 1348 | seconds = delta / 1000; // round to nearest |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1351 | long minutes = seconds / 60; |
| 1352 | long hours = minutes / 60; |
| 1353 | long remainderMinutes = minutes - (hours * 60); |
| 1354 | long remainderSeconds = seconds - (minutes * 60); |
| 1355 | |
| 1356 | String secondsString = Long.toString(remainderSeconds); |
| 1357 | if (secondsString.length() < 2) { |
| 1358 | secondsString = "0" + secondsString; |
| 1359 | } |
| 1360 | String minutesString = Long.toString(remainderMinutes); |
| 1361 | if (minutesString.length() < 2) { |
| 1362 | minutesString = "0" + minutesString; |
| 1363 | } |
| 1364 | String text = minutesString + ":" + secondsString; |
| 1365 | if (hours > 0) { |
| 1366 | String hoursString = Long.toString(hours); |
| 1367 | if (hoursString.length() < 2) { |
| 1368 | hoursString = "0" + hoursString; |
| 1369 | } |
| 1370 | text = hoursString + ":" + text; |
| 1371 | } |
| 1372 | mRecordingTimeView.setText(text); |
| 1373 | |
| 1374 | if (mRecordingTimeCountsDown != countdownRemainingTime) { |
| 1375 | // Avoid setting the color on every update, do it only |
| 1376 | // when it needs changing. |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1377 | mRecordingTimeCountsDown = countdownRemainingTime; |
| 1378 | |
Owen Lin | 482a3a5 | 2009-06-26 15:47:39 -0700 | [diff] [blame] | 1379 | int color = getResources().getColor(countdownRemainingTime |
| 1380 | ? R.color.recording_time_remaining_text |
| 1381 | : R.color.recording_time_elapsed_text); |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1382 | |
| 1383 | mRecordingTimeView.setTextColor(color); |
| 1384 | } |
| 1385 | |
Wu-cheng Li | 13874d7 | 2009-10-05 15:23:01 -0700 | [diff] [blame] | 1386 | mHandler.sendEmptyMessageDelayed( |
| 1387 | UPDATE_RECORD_TIME, next_update_delay); |
Chih-Chung Chang | d8209aa | 2009-04-07 11:45:12 -0700 | [diff] [blame] | 1388 | } |
Owen Lin | e239acc | 2009-06-23 22:18:20 -0700 | [diff] [blame] | 1389 | |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1390 | private void setCameraParameters() { |
| 1391 | mParameters = mCameraDevice.getParameters(); |
| 1392 | |
| 1393 | mParameters.setPreviewSize(mProfile.mVideoWidth, mProfile.mVideoHeight); |
Chih-Chung Chang | 207ad85 | 2009-09-28 14:36:37 -0700 | [diff] [blame] | 1394 | mParameters.setPreviewFrameRate(mProfile.mVideoFps); |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1395 | |
| 1396 | // Set white balance parameter. |
| 1397 | if (mParameters.getSupportedWhiteBalance() != null) { |
| 1398 | String whiteBalance = mPreferences.getString( |
| 1399 | CameraSettings.KEY_WHITE_BALANCE, |
| 1400 | getString(R.string.pref_camera_whitebalance_default)); |
| 1401 | mParameters.setWhiteBalance(whiteBalance); |
| 1402 | } |
| 1403 | |
| 1404 | // Set color effect parameter. |
| 1405 | if (mParameters.getSupportedColorEffects() != null) { |
| 1406 | String colorEffect = mPreferences.getString( |
| 1407 | CameraSettings.KEY_COLOR_EFFECT, |
| 1408 | getString(R.string.pref_camera_coloreffect_default)); |
| 1409 | mParameters.setColorEffect(colorEffect); |
| 1410 | } |
Wu-cheng Li | ffde05c | 2009-09-15 18:52:25 +0800 | [diff] [blame] | 1411 | |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1412 | mCameraDevice.setParameters(mParameters); |
| 1413 | } |
| 1414 | |
Chih-Chung Chang | d918cfa | 2009-07-13 13:11:52 +0800 | [diff] [blame] | 1415 | public boolean onSwitchChanged(Switcher source, boolean onOff) { |
Owen Lin | e239acc | 2009-06-23 22:18:20 -0700 | [diff] [blame] | 1416 | if (onOff == SWITCH_CAMERA) { |
| 1417 | MenuHelper.gotoCameraMode(this); |
Owen Lin | 3283e04 | 2009-06-26 11:59:58 -0700 | [diff] [blame] | 1418 | finish(); |
Owen Lin | e239acc | 2009-06-23 22:18:20 -0700 | [diff] [blame] | 1419 | } |
Chih-Chung Chang | d918cfa | 2009-07-13 13:11:52 +0800 | [diff] [blame] | 1420 | return true; |
Owen Lin | e239acc | 2009-06-23 22:18:20 -0700 | [diff] [blame] | 1421 | } |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1422 | |
| 1423 | public void onSharedPreferenceChanged( |
| 1424 | SharedPreferences preferences, String key) { |
| 1425 | // ignore the events after "onPause()" or preview has not started yet |
| 1426 | if (mPausing) return; |
| 1427 | |
| 1428 | if (CameraSettings.KEY_VIDEO_DURATION.equals(key) |
| 1429 | || CameraSettings.KEY_VIDEO_QUALITY.equals(key)) { |
| 1430 | readVideoPreferences(); |
| 1431 | } |
| 1432 | |
| 1433 | // If mCameraDevice is not ready then we can set the parameter in |
| 1434 | // startPreview(). |
| 1435 | if (mCameraDevice == null) return; |
| 1436 | |
Wu-cheng Li | ffde05c | 2009-09-15 18:52:25 +0800 | [diff] [blame] | 1437 | // We need to restart the preview if preview size is changed. |
| 1438 | Size size = mParameters.getPreviewSize(); |
| 1439 | if (size.width != mProfile.mVideoWidth |
| 1440 | || size.height != mProfile.mVideoHeight) { |
| 1441 | // It is assumed media recorder is released before |
| 1442 | // onSharedPreferenceChanged, so we can close the camera here. |
| 1443 | closeCamera(); |
| 1444 | try { |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 1445 | resizeForPreviewAspectRatio(); |
Wu-cheng Li | ffde05c | 2009-09-15 18:52:25 +0800 | [diff] [blame] | 1446 | startPreview(); // Parameters will be set in startPreview(). |
| 1447 | } catch (CameraHardwareException e) { |
| 1448 | showCameraBusyAndFinish(); |
| 1449 | } |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1450 | } else { |
Wu-cheng Li | d0759fe | 2009-09-18 14:10:47 +0800 | [diff] [blame] | 1451 | try { |
| 1452 | // We need to lock the camera before writing parameters. |
| 1453 | mCameraDevice.lock(); |
| 1454 | } catch (RuntimeException e) { |
| 1455 | // When preferences are added for the first time, this method |
| 1456 | // will be called. But OnScreenSetting is not displayed yet and |
| 1457 | // media recorder still owns the camera. Lock will fail and we |
| 1458 | // just ignore it. |
| 1459 | return; |
Wu-cheng Li | ffde05c | 2009-09-15 18:52:25 +0800 | [diff] [blame] | 1460 | } |
Wu-cheng Li | d0759fe | 2009-09-18 14:10:47 +0800 | [diff] [blame] | 1461 | setCameraParameters(); |
| 1462 | mCameraDevice.unlock(); |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1463 | } |
Owen Lin | 50c6004 | 2009-09-10 14:43:42 +0800 | [diff] [blame] | 1464 | } |
Wei-Ta Chen | 1f0564e | 2009-09-28 18:38:05 -0700 | [diff] [blame] | 1465 | |
| 1466 | public void onSizeChanged() { |
| 1467 | if (mSettings != null) { |
| 1468 | mSettings.updateLayout(); |
| 1469 | } |
| 1470 | |
| 1471 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1472 | } |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1473 | |
| 1474 | // |
| 1475 | // DefaultHashMap is a HashMap which returns a default value if the specified |
| 1476 | // key is not found. |
| 1477 | // |
Owen Lin | 71d56e2 | 2009-09-23 22:27:04 +0800 | [diff] [blame] | 1478 | @SuppressWarnings("serial") |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1479 | class DefaultHashMap<K, V> extends HashMap<K, V> { |
| 1480 | private V mDefaultValue; |
| 1481 | |
| 1482 | public void putDefault(V defaultValue) { |
| 1483 | mDefaultValue = defaultValue; |
| 1484 | } |
| 1485 | |
| 1486 | @Override |
| 1487 | public V get(Object key) { |
| 1488 | V value = super.get(key); |
| 1489 | return (value == null) ? mDefaultValue : value; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | // |
| 1494 | // MediaRecorderProfile reads from system properties to determine the proper |
| 1495 | // values for various parameters for MediaRecorder. |
| 1496 | // |
| 1497 | class MediaRecorderProfile { |
Owen Lin | e594b19 | 2009-08-13 18:04:45 +0800 | [diff] [blame] | 1498 | |
| 1499 | @SuppressWarnings("unused") |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1500 | private static final String TAG = "MediaRecorderProfile"; |
| 1501 | public final boolean mHiQuality; |
| 1502 | public final int mOutputFormat; |
| 1503 | public final int mVideoEncoder; |
| 1504 | public final int mAudioEncoder; |
| 1505 | public final int mVideoWidth; |
| 1506 | public final int mVideoHeight; |
Chih-Chung Chang | 947455a | 2009-07-29 14:50:07 +0800 | [diff] [blame] | 1507 | public final int mVideoFps; |
| 1508 | public final int mVideoBitrate; |
| 1509 | public final int mAudioBitrate; |
| 1510 | public final int mAudioChannels; |
| 1511 | public final int mAudioSamplingRate; |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1512 | |
| 1513 | MediaRecorderProfile(boolean hiQuality) { |
| 1514 | mHiQuality = hiQuality; |
| 1515 | |
| 1516 | mOutputFormat = getFromTable("ro.media.enc.hprof.file.format", |
| 1517 | "ro.media.enc.lprof.file.format", |
| 1518 | OUTPUT_FORMAT_TABLE); |
| 1519 | |
| 1520 | mVideoEncoder = getFromTable("ro.media.enc.hprof.codec.vid", |
| 1521 | "ro.media.enc.lprof.codec.vid", |
| 1522 | VIDEO_ENCODER_TABLE); |
| 1523 | |
| 1524 | mAudioEncoder = getFromTable("ro.media.enc.hprof.codec.aud", |
| 1525 | "ro.media.enc.lprof.codec.aud", |
| 1526 | AUDIO_ENCODER_TABLE); |
| 1527 | |
| 1528 | mVideoWidth = getInt("ro.media.enc.hprof.vid.width", |
| 1529 | "ro.media.enc.lprof.vid.width", |
| 1530 | 352, 176); |
| 1531 | |
| 1532 | mVideoHeight = getInt("ro.media.enc.hprof.vid.height", |
| 1533 | "ro.media.enc.lprof.vid.height", |
| 1534 | 288, 144); |
| 1535 | |
Chih-Chung Chang | 947455a | 2009-07-29 14:50:07 +0800 | [diff] [blame] | 1536 | mVideoFps = getInt("ro.media.enc.hprof.vid.fps", |
| 1537 | "ro.media.enc.lprof.vid.fps", |
| 1538 | 20, 20); |
| 1539 | |
| 1540 | mVideoBitrate = getInt("ro.media.enc.hprof.vid.bps", |
| 1541 | "ro.media.enc.lprof.vid.bps", |
| 1542 | 360000, 192000); |
| 1543 | |
| 1544 | mAudioBitrate = getInt("ro.media.enc.hprof.aud.bps", |
| 1545 | "ro.media.enc.lprof.aud.bps", |
| 1546 | 23450, 23450); |
| 1547 | |
| 1548 | mAudioChannels = getInt("ro.media.enc.hprof.aud.ch", |
| 1549 | "ro.media.enc.lprof.aud.ch", |
| 1550 | 1, 1); |
| 1551 | |
| 1552 | mAudioSamplingRate = getInt("ro.media.enc.hprof.aud.hz", |
| 1553 | "ro.media.enc.lprof.aud.hz", |
| 1554 | 8000, 8000); |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | private int getFromTable(String highKey, String lowKey, |
| 1558 | DefaultHashMap<String, Integer> table) { |
| 1559 | String s; |
| 1560 | s = SystemProperties.get(mHiQuality ? highKey : lowKey); |
| 1561 | return table.get(s); |
| 1562 | } |
| 1563 | |
| 1564 | private int getInt(String highKey, String lowKey, int highDefault, |
| 1565 | int lowDefault) { |
| 1566 | String key = mHiQuality ? highKey : lowKey; |
| 1567 | int defaultValue = mHiQuality ? highDefault : lowDefault; |
| 1568 | return SystemProperties.getInt(key, defaultValue); |
| 1569 | } |
| 1570 | |
| 1571 | private static final DefaultHashMap<String, Integer> |
| 1572 | OUTPUT_FORMAT_TABLE = new DefaultHashMap<String, Integer>(); |
| 1573 | private static final DefaultHashMap<String, Integer> |
| 1574 | VIDEO_ENCODER_TABLE = new DefaultHashMap<String, Integer>(); |
| 1575 | private static final DefaultHashMap<String, Integer> |
| 1576 | AUDIO_ENCODER_TABLE = new DefaultHashMap<String, Integer>(); |
| 1577 | |
| 1578 | static { |
| 1579 | OUTPUT_FORMAT_TABLE.put("3gp", MediaRecorder.OutputFormat.THREE_GPP); |
| 1580 | OUTPUT_FORMAT_TABLE.put("mp4", MediaRecorder.OutputFormat.MPEG_4); |
| 1581 | OUTPUT_FORMAT_TABLE.putDefault(MediaRecorder.OutputFormat.DEFAULT); |
| 1582 | |
| 1583 | VIDEO_ENCODER_TABLE.put("h263", MediaRecorder.VideoEncoder.H263); |
| 1584 | VIDEO_ENCODER_TABLE.put("h264", MediaRecorder.VideoEncoder.H264); |
Snigdha | eb6a553 | 2009-08-06 15:06:00 -0500 | [diff] [blame] | 1585 | VIDEO_ENCODER_TABLE.put("m4v", MediaRecorder.VideoEncoder.MPEG_4_SP); |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1586 | VIDEO_ENCODER_TABLE.putDefault(MediaRecorder.VideoEncoder.DEFAULT); |
| 1587 | |
| 1588 | AUDIO_ENCODER_TABLE.put("amrnb", MediaRecorder.AudioEncoder.AMR_NB); |
| 1589 | AUDIO_ENCODER_TABLE.put("amrwb", MediaRecorder.AudioEncoder.AMR_WB); |
| 1590 | AUDIO_ENCODER_TABLE.put("aac", MediaRecorder.AudioEncoder.AAC); |
| 1591 | AUDIO_ENCODER_TABLE.put("aacplus", MediaRecorder.AudioEncoder.AAC_PLUS); |
Chih-Chung Chang | 522e836 | 2009-08-26 16:12:34 +0800 | [diff] [blame] | 1592 | AUDIO_ENCODER_TABLE.put("eaacplus", |
| 1593 | MediaRecorder.AudioEncoder.EAAC_PLUS); |
Chih-Chung Chang | 97601c7 | 2009-07-27 19:19:10 +0800 | [diff] [blame] | 1594 | AUDIO_ENCODER_TABLE.putDefault(MediaRecorder.AudioEncoder.DEFAULT); |
| 1595 | } |
| 1596 | } |