From 2c0ec28803d405dc211a5a13beeef3aa6f8ebeb3 Mon Sep 17 00:00:00 2001
From: FineFindus <63370021+FineFindus@users.noreply.github.com>
Date: Sat, 20 May 2023 13:20:25 +0200
Subject: [PATCH] Panic responder (#512)
* feat: add panic responder
* refactor: logOut before removing session
* fix(panic): close app after logOut to avoid crash
* build: reset gradle.properties
---
mastodon/src/main/AndroidManifest.xml | 16 ++++++
.../joinmastodon/android/ExitActivity.java | 24 +++++++++
.../android/PanicResponderActivity.java | 49 +++++++++++++++++++
3 files changed, 89 insertions(+)
create mode 100644 mastodon/src/main/java/org/joinmastodon/android/ExitActivity.java
create mode 100644 mastodon/src/main/java/org/joinmastodon/android/PanicResponderActivity.java
diff --git a/mastodon/src/main/AndroidManifest.xml b/mastodon/src/main/AndroidManifest.xml
index c5ffb97e4..69e170179 100644
--- a/mastodon/src/main/AndroidManifest.xml
+++ b/mastodon/src/main/AndroidManifest.xml
@@ -38,6 +38,22 @@
+
+
+
+
+
+
+
+
diff --git a/mastodon/src/main/java/org/joinmastodon/android/ExitActivity.java b/mastodon/src/main/java/org/joinmastodon/android/ExitActivity.java
new file mode 100644
index 000000000..54a5ccbc4
--- /dev/null
+++ b/mastodon/src/main/java/org/joinmastodon/android/ExitActivity.java
@@ -0,0 +1,24 @@
+package org.joinmastodon.android;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class ExitActivity extends Activity {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ finishAndRemoveTask();
+ }
+
+ public static void exit(Context context) {
+ Intent intent = new Intent(context, ExitActivity.class);
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
+ context.startActivity(intent);
+ }
+
+}
diff --git a/mastodon/src/main/java/org/joinmastodon/android/PanicResponderActivity.java b/mastodon/src/main/java/org/joinmastodon/android/PanicResponderActivity.java
new file mode 100644
index 000000000..716907a67
--- /dev/null
+++ b/mastodon/src/main/java/org/joinmastodon/android/PanicResponderActivity.java
@@ -0,0 +1,49 @@
+package org.joinmastodon.android;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+
+import org.joinmastodon.android.api.requests.oauth.RevokeOauthToken;
+import org.joinmastodon.android.api.session.AccountSession;
+import org.joinmastodon.android.api.session.AccountSessionManager;
+
+import me.grishka.appkit.api.Callback;
+import me.grishka.appkit.api.ErrorResponse;
+
+
+public class PanicResponderActivity extends Activity {
+ public static final String PANIC_TRIGGER_ACTION = "info.guardianproject.panic.action.TRIGGER";
+
+ @Override
+ protected void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ final Intent intent = getIntent();
+ if (intent != null && PANIC_TRIGGER_ACTION.equals(intent.getAction())) {
+ AccountSessionManager.getInstance().getLoggedInAccounts().forEach(accountSession -> logOut(accountSession.getID()));
+ ExitActivity.exit(this);
+ }
+ finishAndRemoveTask();
+ }
+
+ private void logOut(String accountID){
+ AccountSession session=AccountSessionManager.getInstance().getAccount(accountID);
+ new RevokeOauthToken(session.app.clientId, session.app.clientSecret, session.token.accessToken)
+ .setCallback(new Callback<>(){
+ @Override
+ public void onSuccess(Object result){
+ onLoggedOut(accountID);
+ }
+
+ @Override
+ public void onError(ErrorResponse error){
+ onLoggedOut(accountID);
+ }
+ })
+ .exec(accountID);
+ }
+
+ private void onLoggedOut(String accountID){
+ AccountSessionManager.getInstance().removeAccount(accountID);
+ }
+}
\ No newline at end of file