ActivityBase.java revision 4edaf0cbbb5d9cfeae786b6fd0560240947f6585
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    private OnScreenHint mStorageHint;
41    protected CameraDevice 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        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
51        super.onCreate(icicle);
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
90        if (mStorageHint != null) {
91            mStorageHint.cancel();
92            mStorageHint = null;
93        }
94
95        mOnResumePending = false;
96    }
97
98    // Put the code of onResume in this method.
99    abstract protected void doOnResume();
100
101    @Override
102    public boolean onSearchRequested() {
103        return false;
104    }
105
106    @Override
107    public boolean onKeyDown(int keyCode, KeyEvent event) {
108        // Prevent software keyboard or voice search from showing up.
109        if (keyCode == KeyEvent.KEYCODE_SEARCH
110                || keyCode == KeyEvent.KEYCODE_MENU) {
111            if (event.isLongPress()) return true;
112        }
113
114        return super.onKeyDown(keyCode, event);
115    }
116
117    protected void setResultEx(int resultCode) {
118        mResultCodeForTesting = resultCode;
119        setResult(resultCode);
120    }
121
122    protected void setResultEx(int resultCode, Intent data) {
123        mResultCodeForTesting = resultCode;
124        mResultDataForTesting = data;
125        setResult(resultCode, data);
126    }
127
128    public int getResultCode() {
129        return mResultCodeForTesting;
130    }
131
132    public Intent getResultData() {
133        return mResultDataForTesting;
134    }
135
136    @Override
137    protected void onDestroy() {
138        PopupManager.removeInstance(this);
139        super.onDestroy();
140    }
141
142    private boolean isKeyguardLocked() {
143        KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
144        if (LOGV) {
145            if (kgm != null) {
146                Log.v(TAG, "kgm.isKeyguardLocked()="+kgm.isKeyguardLocked()
147                        + ". kgm.isKeyguardSecure()="+kgm.isKeyguardSecure());
148            }
149        }
150        // isKeyguardSecure excludes the slide lock case.
151        return (kgm != null) && kgm.isKeyguardLocked() && kgm.isKeyguardSecure();
152    }
153
154    protected void updateStorageHint(long storageSpace) {
155        String message = null;
156        if (storageSpace == Storage.UNAVAILABLE) {
157            message = getString(R.string.no_storage);
158        } else if (storageSpace == Storage.PREPARING) {
159            message = getString(R.string.preparing_sd);
160        } else if (storageSpace == Storage.UNKNOWN_SIZE) {
161            message = getString(R.string.access_sd_fail);
162        } else if (storageSpace < Storage.LOW_STORAGE_THRESHOLD) {
163            message = getString(R.string.spaceIsLow_content);
164        }
165
166        if (message != null) {
167            if (mStorageHint == null) {
168                mStorageHint = OnScreenHint.makeText(this, message);
169            } else {
170                mStorageHint.setText(message);
171            }
172            mStorageHint.show();
173        } else if (mStorageHint != null) {
174            mStorageHint.cancel();
175            mStorageHint = null;
176        }
177    }
178}
179