Skip to content

Instantly share code, notes, and snippets.

@kairos34
Last active October 30, 2023 11:38
Show Gist options
  • Save kairos34/cb10eceab7d08ed852814d07f4ba3ede to your computer and use it in GitHub Desktop.
Save kairos34/cb10eceab7d08ed852814d07f4ba3ede to your computer and use it in GitHub Desktop.
SessionManager implementation in Java for Android
public interface SessionManager {
void startSession(int timeoutMillis);
void keepAlive();
void stopSession();
void checkSession();
void sessionExpired();
void register(SessionManagerCallback callback);
void unRegister();
void executeSessionExpired();
interface SessionManagerCallback {
void onCheckSession();
void onSessionExpired();
void onNotificationHandler();
}
}
public class SessionManagerImpl implements SessionManager {
private static final String TAG = "SessionManager";
private SessionManagerCallback callback;
private int timeoutMillis;
private boolean onAlarmTriggered = false;
private final Context context;
private final AlarmManager alarmManager;
private final Intent intent;
public SessionManagerImpl(Context context) {
this.context = context;
// setup alarm manager
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
intent = new Intent(context, SessionBroadcastReceiver.class);
intent.putExtra(SessionBroadcastReceiver.TIMEOUT_BROADCAST_EXTRA_RECEIVER, new ResultReceiver(new Handler()) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
Log.d(TAG, "onReceiveResult: ");
// update flag timeout triggered.
executeSessionExpired();
}
});
}
@Override
public void startSession(int timeoutMillis) {
int flags = PendingIntent.FLAG_CANCEL_CURRENT;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
flags = PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE;
}
// set timeout
this.timeoutMillis = timeoutMillis;
// start timer
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
SessionBroadcastReceiver.TIMEOUT_BROADCAST_REQUEST_CODE,
intent,
flags);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + timeoutMillis, pendingIntent);
}
@Override
public void keepAlive() {
// restart timer
stopSession();
startSession(timeoutMillis);
}
@Override
public void stopSession() {
int flags = PendingIntent.FLAG_CANCEL_CURRENT;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
flags = PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE;
}
// stop timer
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, SessionBroadcastReceiver.TIMEOUT_BROADCAST_REQUEST_CODE,
intent, flags);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
@Override
public void checkSession() {
if (callback == null) {
return;
}
// check session
callback.onCheckSession();
}
@Override
public void sessionExpired() {
if (callback == null) {
return;
}
// update flag timeout triggered.
this.onAlarmTriggered = false;
// session expired
callback.onSessionExpired();
}
@Override
public void register(SessionManagerCallback callback) {
// set callback
this.callback = callback;
// if the session times out, the user notifies this.
if (onAlarmTriggered) {
sessionExpired();
}
}
@Override
public void unRegister() {
// remove callback
this.callback = null;
}
@Override
public void executeSessionExpired() {
// update flag timeout triggered.
this.onAlarmTriggered = true;
// call session expired
this.sessionExpired();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment