2018-01-30 08:01:57 +01:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.support.annotation.DrawableRes;
|
2018-03-08 15:50:46 +01:00
|
|
|
import android.support.annotation.StringRes;
|
2018-01-30 08:01:57 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-03-14 04:25:22 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
|
|
|
|
2018-01-30 08:01:57 +01:00
|
|
|
public class ServiceHelper {
|
2018-02-12 19:44:35 +01:00
|
|
|
private static final StreamingService DEFAULT_FALLBACK_SERVICE = ServiceList.YouTube;
|
2018-01-30 08:01:57 +01:00
|
|
|
|
|
|
|
@DrawableRes
|
|
|
|
public static int getIcon(int serviceId) {
|
|
|
|
switch (serviceId) {
|
|
|
|
case 0:
|
2018-02-04 01:29:24 +01:00
|
|
|
return R.drawable.place_holder_youtube;
|
2018-01-30 08:01:57 +01:00
|
|
|
case 1:
|
2018-12-23 22:41:42 +01:00
|
|
|
return R.drawable.place_holder_cloud;
|
|
|
|
case 2:
|
|
|
|
return R.drawable.place_holder_gadse;
|
2018-01-30 08:01:57 +01:00
|
|
|
default:
|
2018-12-23 22:41:42 +01:00
|
|
|
return R.drawable.place_holder_circle;
|
2018-01-30 08:01:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:18:57 +02:00
|
|
|
public static String getTranslatedFilterString(String filter, Context c) {
|
|
|
|
switch(filter) {
|
|
|
|
case "all": return c.getString(R.string.all);
|
|
|
|
case "videos": return c.getString(R.string.videos);
|
|
|
|
case "channels": return c.getString(R.string.channels);
|
|
|
|
case "playlists": return c.getString(R.string.playlists);
|
|
|
|
case "tracks": return c.getString(R.string.tracks);
|
|
|
|
case "users": return c.getString(R.string.users);
|
2019-01-29 17:20:30 +01:00
|
|
|
case "conferences" : return c.getString(R.string.conferences);
|
|
|
|
case "events" : return c.getString(R.string.events);
|
2018-07-19 16:18:57 +02:00
|
|
|
default: return filter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 15:50:46 +01:00
|
|
|
/**
|
|
|
|
* Get a resource string with instructions for importing subscriptions for each service.
|
|
|
|
*
|
|
|
|
* @return the string resource containing the instructions or -1 if the service don't support it
|
|
|
|
*/
|
|
|
|
@StringRes
|
|
|
|
public static int getImportInstructions(int serviceId) {
|
|
|
|
switch (serviceId) {
|
|
|
|
case 0:
|
|
|
|
return R.string.import_youtube_instructions;
|
|
|
|
case 1:
|
|
|
|
return R.string.import_soundcloud_instructions;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For services that support importing from a channel url, return a hint that will
|
|
|
|
* be used in the EditText that the user will type in his channel url.
|
|
|
|
*
|
|
|
|
* @return the hint's string resource or -1 if the service don't support it
|
|
|
|
*/
|
|
|
|
@StringRes
|
|
|
|
public static int getImportInstructionsHint(int serviceId) {
|
|
|
|
switch (serviceId) {
|
|
|
|
case 1:
|
|
|
|
return R.string.import_soundcloud_instructions_hint;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 08:01:57 +01:00
|
|
|
public static int getSelectedServiceId(Context context) {
|
|
|
|
|
|
|
|
final String serviceName = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
.getString(context.getString(R.string.current_service_key), context.getString(R.string.default_service_value));
|
|
|
|
|
|
|
|
int serviceId;
|
|
|
|
try {
|
|
|
|
serviceId = NewPipe.getService(serviceName).getServiceId();
|
|
|
|
} catch (ExtractionException e) {
|
|
|
|
serviceId = DEFAULT_FALLBACK_SERVICE.getServiceId();
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setSelectedServiceId(Context context, int serviceId) {
|
|
|
|
String serviceName;
|
|
|
|
try {
|
2018-02-12 19:44:35 +01:00
|
|
|
serviceName = NewPipe.getService(serviceId).getServiceInfo().getName();
|
2018-01-30 08:01:57 +01:00
|
|
|
} catch (ExtractionException e) {
|
2018-02-12 19:44:35 +01:00
|
|
|
serviceName = DEFAULT_FALLBACK_SERVICE.getServiceInfo().getName();
|
2018-01-30 08:01:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setSelectedServicePreferences(context, serviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setSelectedServiceId(Context context, String serviceName) {
|
|
|
|
int serviceId = NewPipe.getIdOfService(serviceName);
|
2018-02-12 19:44:35 +01:00
|
|
|
if (serviceId == -1) serviceName = DEFAULT_FALLBACK_SERVICE.getServiceInfo().getName();
|
2018-01-30 08:01:57 +01:00
|
|
|
|
|
|
|
setSelectedServicePreferences(context, serviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void setSelectedServicePreferences(Context context, String serviceName) {
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(context).edit().
|
|
|
|
putString(context.getString(R.string.current_service_key), serviceName).apply();
|
|
|
|
}
|
2018-03-14 04:25:22 +01:00
|
|
|
|
|
|
|
public static long getCacheExpirationMillis(final int serviceId) {
|
|
|
|
if (serviceId == SoundCloud.getServiceId()) {
|
|
|
|
return TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES);
|
|
|
|
} else {
|
|
|
|
return TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 18:41:11 +02:00
|
|
|
|
|
|
|
public static boolean isBeta(final StreamingService s) {
|
|
|
|
switch(s.getServiceInfo().getName()) {
|
|
|
|
case "YouTube": return false;
|
|
|
|
default: return true;
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 08:01:57 +01:00
|
|
|
}
|