ActivityBase.java revision 2a86dea8eb78891c364e5083d942c05e6019b5c6
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera;
18
19import android.app.KeyguardManager;
20import android.content.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ActivityInfo;
24import android.hardware.Camera.Parameters;
25import android.os.AsyncTask;
26import android.os.Bundle;
27import android.util.Log;
28import android.view.animation.DecelerateInterpolator;
29import android.view.KeyEvent;
30import android.view.View;
31import android.view.Window;
32import android.view.WindowManager;
33
34import com.android.camera.ui.PopupManager;
35import com.android.camera.ui.RotateImageView;
36import com.android.gallery3d.app.AbstractGalleryActivity;
37import com.android.gallery3d.app.PhotoPage;
38import com.android.gallery3d.app.PhotoPage.PageTapListener;
39import com.android.gallery3d.app.GalleryActionBar;
40import com.android.gallery3d.app.StateManager;
41import com.android.gallery3d.util.MediaSetUtils;
42
43import java.io.File;
44
45/**
46 * Superclass of Camera and VideoCamera activities.
47 */
48abstract public class ActivityBase extends AbstractGalleryActivity
49        implements CameraScreenNail.PositionChangedListener,
50                View.OnLayoutChangeListener, PageTapListener {
51
52    private static final String TAG = "ActivityBase";
53    private static boolean LOGV = false;
54    private static final int CAMERA_APP_VIEW_TOGGLE_TIME = 100;  // milliseconds
55    private int mResultCodeForTesting;
56    private Intent mResultDataForTesting;
57    private OnScreenHint mStorageHint;
58    private UpdateCameraAppView mUpdateCameraAppView;
59    private HideCameraAppView mHideCameraAppView;
60    private View mSingleTapArea;
61
62    // The bitmap of the last captured picture thumbnail and the URI of the
63    // original picture.
64    protected Thumbnail mThumbnail;
65    // An imageview showing showing the last captured picture thumbnail.
66    protected RotateImageView mThumbnailView;
67    protected int mThumbnailViewWidth; // layout width of the thumbnail
68    protected AsyncTask<Void, Void, Thumbnail> mLoadThumbnailTask;
69    protected boolean mOpenCameraFail;
70    protected boolean mCameraDisabled;
71    protected CameraManager.CameraProxy mCameraDevice;
72    protected Parameters mParameters;
73    // The activity is paused. The classes that extend this class should set
74    // mPaused the first thing in onResume/onPause.
75    protected boolean mPaused;
76    protected GalleryActionBar mActionBar;
77
78    // multiple cameras support
79    protected int mNumberOfCameras;
80    protected int mCameraId;
81
82    protected CameraScreenNail mCameraScreenNail; // This shows camera preview.
83    // The view containing only camera related widgets like control panel,
84    // indicator bar, focus indicator and etc.
85    protected View mCameraAppView;
86    protected boolean mShowCameraAppView = true;
87
88    protected class CameraOpenThread extends Thread {
89        @Override
90        public void run() {
91            try {
92                mCameraDevice = Util.openCamera(ActivityBase.this, mCameraId);
93                mParameters = mCameraDevice.getParameters();
94            } catch (CameraHardwareException e) {
95                mOpenCameraFail = true;
96            } catch (CameraDisabledException e) {
97                mCameraDisabled = true;
98            }
99        }
100    }
101
102    @Override
103    public void onCreate(Bundle icicle) {
104        if (Util.isTabletUI()) {
105            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
106        } else {
107            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
108        }
109        requestWindowFeature(Window.FEATURE_ACTION_BAR);
110        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
111        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
112        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
113        super.disableToggleStatusBar();
114        super.onCreate(icicle);
115        // The full screen mode might be turned off previously. Add the flag again.
116        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
117        mActionBar = new GalleryActionBar(this);
118    }
119
120    @Override
121    protected void onPause() {
122        super.onPause();
123        if (LOGV) Log.v(TAG, "onPause");
124        saveThumbnailToFile();
125
126        if (mLoadThumbnailTask != null) {
127            mLoadThumbnailTask.cancel(true);
128            mLoadThumbnailTask = null;
129        }
130
131        if (mStorageHint != null) {
132            mStorageHint.cancel();
133            mStorageHint = null;
134        }
135    }
136
137    @Override
138    public boolean onSearchRequested() {
139        return false;
140    }
141
142    @Override
143    public boolean onKeyDown(int keyCode, KeyEvent event) {
144        // Prevent software keyboard or voice search from showing up.
145        if (keyCode == KeyEvent.KEYCODE_SEARCH
146                || keyCode == KeyEvent.KEYCODE_MENU) {
147            if (event.isLongPress()) return true;
148        }
149
150        return super.onKeyDown(keyCode, event);
151    }
152
153    protected void setResultEx(int resultCode) {
154        mResultCodeForTesting = resultCode;
155        setResult(resultCode);
156    }
157
158    protected void setResultEx(int resultCode, Intent data) {
159        mResultCodeForTesting = resultCode;
160        mResultDataForTesting = data;
161        setResult(resultCode, data);
162    }
163
164    public int getResultCode() {
165        return mResultCodeForTesting;
166    }
167
168    public Intent getResultData() {
169        return mResultDataForTesting;
170    }
171
172    @Override
173    protected void onDestroy() {
174        PopupManager.removeInstance(this);
175        super.onDestroy();
176    }
177
178    protected void updateStorageHint(long storageSpace) {
179        String message = null;
180        if (storageSpace == Storage.UNAVAILABLE) {
181            message = getString(R.string.no_storage);
182        } else if (storageSpace == Storage.PREPARING) {
183            message = getString(R.string.preparing_sd);
184        } else if (storageSpace == Storage.UNKNOWN_SIZE) {
185            message = getString(R.string.access_sd_fail);
186        } else if (storageSpace < Storage.LOW_STORAGE_THRESHOLD) {
187            message = getString(R.string.spaceIsLow_content);
188        }
189
190        if (message != null) {
191            if (mStorageHint == null) {
192                mStorageHint = OnScreenHint.makeText(this, message);
193            } else {
194                mStorageHint.setText(message);
195            }
196            mStorageHint.show();
197        } else if (mStorageHint != null) {
198            mStorageHint.cancel();
199            mStorageHint = null;
200        }
201    }
202
203    private void updateThumbnailView() {
204        if (mThumbnail != null) {
205            mThumbnailView.setBitmap(mThumbnail.getBitmap());
206            mThumbnailView.setVisibility(View.VISIBLE);
207        } else {
208            mThumbnailView.setBitmap(null);
209            mThumbnailView.setVisibility(View.GONE);
210        }
211    }
212
213    protected void getLastThumbnail() {
214        mThumbnail = ThumbnailHolder.getLastThumbnail(getContentResolver());
215        // Suppose users tap the thumbnail view, go to the gallery, delete the
216        // image, and coming back to the camera. Thumbnail file will be invalid.
217        // Since the new thumbnail will be loaded in another thread later, the
218        // view should be set to gone to prevent from opening the invalid image.
219        updateThumbnailView();
220        if (mThumbnail == null) {
221            mLoadThumbnailTask = new LoadThumbnailTask().execute();
222        }
223    }
224
225    private class LoadThumbnailTask extends AsyncTask<Void, Void, Thumbnail> {
226        @Override
227        protected Thumbnail doInBackground(Void... params) {
228            // Load the thumbnail from the file.
229            ContentResolver resolver = getContentResolver();
230            Thumbnail t = Thumbnail.getLastThumbnailFromFile(getFilesDir(), resolver);
231
232            if (isCancelled()) return null;
233
234            if (t == null) {
235                // Load the thumbnail from the media provider.
236                t = Thumbnail.getLastThumbnailFromContentResolver(resolver);
237            }
238            return t;
239        }
240
241        @Override
242        protected void onPostExecute(Thumbnail thumbnail) {
243            mThumbnail = thumbnail;
244            updateThumbnailView();
245        }
246    }
247
248    protected void gotoGallery() {
249        PhotoPage photoPage = (PhotoPage) getStateManager().getTopState();
250        // Move the next picture with capture animation. "1" means next.
251        photoPage.switchWithCaptureAnimation(1);
252    }
253
254    protected void saveThumbnailToFile() {
255        if (mThumbnail != null && !mThumbnail.fromFile()) {
256            new SaveThumbnailTask().execute(mThumbnail);
257        }
258    }
259
260    private class SaveThumbnailTask extends AsyncTask<Thumbnail, Void, Void> {
261        @Override
262        protected Void doInBackground(Thumbnail... params) {
263            final int n = params.length;
264            final File filesDir = getFilesDir();
265            for (int i = 0; i < n; i++) {
266                params[i].saveLastThumbnailToFile(filesDir);
267            }
268            return null;
269        }
270    }
271
272    // Call this after setContentView.
273    protected void createCameraScreenNail(boolean getPictures) {
274        mCameraAppView = findViewById(R.id.camera_app_root);
275        Bundle data = new Bundle();
276        String path = "/local/all/";
277        // Intent mode does not show camera roll. Use 0 as a work around for
278        // invalid bucket id.
279        // TODO: add support of empty media set in gallery.
280        path += (getPictures ? MediaSetUtils.CAMERA_BUCKET_ID : "0");
281        data.putString(PhotoPage.KEY_MEDIA_SET_PATH, path);
282        data.putString(PhotoPage.KEY_MEDIA_ITEM_PATH, path);
283
284        // Send a CameraScreenNail to gallery to enable the camera preview.
285        CameraScreenNailHolder holder = new CameraScreenNailHolder(this);
286        data.putParcelable(PhotoPage.KEY_SCREENNAIL_HOLDER, holder);
287        getStateManager().startState(PhotoPage.class, data);
288        mCameraScreenNail = holder.getCameraScreenNail();
289        mCameraScreenNail.setPositionChangedListener(this);
290    }
291
292    private class HideCameraAppView implements Runnable {
293        @Override
294        public void run() {
295            mCameraAppView.setVisibility(View.GONE);
296        }
297    }
298    private class UpdateCameraAppView implements Runnable {
299        @Override
300        public void run() {
301            if (mShowCameraAppView) {
302                mCameraAppView.setVisibility(View.VISIBLE);
303                mCameraAppView.animate()
304                        .setDuration(CAMERA_APP_VIEW_TOGGLE_TIME)
305                        .withLayer().alpha(1);
306            } else {
307                mCameraAppView.animate()
308                        .setDuration(CAMERA_APP_VIEW_TOGGLE_TIME)
309                        .withLayer().alpha(0).withEndAction(mHideCameraAppView);
310            }
311        }
312    }
313
314    @Override
315    public void onPositionChanged(int x, int y, int width, int height, boolean visible) {
316        if (!mPaused && !isFinishing()) {
317            View rootView = (View) getGLRoot();
318            int rootWidth = rootView.getWidth();
319            int rootHeight = rootView.getHeight();
320            boolean showCameraAppView;
321            // Check if the camera preview is in the center.
322            if (visible && (x == 0 && width == rootWidth) ||
323                    (y == 0 && height == rootHeight && Math.abs(x - (rootWidth - width) / 2) <= 1)) {
324                showCameraAppView = true;
325            } else {
326                showCameraAppView = false;
327            }
328
329            if (mShowCameraAppView != showCameraAppView) {
330                mShowCameraAppView = showCameraAppView;
331                // Initialize the animation.
332                if (mUpdateCameraAppView == null) {
333                    mUpdateCameraAppView = new UpdateCameraAppView();
334                    mHideCameraAppView = new HideCameraAppView();
335                    mCameraAppView.animate()
336                        .setInterpolator(new DecelerateInterpolator());
337                }
338                runOnUiThread(mUpdateCameraAppView);
339            }
340        }
341    }
342
343    @Override
344    public GalleryActionBar getGalleryActionBar() {
345        return mActionBar;
346    }
347
348    // Preview frame layout has changed. Move the preview to the center of the
349    // layout.
350    @Override
351    public void onLayoutChange(View v, int left, int top, int right, int bottom,
352            int oldLeft, int oldTop, int oldRight, int oldBottom) {
353        // Find out the left and top of the preview frame layout relative to GL
354        // root view.
355        View root = (View) getGLRoot();
356        int[] rootLocation = new int[2];
357        int[] viewLocation = new int[2];
358        root.getLocationInWindow(rootLocation);
359        v.getLocationInWindow(viewLocation);
360        int relativeLeft = viewLocation[0] - rootLocation[0];
361        int relativeTop = viewLocation[1] - rootLocation[1];
362
363        // Calculate the scale ratio between preview frame layout and GL root
364        // view.
365        int width = root.getWidth();
366        int height = root.getHeight();
367        float scale = Math.max((float) (right - left) / width,
368                (float) (bottom - top) / height);
369        float scalePx = width / 2f;
370        float scalePy = height / 2f;
371
372        // Calculate the translate distance.
373        float translateX = relativeLeft + (right - left - width) / 2f;
374        float translateY = relativeTop + (bottom - top - height) / 2f;
375
376        mCameraScreenNail.setMatrix(scale, scalePx, scalePy, translateX, translateY);
377    }
378
379    protected void setSingleTapUpListener(View singleTapArea) {
380        PhotoPage photoPage = (PhotoPage) getStateManager().getTopState();
381        photoPage.setPageTapListener(this);
382        mSingleTapArea = singleTapArea;
383    }
384
385    // Single tap up from PhotoPage.
386    @Override
387    public boolean onSingleTapUp(int x, int y) {
388        // Camera control is invisible. Ignore.
389        if (!mShowCameraAppView) return false;
390
391        int[] relativeLocation = Util.getRelativeLocation((View) getGLRoot(),
392                mSingleTapArea);
393        x -= relativeLocation[0];
394        y -= relativeLocation[1];
395        if (x >= 0 && x < mSingleTapArea.getWidth() && y >= 0
396                && y < mSingleTapArea.getHeight()) {
397            onSingleTapUp(mSingleTapArea, x, y);
398            return true;
399        }
400        return false;
401    }
402
403    protected void onSingleTapUp(View view, int x, int y) {
404    }
405}
406