diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index b60789678fa..52c7e1f292e 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -88,9 +88,8 @@ import org.schabi.newpipe.util.TLSSocketFactoryCompat; import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.services.InstanceManager; -import org.schabi.newpipe.util.services.PeertubeInstanceManager; +import org.schabi.newpipe.util.services.InstanceManagerHelper; import org.schabi.newpipe.util.services.ServiceHelper; -import org.schabi.newpipe.util.services.YoutubeLikeInstanceManager; import org.schabi.newpipe.views.FocusOverlayView; import java.util.ArrayList; @@ -392,16 +391,9 @@ private void showServices() { .add(R.id.menu_services_group, s.getServiceId(), ORDER, title) .setIcon(ServiceHelper.getIcon(s.getServiceId())); - // peertube specifics - InstanceManager instanceManager = null; - if (s.getServiceId() == 0) { - instanceManager = YoutubeLikeInstanceManager.MANAGER; - } else if (s.getServiceId() == 3) { - instanceManager = PeertubeInstanceManager.MANAGER; - } - if (instanceManager != null) { - enhanceServiceMenu(menuItem, instanceManager); - } + // instance specifics + InstanceManagerHelper.getManagerForServiceId(s.getServiceId()) + .ifPresent(im -> enhanceServiceMenu(menuItem, im)); } drawerLayoutBinding.navigation.getMenu() .getItem(ServiceHelper.getSelectedServiceId(this)) diff --git a/app/src/main/java/org/schabi/newpipe/util/services/AbstractInstanceManager.java b/app/src/main/java/org/schabi/newpipe/util/services/AbstractInstanceManager.java index e7676fd5341..f9a4e3f9f43 100644 --- a/app/src/main/java/org/schabi/newpipe/util/services/AbstractInstanceManager.java +++ b/app/src/main/java/org/schabi/newpipe/util/services/AbstractInstanceManager.java @@ -1,6 +1,7 @@ package org.schabi.newpipe.util.services; import android.content.Context; +import android.util.Log; import androidx.annotation.StringRes; import androidx.preference.PreferenceManager; @@ -11,6 +12,7 @@ import com.grack.nanojson.JsonStringWriter; import com.grack.nanojson.JsonWriter; +import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.extractor.InstanceBasedStreamingService; import org.schabi.newpipe.extractor.instance.Instance; @@ -19,6 +21,8 @@ import java.util.stream.Collectors; public abstract class AbstractInstanceManager implements InstanceManager { + private static final String TAG = "AbsInstanceManager"; + protected abstract InstanceBasedStreamingService getRelatedStreamingService(); protected abstract I createInstanceFromPersistence(JsonObject jsonObject); @@ -28,9 +32,8 @@ public abstract class AbstractInstanceManager implements Ins @StringRes protected abstract int getListPersistenceKey(); - @Override - public List saveInstanceList(final List instances, final Context context) { + public void saveInstanceList(final List instances, final Context context) { final JsonStringWriter jsonWriter = JsonWriter.string().object().array("instances"); for (final I instance : instances) { jsonWriter.object(); @@ -42,7 +45,6 @@ public List saveInstanceList(final List instances, final Context context) .edit() .putString(context.getString(getListPersistenceKey()), jsonToSave) .apply(); - return null; } @Override @@ -50,7 +52,7 @@ public List getInstanceList(final Context context) { final String savedInstanceListKey = context.getString(getListPersistenceKey()); final String savedJson = PreferenceManager.getDefaultSharedPreferences(context) .getString(savedInstanceListKey, null); - if (null == savedJson) { + if (savedJson == null) { return getDefaultInstanceList(); } @@ -90,6 +92,33 @@ public I saveCurrentInstance(final I instance, final Context context) { return instance; } + @Override + public void reloadCurrentInstanceFromPersistence(final Context context) { + final String json = PreferenceManager.getDefaultSharedPreferences(context) + .getString(context.getString(getSelectedInstancePersistenceKey()), null); + if (json == null) { + return; + } + + final JsonObject jsonObject; + try { + jsonObject = JsonParser.object().from(json); + } catch (final JsonParserException e) { + if (MainActivity.DEBUG) { + Log.w(TAG, "Failed to load instance from settings", e); + } + return; + } + + try { + getRelatedStreamingService().setInstance(createInstanceFromPersistence(jsonObject)); + } catch (final Exception e) { + if (MainActivity.DEBUG) { + Log.w(TAG, "Failed to load instance from settings", e); + } + } + } + @Override public I getCurrentInstance() { try { diff --git a/app/src/main/java/org/schabi/newpipe/util/services/InstanceManager.java b/app/src/main/java/org/schabi/newpipe/util/services/InstanceManager.java index 587dba2e339..8dd58d65aae 100644 --- a/app/src/main/java/org/schabi/newpipe/util/services/InstanceManager.java +++ b/app/src/main/java/org/schabi/newpipe/util/services/InstanceManager.java @@ -6,14 +6,55 @@ import java.util.List; +/** + * Manages service instances. + * + * @param + */ public interface InstanceManager { - List saveInstanceList(List instances, Context context); - + /** + * Saves the instance list (to persistence/preferences). + * + * @param instances instance list + * @param context Context + */ + void saveInstanceList(List instances, Context context); + + /** + * Returns the current instance list. + * + * @param context Context + * @return instance list + */ List getInstanceList(Context context); + /** + * Returns the current instance (from memory / the corresponding streaming-service). + * + * @return The current instance + */ I getCurrentInstance(); + /** + * Reloads the current instance from the persistence layer (preferences). + * + * @param context Context + */ + void reloadCurrentInstanceFromPersistence(Context context); + + /** + * Saves the instance as the currently used one (also to the persistence). + * + * @param instance The instance + * @param context Context + * @return the saved instance + */ I saveCurrentInstance(I instance, Context context); + /** + * Restores the default values. + * + * @param context Context + */ void restoreDefaults(Context context); } diff --git a/app/src/main/java/org/schabi/newpipe/util/services/InstanceManagerHelper.java b/app/src/main/java/org/schabi/newpipe/util/services/InstanceManagerHelper.java new file mode 100644 index 00000000000..797cd4d0116 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/services/InstanceManagerHelper.java @@ -0,0 +1,23 @@ +package org.schabi.newpipe.util.services; + +import org.schabi.newpipe.extractor.instance.Instance; + +import java.util.Optional; + +public final class InstanceManagerHelper { + private InstanceManagerHelper() { + // No impl + } + + @SuppressWarnings("java:S1452") // otherwise generics won't work! + public static Optional> getManagerForServiceId( + final int serviceId + ) { + if (serviceId == 0) { + return Optional.of(YoutubeLikeInstanceManager.MANAGER); + } else if (serviceId == 3) { + return Optional.of(PeertubeInstanceManager.MANAGER); + } + return Optional.empty(); + } +} diff --git a/app/src/main/java/org/schabi/newpipe/util/services/ServiceHelper.java b/app/src/main/java/org/schabi/newpipe/util/services/ServiceHelper.java index 33e57522b5e..3525f676412 100644 --- a/app/src/main/java/org/schabi/newpipe/util/services/ServiceHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/services/ServiceHelper.java @@ -3,23 +3,17 @@ import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; import android.content.Context; -import android.content.SharedPreferences; import androidx.annotation.DrawableRes; import androidx.annotation.StringRes; import androidx.preference.PreferenceManager; -import com.grack.nanojson.JsonObject; -import com.grack.nanojson.JsonParser; -import com.grack.nanojson.JsonParserException; - import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.ServiceList; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.instance.Instance; -import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance; import org.schabi.newpipe.extractor.services.youtube.invidious.InvidiousInstance; import java.util.OptionalInt; @@ -174,26 +168,8 @@ public static boolean isBeta(final StreamingService s) { } public static void initService(final Context context, final int serviceId) { - if (serviceId == ServiceList.PeerTube.getServiceId()) { - final SharedPreferences sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(context); - final String json = sharedPreferences.getString(context.getString( - R.string.peertube_selected_instance_key), null); - if (null == json) { - return; - } - - final JsonObject jsonObject; - try { - jsonObject = JsonParser.object().from(json); - } catch (final JsonParserException e) { - return; - } - final String name = jsonObject.getString("name"); - final String url = jsonObject.getString("url"); - final PeertubeInstance instance = new PeertubeInstance(url, name); - ServiceList.PeerTube.setInstance(instance); - } + InstanceManagerHelper.getManagerForServiceId(serviceId) + .ifPresent(im -> im.reloadCurrentInstanceFromPersistence(context)); } public static void initServices(final Context context) {