ActivityBase.java revision 7d9eadd0c6c38b3761b7e8d3fa3658d194810d60
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 com.android.camera.ui.PopupManager;
20
21import android.app.Activity;
22import android.app.KeyguardManager;
23import android.view.KeyEvent;
24import android.content.Context;
25import android.content.Intent;
26import android.hardware.Camera;
27import android.media.AudioManager;
28import android.os.Bundle;
29import android.util.Log;
30
31/**
32 * Superclass of Camera and VideoCamera activities.
33 */
34abstract public class ActivityBase extends Activity {
35    private static final String TAG = "ActivityBase";
36    private static boolean LOGV = false;
37    private int mResultCodeForTesting;
38    private boolean mOnResumePending;
39    private Intent mResultDataForTesting;
40    protected Camera mCameraDevice;
41
42    @Override
43    public void onCreate(Bundle icicle) {
44        super.onCreate(icicle);
45        setVolumeControlStream(AudioManager.STREAM_MUSIC);
46    }
47
48    @Override
49    public void onWindowFocusChanged(boolean hasFocus) {
50        if (LOGV) Log.v(TAG, "onWindowFocusChanged.hasFocus=" + hasFocus
51                + ".mOnResumePending=" + mOnResumePending);
52        if (hasFocus && mOnResumePending) {
53            doOnResume();
54            mOnResumePending = false;
55        }
56    }
57
58    @Override
59    protected void onResume() {
60        super.onResume();
61        // Don't grab the camera if in use by lockscreen. For example, face
62        // unlock may be using the camera. Camera may be already opened in
63        // onCreate. doOnResume should continue if mCameraDevice != null.
64        if (mCameraDevice == null && !hasWindowFocus() && isKeyguardLocked()) {
65            if (LOGV) Log.v(TAG, "onRsume. mOnResumePending=true");
66            mOnResumePending = true;
67        } else {
68            if (LOGV) Log.v(TAG, "onRsume. mOnResumePending=false");
69            doOnResume();
70            mOnResumePending = false;
71        }
72    }
73
74    @Override
75    protected void onPause() {
76        if (LOGV) Log.v(TAG, "onPause");
77        super.onPause();
78        mOnResumePending = false;
79    }
80
81    // Put the code of onResume in this method.
82    abstract protected void doOnResume();
83
84    @Override
85    public boolean onSearchRequested() {
86        return false;
87    }
88
89    @Override
90    public boolean onKeyDown(int keyCode, KeyEvent event) {
91        // Prevent software keyboard or voice search from showing up.
92        if (keyCode == KeyEvent.KEYCODE_SEARCH
93                || keyCode == KeyEvent.KEYCODE_MENU) {
94            if (event.isLongPress()) return true;
95        }
96
97        return super.onKeyDown(keyCode, event);
98    }
99
100    protected void setResultEx(int resultCode) {
101        mResultCodeForTesting = resultCode;
102        setResult(resultCode);
103    }
104
105    protected void setResultEx(int resultCode, Intent data) {
106        mResultCodeForTesting = resultCode;
107        mResultDataForTesting = data;
108        setResult(resultCode, data);
109    }
110
111    public int getResultCode() {
112        return mResultCodeForTesting;
113    }
114
115    public Intent getResultData() {
116        return mResultDataForTesting;
117    }
118
119    @Override
120    protected void onDestroy() {
121        PopupManager.removeInstance(this);
122        super.onDestroy();
123    }
124
125    private boolean isKeyguardLocked() {
126        KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
127        // isKeyguardSecure excludes the slide lock case.
128        return (kgm != null) && kgm.isKeyguardLocked() && kgm.isKeyguardSecure();
129    }
130}
131