fix context leaks
This commit is contained in:
parent
0a596df497
commit
24c293e335
4 changed files with 71 additions and 19 deletions
|
@ -1,5 +1,6 @@
|
||||||
package org.schabi.newpipe.about;
|
package org.schabi.newpipe.about;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
@ -39,7 +40,7 @@ public class LicenseFragment extends Fragment {
|
||||||
* @param license the license to show
|
* @param license the license to show
|
||||||
*/
|
*/
|
||||||
public static void showLicense(Context context, License license) {
|
public static void showLicense(Context context, License license) {
|
||||||
new LicenseFragmentHelper().execute(context, license);
|
new LicenseFragmentHelper((Activity) context).execute(license);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package org.schabi.newpipe.about;
|
package org.schabi.newpipe.about;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
|
@ -10,26 +13,46 @@ import org.schabi.newpipe.util.ThemeHelper;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
public class LicenseFragmentHelper extends AsyncTask<Object, Void, Integer> {
|
public class LicenseFragmentHelper extends AsyncTask<Object, Void, Integer> {
|
||||||
|
|
||||||
private Context context;
|
WeakReference<Activity> weakReference;
|
||||||
private License license;
|
private License license;
|
||||||
|
|
||||||
|
public LicenseFragmentHelper(@Nullable Activity activity) {
|
||||||
|
weakReference = new WeakReference<>(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Activity getActivity() {
|
||||||
|
Activity activity = weakReference.get();
|
||||||
|
|
||||||
|
if (activity != null && activity.isFinishing()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return activity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Integer doInBackground(Object... objects) {
|
protected Integer doInBackground(Object... objects) {
|
||||||
context = (Context) objects[0];
|
license = (License) objects[0];
|
||||||
license = (License) objects[1];
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Integer result){
|
protected void onPostExecute(Integer result){
|
||||||
String webViewData = getFormattedLicense(context, license);
|
Activity activity = getActivity();
|
||||||
AlertDialog.Builder alert = new AlertDialog.Builder(context);
|
if (activity == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String webViewData = getFormattedLicense(activity, license);
|
||||||
|
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
|
||||||
alert.setTitle(license.getName());
|
alert.setTitle(license.getName());
|
||||||
|
|
||||||
WebView wv = new WebView(context);
|
WebView wv = new WebView(activity);
|
||||||
wv.loadData(webViewData, "text/html; charset=UTF-8", null);
|
wv.loadData(webViewData, "text/html; charset=UTF-8", null);
|
||||||
|
|
||||||
alert.setView(wv);
|
alert.setView(wv);
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package us.shandian.giga.ui.adapter;
|
package us.shandian.giga.ui.adapter;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.content.FileProvider;
|
import android.support.v4.content.FileProvider;
|
||||||
import android.support.v4.view.ViewCompat;
|
import android.support.v4.view.ViewCompat;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
@ -24,6 +27,7 @@ import android.widget.Toast;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -46,13 +50,13 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
|
||||||
ALGORITHMS.put(R.id.sha1, "SHA1");
|
ALGORITHMS.put(R.id.sha1, "SHA1");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Context mContext;
|
private Activity mContext;
|
||||||
private LayoutInflater mInflater;
|
private LayoutInflater mInflater;
|
||||||
private DownloadManager mManager;
|
private DownloadManager mManager;
|
||||||
private DownloadManagerService.DMBinder mBinder;
|
private DownloadManagerService.DMBinder mBinder;
|
||||||
private int mLayout;
|
private int mLayout;
|
||||||
|
|
||||||
public MissionAdapter(Context context, DownloadManagerService.DMBinder binder, DownloadManager manager, boolean isLinear) {
|
public MissionAdapter(Activity context, DownloadManagerService.DMBinder binder, DownloadManager manager, boolean isLinear) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mManager = manager;
|
mManager = manager;
|
||||||
mBinder = binder;
|
mBinder = binder;
|
||||||
|
@ -247,7 +251,7 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
|
||||||
case R.id.md5:
|
case R.id.md5:
|
||||||
case R.id.sha1:
|
case R.id.sha1:
|
||||||
DownloadMission mission = mManager.getMission(h.position);
|
DownloadMission mission = mManager.getMission(h.position);
|
||||||
new ChecksumTask().execute(mission.location + "/" + mission.name, ALGORITHMS.get(id));
|
new ChecksumTask(mContext).execute(mission.location + "/" + mission.name, ALGORITHMS.get(id));
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
@ -352,19 +356,27 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ChecksumTask extends AsyncTask<String, Void, String> {
|
private static class ChecksumTask extends AsyncTask<String, Void, String> {
|
||||||
ProgressDialog prog;
|
ProgressDialog prog;
|
||||||
|
WeakReference<Activity> weakReference;
|
||||||
|
|
||||||
|
ChecksumTask(@NonNull Activity activity) {
|
||||||
|
weakReference = new WeakReference<>(activity);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreExecute() {
|
protected void onPreExecute() {
|
||||||
super.onPreExecute();
|
super.onPreExecute();
|
||||||
|
|
||||||
|
Activity activity = getActivity();
|
||||||
|
if (activity != null) {
|
||||||
// Create dialog
|
// Create dialog
|
||||||
prog = new ProgressDialog(mContext);
|
prog = new ProgressDialog(activity);
|
||||||
prog.setCancelable(false);
|
prog.setCancelable(false);
|
||||||
prog.setMessage(mContext.getString(R.string.msg_wait));
|
prog.setMessage(activity.getString(R.string.msg_wait));
|
||||||
prog.show();
|
prog.show();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(String... params) {
|
protected String doInBackground(String... params) {
|
||||||
|
@ -374,8 +386,24 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(String result) {
|
protected void onPostExecute(String result) {
|
||||||
super.onPostExecute(result);
|
super.onPostExecute(result);
|
||||||
|
|
||||||
|
if (prog != null) {
|
||||||
|
Utility.copyToClipboard(prog.getContext(), result);
|
||||||
|
if (getActivity() != null) {
|
||||||
prog.dismiss();
|
prog.dismiss();
|
||||||
Utility.copyToClipboard(mContext, result);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Activity getActivity() {
|
||||||
|
Activity activity = weakReference.get();
|
||||||
|
|
||||||
|
if (activity != null && activity.isFinishing()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return activity;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,7 +129,7 @@ public abstract class MissionsFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateList() {
|
private void updateList() {
|
||||||
mAdapter = new MissionAdapter(mActivity, mBinder, mManager, mLinear);
|
mAdapter = new MissionAdapter((Activity) mActivity, mBinder, mManager, mLinear);
|
||||||
|
|
||||||
if (mLinear) {
|
if (mLinear) {
|
||||||
mList.setLayoutManager(mLinearManager);
|
mList.setLayoutManager(mLinearManager);
|
||||||
|
|
Loading…
Reference in a new issue