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