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