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