@@ -48,28 +48,46 @@ public class ScreenLockManager {
48
48
49
49
public static final String MOBILE_POLICY_PREF = "mobile_policy" ;
50
50
public static final String SCREEN_LOCK = "screen_lock" ;
51
+ public static final String SCREEN_LOCK_TIMEOUT = "screen_lock_timeout" ;
51
52
52
- private boolean backgroundedSinceUnlock = true ;
53
+ private long lastLockedTimestamp = 0 ;
53
54
54
55
/**
55
56
* Stores the mobile policy for the org upon user login.
56
57
*
57
58
* @param account the newly add account
58
59
* @param screenLockRequired if the account requires screen lock or not
60
+ *
61
+ * @deprecated Timeout is now required. This method will be removed in 11.0.
62
+ */
63
+ @ Deprecated
64
+ public void storeMobilePolicy (UserAccount account , boolean screenLockRequired ) { }
65
+
66
+ /**
67
+ * Stores the mobile policy for the org upon user login.
68
+ *
69
+ * @param account the newly add account
70
+ * @param screenLockRequired if the account requires screen lock or not
71
+ * @param timeout timeout in milliseconds
59
72
*/
60
- public void storeMobilePolicy (UserAccount account , boolean screenLockRequired ) {
73
+ public void storeMobilePolicy (UserAccount account , boolean screenLockRequired , int timeout ) {
61
74
Context ctx = SalesforceSDKManager .getInstance ().getAppContext ();
62
75
SharedPreferences accountSharedPrefs = ctx .getSharedPreferences (MOBILE_POLICY_PREF
63
76
+ account .getUserLevelFilenameSuffix (), Context .MODE_PRIVATE );
64
- accountSharedPrefs .edit ().putBoolean (SCREEN_LOCK , screenLockRequired ).apply ();
77
+ accountSharedPrefs .edit ().putBoolean (SCREEN_LOCK , screenLockRequired ).putInt ( SCREEN_LOCK_TIMEOUT , timeout ). apply ();
65
78
66
79
SharedPreferences globalPrefs = ctx .getSharedPreferences (MOBILE_POLICY_PREF , Context .MODE_PRIVATE );
67
80
if (screenLockRequired ) {
68
- globalPrefs .edit ().putBoolean (SCREEN_LOCK , true ).apply ();
81
+ int currentTimeout = globalPrefs .getInt (SCREEN_LOCK_TIMEOUT , 0 );
82
+ SharedPreferences .Editor globalPrefsEditor = globalPrefs .edit ();
83
+
84
+ globalPrefsEditor .putBoolean (SCREEN_LOCK , true );
85
+ if (currentTimeout == 0 || timeout < currentTimeout ) {
86
+ globalPrefsEditor .putInt (SCREEN_LOCK_TIMEOUT , timeout );
87
+ }
88
+ globalPrefsEditor .apply ();
69
89
70
- // This is needed to block access to the app immediately on login
71
- backgroundedSinceUnlock = true ;
72
- onAppForegrounded ();
90
+ lock ();
73
91
}
74
92
}
75
93
@@ -86,7 +104,7 @@ public void onAppForegrounded() {
86
104
* To be called by the protected activity is paused to denote that the app should lock.
87
105
*/
88
106
public void onAppBackgrounded () {
89
- backgroundedSinceUnlock = true ;
107
+ lastLockedTimestamp = System . currentTimeMillis () ;
90
108
}
91
109
92
110
/**
@@ -95,7 +113,7 @@ public void onAppBackgrounded() {
95
113
public void reset () {
96
114
Context ctx = SalesforceSDKManager .getInstance ().getAppContext ();
97
115
SharedPreferences globalPrefs = ctx .getSharedPreferences (MOBILE_POLICY_PREF , Context .MODE_PRIVATE );
98
- globalPrefs .edit ().remove (SCREEN_LOCK ).apply ();
116
+ globalPrefs .edit ().remove (SCREEN_LOCK ).remove ( SCREEN_LOCK_TIMEOUT ). apply ();
99
117
}
100
118
101
119
/**
@@ -110,23 +128,34 @@ public void cleanUp(UserAccount account) {
110
128
if (account != null ) {
111
129
final SharedPreferences accountPrefs = ctx .getSharedPreferences (MOBILE_POLICY_PREF
112
130
+ account .getUserLevelFilenameSuffix (), Context .MODE_PRIVATE );
113
- accountPrefs .edit ().remove (SCREEN_LOCK ).apply ();
131
+ accountPrefs .edit ().remove (SCREEN_LOCK ).remove ( SCREEN_LOCK_TIMEOUT ). apply ();
114
132
}
115
133
116
134
// Determine if any other users still need Screen Lock.
117
135
final List <UserAccount > accounts = SalesforceSDKManager .getInstance ()
118
136
.getUserAccountManager ().getAuthenticatedUsers ();
137
+ int lowestTimeout = Integer .MAX_VALUE ;
138
+
119
139
if (accounts != null ) {
120
140
accounts .remove (account );
121
141
for (final UserAccount mAccount : accounts ) {
122
142
if (mAccount != null ) {
123
143
final SharedPreferences accountPrefs = ctx .getSharedPreferences (MOBILE_POLICY_PREF
124
144
+ mAccount .getUserLevelFilenameSuffix (), Context .MODE_PRIVATE );
125
145
if (accountPrefs .getBoolean (SCREEN_LOCK , false )) {
126
- return ;
146
+ int timeout = accountPrefs .getInt (SCREEN_LOCK_TIMEOUT , Integer .MAX_VALUE );
147
+ if (timeout < lowestTimeout ) {
148
+ lowestTimeout = timeout ;
149
+ }
127
150
}
128
151
}
129
152
}
153
+
154
+ if (lowestTimeout < Integer .MAX_VALUE ) {
155
+ SharedPreferences globalPrefs = ctx .getSharedPreferences (MOBILE_POLICY_PREF , Context .MODE_PRIVATE );
156
+ globalPrefs .edit ().putInt (SCREEN_LOCK_TIMEOUT , lowestTimeout ).apply ();
157
+ return ;
158
+ }
130
159
}
131
160
132
161
// If we have returned, no other accounts require Screen Lock.
@@ -135,20 +164,21 @@ public void cleanUp(UserAccount account) {
135
164
136
165
/**
137
166
* Unlocks the app.
167
+ *
168
+ * @deprecated This method will be removed in 11.0.
138
169
*/
139
- public void unlock () {
140
- backgroundedSinceUnlock = false ;
141
- }
170
+ @ Deprecated
171
+ public void unlock () { }
142
172
143
173
@ VisibleForTesting
144
174
protected boolean shouldLock () {
145
- return backgroundedSinceUnlock && readMobilePolicy ();
146
- }
147
-
148
- private boolean readMobilePolicy () {
175
+ long elapsedTime = System .currentTimeMillis () - lastLockedTimestamp ;
149
176
Context ctx = SalesforceSDKManager .getInstance ().getAppContext ();
150
177
SharedPreferences sharedPrefs = ctx .getSharedPreferences (MOBILE_POLICY_PREF , Context .MODE_PRIVATE );
151
- return sharedPrefs .getBoolean (SCREEN_LOCK , false );
178
+ boolean hasLock = sharedPrefs .getBoolean (SCREEN_LOCK , false );
179
+ int timeout = sharedPrefs .getInt (SCREEN_LOCK_TIMEOUT , 0 );
180
+
181
+ return hasLock && (elapsedTime > timeout );
152
182
}
153
183
154
184
private void lock () {
0 commit comments