memory leak fix

This commit is contained in:
NudeDude 2019-06-09 14:27:28 +02:00
parent 13bd2f8c0d
commit de950130f5
6 changed files with 77 additions and 86 deletions

View File

@ -26,7 +26,6 @@ public class ImageLoader extends AsyncTask<String, Void, Boolean> {
ONLINE, ONLINE,
STORAGE STORAGE
} }
private WeakReference<MediaViewer> ui; private WeakReference<MediaViewer> ui;
private ImageAdapter imageAdapter; private ImageAdapter imageAdapter;
private Bitmap[] images; private Bitmap[] images;

View File

@ -22,35 +22,36 @@ import twitter4j.TwitterException;
public class MessageUpload extends AsyncTask<String, Void, Boolean> { public class MessageUpload extends AsyncTask<String, Void, Boolean> {
private WeakReference<MessagePopup> ui; private WeakReference<MessagePopup> ui;
private WeakReference<Dialog> popup;
private TwitterEngine mTwitter; private TwitterEngine mTwitter;
private TwitterException err; private TwitterException err;
private LayoutInflater inflater;
private Dialog popup;
public MessageUpload(@NonNull MessagePopup c) { public MessageUpload(@NonNull MessagePopup c) {
ui = new WeakReference<>(c); ui = new WeakReference<>(c);
popup = new Dialog(c); popup = new WeakReference<>(new Dialog(c));
inflater = LayoutInflater.from(c);
mTwitter = TwitterEngine.getInstance(c); mTwitter = TwitterEngine.getInstance(c);
} }
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
popup.requestWindowFeature(Window.FEATURE_NO_TITLE); if (popup.get() == null || ui.get() == null) return;
popup.setCanceledOnTouchOutside(false);
if (popup.getWindow() != null) final Dialog window = popup.get();
popup.getWindow().setBackgroundDrawableResource(android.R.color.transparent); window.requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setCanceledOnTouchOutside(false);
if (window.getWindow() != null)
window.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
LayoutInflater inflater = LayoutInflater.from(ui.get());
View load = inflater.inflate(R.layout.item_load, null, false); View load = inflater.inflate(R.layout.item_load, null, false);
View cancelButton = load.findViewById(R.id.kill_button); View cancelButton = load.findViewById(R.id.kill_button);
cancelButton.setOnClickListener(new View.OnClickListener() { cancelButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
popup.dismiss(); window.dismiss();
} }
}); });
popup.setOnDismissListener(new DialogInterface.OnDismissListener() { window.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override @Override
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
if (getStatus() == Status.RUNNING) { if (getStatus() == Status.RUNNING) {
@ -59,8 +60,8 @@ public class MessageUpload extends AsyncTask<String, Void, Boolean> {
} }
} }
}); });
popup.setContentView(load); window.setContentView(load);
popup.show(); window.show();
} }
@ -87,9 +88,8 @@ public class MessageUpload extends AsyncTask<String, Void, Boolean> {
@Override @Override
protected void onPostExecute(Boolean success) { protected void onPostExecute(Boolean success) {
if (ui.get() == null) return; if (ui.get() == null || popup.get() == null) return;
popup.dismiss();
if (success) { if (success) {
Toast.makeText(ui.get(), R.string.dmsend, Toast.LENGTH_SHORT).show(); Toast.makeText(ui.get(), R.string.dmsend, Toast.LENGTH_SHORT).show();
ui.get().finish(); ui.get().finish();
@ -97,11 +97,13 @@ public class MessageUpload extends AsyncTask<String, Void, Boolean> {
if (err != null) if (err != null)
ErrorHandler.printError(ui.get(), err); ErrorHandler.printError(ui.get(), err);
} }
popup.get().dismiss();
} }
@Override @Override
protected void onCancelled() { protected void onCancelled() {
popup.dismiss(); if (popup.get() == null) return;
popup.get().dismiss();
} }
} }

View File

@ -5,7 +5,6 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.text.Editable; import android.text.Editable;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.widget.Button; import android.widget.Button;
@ -25,7 +24,6 @@ import org.nuclearfog.twidda.database.DatabaseAdapter;
import org.nuclearfog.twidda.window.MediaViewer; import org.nuclearfog.twidda.window.MediaViewer;
import org.nuclearfog.twidda.window.ProfileEdit; import org.nuclearfog.twidda.window.ProfileEdit;
import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import twitter4j.TwitterException; import twitter4j.TwitterException;
@ -35,30 +33,26 @@ import static org.nuclearfog.twidda.window.MediaViewer.KEY_MEDIA_TYPE;
import static org.nuclearfog.twidda.window.MediaViewer.MediaType.IMAGE; import static org.nuclearfog.twidda.window.MediaViewer.MediaType.IMAGE;
public class ProfileEditor extends AsyncTask<Void, Void, Void> { public class ProfileEditor extends AsyncTask<Void, Void, Boolean> {
public enum Mode { public enum Mode {
READ_DATA, READ_DATA,
WRITE_DATA WRITE_DATA
} }
private final Mode mode; private final Mode mode;
private boolean failure;
private WeakReference<ProfileEdit> ui; private WeakReference<ProfileEdit> ui;
private WeakReference<Dialog> popup;
private TwitterEngine mTwitter; private TwitterEngine mTwitter;
private TwitterException err; private TwitterException err;
private TwitterUser user; private TwitterUser user;
private Editable edit_name, edit_link, edit_bio, edit_loc; private Editable edit_name, edit_link, edit_bio, edit_loc;
private Dialog popup;
private String image_path; private String image_path;
public ProfileEditor(@NonNull ProfileEdit c, Mode mode) { public ProfileEditor(@NonNull ProfileEdit c, Mode mode) {
ui = new WeakReference<>(c); ui = new WeakReference<>(c);
popup = new WeakReference<>(new Dialog(c));
mTwitter = TwitterEngine.getInstance(c); mTwitter = TwitterEngine.getInstance(c);
popup = new Dialog(c);
this.mode = mode;
EditText name = ui.get().findViewById(R.id.edit_name); EditText name = ui.get().findViewById(R.id.edit_name);
EditText link = ui.get().findViewById(R.id.edit_link); EditText link = ui.get().findViewById(R.id.edit_link);
@ -71,19 +65,22 @@ public class ProfileEditor extends AsyncTask<Void, Void, Void> {
edit_loc = loc.getText(); edit_loc = loc.getText();
edit_bio = bio.getText(); edit_bio = bio.getText();
image_path = text_path.getText().toString(); image_path = text_path.getText().toString();
this.mode = mode;
popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
popup.setCanceledOnTouchOutside(false);
popup.setContentView(new ProgressBar(c));
} }
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
if (popup.getWindow() != null) if (popup.get() == null || ui.get() == null) return;
popup.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
popup.setOnDismissListener(new DialogInterface.OnDismissListener() { Dialog window = popup.get();
window.requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setCanceledOnTouchOutside(false);
window.setContentView(new ProgressBar(ui.get()));
if (window.getWindow() != null)
window.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
window.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override @Override
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
if (getStatus() == Status.RUNNING) { if (getStatus() == Status.RUNNING) {
@ -92,12 +89,12 @@ public class ProfileEditor extends AsyncTask<Void, Void, Void> {
} }
} }
}); });
popup.show(); window.show();
} }
@Override @Override
protected Void doInBackground(Void... v) { protected Boolean doInBackground(Void... v) {
try { try {
switch (mode) { switch (mode) {
case READ_DATA: case READ_DATA:
@ -114,31 +111,25 @@ public class ProfileEditor extends AsyncTask<Void, Void, Void> {
db.storeUser(user); db.storeUser(user);
if (!image_path.trim().isEmpty()) if (!image_path.trim().isEmpty())
mTwitter.updateProfileImage(new File(image_path)); mTwitter.updateProfileImage(image_path);
break; break;
} }
} catch (TwitterException err) { } catch (TwitterException err) {
this.err = err; this.err = err;
failure = true; return false;
} catch (Exception err) { } catch (Exception err) {
if (err.getMessage() != null) err.printStackTrace();
Log.e("E: ProfileEditor", err.getMessage()); return false;
failure = true;
} }
return null; return true;
} }
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Boolean success) {
if (ui.get() == null) return; if (ui.get() == null || popup.get() == null) return;
popup.dismiss(); if (success) {
if (failure) {
ErrorHandler.printError(ui.get(), err);
ui.get().finish();
} else {
switch (mode) { switch (mode) {
case READ_DATA: case READ_DATA:
edit_name.append(user.getUsername()); edit_name.append(user.getUsername());
@ -167,12 +158,17 @@ public class ProfileEditor extends AsyncTask<Void, Void, Void> {
ui.get().finish(); ui.get().finish();
break; break;
} }
} else {
ErrorHandler.printError(ui.get(), err);
ui.get().finish();
} }
popup.get().dismiss();
} }
@Override @Override
protected void onCancelled() { protected void onCancelled() {
popup.dismiss(); if (popup.get() == null) return;
popup.get().dismiss();
} }
} }

View File

@ -4,7 +4,6 @@ import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.text.Spannable; import android.text.Spannable;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
@ -45,7 +44,7 @@ import static org.nuclearfog.twidda.window.TweetDetail.KEY_TWEET_ID;
import static org.nuclearfog.twidda.window.TweetDetail.KEY_TWEET_NAME; import static org.nuclearfog.twidda.window.TweetDetail.KEY_TWEET_NAME;
public class StatusLoader extends AsyncTask<Long, Tweet, Void> { public class StatusLoader extends AsyncTask<Long, Tweet, Boolean> {
public enum Mode { public enum Mode {
LOAD, LOAD,
@ -53,9 +52,7 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
FAVORITE, FAVORITE,
DELETE DELETE
} }
private final Mode mode; private final Mode mode;
private boolean failure = false;
private TwitterEngine mTwitter; private TwitterEngine mTwitter;
private TwitterException err; private TwitterException err;
@ -82,7 +79,7 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
@Override @Override
protected Void doInBackground(Long... data) { protected Boolean doInBackground(Long... data) {
Tweet tweet; Tweet tweet;
final long TWEETID = data[0]; final long TWEETID = data[0];
boolean updateStatus = false; boolean updateStatus = false;
@ -130,13 +127,12 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
int rCode = err.getErrorCode(); int rCode = err.getErrorCode();
if (rCode == 144 || rCode == 34 || rCode == 63) if (rCode == 144 || rCode == 34 || rCode == 63)
db.removeStatus(TWEETID); db.removeStatus(TWEETID);
failure = true; return false;
} catch (Exception err) { } catch (Exception err) {
if (err.getMessage() != null) err.printStackTrace();
Log.e("StatusLoader", err.getMessage()); return false;
failure = true;
} }
return null; return true;
} }
@ -286,10 +282,10 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Boolean success) {
if (ui.get() == null) return; if (ui.get() == null) return;
if (!failure) { if (success) {
switch (mode) { switch (mode) {
case FAVORITE: case FAVORITE:
ui.get().setResult(RETURN_TWEET_CHANGED); ui.get().setResult(RETURN_TWEET_CHANGED);

View File

@ -4,7 +4,6 @@ import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
@ -25,39 +24,40 @@ import twitter4j.TwitterException;
public class StatusUploader extends AsyncTask<Void, Void, Boolean> { public class StatusUploader extends AsyncTask<Void, Void, Boolean> {
private WeakReference<TweetPopup> ui; private WeakReference<TweetPopup> ui;
private WeakReference<Dialog> popup;
private TwitterEngine mTwitter; private TwitterEngine mTwitter;
private TwitterException err; private TwitterException err;
private LayoutInflater inflater;
private Dialog popup;
private TweetHolder tweet; private TweetHolder tweet;
public StatusUploader(@NonNull TweetPopup context, TweetHolder tweet) { public StatusUploader(@NonNull TweetPopup context, TweetHolder tweet) {
ui = new WeakReference<>(context); ui = new WeakReference<>(context);
popup = new WeakReference<>(new Dialog(context));
mTwitter = TwitterEngine.getInstance(context); mTwitter = TwitterEngine.getInstance(context);
inflater = LayoutInflater.from(context);
popup = new Dialog(context);
this.tweet = tweet; this.tweet = tweet;
} }
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
popup.requestWindowFeature(Window.FEATURE_NO_TITLE); if (popup.get() == null || ui.get() == null) return;
popup.setCanceledOnTouchOutside(false);
if (popup.getWindow() != null) final Dialog window = popup.get();
popup.getWindow().setBackgroundDrawableResource(android.R.color.transparent); window.requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setCanceledOnTouchOutside(false);
if (window.getWindow() != null)
window.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
LayoutInflater inflater = LayoutInflater.from(ui.get());
View load = inflater.inflate(R.layout.item_load, null, false); View load = inflater.inflate(R.layout.item_load, null, false);
View cancelButton = load.findViewById(R.id.kill_button); View cancelButton = load.findViewById(R.id.kill_button);
popup.setContentView(load); window.setContentView(load);
cancelButton.setOnClickListener(new View.OnClickListener() { cancelButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
popup.dismiss(); window.dismiss();
} }
}); });
popup.setOnDismissListener(new DialogInterface.OnDismissListener() { window.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override @Override
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
if (getStatus() == Status.RUNNING) { if (getStatus() == Status.RUNNING) {
@ -66,7 +66,7 @@ public class StatusUploader extends AsyncTask<Void, Void, Boolean> {
} }
} }
}); });
popup.show(); window.show();
} }
@ -78,8 +78,7 @@ public class StatusUploader extends AsyncTask<Void, Void, Boolean> {
this.err = err; this.err = err;
return false; return false;
} catch (Exception err) { } catch (Exception err) {
if (err.getMessage() != null) err.printStackTrace();
Log.e("Status Upload", err.getMessage());
return false; return false;
} }
return true; return true;
@ -88,12 +87,10 @@ public class StatusUploader extends AsyncTask<Void, Void, Boolean> {
@Override @Override
protected void onPostExecute(Boolean success) { protected void onPostExecute(Boolean success) {
if (ui.get() == null) return; if (ui.get() == null || popup.get() == null) return;
popup.dismiss();
if (success) { if (success) {
ui.get().close(); ui.get().close();
} else { } else {
if (err != null) if (err != null)
ErrorHandler.printError(ui.get(), err); ErrorHandler.printError(ui.get(), err);
@ -108,13 +105,13 @@ public class StatusUploader extends AsyncTask<Void, Void, Boolean> {
}) })
.setNegativeButton(R.string.cancel, null).show(); .setNegativeButton(R.string.cancel, null).show();
} }
popup.get().dismiss();
} }
@Override @Override
protected void onCancelled() { protected void onCancelled() {
popup.dismiss(); if (popup.get() == null) return;
popup.get().dismiss();
} }
} }

View File

@ -591,10 +591,11 @@ public class TwitterEngine {
/** /**
* Update user profile image_add * Update user profile image_add
* *
* @param image image_add file * @param path image path
* @throws TwitterException if Access is unavailable * @throws TwitterException if Access is unavailable
*/ */
public void updateProfileImage(File image) throws TwitterException { public void updateProfileImage(String path) throws TwitterException {
File image = new File(path);
twitter.updateProfileImage(image); twitter.updateProfileImage(image);
} }