Skip to content

Commit

Permalink
Add possibility to enter Client ID/API Key during login
Browse files Browse the repository at this point in the history
  • Loading branch information
adanski committed Dec 3, 2023
1 parent 97abc5e commit c6081cc
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
compileSdk 33
defaultConfig {
applicationId "ml.docilealligator.infinityforreddit"
minSdk 21
minSdk 26
targetSdk 33
versionCode 143
versionName "6.5.0-beta1"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name=".InfinityPersonal"
android:name=".Infinity"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/application_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dagger.Provides;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.LoopAvailableExoCreator;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.videoautoplay.Config;
Expand Down Expand Up @@ -114,7 +115,12 @@ static SharedPreferences providePostHistorySharedPreferences(Application applica
@Provides
@Named("current_account")
static SharedPreferences provideCurrentAccountSharedPreferences(Application application) {
return application.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
SharedPreferences prefs = application.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
String accountName = prefs.getString(SharedPreferencesUtils.ACCOUNT_NAME, "deleted");
String clientId = prefs.getString(APIUtils.CLIENT_ID_KEY, "NONE");
APIUtils.USER_AGENT = APIUtils.USER_AGENT.replaceFirst("/u/.*\\)", "/u/" + accountName + ")");
APIUtils.CLIENT_ID = clientId;
return prefs;
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public void migrate(@NonNull SupportSQLiteDatabase database) {
"(name TEXT NOT NULL PRIMARY KEY, max_vote INTEGER NOT NULL, min_vote INTEGER NOT NULL, exclude_strings TEXT, exclude_users TEXT)");
database.execSQL("CREATE TABLE comment_filter_usage (name TEXT NOT NULL, usage INTEGER NOT NULL, " +
"name_of_usage TEXT NOT NULL, PRIMARY KEY(name, usage, name_of_usage), FOREIGN KEY(name) REFERENCES comment_filter(name) ON DELETE CASCADE)");
database.execSQL("ALTER TABLE accounts ADD COLUMN client_id TEXT DEFAULT 'NONE' NOT NULL");
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class Account implements Parcelable {
private String code;
@ColumnInfo(name = "is_current_user")
private boolean isCurrentUser;
@ColumnInfo(name = "client_id")
private String clientId;

@Ignore
protected Account(Parcel in) {
Expand All @@ -40,6 +42,7 @@ protected Account(Parcel in) {
refreshToken = in.readString();
code = in.readString();
isCurrentUser = in.readByte() != 0;
clientId = in.readString();
}

public static final Creator<Account> CREATOR = new Creator<Account>() {
Expand All @@ -56,11 +59,12 @@ public Account[] newArray(int size) {

@Ignore
public static Account getAnonymousAccount() {
return new Account("-", null, null, null, null, null, 0, false);
return new Account("-", null, null, null, null, null, 0, false, "NONE");
}

public Account(@NonNull String accountName, String accessToken, String refreshToken, String code,
String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser) {
String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser,
String clientId) {
this.accountName = accountName;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
Expand All @@ -69,6 +73,7 @@ public Account(@NonNull String accountName, String accessToken, String refreshTo
this.bannerImageUrl = bannerImageUrl;
this.karma = karma;
this.isCurrentUser = isCurrentUser;
this.clientId = clientId;
}

@NonNull
Expand Down Expand Up @@ -108,6 +113,10 @@ public boolean isCurrentUser() {
return isCurrentUser;
}

public String getClientId() {
return clientId;
}

@Override
public int describeContents() {
return 0;
Expand All @@ -123,5 +132,6 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(refreshToken);
dest.writeString(code);
dest.writeByte((byte) (isCurrentUser ? 1 : 0));
dest.writeString(clientId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
Expand All @@ -14,9 +15,12 @@
import android.view.InflateException;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -75,6 +79,12 @@ public class LoginActivity extends BaseActivity {
WebView webView;
@BindView(R.id.fab_login_activity)
FloatingActionButton fab;
@BindView(R.id.client_id_info)
TextView clientIdInfoTextView;
@BindView(R.id.client_id_input)
EditText clientIdInputEditText;
@BindView(R.id.client_id_apply)
Button clientApplyButton;
@Inject
@Named("no_oauth")
Retrofit mRetrofit;
Expand Down Expand Up @@ -150,9 +160,63 @@ protected void onCreate(Bundle savedInstanceState) {
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(enableDom);

String clientId = mCurrentAccountSharedPreferences.getString(APIUtils.CLIENT_ID_KEY, "NONE");
if (!clientId.isBlank() && !clientId.equals("NONE")) {
clientApplyButton.setText("Update");
clientIdInfoTextView.setText(R.string.login_activity_input_client_id_update);
clientIdInputEditText.setText(clientId);
loadLoginWebview(clientId);
}

clientApplyButton.setOnClickListener(view -> {
view.setClickable(false);
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);

String cId = clientIdInputEditText.getText().toString();

if (cId.isBlank()) {
Toast.makeText(LoginActivity.this, "Please enter a Client ID/API Key first", Toast.LENGTH_LONG).show();
view.setClickable(true);
return;
}
mCurrentAccountSharedPreferences.edit().putString(APIUtils.CLIENT_ID_KEY, cId).apply();
APIUtils.CLIENT_ID = cId;
view.setClickable(true);
loadLoginWebview(cId);
});

if (!isAgreeToUserAgreement) {
TextView messageTextView = new TextView(this);
int padding = (int) Utils.convertDpToPixel(24, this);
messageTextView.setPaddingRelative(padding, padding, padding, padding);
SpannableString message = new SpannableString(getString(R.string.user_agreement_message, "https://www.redditinc.com/policies/user-agreement-september-12-2021", "https://docile-alligator.github.io"));
Linkify.addLinks(message, Linkify.WEB_URLS);
messageTextView.setMovementMethod(BetterLinkMovementMethod.newInstance().setOnLinkClickListener(new BetterLinkMovementMethod.OnLinkClickListener() {
@Override
public boolean onClick(TextView textView, String url) {
Intent intent = new Intent(LoginActivity.this, LinkResolverActivity.class);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
}));
messageTextView.setLinkTextColor(getResources().getColor(R.color.colorAccent));
messageTextView.setText(message);
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(getString(R.string.user_agreement_dialog_title))
.setView(messageTextView)
.setPositiveButton(R.string.agree, (dialogInterface, i) -> isAgreeToUserAgreement = true)
.setNegativeButton(R.string.do_not_agree, (dialogInterface, i) -> finish())
.setCancelable(false)
.show();
}
}

void loadLoginWebview(String clientId) {
Uri baseUri = Uri.parse(APIUtils.OAUTH_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter(APIUtils.CLIENT_ID_KEY, APIUtils.CLIENT_ID);
uriBuilder.appendQueryParameter(APIUtils.CLIENT_ID_KEY, clientId);
uriBuilder.appendQueryParameter(APIUtils.RESPONSE_TYPE_KEY, APIUtils.RESPONSE_TYPE);
uriBuilder.appendQueryParameter(APIUtils.STATE_KEY, APIUtils.STATE);
uriBuilder.appendQueryParameter(APIUtils.REDIRECT_URI_KEY, APIUtils.REDIRECT_URI);
Expand Down Expand Up @@ -201,15 +265,19 @@ public void onResponse(@NonNull Call<String> call, @NonNull Response<String> res
@Override
public void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken)
.putString(APIUtils.CLIENT_ID_KEY, clientId)
.putString(SharedPreferencesUtils.ACCOUNT_NAME, name)
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, profileImageUrl).apply();
APIUtils.CLIENT_ID = clientId;
APIUtils.USER_AGENT = APIUtils.USER_AGENT.replaceFirst("/u/.*\\)", "/u/" + name + ")");
ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name, accessToken, refreshToken, profileImageUrl, bannerImageUrl,
karma, authCode, mRedditDataRoomDatabase.accountDao(),
() -> {
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
});
},
clientId);
}

@Override
Expand All @@ -222,7 +290,7 @@ public void onFetchMyInfoFailed(boolean parseFailed) {

finish();
}
});
});
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(LoginActivity.this, R.string.parse_json_response_error, Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -266,32 +334,6 @@ public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});

if (!isAgreeToUserAgreement) {
TextView messageTextView = new TextView(this);
int padding = (int) Utils.convertDpToPixel(24, this);
messageTextView.setPaddingRelative(padding, padding, padding, padding);
SpannableString message = new SpannableString(getString(R.string.user_agreement_message, "https://www.redditinc.com/policies/user-agreement", "https://docile-alligator.github.io"));
Linkify.addLinks(message, Linkify.WEB_URLS);
messageTextView.setMovementMethod(BetterLinkMovementMethod.newInstance().setOnLinkClickListener(new BetterLinkMovementMethod.OnLinkClickListener() {
@Override
public boolean onClick(TextView textView, String url) {
Intent intent = new Intent(LoginActivity.this, LinkResolverActivity.class);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
}));
messageTextView.setLinkTextColor(getResources().getColor(R.color.colorAccent));
messageTextView.setText(message);
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(getString(R.string.user_agreement_dialog_title))
.setView(messageTextView)
.setPositiveButton(R.string.agree, (dialogInterface, i) -> isAgreeToUserAgreement = true)
.setNegativeButton(R.string.do_not_agree, (dialogInterface, i) -> finish())
.setCancelable(false)
.show();
}
}

@Override
Expand Down Expand Up @@ -322,6 +364,11 @@ protected void applyCustomTheme() {
if (typeface != null) {
twoFAInfoTextView.setTypeface(typeface);
}
clientIdInfoTextView.setTextColor(mCustomThemeWrapper.getPrimaryTextColor());
clientIdInputEditText.setTextColor(mCustomThemeWrapper.getPrimaryTextColor());
clientApplyButton.setBackgroundTintList(ColorStateList.valueOf(customThemeWrapper.getColorAccent()));
clientApplyButton.setTextColor(mCustomThemeWrapper.getButtonTextColor());

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public class ParseAndInsertNewAccount {
public static void parseAndInsertNewAccount(Executor executor, Handler handler, String username,
String accessToken, String refreshToken, String profileImageUrl,
String bannerImageUrl, int karma, String code, AccountDao accountDao,
ParseAndInsertAccountListener parseAndInsertAccountListener) {
ParseAndInsertAccountListener parseAndInsertAccountListener,
String clientId) {
executor.execute(() -> {
Account account = new Account(username, accessToken, refreshToken, code, profileImageUrl,
bannerImageUrl, karma, true);
bannerImageUrl, karma, true, clientId);
accountDao.markAllAccountsNonCurrent();
accountDao.insert(account);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;

public class SwitchAccount {
Expand All @@ -22,6 +23,7 @@ public static void switchAccount(RedditDataRoomDatabase redditDataRoomDatabase,
.putString(SharedPreferencesUtils.ACCESS_TOKEN, account.getAccessToken())
.putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getAccountName())
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, account.getProfileImageUrl()).apply();
APIUtils.USER_AGENT = APIUtils.USER_AGENT.replaceFirst("/u/.*\\)", "/u/" + account.getAccountName() + ")");
handler.post(() -> switchAccountListener.switched(account));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class APIUtils {

public static final String CLIENT_ID_KEY = "client_id";
public static final String CLIENT_SECRET_KEY = "client_secret";
public static final String CLIENT_ID = "NOe2iKrPPzwscA";
public static volatile String CLIENT_ID = "NONE";
public static final String IMGUR_CLIENT_ID = "Client-ID cc671794e0ab397";
public static final String REDGIFS_CLIENT_ID = "1828d0bcc93-15ac-bde6-0005-d2ecbe8daab3";
public static final String REDGIFS_CLIENT_SECRET = "TJBlw7jRXW65NAGgFBtgZHu97WlzRXHYybK81sZ9dLM=";
Expand All @@ -36,7 +36,7 @@ public class APIUtils {
public static final String STATE_KEY = "state";
public static final String STATE = "23ro8xlxvzp4asqd";
public static final String REDIRECT_URI_KEY = "redirect_uri";
public static final String REDIRECT_URI = "infinity://localhost";
public static final String REDIRECT_URI = "http://localhost:4321";
public static final String DURATION_KEY = "duration";
public static final String DURATION = "permanent";
public static final String SCOPE_KEY = "scope";
Expand All @@ -46,7 +46,7 @@ public class APIUtils {
public static final String AUTHORIZATION_KEY = "Authorization";
public static final String AUTHORIZATION_BASE = "bearer ";
public static final String USER_AGENT_KEY = "User-Agent";
public static final String USER_AGENT = "android:ml.docilealligator.infinityforreddit:v6.2.5 (by /u/Hostilenemy)";
public static volatile String USER_AGENT = "android:infinity-personal:v6.5.0 (by /u/deleted)";

public static final String GRANT_TYPE_KEY = "grant_type";
public static final String GRANT_TYPE_REFRESH_TOKEN = "refresh_token";
Expand Down
Loading

0 comments on commit c6081cc

Please sign in to comment.