CameraActivity.java revision b19c713286b7beed376ec6c4390e7272a80aa5b9
1/*
2 * Copyright (C) 2012 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.content.Intent;
20import android.content.res.Configuration;
21import android.graphics.drawable.Drawable;
22import android.os.Bundle;
23import android.util.Log;
24import android.view.KeyEvent;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.ViewGroup.LayoutParams;
28import android.widget.FrameLayout;
29import android.widget.ImageView;
30
31import com.android.camera.ui.CameraSwitcher;
32
33public class CameraActivity extends ActivityBase
34        implements CameraSwitcher.CameraSwitchListener {
35
36    CameraModule mCurrentModule;
37    private FrameLayout mFrame;
38    private ShutterButton mShutter;
39    private CameraSwitcher mSwitcher;
40    private Drawable[] mDrawables;
41    private int mSelectedModule;
42
43    private static final String TAG = "CAM_activity";
44
45    private static final int[] DRAW_IDS = {
46            R.drawable.ic_switch_video_holo_light,
47            R.drawable.ic_switch_camera_holo_light,
48            R.drawable.ic_switch_pan_holo_light
49    };
50
51    public void onCreate(Bundle state) {
52        super.onCreate(state);
53        setContentView(R.layout.camera_main);
54        mFrame =(FrameLayout) findViewById(R.id.main_content);
55        mShutter = (ShutterButton) findViewById(R.id.shutter_button);
56        mSwitcher = (CameraSwitcher) findViewById(R.id.camera_switcher);
57        mDrawables = new Drawable[DRAW_IDS.length];
58        for (int i = 0; i < DRAW_IDS.length; i++) {
59            Drawable d = getResources().getDrawable(DRAW_IDS[i]);
60            mDrawables[i] = d;
61        }
62        for (int i = 0; i < mDrawables.length; i++) {
63            ImageView iv = new ImageView(this);
64            iv.setImageDrawable(mDrawables[i]);
65            mSwitcher.add(iv, new LayoutParams(LayoutParams.WRAP_CONTENT,
66                    LayoutParams.WRAP_CONTENT));
67        }
68
69        mSwitcher.setSwitchListener(this);
70        mCurrentModule = new PhotoModule();
71        mCurrentModule.init(this, mFrame);
72        mSelectedModule = 1;
73        mSwitcher.setCurrentModule(mSelectedModule);
74    }
75
76    @Override
77    public void onScroll() {
78    }
79
80    @Override
81    public void onCameraSelected(int i) {
82        if (i != mSelectedModule) {
83            mPaused = true;
84            closeModule(mCurrentModule);
85            mSelectedModule = i;
86            switch (i) {
87            case 0:
88                mCurrentModule = new VideoModule();
89                break;
90            case 1:
91                mCurrentModule = new PhotoModule();
92                break;
93            case 2:
94                mCurrentModule = new VideoModule();
95                break;
96            }
97            openModule(mCurrentModule);
98        }
99    }
100
101    private void openModule(CameraModule module) {
102        module.init(this, mFrame);
103        mPaused = false;
104        module.onResumeBeforeSuper();
105        module.onResumeAfterSuper();
106    }
107
108    private void closeModule(CameraModule module) {
109        module.onPauseBeforeSuper();
110        module.onPauseAfterSuper();
111        mFrame.removeAllViews();
112    }
113
114    public ShutterButton getShutterButton() {
115        return mShutter;
116    }
117
118    public void hideUI() {
119        mSwitcher.setVisibility(View.INVISIBLE);
120        mShutter.setVisibility(View.INVISIBLE);
121    }
122
123    public void showUI() {
124        mSwitcher.setVisibility(View.VISIBLE);
125        mShutter.setVisibility(View.VISIBLE);
126    }
127
128    @Override
129    public void onConfigurationChanged(Configuration config) {
130        super.onConfigurationChanged(config);
131        mCurrentModule.onConfigurationChanged(config);
132    }
133
134    @Override
135    public void onPause() {
136        mPaused = true;
137        mCurrentModule.onPauseBeforeSuper();
138        super.onPause();
139        mCurrentModule.onPauseAfterSuper();
140    }
141
142    @Override
143    public void onResume() {
144        mPaused = false;
145        mCurrentModule.onResumeBeforeSuper();
146        super.onResume();
147        mCurrentModule.onResumeAfterSuper();
148    }
149
150    @Override
151    protected void onFullScreenChanged(boolean full) {
152        super.onFullScreenChanged(full);
153        mCurrentModule.onFullScreenChanged(full);
154    }
155
156    @Override
157    protected void onStop() {
158        super.onStop();
159        mCurrentModule.onStop();
160    }
161
162    @Override
163    protected void onNewIntent(Intent intent) {
164        super.onNewIntent(intent);
165        getStateManager().clearActivityResult();
166    }
167
168    @Override
169    protected void installIntentFilter() {
170        super.installIntentFilter();
171        mCurrentModule.installIntentFilter();
172    }
173
174    @Override
175    protected void onActivityResult(
176            int requestCode, int resultCode, Intent data) {
177        super.onActivityResult(requestCode, resultCode, data);
178        mCurrentModule.onActivityResult(requestCode, resultCode, data);
179    }
180
181    // Preview area is touched. Handle touch focus.
182    @Override
183    protected void onSingleTapUp(View view, int x, int y) {
184        mCurrentModule.onSingleTapUp(view, x, y);
185    }
186
187    @Override
188    public void onBackPressed() {
189        if (!mCurrentModule.onBackPressed()) {
190            super.onBackPressed();
191        }
192    }
193
194    @Override
195    public boolean onKeyDown(int keyCode, KeyEvent event) {
196        return mCurrentModule.onKeyDown(keyCode,  event)
197                || super.onKeyDown(keyCode, event);
198    }
199
200    @Override
201    public boolean onKeyUp(int keyCode, KeyEvent event) {
202        return mCurrentModule.onKeyUp(keyCode,  event)
203                || super.onKeyUp(keyCode, event);
204    }
205
206    @Override
207    public boolean dispatchTouchEvent(MotionEvent m) {
208        return mCurrentModule.dispatchTouchEvent(m)
209                || super.dispatchTouchEvent(m);
210    }
211
212    // Preview texture has been copied. Now camera can be released and the
213    // animation can be started.
214    @Override
215    public void onPreviewTextureCopied() {
216        mCurrentModule.onPreviewTextureCopied();
217    }
218
219    @Override
220    public void onUserInteraction() {
221        super.onUserInteraction();
222        mCurrentModule.onUserInteraction();
223    }
224
225    @Override
226    protected boolean updateStorageHintOnResume() {
227        return mCurrentModule.updateStorageHintOnResume();
228    }
229
230    @Override
231    public void updateCameraAppView() {
232        super.updateCameraAppView();
233        mCurrentModule.updateCameraAppView();
234    }
235
236}
237