Finally fixed java.lang.RuntimeException: Canvas: trying to draw too large bitmap. Rewrite some AsyncTasks using Executor.

This commit is contained in:
Alex Ning 2021-01-23 23:07:54 +08:00
parent 61b94718ef
commit 012dca977a
18 changed files with 198 additions and 263 deletions

View File

@ -22,8 +22,6 @@ import com.r0adkll.slidr.Slidr;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -33,7 +31,6 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment;
@ -61,8 +58,6 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect
@BindView(R.id.toolbar_account_posts_activity)
Toolbar toolbar;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@ -73,8 +68,6 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private boolean isInLazyMode = false;
private String mAccessToken;
private String mAccountName;

View File

@ -32,8 +32,6 @@ import org.commonmark.ext.gfm.tables.TableBlock;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -101,8 +99,6 @@ public class CommentActivity extends BaseActivity {
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private String mAccessToken;
private String parentFullname;
private int parentDepth;

View File

@ -23,7 +23,6 @@ import com.google.android.material.snackbar.Snackbar;
import com.r0adkll.slidr.Slidr;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -78,8 +77,6 @@ public class CreateMultiRedditActivity extends BaseActivity {
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private String mAccessToken;
private String mAccountName;
private ArrayList<String> mSubreddits;

View File

@ -5,6 +5,7 @@ import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
@ -29,6 +30,8 @@ import com.google.gson.JsonSyntaxException;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -37,8 +40,8 @@ import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.adapters.CustomThemeListingRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.ChangeThemeNameAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.DeleteThemeAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.GetCustomThemeAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.InsertCustomThemeAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.GetCustomTheme;
import ml.docilealligator.infinityforreddit.asynctasks.InsertCustomTheme;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.CreateThemeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.CustomThemeOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomTheme;
@ -82,6 +85,8 @@ public class CustomThemeListingActivity extends BaseActivity implements
@Inject
@Named("amoled_theme")
SharedPreferences amoledThemeSharedPreferences;
@Inject
Executor executor;
public CustomThemeViewModel customThemeViewModel;
@Override
@ -183,7 +188,8 @@ public class CustomThemeListingActivity extends BaseActivity implements
@Override
public void shareTheme(String themeName) {
new GetCustomThemeAsyncTask(redditDataRoomDatabase, themeName, customTheme -> {
GetCustomTheme.getCustomTheme(executor, new Handler(), redditDataRoomDatabase, themeName,
customTheme -> {
if (customTheme != null) {
String jsonModel = customTheme.getJSONModel();
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
@ -197,7 +203,7 @@ public class CustomThemeListingActivity extends BaseActivity implements
} else {
Snackbar.make(coordinatorLayout, R.string.cannot_find_theme, Snackbar.LENGTH_SHORT).show();
}
}).execute();
});
}
@Override
@ -277,9 +283,9 @@ public class CustomThemeListingActivity extends BaseActivity implements
}
private void checkDuplicateAndImportTheme(CustomTheme customTheme, boolean checkDuplicate) {
new InsertCustomThemeAsyncTask(redditDataRoomDatabase, lightThemeSharedPreferences,
InsertCustomTheme.insertCustomTheme(executor, new Handler(), redditDataRoomDatabase, lightThemeSharedPreferences,
darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme, checkDuplicate,
new InsertCustomThemeAsyncTask.InsertCustomThemeAsyncTaskListener() {
new InsertCustomTheme.InsertCustomThemeListener() {
@Override
public void success() {
Toast.makeText(CustomThemeListingActivity.this, R.string.import_theme_success, Toast.LENGTH_SHORT).show();
@ -327,6 +333,6 @@ public class CustomThemeListingActivity extends BaseActivity implements
})
.show();
}
}).execute();
});
}
}

View File

@ -4,6 +4,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
@ -20,6 +21,7 @@ import com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -27,8 +29,8 @@ import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.adapters.CustomizeThemeRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.GetCustomThemeAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.InsertCustomThemeAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.GetCustomTheme;
import ml.docilealligator.infinityforreddit.asynctasks.InsertCustomTheme;
import ml.docilealligator.infinityforreddit.customtheme.CustomTheme;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeSettingsItem;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
@ -74,6 +76,8 @@ public class CustomizeThemeActivity extends BaseActivity {
RedditDataRoomDatabase redditDataRoomDatabase;
@Inject
CustomThemeWrapper customThemeWrapper;
@Inject
Executor mExecutor;
private String themeName;
private boolean isPredefinedTheme;
@ -112,8 +116,7 @@ public class CustomizeThemeActivity extends BaseActivity {
if (customThemeSettingsItems == null) {
if (getIntent().hasExtra(EXTRA_THEME_TYPE)) {
int themeType = getIntent().getIntExtra(EXTRA_THEME_TYPE, EXTRA_LIGHT_THEME);
new GetCustomThemeAsyncTask(redditDataRoomDatabase, themeType, customTheme -> {
GetCustomTheme.getCustomTheme(mExecutor, new Handler(), redditDataRoomDatabase, themeType, customTheme -> {
if (customTheme == null) {
isPredefinedTheme = true;
switch (themeType) {
@ -147,7 +150,7 @@ public class CustomizeThemeActivity extends BaseActivity {
adapter = new CustomizeThemeRecyclerViewAdapter(this, themeName, isPredefinedTheme);
recyclerView.setAdapter(adapter);
adapter.setCustomThemeSettingsItem(customThemeSettingsItems);
}).execute();
});
} else {
isPredefinedTheme = getIntent().getBooleanExtra(EXTRA_IS_PREDEFIINED_THEME, false);
themeName = getIntent().getStringExtra(EXTRA_THEME_NAME);
@ -163,13 +166,13 @@ public class CustomizeThemeActivity extends BaseActivity {
recyclerView.setAdapter(adapter);
adapter.setCustomThemeSettingsItem(customThemeSettingsItems);
} else {
new GetCustomThemeAsyncTask(redditDataRoomDatabase, themeName,
customTheme -> {
GetCustomTheme.getCustomTheme(mExecutor, new Handler(), redditDataRoomDatabase,
themeName, customTheme -> {
customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem(
CustomizeThemeActivity.this, customTheme, androidVersion);
adapter.setCustomThemeSettingsItem(customThemeSettingsItems);
}).execute();
});
}
}
} else {
@ -206,13 +209,13 @@ public class CustomizeThemeActivity extends BaseActivity {
return true;
}
CustomTheme customTheme = CustomTheme.convertSettingsItemsToCustomTheme(customThemeSettingsItems, themeName);
new InsertCustomThemeAsyncTask(redditDataRoomDatabase, lightThemeSharedPreferences,
InsertCustomTheme.insertCustomTheme(mExecutor, new Handler(), redditDataRoomDatabase, lightThemeSharedPreferences,
darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme,
false, () -> {
Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new RecreateActivityEvent());
finish();
}).execute();
Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new RecreateActivityEvent());
finish();
});
}
return true;

View File

@ -39,6 +39,7 @@ import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
@ -49,7 +50,6 @@ public class EditCommentActivity extends BaseActivity {
public static final String EXTRA_CONTENT = "EC";
public static final String EXTRA_FULLNAME = "EF";
public static final String EXTRA_ACCESS_TOKEN = "EAT";
public static final String EXTRA_POSITION = "EP";
static final String EXTRA_EDITED_COMMENT_CONTENT = "EECC";
@ -72,6 +72,9 @@ public class EditCommentActivity extends BaseActivity {
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
private String mFullName;
private String mAccessToken;
@ -102,7 +105,7 @@ public class EditCommentActivity extends BaseActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFullName = getIntent().getStringExtra(EXTRA_FULLNAME);
mAccessToken = getIntent().getStringExtra(EXTRA_ACCESS_TOKEN);
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
mCommentContent = getIntent().getStringExtra(EXTRA_CONTENT);
contentEditText.setText(mCommentContent);

View File

@ -24,8 +24,6 @@ import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.snackbar.Snackbar;
import com.r0adkll.slidr.Slidr;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -86,8 +84,6 @@ public class EditMultiRedditActivity extends BaseActivity {
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private String mAccessToken;
private String mAccountName;
private MultiReddit multiReddit;

View File

@ -53,7 +53,6 @@ public class EditPostActivity extends BaseActivity {
static final String EXTRA_TITLE = "ET";
static final String EXTRA_CONTENT = "EC";
static final String EXTRA_FULLNAME = "EF";
static final String EXTRA_ACCESS_TOKEN = "EAT";
@BindView(R.id.coordinator_layout_edit_post_activity)
CoordinatorLayout coordinatorLayout;
@ -76,6 +75,9 @@ public class EditPostActivity extends BaseActivity {
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
private String mFullName;
private String mAccessToken;
@ -110,7 +112,7 @@ public class EditPostActivity extends BaseActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFullName = getIntent().getStringExtra(EXTRA_FULLNAME);
mAccessToken = getIntent().getStringExtra(EXTRA_ACCESS_TOKEN);
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
titleTextView.setText(getIntent().getStringExtra(EXTRA_TITLE));
mPostContent = getIntent().getStringExtra(EXTRA_CONTENT);
contentEditText.setText(mPostContent);

View File

@ -7,6 +7,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
@ -122,8 +123,8 @@ public class GiveAwardActivity extends BaseActivity {
.setPositiveButton(R.string.yes, (dialogInterface, i) -> {
boolean isAnonymous = switchMaterial.isChecked();
GiveAward.giveAwardV2(mOauthRetrofit, mAccessToken, thingFullname, award.getId(),
isAnonymous, new GiveAward.GiveAwardListener() {
GiveAward.giveAwardV2(mExecutor, new Handler(), mOauthRetrofit, mAccessToken,
thingFullname, award.getId(), isAnonymous, new GiveAward.GiveAwardListener() {
@Override
public void success(String awardsHTML, int awardCount) {
Intent data = new Intent();

View File

@ -1612,7 +1612,6 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
return true;
case R.id.action_edit_view_post_detail_activity:
Intent editPostIntent = new Intent(this, EditPostActivity.class);
editPostIntent.putExtra(EditPostActivity.EXTRA_ACCESS_TOKEN, mAccessToken);
editPostIntent.putExtra(EditPostActivity.EXTRA_FULLNAME, mPost.getFullName());
editPostIntent.putExtra(EditPostActivity.EXTRA_TITLE, mPost.getTitle());
editPostIntent.putExtra(EditPostActivity.EXTRA_CONTENT, mPost.getSelfText());

View File

@ -883,7 +883,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
if (preview != null) {
((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewWidth() / preview.getPreviewHeight());
if (mImageViewWidth > preview.getPreviewWidth()) {
mGlide.load(preview.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView);
mGlide.load(preview.getPreviewUrl()).override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView);
} else {
mGlide.load(preview.getPreviewUrl()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView);
}
@ -1500,7 +1500,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
preview.setPreviewWidth(preview.getPreviewWidth() / divisor);
preview.setPreviewHeight(preview.getPreviewHeight() / divisor);
divisor *= 2;
} while (preview.getPreviewWidth() * preview.getPreviewHeight() / divisor / divisor > 10_000_000);
} while (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000);
}
return preview;
@ -1537,7 +1537,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
} else {
if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
} else {
imageRequestBuilder.into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView);
}
@ -1569,7 +1569,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
} else {
if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
} else {
imageRequestBuilder.into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView);
}
@ -1601,7 +1601,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
.into(((PostDetailLinkViewHolder) holder).mImageView);
} else {
if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailLinkViewHolder) holder).mImageView);
imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailLinkViewHolder) holder).mImageView);
} else {
imageRequestBuilder.into(((PostDetailLinkViewHolder) holder).mImageView);
}
@ -1632,7 +1632,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(((PostDetailGalleryViewHolder) holder).mImageView);
} else {
if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostDetailGalleryViewHolder) holder).mImageView);
imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostDetailGalleryViewHolder) holder).mImageView);
} else {
imageRequestBuilder.into(((PostDetailGalleryViewHolder) holder).mImageView);
}

View File

@ -10,6 +10,7 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -1067,14 +1068,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
}
if (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000) {
Log.i("afasfasdf", "ssss " + preview.getPreviewUrl());
int divisor = 2;
do {
preview.setPreviewWidth(preview.getPreviewWidth() / divisor);
preview.setPreviewHeight(preview.getPreviewHeight() / divisor);
divisor *= 2;
} while (preview.getPreviewWidth() * preview.getPreviewHeight() / divisor / divisor > 10_000_000);
} while (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000);
}
Log.i("afasfasdf", "s " + preview.getPreviewWidth() * preview.getPreviewHeight() + " " + preview.getPreviewHeight() + " " + preview.getPreviewWidth() + " " + preview.getPreviewUrl());
return preview;
}
@ -1116,7 +1119,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
.into(((PostWithPreviewTypeViewHolder) holder).imageView);
} else {
if (mImageViewWidth > preview.getPreviewWidth()) {
imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostWithPreviewTypeViewHolder) holder).imageView);
imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostWithPreviewTypeViewHolder) holder).imageView);
} else {
imageRequestBuilder.into(((PostWithPreviewTypeViewHolder) holder).imageView);
}

View File

@ -0,0 +1,45 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.os.Handler;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.customtheme.CustomTheme;
import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils;
public class GetCustomTheme {
public static void getCustomTheme(Executor executor, Handler handler,
RedditDataRoomDatabase redditDataRoomDatabase,
String customThemeName,
GetCustomThemeListener getCustomThemeListener) {
executor.execute(() -> {
CustomTheme customTheme = redditDataRoomDatabase.customThemeDao().getCustomTheme(customThemeName);
handler.post(() -> getCustomThemeListener.success(customTheme));
});
}
public static void getCustomTheme(Executor executor, Handler handler,
RedditDataRoomDatabase redditDataRoomDatabase,
int themeType,
GetCustomThemeListener getCustomThemeListener) {
executor.execute(() -> {
CustomTheme customTheme;
switch (themeType) {
case CustomThemeSharedPreferencesUtils.DARK:
customTheme = redditDataRoomDatabase.customThemeDao().getDarkCustomTheme();
break;
case CustomThemeSharedPreferencesUtils.AMOLED:
customTheme = redditDataRoomDatabase.customThemeDao().getAmoledCustomTheme();
break;
default:
customTheme = redditDataRoomDatabase.customThemeDao().getLightCustomTheme();
}
handler.post(() -> getCustomThemeListener.success(customTheme));
});
}
public interface GetCustomThemeListener {
void success(CustomTheme customTheme);
}
}

View File

@ -1,60 +0,0 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.os.AsyncTask;
import ml.docilealligator.infinityforreddit.customtheme.CustomTheme;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils;
public class GetCustomThemeAsyncTask extends AsyncTask<Void, Void, Void> {
private RedditDataRoomDatabase redditDataRoomDatabase;
private String customThemeName;
private int themeType;
private GetCustomThemeAsyncTaskListener getCustomThemeAsyncTaskListener;
private CustomTheme customTheme;
public interface GetCustomThemeAsyncTaskListener {
void success(CustomTheme customTheme);
}
public GetCustomThemeAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
String customThemeName,
GetCustomThemeAsyncTaskListener getCustomThemeAsyncTaskListener) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.customThemeName = customThemeName;
this.getCustomThemeAsyncTaskListener = getCustomThemeAsyncTaskListener;
}
public GetCustomThemeAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
int themeType,
GetCustomThemeAsyncTaskListener getCustomThemeAsyncTaskListener) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.themeType = themeType;
this.getCustomThemeAsyncTaskListener = getCustomThemeAsyncTaskListener;
}
@Override
protected Void doInBackground(Void... voids) {
if (customThemeName != null) {
customTheme = redditDataRoomDatabase.customThemeDao().getCustomTheme(customThemeName);
} else {
switch (themeType) {
case CustomThemeSharedPreferencesUtils.DARK:
customTheme = redditDataRoomDatabase.customThemeDao().getDarkCustomTheme();
break;
case CustomThemeSharedPreferencesUtils.AMOLED:
customTheme = redditDataRoomDatabase.customThemeDao().getAmoledCustomTheme();
break;
default:
customTheme = redditDataRoomDatabase.customThemeDao().getLightCustomTheme();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
getCustomThemeAsyncTaskListener.success(customTheme);
}
}

View File

@ -0,0 +1,56 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.content.SharedPreferences;
import android.os.Handler;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.customtheme.CustomTheme;
import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils;
public class InsertCustomTheme {
public static void insertCustomTheme(Executor executor, Handler handler,
RedditDataRoomDatabase redditDataRoomDatabase,
SharedPreferences lightThemeSharedPreferences,
SharedPreferences darkThemeSharedPreferences,
SharedPreferences amoledThemeSharedPreferences,
CustomTheme customTheme, boolean checkDuplicate,
InsertCustomThemeListener insertCustomThemeListener) {
executor.execute(() -> {
if (checkDuplicate) {
if (redditDataRoomDatabase.customThemeDao().getCustomTheme(customTheme.name) != null) {
handler.post(insertCustomThemeListener::duplicate);
}
}
CustomTheme previousTheme = redditDataRoomDatabase.customThemeDao().getCustomTheme(customTheme.name);
if (customTheme.isLightTheme) {
redditDataRoomDatabase.customThemeDao().unsetLightTheme();
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, lightThemeSharedPreferences);
} else if (previousTheme != null && previousTheme.isLightTheme) {
lightThemeSharedPreferences.edit().clear().apply();
}
if (customTheme.isDarkTheme) {
redditDataRoomDatabase.customThemeDao().unsetDarkTheme();
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, darkThemeSharedPreferences);
} else if (previousTheme != null && previousTheme.isDarkTheme) {
darkThemeSharedPreferences.edit().clear().apply();
}
if (customTheme.isAmoledTheme) {
redditDataRoomDatabase.customThemeDao().unsetAmoledTheme();
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, amoledThemeSharedPreferences);
} else if (previousTheme != null && previousTheme.isAmoledTheme) {
amoledThemeSharedPreferences.edit().clear().apply();
}
redditDataRoomDatabase.customThemeDao().insert(customTheme);
handler.post(insertCustomThemeListener::success);
});
}
public interface InsertCustomThemeListener {
void success();
default void duplicate() {}
}
}

View File

@ -1,80 +0,0 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import ml.docilealligator.infinityforreddit.customtheme.CustomTheme;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils;
public class InsertCustomThemeAsyncTask extends AsyncTask<Void, Void, Void> {
private RedditDataRoomDatabase redditDataRoomDatabase;
private SharedPreferences lightThemeSharedPreferences;
private SharedPreferences darkThemeSharedPreferences;
private SharedPreferences amoledThemeSharedPreferences;
private CustomTheme customTheme;
private boolean checkDuplicate;
private InsertCustomThemeAsyncTaskListener insertCustomThemeAsyncTaskListener;
private boolean isDuplicate = false;
public interface InsertCustomThemeAsyncTaskListener {
void success();
default void duplicate() {}
}
public InsertCustomThemeAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
SharedPreferences lightThemeSharedPreferences,
SharedPreferences darkThemeSharedPreferences,
SharedPreferences amoledThemeSharedPreferences,
CustomTheme customTheme, boolean checkDuplicate,
InsertCustomThemeAsyncTaskListener insertCustomThemeAsyncTaskListener) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.lightThemeSharedPreferences = lightThemeSharedPreferences;
this.darkThemeSharedPreferences = darkThemeSharedPreferences;
this.amoledThemeSharedPreferences = amoledThemeSharedPreferences;
this.customTheme = customTheme;
this.checkDuplicate = checkDuplicate;
this.insertCustomThemeAsyncTaskListener = insertCustomThemeAsyncTaskListener;
}
@Override
protected Void doInBackground(Void... voids) {
if (checkDuplicate) {
if (redditDataRoomDatabase.customThemeDao().getCustomTheme(customTheme.name) != null) {
isDuplicate = true;
return null;
}
}
CustomTheme previousTheme = redditDataRoomDatabase.customThemeDao().getCustomTheme(customTheme.name);
if (customTheme.isLightTheme) {
redditDataRoomDatabase.customThemeDao().unsetLightTheme();
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, lightThemeSharedPreferences);
} else if (previousTheme != null && previousTheme.isLightTheme) {
lightThemeSharedPreferences.edit().clear().apply();
}
if (customTheme.isDarkTheme) {
redditDataRoomDatabase.customThemeDao().unsetDarkTheme();
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, darkThemeSharedPreferences);
} else if (previousTheme != null && previousTheme.isDarkTheme) {
darkThemeSharedPreferences.edit().clear().apply();
}
if (customTheme.isAmoledTheme) {
redditDataRoomDatabase.customThemeDao().unsetAmoledTheme();
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, amoledThemeSharedPreferences);
} else if (previousTheme != null && previousTheme.isAmoledTheme) {
amoledThemeSharedPreferences.edit().clear().apply();
}
redditDataRoomDatabase.customThemeDao().insert(customTheme);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (isDuplicate) {
insertCustomThemeAsyncTaskListener.duplicate();
} else {
insertCustomThemeAsyncTaskListener.success();
}
}
}

View File

@ -1,6 +1,6 @@
package ml.docilealligator.infinityforreddit.award;
import android.os.AsyncTask;
import android.os.Handler;
import android.text.Html;
import androidx.annotation.NonNull;
@ -11,6 +11,7 @@ import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
@ -26,8 +27,9 @@ public class GiveAward {
void failed(int code, String message);
}
public static void giveAwardV2(Retrofit oauthRetrofit, String accessToken, String thingFullName, String awardId,
boolean isAnonymous, GiveAwardListener giveAwardListener) {
public static void giveAwardV2(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
String thingFullName, String awardId, boolean isAnonymous,
GiveAwardListener giveAwardListener) {
Map<String, String> params = new HashMap<>();
params.put(APIUtils.GILD_TYPE, awardId);
params.put(APIUtils.IS_ANONYMOUS, Boolean.toString(isAnonymous));
@ -36,7 +38,7 @@ public class GiveAward {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseResponseAsyncTask(response.body(), new ParseResponseAsyncTask.ParseResponseAsyncTaskListener() {
executor.execute(() -> parseResponse(handler, response.body(), new ParseResponseAsyncTaskListener() {
@Override
public void success(String awardsHTML, int awardCount) {
giveAwardListener.success(awardsHTML, awardCount);
@ -46,7 +48,7 @@ public class GiveAward {
public void failed(String errorMessage) {
giveAwardListener.failed(response.code(), response.body());
}
}).execute();
}));
} else {
giveAwardListener.failed(response.code(), response.body());
}
@ -59,76 +61,50 @@ public class GiveAward {
});
}
private static class ParseResponseAsyncTask extends AsyncTask<Void, Void, Void> {
private static void parseResponse(Handler handler, String response, ParseResponseAsyncTaskListener parseResponseAsyncTaskListener) {
try {
//Check for error
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
private String response;
private boolean error = false;
private String awardsHTML;
private int awardCount;
private String errorMessage = null;
private ParseResponseAsyncTaskListener parseResponseAsyncTaskListener;
public ParseResponseAsyncTask(String response, ParseResponseAsyncTaskListener parseResponseAsyncTaskListener) {
this.response = response;
this.parseResponseAsyncTaskListener = parseResponseAsyncTaskListener;
}
interface ParseResponseAsyncTaskListener {
void success(String awardsHTML, int awardCount);
void failed(String errorMessage);
}
@Override
protected Void doInBackground(Void... voids) {
try {
//Check for error
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
JSONArray errorArray = responseObject.getJSONArray(JSONUtils.ERRORS_KEY)
.getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1);
if (errorArray.length() != 0) {
String errorString;
if (errorArray.length() >= 2) {
errorString = errorArray.getString(1);
} else {
errorString = errorArray.getString(0);
}
errorMessage = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
error = true;
return null;
if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
JSONArray errorArray = responseObject.getJSONArray(JSONUtils.ERRORS_KEY)
.getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1);
if (errorArray.length() != 0) {
String errorString;
if (errorArray.length() >= 2) {
errorString = errorArray.getString(1);
} else {
errorString = errorArray.getString(0);
}
String errorMessage = errorString.substring(0, 1).toUpperCase() + errorString.substring(1);
handler.post(() -> parseResponseAsyncTaskListener.failed(errorMessage));
}
} catch (JSONException ignore) {}
try {
JSONArray awardingsArray = new JSONObject(response).getJSONArray(JSONUtils.ALL_AWARDINGS_KEY);
StringBuilder awardingsBuilder = new StringBuilder();
awardCount = 0;
for (int i = 0; i < awardingsArray.length(); i++) {
JSONObject award = awardingsArray.getJSONObject(i);
int count = award.getInt(JSONUtils.COUNT_KEY);
awardCount += count;
String iconUrl = award.getString(JSONUtils.ICON_URL_KEY);
awardingsBuilder.append("<img src=\"").append(Html.escapeHtml(iconUrl)).append("\"> ").append("x").append(count).append(" ");
}
awardsHTML = awardingsBuilder.toString();
} catch (JSONException e) {
e.printStackTrace();
error = true;
}
return null;
}
} catch (JSONException ignore) {}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (error) {
parseResponseAsyncTaskListener.failed(errorMessage);
} else {
parseResponseAsyncTaskListener.success(awardsHTML, awardCount);
try {
JSONArray awardingsArray = new JSONObject(response).getJSONArray(JSONUtils.ALL_AWARDINGS_KEY);
StringBuilder awardingsBuilder = new StringBuilder();
int awardCount = 0;
for (int i = 0; i < awardingsArray.length(); i++) {
JSONObject award = awardingsArray.getJSONObject(i);
int count = award.getInt(JSONUtils.COUNT_KEY);
awardCount += count;
String iconUrl = award.getString(JSONUtils.ICON_URL_KEY);
awardingsBuilder.append("<img src=\"").append(Html.escapeHtml(iconUrl)).append("\"> ").append("x").append(count).append(" ");
}
String awardsHTML = awardingsBuilder.toString();
int finalAwardCount = awardCount;
handler.post(() -> parseResponseAsyncTaskListener.success(awardsHTML, finalAwardCount));
} catch (JSONException e) {
e.printStackTrace();
handler.post(() -> parseResponseAsyncTaskListener.failed(null));
}
}
interface ParseResponseAsyncTaskListener {
void success(String awardsHTML, int awardCount);
void failed(String errorMessage);
}
}

View File

@ -108,7 +108,6 @@ public class CommentMoreBottomSheetFragment extends RoundedBottomSheetDialogFrag
editTextView.setOnClickListener(view -> {
Intent intent = new Intent(activity, EditCommentActivity.class);
intent.putExtra(EditCommentActivity.EXTRA_ACCESS_TOKEN, accessToken);
intent.putExtra(EditCommentActivity.EXTRA_FULLNAME, comment.getFullName());
intent.putExtra(EditCommentActivity.EXTRA_CONTENT, comment.getCommentMarkdown());
intent.putExtra(EditCommentActivity.EXTRA_POSITION, bundle.getInt(EXTRA_POSITION));