2018-01-30 08:01:57 +01:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-12-29 18:36:39 +01:00
|
|
|
import android.content.SharedPreferences;
|
2018-01-30 08:01:57 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2019-11-24 16:38:06 +01:00
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.annotation.DrawableRes;
|
|
|
|
import androidx.annotation.StringRes;
|
2018-01-30 08:01:57 +01:00
|
|
|
|
2019-11-24 16:38:06 +01:00
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
|
|
|
|
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;
|
2019-11-22 20:01:35 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
|
2018-01-30 08:01:57 +01:00
|
|
|
|
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-29 18:36:39 +01:00
|
|
|
return R.drawable.place_holder_cloud;
|
|
|
|
case 2:
|
2018-12-23 22:41:42 +01:00
|
|
|
return R.drawable.place_holder_gadse;
|
2019-03-23 15:19:37 +01:00
|
|
|
case 3:
|
|
|
|
return R.drawable.place_holder_peertube;
|
2018-01-30 08:01:57 +01:00
|
|
|
default:
|
2018-12-29 18:36:39 +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) {
|
2019-11-23 20:04:40 +01:00
|
|
|
switch (filter) {
|
2018-07-19 16:18:57 +02:00
|
|
|
case "all": return c.getString(R.string.all);
|
Multiple localization fixes
With the extractor PR, fixes title & description shown in the wrong language.
Fixed views / spectators counts possibly in the wrong language
Fixed live spectators not showing full count on detail page
Fixed LIVE shown on players, it shows translated instead
Fixed Videos string in search / three dots not available in Weblate
(because it was videos, but there already was a plural string named videos, in Weblate)
Subscriber count is always giving the short count.
We can't get exact number since this YouTube update: https://support.google.com/youtube/thread/6543166
But only short count (B, M, k), so showing full number, eg for 1.9M: 1,900,000, is wrong because the number could be 1,923,490 or 1,897,789…
Added a « sytem default » option to content language and country language selector.
It's the one selected by default (not en-GB anymore then), and correspond to the
language of the system / country of the system
By system I mean phone, tablet, TV…
Fixed russian showing - before time ago (eg 19hrs ago)
This is a workaround fix, I opened an issue on prettytime library repo.
Fixed russian plurals:
other was used instead of many for videos and subscribers
Fixed seek_duration english only
2020-02-14 18:19:35 +01:00
|
|
|
case "videos": return c.getString(R.string.videos_string);
|
2018-07-19 16:18:57 +02:00
|
|
|
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) {
|
2019-11-23 20:04:40 +01:00
|
|
|
switch (s.getServiceInfo().getName()) {
|
2018-03-30 18:41:11 +02:00
|
|
|
case "YouTube": return false;
|
|
|
|
default: return true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 18:36:39 +01:00
|
|
|
|
|
|
|
public static void initService(Context context, int serviceId) {
|
2019-11-24 16:38:06 +01:00
|
|
|
if (serviceId == ServiceList.PeerTube.getServiceId()) {
|
2018-12-29 18:36:39 +01:00
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-11-24 16:38:06 +01:00
|
|
|
String json = sharedPreferences.getString(context.getString(R.string.peertube_selected_instance_key), null);
|
|
|
|
if (null == json) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject jsonObject = null;
|
|
|
|
try {
|
|
|
|
jsonObject = JsonParser.object().from(json);
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String name = jsonObject.getString("name");
|
|
|
|
String url = jsonObject.getString("url");
|
|
|
|
PeertubeInstance instance = new PeertubeInstance(url, name);
|
2019-11-22 20:01:35 +01:00
|
|
|
ServiceList.PeerTube.setInstance(instance);
|
2018-12-29 18:36:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:04:40 +01:00
|
|
|
public static void initServices(Context context) {
|
|
|
|
for (StreamingService s : ServiceList.all()) {
|
2018-12-29 18:36:39 +01:00
|
|
|
initService(context, s.getServiceId());
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 08:01:57 +01:00
|
|
|
}
|