Advertisement
Ankhwatcher

AbstractTimeoutActivity

Sep 4th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.SystemClock;
  6.  
  7. /* AbstractTimeOutActivity is subclassed by the activities which need to timeout. */
  8.  
  9. public class AbstractTimeoutActivity extends AbstractActivity {
  10.  
  11.     public static final String TIMEOUT_TIME = "timeout time";
  12.     //Disconnect timeout is set in milliseconds
  13.     public static final long DISCONNECT_TIMEOUT = 15 * 60 * 1000;
  14.     private long timeoutTime;
  15.     private Handler timeoutHandler = new Handler();
  16.     private Runnable disconnectCallback = new Runnable() {
  17.         @Override
  18.         public void run() {
  19.             startSignIn();
  20.  
  21.         }
  22.     };
  23.  
  24.     private void startSignIn() {
  25.         getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().putBoolean(IS_LOGGED_OUT, true).commit();
  26.         RSAApplication app = getRSAApplication();
  27.         Intent intent = new Intent(AbstractTimeoutActivity.this, SignInActivity.class);
  28.         intent.putExtra(TIMEOUT_REACHED, true);
  29.         intent.putExtra(TIMEOUT_USER, app.getUsername());
  30.         startActivity(intent);
  31.         finish();
  32.     }
  33.  
  34.     public void resetDisconnectTimer() {
  35.         timeoutHandler.removeCallbacks(disconnectCallback);
  36.         timeoutTime = System.currentTimeMillis() + DISCONNECT_TIMEOUT;
  37.         timeoutHandler.postAtTime(disconnectCallback, SystemClock.uptimeMillis() + DISCONNECT_TIMEOUT);
  38.     }
  39.  
  40.     public void stopDisconnectTimer() {
  41.         timeoutHandler.removeCallbacks(disconnectCallback);
  42.     }
  43.  
  44.     @Override
  45.     public void onUserInteraction() {
  46.         resetDisconnectTimer();
  47.     }
  48.  
  49.     @Override
  50.     public void onResume() {
  51.         super.onResume();
  52.  
  53.         resetDisconnectTimer();
  54.         if (getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getBoolean(IS_LOGGED_OUT, true)) {
  55.             //startSignIn();
  56.             finish();
  57.         } else if (System.currentTimeMillis() > getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getLong(TIMEOUT_TIME, 0))
  58.             startSignIn();
  59.  
  60.     }
  61.  
  62.     @Override
  63.     protected void onCreate(Bundle savedInstanceState) {
  64.         super.onCreate(savedInstanceState);
  65.         getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().putLong(TIMEOUT_TIME, System.currentTimeMillis() + DISCONNECT_TIMEOUT).commit();
  66.     }
  67.  
  68.     @Override
  69.     public void onStop() {
  70.         super.onStop();
  71.         stopDisconnectTimer();
  72.         getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().putLong(TIMEOUT_TIME, timeoutTime).commit();
  73.     }
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement