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,
STORAGE
}
private WeakReference<MediaViewer> ui;
private ImageAdapter imageAdapter;
private Bitmap[] images;

View File

@ -22,35 +22,36 @@ import twitter4j.TwitterException;
public class MessageUpload extends AsyncTask<String, Void, Boolean> {
private WeakReference<MessagePopup> ui;
private WeakReference<Dialog> popup;
private TwitterEngine mTwitter;
private TwitterException err;
private LayoutInflater inflater;
private Dialog popup;
public MessageUpload(@NonNull MessagePopup c) {
ui = new WeakReference<>(c);
popup = new Dialog(c);
inflater = LayoutInflater.from(c);
popup = new WeakReference<>(new Dialog(c));
mTwitter = TwitterEngine.getInstance(c);
}
@Override
protected void onPreExecute() {
popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
popup.setCanceledOnTouchOutside(false);
if (popup.getWindow() != null)
popup.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
if (popup.get() == null || ui.get() == null) return;
final Dialog window = popup.get();
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 cancelButton = load.findViewById(R.id.kill_button);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
window.dismiss();
}
});
popup.setOnDismissListener(new DialogInterface.OnDismissListener() {
window.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (getStatus() == Status.RUNNING) {
@ -59,8 +60,8 @@ public class MessageUpload extends AsyncTask<String, Void, Boolean> {
}
}
});
popup.setContentView(load);
popup.show();
window.setContentView(load);
window.show();
}
@ -87,9 +88,8 @@ public class MessageUpload extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPostExecute(Boolean success) {
if (ui.get() == null) return;
if (ui.get() == null || popup.get() == null) return;
popup.dismiss();
if (success) {
Toast.makeText(ui.get(), R.string.dmsend, Toast.LENGTH_SHORT).show();
ui.get().finish();
@ -97,11 +97,13 @@ public class MessageUpload extends AsyncTask<String, Void, Boolean> {
if (err != null)
ErrorHandler.printError(ui.get(), err);
}
popup.get().dismiss();
}
@Override
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.os.AsyncTask;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.Window;
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.ProfileEdit;
import java.io.File;
import java.lang.ref.WeakReference;
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;
public class ProfileEditor extends AsyncTask<Void, Void, Void> {
public class ProfileEditor extends AsyncTask<Void, Void, Boolean> {
public enum Mode {
READ_DATA,
WRITE_DATA
}
private final Mode mode;
private boolean failure;
private WeakReference<ProfileEdit> ui;
private WeakReference<Dialog> popup;
private TwitterEngine mTwitter;
private TwitterException err;
private TwitterUser user;
private Editable edit_name, edit_link, edit_bio, edit_loc;
private Dialog popup;
private String image_path;
public ProfileEditor(@NonNull ProfileEdit c, Mode mode) {
ui = new WeakReference<>(c);
popup = new WeakReference<>(new Dialog(c));
mTwitter = TwitterEngine.getInstance(c);
popup = new Dialog(c);
this.mode = mode;
EditText name = ui.get().findViewById(R.id.edit_name);
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_bio = bio.getText();
image_path = text_path.getText().toString();
popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
popup.setCanceledOnTouchOutside(false);
popup.setContentView(new ProgressBar(c));
this.mode = mode;
}
@Override
protected void onPreExecute() {
if (popup.getWindow() != null)
popup.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
if (popup.get() == null || ui.get() == null) return;
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
public void onDismiss(DialogInterface dialog) {
if (getStatus() == Status.RUNNING) {
@ -92,12 +89,12 @@ public class ProfileEditor extends AsyncTask<Void, Void, Void> {
}
}
});
popup.show();
window.show();
}
@Override
protected Void doInBackground(Void... v) {
protected Boolean doInBackground(Void... v) {
try {
switch (mode) {
case READ_DATA:
@ -114,31 +111,25 @@ public class ProfileEditor extends AsyncTask<Void, Void, Void> {
db.storeUser(user);
if (!image_path.trim().isEmpty())
mTwitter.updateProfileImage(new File(image_path));
mTwitter.updateProfileImage(image_path);
break;
}
} catch (TwitterException err) {
this.err = err;
failure = true;
return false;
} catch (Exception err) {
if (err.getMessage() != null)
Log.e("E: ProfileEditor", err.getMessage());
failure = true;
err.printStackTrace();
return false;
}
return null;
return true;
}
@Override
protected void onPostExecute(Void v) {
if (ui.get() == null) return;
protected void onPostExecute(Boolean success) {
if (ui.get() == null || popup.get() == null) return;
popup.dismiss();
if (failure) {
ErrorHandler.printError(ui.get(), err);
ui.get().finish();
} else {
if (success) {
switch (mode) {
case READ_DATA:
edit_name.append(user.getUsername());
@ -167,12 +158,17 @@ public class ProfileEditor extends AsyncTask<Void, Void, Void> {
ui.get().finish();
break;
}
} else {
ErrorHandler.printError(ui.get(), err);
ui.get().finish();
}
popup.get().dismiss();
}
@Override
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.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
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;
public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
public class StatusLoader extends AsyncTask<Long, Tweet, Boolean> {
public enum Mode {
LOAD,
@ -53,9 +52,7 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
FAVORITE,
DELETE
}
private final Mode mode;
private boolean failure = false;
private TwitterEngine mTwitter;
private TwitterException err;
@ -82,7 +79,7 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
@Override
protected Void doInBackground(Long... data) {
protected Boolean doInBackground(Long... data) {
Tweet tweet;
final long TWEETID = data[0];
boolean updateStatus = false;
@ -130,13 +127,12 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
int rCode = err.getErrorCode();
if (rCode == 144 || rCode == 34 || rCode == 63)
db.removeStatus(TWEETID);
failure = true;
return false;
} catch (Exception err) {
if (err.getMessage() != null)
Log.e("StatusLoader", err.getMessage());
failure = true;
err.printStackTrace();
return false;
}
return null;
return true;
}
@ -286,10 +282,10 @@ public class StatusLoader extends AsyncTask<Long, Tweet, Void> {
@Override
protected void onPostExecute(Void v) {
protected void onPostExecute(Boolean success) {
if (ui.get() == null) return;
if (!failure) {
if (success) {
switch (mode) {
case FAVORITE:
ui.get().setResult(RETURN_TWEET_CHANGED);

View File

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