Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] replacing leaking asyncTask #676

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.FragmentActivity;

import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -41,10 +40,11 @@
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

public class UserStatsFragment extends AuthenticatedFragment {
public static final int MSG_USER_DONE = 101;
private static final int MENU_SITE_STATS = 201;
private static final int MENU_RANK_STATS = 202;

Expand Down Expand Up @@ -223,7 +223,9 @@ public void handleUserStats(final View view, final Resources resources, final St
}
}
if (null != stats.getImageBadgeUrl() && !stats.getImageBadgeUrl().isEmpty()) {
new DownloadBadgeImageTask(view.findViewById(R.id.badgeImage)).execute(UrlConfig.WIGLE_BASE_URL+stats.getImageBadgeUrl());
ExecutorService executor = Executors.newSingleThreadExecutor();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this really is single use - this activity doesn't even have a "swipe to refresh" command.

Handler handler = new Handler(Looper.getMainLooper());
executor.submit(new BadgeImageRunnable(handler, view.findViewById(R.id.badgeImage)));
}
}

Expand Down Expand Up @@ -294,14 +296,14 @@ public void onConfigurationChanged(@NonNull final Configuration newConfig ) {
public void onCreateOptionsMenu (final Menu menu, @NonNull final MenuInflater inflater) {
MenuItem item = menu.add(0, MENU_SITE_STATS, 0, getString(R.string.site_stats_app_name));
item.setIcon( R.drawable.ic_planet_small_white );
MenuItemCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

item = menu.add(0, MENU_SITE_STATS, 0, getString(R.string.site_stats_app_name));
item.setIcon(R.drawable.ic_planet_small_white);

item = menu.add(0, MENU_RANK_STATS, 0, getString(R.string.rank_stats_app_name));
item.setIcon(android.R.drawable.ic_menu_sort_by_size);
MenuItemCompat.setShowAsAction(item, MenuItem.SHOW_AS_ACTION_IF_ROOM);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

item = menu.add(0, MENU_RANK_STATS, 0, getString(R.string.rank_stats_app_name));
item.setIcon(android.R.drawable.ic_menu_sort_by_size);
Expand All @@ -311,7 +313,7 @@ public void onCreateOptionsMenu (final Menu menu, @NonNull final MenuInflater in

/* Handles item selections */
@Override
public boolean onOptionsItemSelected( final MenuItem item ) {
public boolean onOptionsItemSelected(@NonNull final MenuItem item ) {
final MainActivity main = MainActivity.getMainActivity();
final Activity currentActivity = getActivity();
if (null != currentActivity) {
Expand All @@ -328,26 +330,26 @@ public boolean onOptionsItemSelected( final MenuItem item ) {
return false;
}

private static class DownloadBadgeImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView badgeImage;
private class BadgeImageRunnable implements Runnable {
final Handler handler;
final ImageView badgeImageView;

public DownloadBadgeImageTask(ImageView image) {
this.badgeImage = image;
public BadgeImageRunnable(final Handler handler, final ImageView badgeImageView) {
this.handler = handler;
this.badgeImageView = badgeImageView;
}
protected Bitmap doInBackground(String... urls) {
String badgeUrl = urls[0];
Bitmap badge = null;

@Override
public void run() {
try {
InputStream in = new java.net.URL(badgeUrl).openStream();
badge = BitmapFactory.decodeStream(in);
InputStream in = new java.net.URL(UrlConfig.WIGLE_BASE_URL+stats.getImageBadgeUrl()).openStream();
final Bitmap badge = BitmapFactory.decodeStream(in);
handler.post(() -> {
badgeImageView.setImageBitmap(badge);
});
} catch (Exception e) {
Logging.error("Failed to download bage image ", e);
}
return badge;
}

protected void onPostExecute(Bitmap result) {
badgeImage.setImageBitmap(result);
}
}
}