6b89ab6b6de4c36ab33211611512a156b01ac0da
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / PassCodeManager.java
1 package com.owncloud.android.authentication;
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.os.PowerManager;
8 import android.preference.PreferenceManager;
9 import android.view.WindowManager;
10
11 import com.owncloud.android.MainApp;
12 import com.owncloud.android.ui.activity.PassCodeActivity;
13
14 import java.util.HashSet;
15 import java.util.Set;
16
17 public class PassCodeManager {
18
19 private static final Set<Class> sExemptOfPasscodeActivites;
20
21 static {
22 sExemptOfPasscodeActivites = new HashSet<Class>();
23 sExemptOfPasscodeActivites.add(PassCodeActivity.class);
24 // other activities may be exempted, if needed
25 }
26
27 private static int PASS_CODE_TIMEOUT = 1000;
28 // keeping a "low" positive value is the easiest way to prevent the pass code is requested on rotations
29
30 public static PassCodeManager mPassCodeManagerInstance = null;
31
32 public static PassCodeManager getPassCodeManager() {
33 if (mPassCodeManagerInstance == null) {
34 mPassCodeManagerInstance = new PassCodeManager();
35 }
36 return mPassCodeManagerInstance;
37 }
38
39 private Long mTimestamp = 0l;
40 private int mVisibleActivitiesCounter = 0;
41
42 protected PassCodeManager() {};
43
44 public void onActivityCreated(Activity activity) {
45 if (passCodeIsEnabled()) {
46 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
47 } else {
48 activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
49 }
50 }
51
52 public void onActivityStarted(Activity activity) {
53 if (!sExemptOfPasscodeActivites.contains(activity.getClass()) &&
54 passCodeShouldBeRequested()
55 ){
56
57 Intent i = new Intent(MainApp.getAppContext(), PassCodeActivity.class);
58 i.setAction(PassCodeActivity.ACTION_REQUEST);
59 i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
60 activity.startActivity(i);
61
62 }
63
64 mVisibleActivitiesCounter++; // keep it AFTER passCodeShouldBeRequested was checked
65 }
66
67 public void onActivityStopped(Activity activity) {
68 if (mVisibleActivitiesCounter > 0) {
69 mVisibleActivitiesCounter--;
70 }
71 setUnlockTimestamp();
72 PowerManager powerMgr = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
73 if (passCodeIsEnabled() && powerMgr != null && !powerMgr.isScreenOn()) {
74 activity.moveTaskToBack(true);
75 }
76 }
77
78 private void setUnlockTimestamp() {
79 mTimestamp = System.currentTimeMillis();
80 }
81
82 private boolean passCodeShouldBeRequested(){
83 if ((System.currentTimeMillis() - mTimestamp) > PASS_CODE_TIMEOUT &&
84 mVisibleActivitiesCounter <= 0
85 ){
86 return passCodeIsEnabled();
87 }
88 return false;
89 }
90
91 private boolean passCodeIsEnabled() {
92 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext());
93 return (appPrefs.getBoolean("set_pincode", false));
94 }
95
96 }