Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.SystemClock;
- /* AbstractTimeOutActivity is subclassed by the activities which need to timeout. */
- public class AbstractTimeoutActivity extends AbstractActivity {
- public static final String TIMEOUT_TIME = "timeout time";
- //Disconnect timeout is set in milliseconds
- public static final long DISCONNECT_TIMEOUT = 15 * 60 * 1000;
- private long timeoutTime;
- private Handler timeoutHandler = new Handler();
- private Runnable disconnectCallback = new Runnable() {
- @Override
- public void run() {
- startSignIn();
- }
- };
- private void startSignIn() {
- getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().putBoolean(IS_LOGGED_OUT, true).commit();
- RSAApplication app = getRSAApplication();
- Intent intent = new Intent(AbstractTimeoutActivity.this, SignInActivity.class);
- intent.putExtra(TIMEOUT_REACHED, true);
- intent.putExtra(TIMEOUT_USER, app.getUsername());
- startActivity(intent);
- finish();
- }
- public void resetDisconnectTimer() {
- timeoutHandler.removeCallbacks(disconnectCallback);
- timeoutTime = System.currentTimeMillis() + DISCONNECT_TIMEOUT;
- timeoutHandler.postAtTime(disconnectCallback, SystemClock.uptimeMillis() + DISCONNECT_TIMEOUT);
- }
- public void stopDisconnectTimer() {
- timeoutHandler.removeCallbacks(disconnectCallback);
- }
- @Override
- public void onUserInteraction() {
- resetDisconnectTimer();
- }
- @Override
- public void onResume() {
- super.onResume();
- resetDisconnectTimer();
- if (getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getBoolean(IS_LOGGED_OUT, true)) {
- //startSignIn();
- finish();
- } else if (System.currentTimeMillis() > getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getLong(TIMEOUT_TIME, 0))
- startSignIn();
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().putLong(TIMEOUT_TIME, System.currentTimeMillis() + DISCONNECT_TIMEOUT).commit();
- }
- @Override
- public void onStop() {
- super.onStop();
- stopDisconnectTimer();
- getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().putLong(TIMEOUT_TIME, timeoutTime).commit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement