Merge branch 'develop' of https://github.com/owncloud/android into material_toolbar
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / PassCodeManager.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20 package com.owncloud.android.authentication;
21
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;
29
30 import com.owncloud.android.MainApp;
31 import com.owncloud.android.ui.activity.PassCodeActivity;
32
33 import java.util.HashSet;
34 import java.util.Set;
35
36 public class PassCodeManager {
37
38 private static final Set<Class> sExemptOfPasscodeActivites;
39
40 static {
41 sExemptOfPasscodeActivites = new HashSet<Class>();
42 sExemptOfPasscodeActivites.add(PassCodeActivity.class);
43 // other activities may be exempted, if needed
44 }
45
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
48
49 public static PassCodeManager mPassCodeManagerInstance = null;
50
51 public static PassCodeManager getPassCodeManager() {
52 if (mPassCodeManagerInstance == null) {
53 mPassCodeManagerInstance = new PassCodeManager();
54 }
55 return mPassCodeManagerInstance;
56 }
57
58 private Long mTimestamp = 0l;
59 private int mVisibleActivitiesCounter = 0;
60
61 protected PassCodeManager() {};
62
63 public void onActivityCreated(Activity activity) {
64 if (passCodeIsEnabled()) {
65 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
66 } else {
67 activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
68 }
69 }
70
71 public void onActivityStarted(Activity activity) {
72 if (!sExemptOfPasscodeActivites.contains(activity.getClass()) &&
73 passCodeShouldBeRequested()
74 ){
75
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);
80
81 }
82
83 mVisibleActivitiesCounter++; // keep it AFTER passCodeShouldBeRequested was checked
84 }
85
86 public void onActivityStopped(Activity activity) {
87 if (mVisibleActivitiesCounter > 0) {
88 mVisibleActivitiesCounter--;
89 }
90 setUnlockTimestamp();
91 PowerManager powerMgr = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
92 if (passCodeIsEnabled() && powerMgr != null && !powerMgr.isScreenOn()) {
93 activity.moveTaskToBack(true);
94 }
95 }
96
97 private void setUnlockTimestamp() {
98 mTimestamp = System.currentTimeMillis();
99 }
100
101 private boolean passCodeShouldBeRequested(){
102 if ((System.currentTimeMillis() - mTimestamp) > PASS_CODE_TIMEOUT &&
103 mVisibleActivitiesCounter <= 0
104 ){
105 return passCodeIsEnabled();
106 }
107 return false;
108 }
109
110 private boolean passCodeIsEnabled() {
111 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
112 return (appPrefs.getBoolean("set_pincode", false));
113 }
114
115 }