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