Skip to content

Commit

Permalink
fixed user agent, added shared links support
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rc committed May 31, 2024
1 parent 7006318 commit 846a06d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void onCreate(Bundle savedInstance) {
}
final OAuthHelper oAuthHelper = Authentication.reddit.getOAuthHelper();
final Credentials credentials = Credentials.installedApp(
Authentication.authentication.getString("CLIENT_ID", ""),
Authentication.authentication.getString("CLIENT_ID", Authentication.CLIENT_ID_FALLBACK),
Authentication.authentication.getString("REDIRECT_URL", Authentication.REDIRECT_URL_FALLBACK)
);
String authorizationUrl =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2190,12 +2190,15 @@ private void changeClientId() {

EditText inputId = layout.findViewById(R.id.client_id_input);
EditText inputUrl = layout.findViewById(R.id.redirect_url_input);
inputId.setText(Authentication.authentication.getString("CLIENT_ID", ""));
inputId.setText(Authentication.authentication.getString("CLIENT_ID", Authentication.CLIENT_ID_FALLBACK));
inputUrl.setText(Authentication.authentication.getString("REDIRECT_URL", Authentication.REDIRECT_URL_FALLBACK));
inputId.requestFocus();

builder.setPositiveButton("Save & Restart App", (dialog, which) -> {
String clientId = inputId.getText().toString();
if (clientId.isEmpty()) {
clientId = Authentication.CLIENT_ID_FALLBACK;
}
String redirectUrl = inputUrl.getText().toString();
if (! redirectUrl.matches("https?://.+")) {
redirectUrl = Authentication.REDIRECT_URL_FALLBACK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onCreate(Bundle savedInstance) {
String[] scopes = {"identity", "modcontributors", "modconfig", "modothers", "modwiki", "creddits", "livemanage", "account", "privatemessages", "modflair", "modlog", "report", "modposts", "modwiki", "read", "vote", "edit", "submit", "subscribe", "save", "wikiread", "flair", "history", "mysubreddits", "wikiedit"};
final OAuthHelper oAuthHelper = Authentication.reddit.getOAuthHelper();
final Credentials credentials = Credentials.installedApp(
Authentication.authentication.getString("CLIENT_ID", ""),
Authentication.authentication.getString("CLIENT_ID", Authentication.CLIENT_ID_FALLBACK),
Authentication.authentication.getString("REDIRECT_URL", Authentication.REDIRECT_URL_FALLBACK)
);
String authorizationUrl = oAuthHelper.getAuthorizationUrl(credentials, true, scopes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2343,7 +2343,7 @@ protected String doInBackground(String... comment) {
private RedditClient getAuthenticatedClient(String profileName) {
String token;
RedditClient reddit = new RedditClient(
UserAgent.of("android:me.ccrama.RedditSlide:v" + BuildConfig.VERSION_NAME));
UserAgent.of(Authentication.authentication.getString("USER_AGENT", Authentication.USER_AGENT)));
final HashMap<String, String> accounts = new HashMap<>();

for (String s : Authentication.authentication.getStringSet("accounts",
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/java/me/ccrama/redditslide/Authentication.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
public class Authentication {
public static final String REDIRECT_URL_FALLBACK = "http://127.0.0.1";
public static final String CLIENT_ID_FALLBACK = "";
public static final String USER_AGENT = "me.ccrama.RedditSlide:v" + BuildConfig.VERSION_NAME;
public static boolean isLoggedIn;
public static RedditClient reddit;
public static LoggedInAccount me;
Expand Down Expand Up @@ -63,9 +65,7 @@ public Authentication(Context context) {
hasDone = true;
httpAdapter = new OkHttpAdapter(Reddit.client, Protocol.HTTP_2);
isLoggedIn = false;
reddit = new RedditClient(
UserAgent.of("android:me.ccrama.RedditSlide:v" + BuildConfig.VERSION_NAME),
httpAdapter);
reddit = new RedditClient(UserAgent.of(USER_AGENT), httpAdapter);
reddit.setRetryLimit(2);
if (BuildConfig.DEBUG) reddit.setLoggingMode(LoggingMode.ALWAYS);
didOnline = true;
Expand Down Expand Up @@ -95,8 +95,7 @@ public void updateToken(Context c) {
if (reddit == null) {
hasDone = true;
isLoggedIn = false;
reddit = new RedditClient(
UserAgent.of("android:me.ccrama.RedditSlide:v" + BuildConfig.VERSION_NAME));
reddit = new RedditClient(UserAgent.of(USER_AGENT));
reddit.setLoggingMode(LoggingMode.ALWAYS);
didOnline = true;

Expand Down Expand Up @@ -127,7 +126,7 @@ protected Void doInBackground(Void... params) {

final Credentials credentials =
Credentials.installedApp(
authentication.getString("CLIENT_ID", ""),
authentication.getString("CLIENT_ID", CLIENT_ID_FALLBACK),
authentication.getString("REDIRECT_URL", REDIRECT_URL_FALLBACK)
);
Log.v(LogUtil.getTag(), "REAUTH LOGGED IN");
Expand Down Expand Up @@ -170,7 +169,7 @@ protected Void doInBackground(Void... params) {

} else {
final Credentials fcreds =
Credentials.userlessApp(authentication.getString("CLIENT_ID", ""), UUID.randomUUID());
Credentials.userlessApp(authentication.getString("CLIENT_ID", CLIENT_ID_FALLBACK), UUID.randomUUID());
OAuthData authData;
if (BuildConfig.DEBUG) LogUtil.v("Not logged in");
try {
Expand Down Expand Up @@ -257,7 +256,7 @@ public static void doVerify(String lastToken, RedditClient baseReddit,boolean si
if (!lastToken.isEmpty()) {

Credentials credentials = Credentials.installedApp(
authentication.getString("CLIENT_ID", ""),
authentication.getString("CLIENT_ID", CLIENT_ID_FALLBACK),
authentication.getString("REDIRECT_URL", REDIRECT_URL_FALLBACK)
);
OAuthHelper oAuthHelper = baseReddit.getOAuthHelper();
Expand Down Expand Up @@ -311,7 +310,7 @@ public static void doVerify(String lastToken, RedditClient baseReddit,boolean si
if (BuildConfig.DEBUG) LogUtil.v("NOT LOGGED IN");

final Credentials fcreds =
Credentials.userlessApp(authentication.getString("CLIENT_ID", ""), UUID.randomUUID());
Credentials.userlessApp(authentication.getString("CLIENT_ID", CLIENT_ID_FALLBACK), UUID.randomUUID());
OAuthData authData;
try {

Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/me/ccrama/redditslide/OpenRedditLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.StrictMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -234,6 +237,27 @@ public static boolean openUrl(Context context, String url, boolean openIfOther)
i.putExtra(Profile.EXTRA_PROFILE, name);
break;
}
case SHARED: {
// fetch the actual reddit url from HTTP HEAD response header "location" and call this method again
StrictMode.ThreadPolicy gfgPolicy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(gfgPolicy);
HttpURLConnection urlConnection = null;
try {
URL newUrl = new URL(url);
urlConnection = (HttpURLConnection) newUrl.openConnection();
urlConnection.setRequestMethod("HEAD");
urlConnection.setInstanceFollowRedirects(false);
url = urlConnection.getHeaderField("location");
} catch (Exception e) {
url = "";
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return openUrl(context, url, openIfOther);
}
case HOME: {
i = new Intent(context, MainActivity.class);
break;
Expand Down Expand Up @@ -387,6 +411,9 @@ public static RedditLinkType getRedditLinkType(@NonNull Uri uri) {
} else if (path.matches("(?i)/r/[a-z0-9-_.]+/search.*")) {
// Wiki link. Format: reddit.com/r/$subreddit/search?q= [optional]
return RedditLinkType.SEARCH;
} else if (path.matches("(?i)/r/[a-z0-9-_.]+/s/.*")) {
// Shared link. Format: reddit.com/r/$subreddit/s/
return RedditLinkType.SHARED;
} else if (path.matches("(?i)/r/[a-z0-9-_.]+/submit.*")) {
// Submit post link. Format: reddit.com/r/$subreddit/submit
return RedditLinkType.SUBMIT;
Expand Down Expand Up @@ -421,6 +448,7 @@ public enum RedditLinkType {
SHORTENED,
WIKI,
COMMENT_PERMALINK,
SHARED,
SUBMISSION,
SUBMISSION_WITHOUT_SUB,
SUBREDDIT,
Expand Down

0 comments on commit 846a06d

Please sign in to comment.