2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.authentication
;
22 import android
.app
.Activity
;
23 import android
.content
.Context
;
24 import android
.content
.Intent
;
25 import android
.content
.SharedPreferences
;
26 import android
.os
.PowerManager
;
27 import android
.preference
.PreferenceManager
;
28 import android
.view
.WindowManager
;
30 import com
.owncloud
.android
.MainApp
;
31 import com
.owncloud
.android
.ui
.activity
.PassCodeActivity
;
33 import java
.util
.HashSet
;
36 public class PassCodeManager
{
38 private static final Set
<Class
> sExemptOfPasscodeActivites
;
41 sExemptOfPasscodeActivites
= new HashSet
<Class
>();
42 sExemptOfPasscodeActivites
.add(PassCodeActivity
.class);
43 // other activities may be exempted, if needed
46 private static int PASS_CODE_TIMEOUT
= 1000;
47 // keeping a "low" positive value is the easiest way to prevent the pass code is requested on rotations
49 public static PassCodeManager mPassCodeManagerInstance
= null
;
51 public static PassCodeManager
getPassCodeManager() {
52 if (mPassCodeManagerInstance
== null
) {
53 mPassCodeManagerInstance
= new PassCodeManager();
55 return mPassCodeManagerInstance
;
58 private Long mTimestamp
= 0l;
59 private int mVisibleActivitiesCounter
= 0;
61 protected PassCodeManager() {};
63 public void onActivityCreated(Activity activity
) {
64 if (passCodeIsEnabled()) {
65 activity
.getWindow().addFlags(WindowManager
.LayoutParams
.FLAG_SECURE
);
67 activity
.getWindow().clearFlags(WindowManager
.LayoutParams
.FLAG_SECURE
);
71 public void onActivityStarted(Activity activity
) {
72 if (!sExemptOfPasscodeActivites
.contains(activity
.getClass()) &&
73 passCodeShouldBeRequested()
76 Intent i
= new Intent(MainApp
.getAppContext(), PassCodeActivity
.class);
77 i
.setAction(PassCodeActivity
.ACTION_REQUEST
);
78 i
.setFlags(Intent
.FLAG_ACTIVITY_REORDER_TO_FRONT
);
79 activity
.startActivity(i
);
83 mVisibleActivitiesCounter
++; // keep it AFTER passCodeShouldBeRequested was checked
86 public void onActivityStopped(Activity activity
) {
87 if (mVisibleActivitiesCounter
> 0) {
88 mVisibleActivitiesCounter
--;
91 PowerManager powerMgr
= (PowerManager
) activity
.getSystemService(Context
.POWER_SERVICE
);
92 if (passCodeIsEnabled() && powerMgr
!= null
&& !powerMgr
.isScreenOn()) {
93 activity
.moveTaskToBack(true
);
97 private void setUnlockTimestamp() {
98 mTimestamp
= System
.currentTimeMillis();
101 private boolean passCodeShouldBeRequested(){
102 if ((System
.currentTimeMillis() - mTimestamp
) > PASS_CODE_TIMEOUT
&&
103 mVisibleActivitiesCounter
<= 0
105 return passCodeIsEnabled();
110 private boolean passCodeIsEnabled() {
111 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(MainApp
.getAppContext());
112 return (appPrefs
.getBoolean("set_pincode", false
));