diff options
25 files changed, 106 insertions, 47 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index eafc549f..99858508 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -129,6 +129,7 @@ class AccessTokenAuthenticator implements Authenticator { String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, Account.ANONYMOUS_ACCOUNT).equals(Account.ANONYMOUS_ACCOUNT)) { mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); + mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, newAccessToken).apply(); } return newAccessToken; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ApplicationOnlyAccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ApplicationOnlyAccessTokenAuthenticator.java index 8dc2e461..75f0ed13 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ApplicationOnlyAccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ApplicationOnlyAccessTokenAuthenticator.java @@ -80,6 +80,7 @@ class ApplicationOnlyAccessTokenAuthenticator implements Authenticator { JSONObject jsonObject = new JSONObject(response.body()); String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); mRedditDataRoomDatabase.accountDao().updateAccessToken(Account.ANONYMOUS_ACCOUNT, newAccessToken); + mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, newAccessToken).apply(); if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, Account.ANONYMOUS_ACCOUNT).equals(Account.ANONYMOUS_ACCOUNT)) { mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java index e92ed14a..5aff6fc1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java @@ -12,9 +12,11 @@ import dagger.Provides; import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.network.SortTypeConverterFactory; import ml.docilealligator.infinityforreddit.utils.APIUtils; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import okhttp3.ConnectionPool; import okhttp3.Interceptor; import okhttp3.OkHttpClient; +import okhttp3.Request; import retrofit2.Retrofit; import retrofit2.adapter.guava.GuavaCallAdapterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory; @@ -77,6 +79,18 @@ abstract class NetworkModule { return retrofit.newBuilder() .baseUrl(APIUtils.OAUTH_API_BASE_URI) .client(httpClient.newBuilder() + .addInterceptor(chain -> { + if (!chain.request().headers().names().contains(APIUtils.AUTHORIZATION_KEY)) { + Request newRequest = chain.request().newBuilder() + .addHeader(APIUtils.AUTHORIZATION_KEY, + APIUtils.AUTHORIZATION_BASE + + currentAccountSharedPreferences.getString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, null)) + .build(); + return chain.proceed(newRequest); + } + + return chain.proceed(chain.request()); + }) .authenticator(new ApplicationOnlyAccessTokenAuthenticator(retrofit, accountRoomDatabase, currentAccountSharedPreferences)) .connectionPool(connectionPool) .build()) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java index f6b61a39..57ad6270 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -531,7 +531,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee private void loadSubredditIcon() { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java index 34bda5e2..201ef313 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java @@ -493,7 +493,7 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF private void loadSubredditIcon() { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java index 17b2e887..5f474442 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java @@ -482,7 +482,7 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr private void loadSubredditIcon() { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java index 740cd81f..bc7c3f9a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java @@ -529,7 +529,7 @@ public class PostPollActivity extends BaseActivity implements FlairBottomSheetFr private void loadSubredditIcon() { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java index f0d151a4..475cfcbf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java @@ -486,7 +486,7 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr private void loadSubredditIcon() { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java index 6ff3c081..bd6463b5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java @@ -512,7 +512,7 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF private void loadSubredditIcon() { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java index c10e6994..446f876b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java @@ -501,7 +501,7 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom private void loadSubredditIcon() { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java index 61a60034..128a69c9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java @@ -77,8 +77,8 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("no_oauth") - Retrofit mRetrofit; + @Named("application_only_oauth") + Retrofit mApplicationOnlyOauth; @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @@ -249,7 +249,7 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit mProvideUserAvatarCallbacks.add(provideUserAvatarCallback); if (!isLoadingUserAvatar) { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, - username, mRetrofit, iconImageUrl -> { + username, mApplicationOnlyOauth, iconImageUrl -> { isLoadingUserAvatar = false; mUserAvatar = iconImageUrl == null ? "" : iconImageUrl; for (ProvideUserAvatarCallback provideUserAvatarCallbackInArrayList : mProvideUserAvatarCallbacks) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index e57881f1..578001b9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -174,6 +174,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele @Named("oauth") Retrofit mOauthRetrofit; @Inject + @Named("application_only_oauth") + Retrofit mApplicationOnlyOauth; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -442,7 +445,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele subscriptionReady = false; if (resources.getString(R.string.follow).contentEquals(subscribeUserChip.getText())) { if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { - UserFollowing.anonymousFollowUser(mExecutor, new Handler(), mRetrofit, + UserFollowing.anonymousFollowUser(mExecutor, new Handler(), mApplicationOnlyOauth, username, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override public void onUserFollowingSuccess() { @@ -459,7 +462,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } }); } else { - UserFollowing.followUser(mOauthRetrofit, mRetrofit, mAccessToken, + UserFollowing.followUser(mOauthRetrofit, mApplicationOnlyOauth, mAccessToken, username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override public void onUserFollowingSuccess() { @@ -494,7 +497,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } }); } else { - UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, mAccessToken, + UserFollowing.unfollowUser(mOauthRetrofit, mApplicationOnlyOauth, mAccessToken, username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override public void onUserFollowingSuccess() { @@ -1064,7 +1067,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele private void fetchUserInfo() { if (!mFetchUserInfoSuccess) { - FetchUserData.fetchUserData(mRetrofit, username, new FetchUserData.FetchUserDataListener() { + FetchUserData.fetchUserData(mApplicationOnlyOauth, username, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { new ViewUserDetailActivity.InsertUserDataAsyncTask(mRedditDataRoomDatabase.userDao(), userData, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 03a28620..1f27a19d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -527,7 +527,8 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler if (mPost.getSubredditNamePrefixed().startsWith("u/")) { if (mPost.getAuthorIconUrl() == null) { String authorName = mPost.isAuthorDeleted() ? mPost.getSubredditNamePrefixed().substring(2) : mPost.getAuthor(); - LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, authorName, mOauthRetrofit, iconImageUrl -> { + LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, + authorName, mApplicationOnlyRetrofit, iconImageUrl -> { if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) @@ -561,7 +562,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler if (mPost.getSubredditIconUrl() == null) { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mPost.getSubredditNamePrefixed().substring(2), - mAccessToken, mApplicationOnlyRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java index 4c9ef8f7..0a013d12 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java @@ -59,7 +59,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R private final BaseActivity activity; private final Executor executor; private final Retrofit oauthRetrofit; - private final Retrofit retrofit; + private final Retrofit applicationOnlyOauth; private final String accessToken; private final String accountName; private final RedditDataRoomDatabase redditDataRoomDatabase; @@ -74,7 +74,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R private NetworkState networkState; private final Callback callback; - public UserListingRecyclerViewAdapter(BaseActivity activity, Executor executor, Retrofit oauthRetrofit, Retrofit retrofit, + public UserListingRecyclerViewAdapter(BaseActivity activity, Executor executor, Retrofit oauthRetrofit, Retrofit applicationOnlyOauth, CustomThemeWrapper customThemeWrapper, @Nullable String accessToken, @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, boolean isMultiSelection, Callback callback) { @@ -82,7 +82,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R this.activity = activity; this.executor = executor; this.oauthRetrofit = oauthRetrofit; - this.retrofit = retrofit; + this.applicationOnlyOauth = applicationOnlyOauth; this.accessToken = accessToken; this.accountName = accountName; this.redditDataRoomDatabase = redditDataRoomDatabase; @@ -150,7 +150,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R public void isNotSubscribed() { ((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE); ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { - UserFollowing.followUser(oauthRetrofit, retrofit, + UserFollowing.followUser(oauthRetrofit, applicationOnlyOauth, accessToken, userData.getName(), accountName, redditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java index ab11cb68..e8d0180c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java @@ -15,7 +15,7 @@ import retrofit2.Retrofit; public class LoadSubredditIcon { public static void loadSubredditIcon(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, - String subredditName, String accessToken, Retrofit applicationOnlyOauthRetrofit, + String subredditName, Retrofit applicationOnlyOauthRetrofit, LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener) { executor.execute(() -> { SubredditDao subredditDao = redditDataRoomDatabase.subredditDao(); @@ -25,7 +25,7 @@ public class LoadSubredditIcon { handler.post(() -> loadSubredditIconAsyncTaskListener.loadIconSuccess(iconImageUrl)); } else { handler.post(() -> FetchSubredditData.fetchSubredditData(applicationOnlyOauthRetrofit, - subredditName, accessToken, new FetchSubredditData.FetchSubredditDataListener() { + subredditName, new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(SubredditData subredditData1, int nCurrentOnlineSubscribers) { ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java index 2406f8f7..42e0c7ce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java @@ -13,14 +13,14 @@ import retrofit2.Retrofit; public class LoadUserData { public static void loadUserData(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, String userName, - Retrofit retrofit, LoadUserDataAsyncTaskListener loadUserDataAsyncTaskListener) { + Retrofit applicationOnlyOauth, LoadUserDataAsyncTaskListener loadUserDataAsyncTaskListener) { executor.execute(() -> { UserDao userDao = redditDataRoomDatabase.userDao(); if (userDao.getUserData(userName) != null) { String iconImageUrl = userDao.getUserData(userName).getIconUrl(); handler.post(() -> loadUserDataAsyncTaskListener.loadUserDataSuccess(iconImageUrl)); } else { - handler.post(() -> FetchUserData.fetchUserData(retrofit, userName, new FetchUserData.FetchUserDataListener() { + handler.post(() -> FetchUserData.fetchUserData(applicationOnlyOauth, userName, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { InsertUserData.insertUserData(executor, handler, redditDataRoomDatabase, userData, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java index d41d04bc..7a858064 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java @@ -21,8 +21,12 @@ public class SwitchToAnonymousMode { accountDao.markAllAccountsNonCurrent(); String redgifsAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, ""); + String applicationOnlyAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, null); + currentAccountSharedPreferences.edit().clear().apply(); currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, redgifsAccessToken).apply(); + currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, applicationOnlyAccessToken).apply(); + currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, applicationOnlyAccessToken).apply(); handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java index ceff2214..4d7f1130 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java @@ -157,9 +157,6 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato TextView mFetchPostInfoTextView; HistoryPostViewModel mHistoryPostViewModel; @Inject - @Named("no_oauth") - Retrofit mRetrofit; - @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -934,14 +931,14 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } else { if (isSubreddit) { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, - subredditOrUserName, accessToken, mApplicationOnlyRetrofit, + subredditOrUserName, mApplicationOnlyRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); } else { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, - mRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 5e3caec6..b5632e1c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -1745,14 +1745,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } else { if (isSubreddit) { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, - subredditOrUserName, accessToken, mApplicationOnlyRetrofit, + subredditOrUserName, mApplicationOnlyRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); } else { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, - mRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java index a12e5bf3..4ce99fe9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java @@ -83,6 +83,9 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato @Named("oauth") Retrofit mOauthRetrofit; @Inject + @Named("application_only_oauth") + Retrofit mApplicationOnlyOauth; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -122,7 +125,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato Resources resources = getResources(); - if ((mActivity instanceof BaseActivity && ((BaseActivity) mActivity).isImmersiveInterface())) { + if (((BaseActivity) mActivity).isImmersiveInterface()) { mUserListingRecyclerView.setPadding(0, 0, 0, ((BaseActivity) mActivity).getNavBarHeight()); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mSharedPreferences.getBoolean(SharedPreferencesUtils.IMMERSIVE_INTERFACE_KEY, true)) { @@ -143,7 +146,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); boolean nsfw = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean((accountName.equals(Account.ANONYMOUS_ACCOUNT) ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); - mAdapter = new UserListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mRetrofit, + mAdapter = new UserListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mApplicationOnlyOauth, mCustomThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase, getArguments().getBoolean(EXTRA_IS_MULTI_SELECTION, false), new UserListingRecyclerViewAdapter.Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index 9ac12810..d1fb5e8d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -920,7 +920,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic loadIconListener.loadIconSuccess(authorName, activity.authorIcons.get(authorName)); } else { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, authorName, - mRetrofit, iconImageUrl -> { + mApplicationOnlyRetrofit, iconImageUrl -> { activity.authorIcons.put(authorName, iconImageUrl); loadIconListener.loadIconSuccess(authorName, iconImageUrl); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java index c4c774f1..b4ca4f0f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.subreddit; import androidx.annotation.NonNull; import java.util.ArrayList; +import java.util.HashMap; import java.util.Map; import ml.docilealligator.infinityforreddit.SortType; @@ -44,6 +45,37 @@ public class FetchSubredditData { }); } + public static void fetchSubredditData(Retrofit applicationOnlyOauthRetrofit, String subredditName, + final FetchSubredditDataListener fetchSubredditDataListener) { + RedditAPI oauthApi = applicationOnlyOauthRetrofit.create(RedditAPI.class); + Call<String> subredditData = oauthApi.getSubredditDataOauth(subredditName, new HashMap<>()); + subredditData.enqueue(new Callback<>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + ParseSubredditData.parseSubredditData(response.body(), new ParseSubredditData.ParseSubredditDataListener() { + @Override + public void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + fetchSubredditDataListener.onFetchSubredditDataSuccess(subredditData, nCurrentOnlineSubscribers); + } + + @Override + public void onParseSubredditDataFail() { + fetchSubredditDataListener.onFetchSubredditDataFail(false); + } + }); + } else { + fetchSubredditDataListener.onFetchSubredditDataFail(response.code() == 403); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + fetchSubredditDataListener.onFetchSubredditDataFail(false); + } + }); + } + static void fetchSubredditListingData(Retrofit retrofit, String query, String after, SortType.Type sortType, String accessToken, boolean nsfw, final FetchSubredditListingDataListener fetchSubredditListingDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java index b2a69826..a94eef35 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.user; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.util.ArrayList; @@ -13,11 +14,12 @@ import retrofit2.Callback; import retrofit2.Retrofit; public class FetchUserData { - public static void fetchUserData(Retrofit retrofit, String userName, FetchUserDataListener fetchUserDataListener) { - fetchUserData(null, retrofit, null, userName, fetchUserDataListener); + public static void fetchUserData(Retrofit applicationOnlyOauth, String userName, + FetchUserDataListener fetchUserDataListener) { + fetchUserData(null, applicationOnlyOauth, null, userName, fetchUserDataListener); } - public static void fetchUserData(RedditDataRoomDatabase redditDataRoomDatabase, Retrofit retrofit, + public static void fetchUserData(@Nullable RedditDataRoomDatabase redditDataRoomDatabase, Retrofit retrofit, String accessToken, String userName, FetchUserDataListener fetchUserDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java index 871dfc78..a6c181ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java @@ -21,18 +21,18 @@ import retrofit2.Callback; import retrofit2.Retrofit; public class UserFollowing { - public static void followUser(Retrofit oauthRetrofit, Retrofit retrofit, + public static void followUser(Retrofit oauthRetrofit, Retrofit applicationOnlyOauth, @Nullable String accessToken, String username, @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, UserFollowingListener userFollowingListener) { - userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "sub", + userFollowing(oauthRetrofit, applicationOnlyOauth, accessToken, username, accountName, "sub", redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); } - public static void anonymousFollowUser(Executor executor, Handler handler, Retrofit retrofit, String username, - RedditDataRoomDatabase redditDataRoomDatabase, + public static void anonymousFollowUser(Executor executor, Handler handler, Retrofit applicationOnlyOauth, + String username, RedditDataRoomDatabase redditDataRoomDatabase, UserFollowingListener userFollowingListener) { - FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { + FetchUserData.fetchUserData(applicationOnlyOauth, username, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { executor.execute(() -> { @@ -53,11 +53,11 @@ public class UserFollowing { }); } - public static void unfollowUser(Retrofit oauthRetrofit, Retrofit retrofit, + public static void unfollowUser(Retrofit oauthRetrofit, Retrofit applicationOnlyOauth, @Nullable String accessToken, String username, @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, UserFollowingListener userFollowingListener) { - userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "unsub", + userFollowing(oauthRetrofit, applicationOnlyOauth, accessToken, username, accountName, "unsub", redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); } @@ -71,7 +71,7 @@ public class UserFollowing { }); } - private static void userFollowing(Retrofit oauthRetrofit, Retrofit retrofit, @Nullable String accessToken, + private static void userFollowing(Retrofit oauthRetrofit, Retrofit applicationOnlyOauth, @Nullable String accessToken, String username, @NonNull String accountName, String action, SubscribedUserDao subscribedUserDao, UserFollowingListener userFollowingListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); @@ -81,12 +81,12 @@ public class UserFollowing { params.put(APIUtils.SR_NAME_KEY, "u_" + username); Call<String> subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); - subredditSubscriptionCall.enqueue(new Callback<String>() { + subredditSubscriptionCall.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { if (action.equals("sub")) { - FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { + FetchUserData.fetchUserData(applicationOnlyOauth, username, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { new UpdateSubscriptionAsyncTask(subscribedUserDao, userData, accountName, true).execute(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index de2e72d4..863a49a6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -339,6 +339,7 @@ public class SharedPreferencesUtils { public static final String CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.current_account"; public static final String ACCOUNT_NAME = "account_name"; public static final String ACCESS_TOKEN = "access_token"; + public static final String APPLICATION_ONLY_ACCESS_TOKEN = "app_only_access_token"; public static final String ACCOUNT_IMAGE_URL = "account_image_url"; public static final String REDGIFS_ACCESS_TOKEN = "redgifs_access_token"; |