Skip to content

Commit

Permalink
Merge pull request #4127 from nmurali94/bugfix-keep-license-on-rotate
Browse files Browse the repository at this point in the history
Restore license pop-up when orientation changes
  • Loading branch information
Stypox authored Sep 2, 2020
2 parents c66c812 + daa427d commit a84b54f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/org/schabi/newpipe/about/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

/**
* Class for storing information about a software license.
*/
public class License implements Parcelable {
public class License implements Parcelable, Serializable {
public static final Creator<License> CREATOR = new Creator<License>() {
@Override
public License createFromParcel(final Parcel source) {
Expand Down
40 changes: 32 additions & 8 deletions app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.schabi.newpipe.about;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
Expand All @@ -11,12 +10,14 @@
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.schabi.newpipe.R;
import org.schabi.newpipe.util.ShareUtils;

import java.io.Serializable;
import java.util.Arrays;

/**
Expand All @@ -26,6 +27,8 @@ public class LicenseFragment extends Fragment {
private static final String ARG_COMPONENTS = "components";
private SoftwareComponent[] softwareComponents;
private SoftwareComponent componentForContextMenu;
private License activeLicense;
private static final String LICENSE_KEY = "ACTIVE_LICENSE";

public static LicenseFragment newInstance(final SoftwareComponent[] softwareComponents) {
if (softwareComponents == null) {
Expand All @@ -44,8 +47,8 @@ public static LicenseFragment newInstance(final SoftwareComponent[] softwareComp
* @param context the context to use
* @param license the license to show
*/
private static void showLicense(final Context context, final License license) {
new LicenseFragmentHelper((Activity) context).execute(license);
private static void showLicense(final Activity context, final License license) {
new LicenseFragmentHelper(context).execute(license);
}

@Override
Expand All @@ -54,6 +57,12 @@ public void onCreate(@Nullable final Bundle savedInstanceState) {
softwareComponents = (SoftwareComponent[]) getArguments()
.getParcelableArray(ARG_COMPONENTS);

if (savedInstanceState != null) {
final Serializable license = savedInstanceState.getSerializable(LICENSE_KEY);
if (license != null) {
activeLicense = (License) license;
}
}
// Sort components by name
Arrays.sort(softwareComponents, (o1, o2) -> o1.getName().compareTo(o2.getName()));
}
Expand All @@ -66,8 +75,10 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
final ViewGroup softwareComponentsView = rootView.findViewById(R.id.software_components);

final View licenseLink = rootView.findViewById(R.id.app_read_license);
licenseLink.setOnClickListener(v ->
showLicense(getActivity(), StandardLicenses.GPL3));
licenseLink.setOnClickListener(v -> {
activeLicense = StandardLicenses.GPL3;
showLicense(getActivity(), StandardLicenses.GPL3);
});

for (final SoftwareComponent component : softwareComponents) {
final View componentView = inflater
Expand All @@ -81,11 +92,16 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
component.getLicense().getAbbreviation()));

componentView.setTag(component);
componentView.setOnClickListener(v ->
showLicense(getActivity(), component.getLicense()));
componentView.setOnClickListener(v -> {
activeLicense = component.getLicense();
showLicense(getActivity(), component.getLicense());
});
softwareComponentsView.addView(componentView);
registerForContextMenu(componentView);
}
if (activeLicense != null) {
showLicense(getActivity(), activeLicense);
}
return rootView;
}

Expand All @@ -101,7 +117,7 @@ public void onCreateContextMenu(final ContextMenu menu, final View v,
}

@Override
public boolean onContextItemSelected(final MenuItem item) {
public boolean onContextItemSelected(@NonNull final MenuItem item) {
// item.getMenuInfo() is null so we use the tag of the view
final SoftwareComponent component = componentForContextMenu;
if (component == null) {
Expand All @@ -116,4 +132,12 @@ public boolean onContextItemSelected(final MenuItem item) {
}
return false;
}

@Override
public void onSaveInstanceState(@NonNull final Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
if (activeLicense != null) {
savedInstanceState.putSerializable(LICENSE_KEY, activeLicense);
}
}
}

0 comments on commit a84b54f

Please sign in to comment.