1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-02-08 23:20:10 +00:00

Use RxJava instead of AsyncTask in LicenseFragmentHelper.

This commit is contained in:
Isira Seneviratne 2020-10-12 12:42:59 +05:30
parent ef5084036c
commit 8ec55ef394
2 changed files with 49 additions and 62 deletions

View File

@ -1,6 +1,5 @@
package org.schabi.newpipe.about; package org.schabi.newpipe.about;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -21,15 +20,19 @@ import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import io.reactivex.disposables.CompositeDisposable;
/** /**
* Fragment containing the software licenses. * Fragment containing the software licenses.
*/ */
public class LicenseFragment extends Fragment { public class LicenseFragment extends Fragment {
private static final String ARG_COMPONENTS = "components"; private static final String ARG_COMPONENTS = "components";
private static final String LICENSE_KEY = "ACTIVE_LICENSE";
private SoftwareComponent[] softwareComponents; private SoftwareComponent[] softwareComponents;
private SoftwareComponent componentForContextMenu; private SoftwareComponent componentForContextMenu;
private License activeLicense; private License activeLicense;
private static final String LICENSE_KEY = "ACTIVE_LICENSE"; private final CompositeDisposable compositeDisposable = new CompositeDisposable();
public static LicenseFragment newInstance(final SoftwareComponent[] softwareComponents) { public static LicenseFragment newInstance(final SoftwareComponent[] softwareComponents) {
if (softwareComponents == null) { if (softwareComponents == null) {
@ -42,16 +45,6 @@ public class LicenseFragment extends Fragment {
return fragment; return fragment;
} }
/**
* Shows a popup containing the license.
*
* @param context the context to use
* @param license the license to show
*/
private static void showLicense(final Activity context, final License license) {
new LicenseFragmentHelper(context).execute(license);
}
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -68,6 +61,12 @@ public class LicenseFragment extends Fragment {
Arrays.sort(softwareComponents, Comparator.comparing(SoftwareComponent::getName)); Arrays.sort(softwareComponents, Comparator.comparing(SoftwareComponent::getName));
} }
@Override
public void onDestroy() {
compositeDisposable.dispose();
super.onDestroy();
}
@Nullable @Nullable
@Override @Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container,
@ -78,7 +77,8 @@ public class LicenseFragment extends Fragment {
final View licenseLink = rootView.findViewById(R.id.app_read_license); final View licenseLink = rootView.findViewById(R.id.app_read_license);
licenseLink.setOnClickListener(v -> { licenseLink.setOnClickListener(v -> {
activeLicense = StandardLicenses.GPL3; activeLicense = StandardLicenses.GPL3;
showLicense(getActivity(), StandardLicenses.GPL3); compositeDisposable.add(LicenseFragmentHelper.showLicense(getActivity(),
StandardLicenses.GPL3));
}); });
for (final SoftwareComponent component : softwareComponents) { for (final SoftwareComponent component : softwareComponents) {
@ -95,13 +95,15 @@ public class LicenseFragment extends Fragment {
componentView.setTag(component); componentView.setTag(component);
componentView.setOnClickListener(v -> { componentView.setOnClickListener(v -> {
activeLicense = component.getLicense(); activeLicense = component.getLicense();
showLicense(getActivity(), component.getLicense()); compositeDisposable.add(LicenseFragmentHelper.showLicense(getActivity(),
component.getLicense()));
}); });
softwareComponentsView.addView(componentView); softwareComponentsView.addView(componentView);
registerForContextMenu(componentView); registerForContextMenu(componentView);
} }
if (activeLicense != null) { if (activeLicense != null) {
showLicense(getActivity(), activeLicense); compositeDisposable.add(LicenseFragmentHelper.showLicense(getActivity(),
activeLicense));
} }
return rootView; return rootView;
} }
@ -129,7 +131,8 @@ public class LicenseFragment extends Fragment {
ShareUtils.openUrlInBrowser(getActivity(), component.getLink()); ShareUtils.openUrlInBrowser(getActivity(), component.getLink());
return true; return true;
case R.id.action_show_license: case R.id.action_show_license:
showLicense(getActivity(), component.getLicense()); compositeDisposable.add(LicenseFragmentHelper.showLicense(getActivity(),
component.getLicense()));
} }
return false; return false;
} }

View File

@ -1,8 +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.os.AsyncTask;
import android.util.Base64; import android.util.Base64;
import android.webkit.WebView; import android.webkit.WebView;
@ -16,18 +14,18 @@ import org.schabi.newpipe.util.ThemeHelper;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import io.reactivex.schedulers.Schedulers;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
public class LicenseFragmentHelper extends AsyncTask<Object, Void, Integer> { public final class LicenseFragmentHelper {
private final WeakReference<Activity> weakReference; private LicenseFragmentHelper() { }
private License license;
public LicenseFragmentHelper(@Nullable final Activity activity) {
weakReference = new WeakReference<>(activity);
}
/** /**
* @param context the context to use * @param context the context to use
@ -62,7 +60,7 @@ public class LicenseFragmentHelper extends AsyncTask<Object, Void, Integer> {
* @param context * @param context
* @return String which is a CSS stylesheet according to the context's theme * @return String which is a CSS stylesheet according to the context's theme
*/ */
private static String getLicenseStylesheet(final Context context) { private static String getLicenseStylesheet(@NonNull final Context context) {
final boolean isLightTheme = ThemeHelper.isLightThemeSelected(context); final boolean isLightTheme = ThemeHelper.isLightThemeSelected(context);
return "body{padding:12px 15px;margin:0;" return "body{padding:12px 15px;margin:0;"
+ "background:#" + getHexRGBColor(context, isLightTheme + "background:#" + getHexRGBColor(context, isLightTheme
@ -84,45 +82,31 @@ public class LicenseFragmentHelper extends AsyncTask<Object, Void, Integer> {
* @param color the color number from R.color * @param color the color number from R.color
* @return a six characters long String with hexadecimal RGB values * @return a six characters long String with hexadecimal RGB values
*/ */
private static String getHexRGBColor(final Context context, final int color) { private static String getHexRGBColor(@NonNull final Context context, final int color) {
return context.getResources().getString(color).substring(3); return context.getResources().getString(color).substring(3);
} }
@Nullable static Disposable showLicense(@Nullable final Context context, @NonNull final License license) {
private Activity getActivity() { if (context == null) {
final Activity activity = weakReference.get(); return Disposables.empty();
if (activity != null && activity.isFinishing()) {
return null;
} else {
return activity;
}
} }
@Override return Observable.fromCallable(() -> getFormattedLicense(context, license))
protected Integer doInBackground(final Object... objects) { .subscribeOn(Schedulers.io())
license = (License) objects[0]; .observeOn(AndroidSchedulers.mainThread())
return 1; .subscribe(formattedLicense -> {
} final String webViewData = Base64.encodeToString(formattedLicense
@Override
protected void onPostExecute(final Integer result) {
final Activity activity = getActivity();
if (activity == null) {
return;
}
final String webViewData = Base64.encodeToString(getFormattedLicense(activity, license)
.getBytes(StandardCharsets.UTF_8), Base64.NO_PADDING); .getBytes(StandardCharsets.UTF_8), Base64.NO_PADDING);
final WebView webView = new WebView(activity); final WebView webView = new WebView(context);
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64"); webView.loadData(webViewData, "text/html; charset=UTF-8", "base64");
final AlertDialog.Builder alert = new AlertDialog.Builder(activity); final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(license.getName()); alert.setTitle(license.getName());
alert.setView(webView); alert.setView(webView);
assureCorrectAppLanguage(activity); assureCorrectAppLanguage(context);
alert.setNegativeButton(activity.getString(R.string.finish), alert.setNegativeButton(context.getString(R.string.finish),
(dialog, which) -> dialog.dismiss()); (dialog, which) -> dialog.dismiss());
alert.show(); alert.show();
});
} }
} }