mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-22 23:08:07 +01:00
FIX: Android widget had leaks due to wrongly used constructor
This commit is contained in:
parent
5041ac4841
commit
1e7c4f82a8
1 changed files with 91 additions and 72 deletions
|
@ -8,16 +8,18 @@ import android.content.ComponentName;
|
|||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class BitcoinPriceWidget extends AppWidgetProvider {
|
||||
|
||||
|
@ -25,6 +27,7 @@ public class BitcoinPriceWidget extends AppWidgetProvider {
|
|||
private static final String ACTION_UPDATE = "io.bluewallet.bluewallet.UPDATE";
|
||||
private static final String PREFS_NAME = "BitcoinPriceWidgetPrefs";
|
||||
private static final String PREF_PREFIX_KEY = "appwidget_";
|
||||
private static final ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
@Override
|
||||
public void onEnabled(Context context) {
|
||||
|
@ -36,25 +39,26 @@ public class BitcoinPriceWidget extends AppWidgetProvider {
|
|||
public void onDisabled(Context context) {
|
||||
super.onDisabled(context);
|
||||
cancelUpdate(context);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
for (int appWidgetId : appWidgetIds) {
|
||||
Log.d(TAG, "Updating widget ID: " + appWidgetId);
|
||||
|
||||
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
||||
|
||||
|
||||
// Set up the pending intent to open the app when the widget is clicked
|
||||
Intent launchAppIntent = new Intent(context, MainActivity.class);
|
||||
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
|
||||
views.setOnClickPendingIntent(R.id.widget_layout, launchAppPendingIntent);
|
||||
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
|
||||
fetchBitcoinPrice(context, appWidgetManager, appWidgetId);
|
||||
|
||||
executorService.execute(new FetchBitcoinPriceTask(context, appWidgetManager, appWidgetId));
|
||||
}
|
||||
|
||||
|
||||
scheduleNextUpdate(context);
|
||||
}
|
||||
|
||||
|
@ -93,78 +97,93 @@ public class BitcoinPriceWidget extends AppWidgetProvider {
|
|||
alarmManager.cancel(pendingIntent);
|
||||
}
|
||||
|
||||
private void fetchBitcoinPrice(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
|
||||
new AsyncTask<Void, Void, String>() {
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
Log.d(TAG, "Starting to fetch Bitcoin price...");
|
||||
private static class FetchBitcoinPriceTask implements Runnable {
|
||||
private final WeakReference<Context> contextRef;
|
||||
private final AppWidgetManager appWidgetManager;
|
||||
private final int appWidgetId;
|
||||
|
||||
FetchBitcoinPriceTask(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
|
||||
this.contextRef = new WeakReference<>(context);
|
||||
this.appWidgetManager = appWidgetManager;
|
||||
this.appWidgetId = appWidgetId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Context context = contextRef.get();
|
||||
if (context == null) return;
|
||||
|
||||
Log.d(TAG, "Starting to fetch Bitcoin price...");
|
||||
|
||||
String price = MarketAPI.fetchPrice("USD"); // Using hardcoded "USD" for now
|
||||
|
||||
if (price != null) {
|
||||
updateWidgetWithPrice(context, price);
|
||||
} else {
|
||||
handleError(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... voids) {
|
||||
return MarketAPI.fetchPrice("USD"); // Using hardcoded "USD" for now
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String price) {
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
|
||||
String prevPrice = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
|
||||
if (price != null) {
|
||||
Log.d(TAG, "Fetch completed with price: " + price);
|
||||
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
|
||||
views.setTextViewText(R.id.price_value, currencyFormat.format(Double.parseDouble(price)));
|
||||
|
||||
if (prevPrice != null) {
|
||||
double previousPrice = Double.parseDouble(prevPrice);
|
||||
double currentPrice = Double.parseDouble(price);
|
||||
if (currentPrice > previousPrice) {
|
||||
views.setImageViewResource(R.id.price_arrow, android.R.drawable.arrow_up_float);
|
||||
} else if (currentPrice < previousPrice) {
|
||||
views.setImageViewResource(R.id.price_arrow, android.R.drawable.arrow_down_float);
|
||||
} else {
|
||||
views.setImageViewResource(R.id.price_arrow, 0);
|
||||
}
|
||||
|
||||
if (currentPrice != previousPrice) {
|
||||
views.setTextViewText(R.id.previous_price, "from " + currencyFormat.format(previousPrice));
|
||||
views.setViewVisibility(R.id.price_arrow, View.VISIBLE);
|
||||
views.setViewVisibility(R.id.previous_price, View.VISIBLE);
|
||||
} else {
|
||||
views.setTextViewText(R.id.previous_price, "");
|
||||
views.setViewVisibility(R.id.price_arrow, View.GONE);
|
||||
views.setViewVisibility(R.id.previous_price, View.GONE);
|
||||
}
|
||||
} else {
|
||||
views.setImageViewResource(R.id.price_arrow, 0);
|
||||
views.setTextViewText(R.id.previous_price, "");
|
||||
views.setViewVisibility(R.id.price_arrow, View.GONE);
|
||||
views.setViewVisibility(R.id.previous_price, View.GONE);
|
||||
}
|
||||
|
||||
editor.putString(PREF_PREFIX_KEY + appWidgetId, price);
|
||||
editor.apply();
|
||||
|
||||
String currentTime = new SimpleDateFormat("hh:mm a", Locale.getDefault()).format(new Date());
|
||||
views.setTextViewText(R.id.last_updated, "Last Updated");
|
||||
views.setTextViewText(R.id.last_updated_time, currentTime);
|
||||
}
|
||||
|
||||
private void updateWidgetWithPrice(Context context, String price) {
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
|
||||
String prevPrice = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
|
||||
Log.d(TAG, "Fetch completed with price: " + price);
|
||||
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);
|
||||
views.setTextViewText(R.id.price_value, currencyFormat.format(Double.parseDouble(price)));
|
||||
|
||||
if (prevPrice != null) {
|
||||
double previousPrice = Double.parseDouble(prevPrice);
|
||||
double currentPrice = Double.parseDouble(price);
|
||||
if (currentPrice > previousPrice) {
|
||||
views.setImageViewResource(R.id.price_arrow, android.R.drawable.arrow_up_float);
|
||||
} else if (currentPrice < previousPrice) {
|
||||
views.setImageViewResource(R.id.price_arrow, android.R.drawable.arrow_down_float);
|
||||
} else {
|
||||
String errorMessage = "Network Error";
|
||||
Log.e(TAG, errorMessage);
|
||||
views.setTextViewText(R.id.price_value, errorMessage);
|
||||
views.setTextViewText(R.id.last_updated, "");
|
||||
views.setTextViewText(R.id.last_updated_time, "");
|
||||
views.setImageViewResource(R.id.price_arrow, 0);
|
||||
}
|
||||
|
||||
if (currentPrice != previousPrice) {
|
||||
views.setTextViewText(R.id.previous_price, "from " + currencyFormat.format(previousPrice));
|
||||
views.setViewVisibility(R.id.price_arrow, View.VISIBLE);
|
||||
views.setViewVisibility(R.id.previous_price, View.VISIBLE);
|
||||
} else {
|
||||
views.setTextViewText(R.id.previous_price, "");
|
||||
views.setViewVisibility(R.id.price_arrow, View.GONE);
|
||||
views.setViewVisibility(R.id.previous_price, View.GONE);
|
||||
Toast.makeText(context, "Failed to fetch Bitcoin price", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
} else {
|
||||
views.setImageViewResource(R.id.price_arrow, 0);
|
||||
views.setTextViewText(R.id.previous_price, "");
|
||||
views.setViewVisibility(R.id.price_arrow, View.GONE);
|
||||
views.setViewVisibility(R.id.previous_price, View.GONE);
|
||||
}
|
||||
}.execute();
|
||||
|
||||
editor.putString(PREF_PREFIX_KEY + appWidgetId, price);
|
||||
editor.apply();
|
||||
|
||||
String currentTime = new SimpleDateFormat("hh:mm a", Locale.getDefault()).format(new Date());
|
||||
views.setTextViewText(R.id.last_updated, "Last Updated");
|
||||
views.setTextViewText(R.id.last_updated_time, currentTime);
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
}
|
||||
|
||||
private void handleError(Context context) {
|
||||
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
||||
String errorMessage = "Network Error";
|
||||
Log.e(TAG, errorMessage);
|
||||
views.setTextViewText(R.id.price_value, errorMessage);
|
||||
views.setTextViewText(R.id.last_updated, "");
|
||||
views.setTextViewText(R.id.last_updated_time, "");
|
||||
views.setImageViewResource(R.id.price_arrow, 0);
|
||||
views.setTextViewText(R.id.previous_price, "");
|
||||
views.setViewVisibility(R.id.price_arrow, View.GONE);
|
||||
views.setViewVisibility(R.id.previous_price, View.GONE);
|
||||
Toast.makeText(context, "Failed to fetch Bitcoin price", Toast.LENGTH_SHORT).show();
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue