From 1bfa28078a0b0a15bbae8fadfde64a1e994963c2 Mon Sep 17 00:00:00 2001 From: stom79 Date: Tue, 29 Jan 2019 14:08:02 +0100 Subject: [PATCH] Logout users --- .../mastodon/activities/LoginActivity.java | 12 +++- .../etalab/mastodon/helper/CrossActions.java | 2 +- .../gouv/etalab/mastodon/helper/Helper.java | 64 +++++++++++-------- .../mastodon/jobs/NotificationsSyncJob.java | 2 +- .../services/LiveNotificationService.java | 2 +- .../etalab/mastodon/sqlite/AccountDAO.java | 28 ++++++++ 6 files changed, 79 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java index 8a3710559..ae80ec54e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java @@ -97,11 +97,14 @@ public class LoginActivity extends BaseActivity { private CheckBox peertube_instance; private Button connectionButton; private String actionToken; - + private String autofilledInstance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + Bundle b = getIntent().getExtras(); + if(b != null) { + autofilledInstance = b.getString("instance", null); + } if( getIntent() != null && getIntent().getData() != null && getIntent().getData().toString().contains("mastalab://backtomastalab?code=")){ String url = getIntent().getData().toString(); String val[] = url.split("code="); @@ -190,6 +193,11 @@ public class LoginActivity extends BaseActivity { info_2FA = findViewById(R.id.info_2FA); peertube_instance = findViewById(R.id.peertube_instance); + if( autofilledInstance != null){ + login_instance.setText(autofilledInstance.trim()); + retrievesClientId(); + login_uid.requestFocus(); + } peertube_instance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java index 04198fad2..f062c679f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java @@ -69,7 +69,7 @@ public class CrossActions { private static List connectedAccounts(Context context, Status status, boolean limitedToOwner){ final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - List accountstmp = new AccountDAO(context, db).getAllAccount(); + List accountstmp = new AccountDAO(context, db).getAllAccountCrossAction(); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); Account currentAccount = new AccountDAO(context, db).getAccountByID(userId); List accounts = new ArrayList<>(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 6d1aab6ea..83daebc75 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -1720,21 +1720,27 @@ public class Helper { subActionButtonAcc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, accountChoice.getToken()); - editor.putString(Helper.PREF_KEY_ID, accountChoice.getId()); - editor.putString(Helper.PREF_INSTANCE, accountChoice.getInstance().trim()); - editor.putBoolean(Helper.PREF_IS_MODERATOR, accountChoice.isModerator()); - editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, accountChoice.isAdmin()); - editor.commit(); - if(accountChoice.getSocial() != null && accountChoice.getSocial().equals("PEERTUBE")) - Toasty.info(activity, activity.getString(R.string.toast_account_changed, "@" + accountChoice.getAcct()), Toast.LENGTH_LONG).show(); - else - Toasty.info(activity, activity.getString(R.string.toast_account_changed, "@" + accountChoice.getAcct() + "@" + accountChoice.getInstance()), Toast.LENGTH_LONG).show(); - Intent changeAccount = new Intent(activity, MainActivity.class); - changeAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - activity.finish(); - activity.startActivity(changeAccount); + if( !accountChoice.getToken().equals("null")) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, accountChoice.getToken()); + editor.putString(Helper.PREF_KEY_ID, accountChoice.getId()); + editor.putString(Helper.PREF_INSTANCE, accountChoice.getInstance().trim()); + editor.putBoolean(Helper.PREF_IS_MODERATOR, accountChoice.isModerator()); + editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, accountChoice.isAdmin()); + editor.commit(); + if (accountChoice.getSocial() != null && accountChoice.getSocial().equals("PEERTUBE")) + Toasty.info(activity, activity.getString(R.string.toast_account_changed, "@" + accountChoice.getAcct()), Toast.LENGTH_LONG).show(); + else + Toasty.info(activity, activity.getString(R.string.toast_account_changed, "@" + accountChoice.getAcct() + "@" + accountChoice.getInstance()), Toast.LENGTH_LONG).show(); + Intent changeAccount = new Intent(activity, MainActivity.class); + changeAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + activity.finish(); + activity.startActivity(changeAccount); + }else{ //The account was logged out + Intent intent = new Intent(activity, LoginActivity.class); + intent.putExtra("instance", accountChoice.getInstance()); + activity.startActivity(intent); + } } }); actionMenuAccBuilder.addSubActionView(subActionButtonAcc); @@ -1775,17 +1781,23 @@ public class Helper { public void onClick(View v) { for(final Account accountChoice: accounts) { if (!currrentUserId.equals(accountChoice.getId())) { - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, accountChoice.getToken()); - editor.putString(Helper.PREF_KEY_ID, accountChoice.getId()); - editor.putString(Helper.PREF_INSTANCE, accountChoice.getInstance().trim()); - editor.putBoolean(Helper.PREF_IS_MODERATOR, accountChoice.isModerator()); - editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, accountChoice.isAdmin()); - editor.commit(); - Intent changeAccount = new Intent(activity, MainActivity.class); - changeAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - activity.finish(); - activity.startActivity(changeAccount); + if( !accountChoice.getToken().equals("null")) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, accountChoice.getToken()); + editor.putString(Helper.PREF_KEY_ID, accountChoice.getId()); + editor.putString(Helper.PREF_INSTANCE, accountChoice.getInstance().trim()); + editor.putBoolean(Helper.PREF_IS_MODERATOR, accountChoice.isModerator()); + editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, accountChoice.isAdmin()); + editor.commit(); + Intent changeAccount = new Intent(activity, MainActivity.class); + changeAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + activity.finish(); + activity.startActivity(changeAccount); + }else{ //The account was logged out + Intent intent = new Intent(activity, LoginActivity.class); + intent.putExtra("instance", accountChoice.getInstance()); + activity.startActivity(intent); + } } } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java index 209c5cbdc..c59e8053b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java @@ -125,7 +125,7 @@ public class NotificationsSyncJob extends Job { return; //If WIFI only and on WIFI OR user defined any connections to use the service. if(!sharedpreferences.getBoolean(Helper.SET_WIFI_ONLY, false) || Helper.isOnWIFI(getContext())) { - List accounts = new AccountDAO(getContext(),db).getAllAccount(); + List accounts = new AccountDAO(getContext(),db).getAllAccountCrossAction(); //It means there is no user in DB. if( accounts == null ) return; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java index 9082c58a4..a02f07b16 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/LiveNotificationService.java @@ -126,7 +126,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece } it.remove(); } - List accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccount(); + List accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); if (accountStreams != null) { for (final Account accountStream : accountStreams) { if( accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON")) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/AccountDAO.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/AccountDAO.java index 8d926a0ea..d35676a6f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/AccountDAO.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/AccountDAO.java @@ -206,6 +206,34 @@ public class AccountDAO { } } + /** + * Returns all Account in db + * @return Account List + */ + public List getAllAccountActivated(){ + + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " != 'null'", null, null, null, Sqlite.COL_INSTANCE + " ASC", null); + return cursorToListUser(c); + } catch (Exception e) { + return null; + } + } + + /** + * Returns all Account in db + * @return Account List + */ + public List getAllAccountCrossAction(){ + + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_SOCIAL + " != 'PEERTUBE AND " + Sqlite.COL_OAUTHTOKEN + " != 'null'", null, null, null, Sqlite.COL_INSTANCE + " ASC", null); + return cursorToListUser(c); + } catch (Exception e) { + return null; + } + } + /** * Returns an Account by token