Skip to content

Commit

Permalink
Use RxJava instead of AsyncTask in LicenseFragmentHelper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Oct 12, 2020
1 parent b2fe1c9 commit 171a8c9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 61 deletions.
19 changes: 4 additions & 15 deletions app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.schabi.newpipe.about;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -41,16 +40,6 @@ public static LicenseFragment newInstance(final SoftwareComponent[] softwareComp
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
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -77,7 +66,7 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
final View licenseLink = rootView.findViewById(R.id.app_read_license);
licenseLink.setOnClickListener(v -> {
activeLicense = StandardLicenses.GPL3;
showLicense(getActivity(), StandardLicenses.GPL3);
LicenseFragmentHelper.showLicense(getActivity(), StandardLicenses.GPL3);
});

for (final SoftwareComponent component : softwareComponents) {
Expand All @@ -94,13 +83,13 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
componentView.setTag(component);
componentView.setOnClickListener(v -> {
activeLicense = component.getLicense();
showLicense(getActivity(), component.getLicense());
LicenseFragmentHelper.showLicense(getActivity(), component.getLicense());
});
softwareComponentsView.addView(componentView);
registerForContextMenu(componentView);
}
if (activeLicense != null) {
showLicense(getActivity(), activeLicense);
LicenseFragmentHelper.showLicense(getActivity(), activeLicense);
}
return rootView;
}
Expand Down Expand Up @@ -128,7 +117,7 @@ public boolean onContextItemSelected(@NonNull final MenuItem item) {
ShareUtils.openUrlInBrowser(getActivity(), component.getLink());
return true;
case R.id.action_show_license:
showLicense(getActivity(), component.getLicense());
LicenseFragmentHelper.showLicense(getActivity(), component.getLicense());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.schabi.newpipe.about;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Base64;
import android.webkit.WebView;

Expand All @@ -16,19 +14,15 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.nio.charset.StandardCharsets;

import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

public class LicenseFragmentHelper extends AsyncTask<Object, Void, Integer> {
private final WeakReference<Activity> weakReference;
private License license;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public LicenseFragmentHelper(@Nullable final Activity activity) {
weakReference = new WeakReference<>(activity);
}
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;

public class LicenseFragmentHelper {
/**
* @param context the context to use
* @param license the license
Expand Down Expand Up @@ -62,7 +56,7 @@ private static String getFormattedLicense(@NonNull final Context context,
* @param context
* @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);
return "body{padding:12px 15px;margin:0;"
+ "background:#" + getHexRGBColor(context, isLightTheme
Expand All @@ -84,45 +78,31 @@ private static String getLicenseStylesheet(final Context context) {
* @param color the color number from R.color
* @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);
}

@Nullable
private Activity getActivity() {
final Activity activity = weakReference.get();

if (activity != null && activity.isFinishing()) {
return null;
} else {
return activity;
}
}

@Override
protected Integer doInBackground(final Object... objects) {
license = (License) objects[0];
return 1;
}

@Override
protected void onPostExecute(final Integer result) {
final Activity activity = getActivity();
if (activity == null) {
static void showLicense(@Nullable final Context context, @NonNull final License license) {
if (context == null) {
return;
}

final String webViewData = Base64.encodeToString(getFormattedLicense(activity, license)
.getBytes(StandardCharsets.UTF_8), Base64.NO_PADDING);
final WebView webView = new WebView(activity);
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64");

final AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle(license.getName());
alert.setView(webView);
assureCorrectAppLanguage(activity);
alert.setNegativeButton(activity.getString(R.string.finish),
(dialog, which) -> dialog.dismiss());
alert.show();
Observable.fromCallable(() -> getFormattedLicense(context, license))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(formattedLicense -> {
final String webViewData = Base64.encodeToString(formattedLicense
.getBytes(StandardCharsets.UTF_8), Base64.NO_PADDING);
final WebView webView = new WebView(context);
webView.loadData(webViewData, "text/html; charset=UTF-8", "base64");

final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(license.getName());
alert.setView(webView);
assureCorrectAppLanguage(context);
alert.setNegativeButton(context.getString(R.string.finish),
(dialog, which) -> dialog.dismiss());
alert.show();
});
}
}

0 comments on commit 171a8c9

Please sign in to comment.