Skip to content

Commit

Permalink
Reauthentication prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Jun 25, 2023
1 parent cfac460 commit 0566311
Show file tree
Hide file tree
Showing 18 changed files with 401 additions and 250 deletions.
3 changes: 3 additions & 0 deletions src/main/assets/changelog-alpha.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/Alpha 334 (2023-06-25)
Due to Reddit API changes, any active accounts must be logged in again

/Alpha 333 (2023-06-18)
Show Reddit user agreement prompt at startup
Simplified error handling
Expand Down
1 change: 1 addition & 0 deletions src/main/assets/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
105/1.21
Show Reddit user agreement prompt at startup
Due to Reddit API changes, any active accounts must be logged in again
More specific error message when a subreddit is private
Show avatar in user profile dialog (thanks to mgurga)
Subreddit emotes now supported in comments (thanks to bharatknv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@
package org.quantumbadger.redreader.account;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.quantumbadger.redreader.common.StringUtils;
import org.quantumbadger.redreader.reddit.api.RedditOAuth;

public class RedditAccount {

@NonNull public final String username;
public final RedditOAuth.RefreshToken refreshToken;
public final boolean usesNewClientId;

private RedditOAuth.AccessToken accessToken;

public final long priority;
@Nullable public final String clientId;

public RedditAccount(
@NonNull final String username,
final RedditOAuth.RefreshToken refreshToken,
final boolean usesNewClientId,
final long priority) {
final long priority,
@Nullable final String clientId) {

//noinspection ConstantConditions
if(username == null) {
Expand All @@ -44,8 +45,8 @@ public RedditAccount(

this.username = username.trim();
this.refreshToken = refreshToken;
this.usesNewClientId = usesNewClientId;
this.priority = priority;
this.clientId = clientId;
}

public boolean isAnonymous() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public final class RedditAccountManager extends SQLiteOpenHelper {
private static final RedditAccount ANON = new RedditAccount(
"",
null,
true,
10);
10,
null
);

private final Context context;

Expand All @@ -60,9 +61,10 @@ protected void notifyListener(final RedditAccountChangeListener listener) {
private static final String FIELD_USERNAME = "username";
private static final String FIELD_REFRESH_TOKEN = "refresh_token";
private static final String FIELD_PRIORITY = "priority";
private static final String FIELD_CLIENT_ID = "client_id";
private static final String FIELD_USES_NEW_CLIENT_ID = "uses_new_client_id";

private static final int ACCOUNTS_DB_VERSION = 3;
private static final int ACCOUNTS_DB_VERSION = 4;

@SuppressLint("StaticFieldLeak") private static RedditAccountManager singleton;

Expand Down Expand Up @@ -94,12 +96,14 @@ public void onCreate(final SQLiteDatabase db) {
"%s TEXT NOT NULL PRIMARY KEY ON CONFLICT REPLACE," +
"%s TEXT," +
"%s INTEGER," +
"%s BOOLEAN NOT NULL)",
"%s BOOLEAN NOT NULL," +
"%s TEXT)",
TABLE,
FIELD_USERNAME,
FIELD_REFRESH_TOKEN,
FIELD_PRIORITY,
FIELD_USES_NEW_CLIENT_ID);
FIELD_USES_NEW_CLIENT_ID,
FIELD_CLIENT_ID);

db.execSQL(queryString);

Expand Down Expand Up @@ -130,6 +134,14 @@ public void onUpgrade(
TABLE,
FIELD_USES_NEW_CLIENT_ID));
}

if(oldVersion < 4) {
db.execSQL(String.format(
Locale.US,
"ALTER TABLE %s ADD COLUMN %s TEXT DEFAULT NULL",
TABLE,
FIELD_CLIENT_ID));
}
}

public synchronized void addAccount(final RedditAccount account) {
Expand Down Expand Up @@ -158,7 +170,8 @@ private synchronized void addAccount(
}

row.put(FIELD_PRIORITY, account.priority);
row.put(FIELD_USES_NEW_CLIENT_ID, account.usesNewClientId);
row.put(FIELD_USES_NEW_CLIENT_ID, 1);
row.put(FIELD_CLIENT_ID, account.clientId);

db.insert(TABLE, null, row);

Expand Down Expand Up @@ -239,7 +252,7 @@ private synchronized void reloadAccounts(final SQLiteDatabase db) {
FIELD_USERNAME,
FIELD_REFRESH_TOKEN,
FIELD_PRIORITY,
FIELD_USES_NEW_CLIENT_ID};
FIELD_CLIENT_ID};

final Cursor cursor = db.query(
TABLE,
Expand Down Expand Up @@ -267,13 +280,13 @@ private synchronized void reloadAccounts(final SQLiteDatabase db) {
}

final long priority = cursor.getLong(2);
final boolean usesNewClientId = cursor.getInt(3) != 0;
@Nullable final String clientId = cursor.getString(3);

final RedditAccount account = new RedditAccount(
username,
refreshToken,
usesNewClientId,
priority);
priority,
clientId);

accountsCache.add(account);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.quantumbadger.redreader.reddit.PostSort;
import org.quantumbadger.redreader.reddit.RedditSubredditHistory;
import org.quantumbadger.redreader.reddit.UserCommentSort;
import org.quantumbadger.redreader.reddit.api.RedditOAuth;
import org.quantumbadger.redreader.reddit.api.RedditSubredditSubscriptionManager;
import org.quantumbadger.redreader.reddit.api.SubredditSubscriptionState;
import org.quantumbadger.redreader.reddit.prepared.RedditPreparedPost;
Expand Down Expand Up @@ -176,9 +177,7 @@ && getIntent().getAction().equals(Intent.ACTION_MAIN)) {
.setMessage(R.string.firstrun_login_message)
.setPositiveButton(
R.string.firstrun_login_button_now,
(dialog, which) -> new AccountListDialog().show(
this.getSupportFragmentManager(),
null))
(dialog, which) -> AccountListDialog.show(this))
.setNegativeButton(R.string.firstrun_login_button_later, null)
.show();

Expand All @@ -200,6 +199,10 @@ && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

FeatureFlagHandler.handleUpgrade(this);

if(RedditOAuth.anyNeedRelogin(this)) {
General.showMustReloginDialog(this);
}

recreateSubscriptionListener();

doRefresh(RefreshableFragment.MAIN_RELAYOUT, false, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,7 @@ private static void add(
? R.string.options_account_manager
: R.string.options_accounts))
.setOnMenuItemClickListener(item -> {
new AccountListDialog().show(
activity.getSupportFragmentManager(),
null);
AccountListDialog.show(activity);
return true;
});

Expand Down

This file was deleted.

1 comment on commit 0566311

@Eskuero
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫡

Please sign in to comment.