ActivityBase.java revision 913f3784d368a5e11fee5d5db2c355ef832685da
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.content.Context;
24import android.content.Intent;
25import android.content.pm.ActivityInfo;
26import android.os.Bundle;
27import android.util.Log;
28import android.view.KeyEvent;
29
30/**
31 * Superclass of Camera and VideoCamera activities.
32 */
33abstract public class ActivityBase extends Activity {
34    private static final String TAG = "ActivityBase";
35    private static boolean LOGV = false;
36    private int mResultCodeForTesting;
37    private boolean mOnResumePending;
38    private Intent mResultDataForTesting;
39    protected CameraDevice mCameraDevice;
40
41    @Override
42    public void onCreate(Bundle icicle) {
43        if (Util.isTabletUI()) {
44            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
45        } else {
46            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
47        }
48        super.onCreate(icicle);
49    }
50
51    @Override
52    public void onWindowFocusChanged(boolean hasFocus) {
53        if (LOGV) Log.v(TAG, "onWindowFocusChanged.hasFocus=" + hasFocus
54                + ".mOnResumePending=" + mOnResumePending);
55        if (hasFocus && mOnResumePending) {
56            doOnResume();
57            mOnResumePending = false;
58        }
59    }
60
61    @Override
62    protected void onResume() {
63        super.onResume();
64        // Don't grab the camera if in use by lockscreen. For example, face
65        // unlock may be using the camera. Camera may be already opened in
66        // onCreate. doOnResume should continue if mCameraDevice != null.
67        // Suppose camera app is in the foreground. If users turn off and turn
68        // on the screen very fast, camera app can still have the focus when the
69        // lock screen shows up. The keyguard takes input focus, so the caemra
70        // app will lose focus when it is displayed.
71        if (LOGV) Log.v(TAG, "onResume. hasWindowFocus()=" + hasWindowFocus());
72        if (mCameraDevice == null && isKeyguardLocked()) {
73            if (LOGV) Log.v(TAG, "onResume. mOnResumePending=true");
74            mOnResumePending = true;
75        } else {
76            if (LOGV) Log.v(TAG, "onResume. mOnResumePending=false");
77            doOnResume();
78            mOnResumePending = false;
79        }
80    }
81
82    @Override
83    protected void onPause() {
84        if (LOGV) Log.v(TAG, "onPause");
85        super.onPause();
86        mOnResumePending = false;
87    }
88
89    // Put the code of onResume in this method.
90    abstract protected void doOnResume();
91
92    @Override
93    public boolean onSearchRequested() {
94        return false;
95    }
96
97    @Override
98    public boolean onKeyDown(int keyCode, KeyEvent event) {
99        // Prevent software keyboard or voice search from showing up.
100        if (keyCode == KeyEvent.KEYCODE_SEARCH
101                || keyCode == KeyEvent.KEYCODE_MENU) {
102            if (event.isLongPress()) return true;
103        }
104
105        return super.onKeyDown(keyCode, event);
106    }
107
108    protected void setResultEx(int resultCode) {
109        mResultCodeForTesting = resultCode;
110        setResult(resultCode);
111    }
112
113    protected void setResultEx(int resultCode, Intent data) {
114        mResultCodeForTesting = resultCode;
115        mResultDataForTesting = data;
116        setResult(resultCode, data);
117    }
118
119    public int getResultCode() {
120        return mResultCodeForTesting;
121    }
122
123    public Intent getResultData() {
124        return mResultDataForTesting;
125    }
126
127    @Override
128    protected void onDestroy() {
129        PopupManager.removeInstance(this);
130        super.onDestroy();
131    }
132
133    private boolean isKeyguardLocked() {
134        KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
135        if (LOGV) {
136            if (kgm != null) {
137                Log.v(TAG, "kgm.isKeyguardLocked()="+kgm.isKeyguardLocked()
138                        + ". kgm.isKeyguardSecure()="+kgm.isKeyguardSecure());
139            }
140        }
141        // isKeyguardSecure excludes the slide lock case.
142        return (kgm != null) && kgm.isKeyguardLocked() && kgm.isKeyguardSecure();
143    }
144}
145