2017-05-08 13:28:33 +00:00
|
|
|
package org.schabi.newpipe.util;
|
2015-12-29 14:35:51 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.preference.PreferenceManager;
|
2018-01-28 06:14:38 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.support.annotation.PluralsRes;
|
|
|
|
import android.support.annotation.StringRes;
|
2017-10-13 03:47:12 +00:00
|
|
|
import android.text.TextUtils;
|
2015-12-29 14:35:51 +00:00
|
|
|
|
2017-05-08 13:28:33 +00:00
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
|
2015-12-29 14:35:51 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.NumberFormat;
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
2018-01-28 06:14:38 +00:00
|
|
|
import java.util.Arrays;
|
2015-12-29 14:35:51 +00:00
|
|
|
import java.util.Date;
|
2018-01-28 06:14:38 +00:00
|
|
|
import java.util.List;
|
2015-12-29 14:35:51 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
/*
|
2015-12-29 14:35:51 +00:00
|
|
|
* Created by chschtsch on 12/29/15.
|
2016-01-05 20:41:55 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) Gregory Arkhipov 2015
|
|
|
|
* Localization.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
2015-12-29 14:35:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class Localization {
|
|
|
|
|
2018-01-28 06:14:38 +00:00
|
|
|
public final static String DOT_SEPARATOR = " • ";
|
|
|
|
|
2016-03-10 09:50:42 +00:00
|
|
|
private Localization() {
|
|
|
|
}
|
|
|
|
|
2018-01-28 06:14:38 +00:00
|
|
|
@NonNull
|
|
|
|
public static String concatenateStrings(final String... strings) {
|
|
|
|
return concatenateStrings(Arrays.asList(strings));
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static String concatenateStrings(final List<String> strings) {
|
|
|
|
if (strings.isEmpty()) return "";
|
|
|
|
|
|
|
|
final StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
stringBuilder.append(strings.get(0));
|
|
|
|
|
|
|
|
for (int i = 1; i < strings.size(); i++) {
|
|
|
|
final String string = strings.get(i);
|
|
|
|
if (!TextUtils.isEmpty(string)) {
|
|
|
|
stringBuilder.append(DOT_SEPARATOR).append(strings.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:19:21 +00:00
|
|
|
public static org.schabi.newpipe.extractor.utils.Localization getPreferredExtractorLocal(Context context) {
|
|
|
|
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
|
|
|
String languageCode = sp.getString(context.getString(R.string.content_language_key),
|
|
|
|
context.getString(R.string.default_language_value));
|
|
|
|
|
|
|
|
String countryCode = sp.getString(context.getString(R.string.content_country_key),
|
|
|
|
context.getString(R.string.default_country_value));
|
|
|
|
|
|
|
|
return new org.schabi.newpipe.extractor.utils.Localization(countryCode, languageCode);
|
|
|
|
}
|
|
|
|
|
2015-12-29 14:35:51 +00:00
|
|
|
public static Locale getPreferredLocale(Context context) {
|
|
|
|
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
2018-10-05 14:19:21 +00:00
|
|
|
String languageCode = sp.getString(context.getString(R.string.content_language_key),
|
|
|
|
context.getString(R.string.default_language_value));
|
2015-12-29 14:35:51 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
try {
|
|
|
|
if (languageCode.length() == 2) {
|
|
|
|
return new Locale(languageCode);
|
|
|
|
} else if (languageCode.contains("_")) {
|
|
|
|
String country = languageCode.substring(languageCode.indexOf("_"), languageCode.length());
|
|
|
|
return new Locale(languageCode.substring(0, 2), country);
|
|
|
|
}
|
|
|
|
} catch (Exception ignored) {
|
2015-12-29 14:35:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
return Locale.getDefault();
|
2015-12-29 14:35:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
public static String localizeNumber(Context context, long number) {
|
2015-12-29 14:35:51 +00:00
|
|
|
Locale locale = getPreferredLocale(context);
|
|
|
|
NumberFormat nf = NumberFormat.getInstance(locale);
|
|
|
|
return nf.format(number);
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
private static String formatDate(Context context, String date) {
|
2015-12-29 14:35:51 +00:00
|
|
|
Locale locale = getPreferredLocale(context);
|
|
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
Date datum = null;
|
|
|
|
try {
|
|
|
|
datum = formatter.parse(date);
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
|
|
|
|
|
|
|
|
return df.format(datum);
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
public static String localizeDate(Context context, String date) {
|
2015-12-29 14:35:51 +00:00
|
|
|
Resources res = context.getResources();
|
2016-01-05 19:56:40 +00:00
|
|
|
String dateString = res.getString(R.string.upload_date_text);
|
2015-12-29 14:35:51 +00:00
|
|
|
|
2017-09-03 06:04:18 +00:00
|
|
|
String formattedDate = formatDate(context, date);
|
2015-12-29 14:35:51 +00:00
|
|
|
return String.format(dateString, formattedDate);
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
public static String localizeViewCount(Context context, long viewCount) {
|
|
|
|
return getQuantity(context, R.plurals.views, R.string.no_views, viewCount, localizeNumber(context, viewCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String localizeSubscribersCount(Context context, long subscriberCount) {
|
|
|
|
return getQuantity(context, R.plurals.subscribers, R.string.no_subscribers, subscriberCount, localizeNumber(context, subscriberCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String localizeStreamCount(Context context, long streamCount) {
|
|
|
|
return getQuantity(context, R.plurals.videos, R.string.no_videos, streamCount, localizeNumber(context, streamCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String shortCount(Context context, long count) {
|
|
|
|
if (count >= 1000000000) {
|
|
|
|
return Long.toString(count / 1000000000) + context.getString(R.string.short_billion);
|
|
|
|
} else if (count >= 1000000) {
|
|
|
|
return Long.toString(count / 1000000) + context.getString(R.string.short_million);
|
|
|
|
} else if (count >= 1000) {
|
|
|
|
return Long.toString(count / 1000) + context.getString(R.string.short_thousand);
|
|
|
|
} else {
|
|
|
|
return Long.toString(count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String shortViewCount(Context context, long viewCount) {
|
|
|
|
return getQuantity(context, R.plurals.views, R.string.no_views, viewCount, shortCount(context, viewCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String shortSubscriberCount(Context context, long subscriberCount) {
|
|
|
|
return getQuantity(context, R.plurals.subscribers, R.string.no_subscribers, subscriberCount, shortCount(context, subscriberCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getQuantity(Context context, @PluralsRes int pluralId, @StringRes int zeroCaseStringId, long count, String formattedCount) {
|
|
|
|
if (count == 0) return context.getString(zeroCaseStringId);
|
|
|
|
|
|
|
|
// As we use the already formatted count, is not the responsibility of this method handle long numbers
|
|
|
|
// (it probably will fall in the "other" category, or some language have some specific rule... then we have to change it)
|
|
|
|
int safeCount = count > Integer.MAX_VALUE ? Integer.MAX_VALUE : count < Integer.MIN_VALUE ? Integer.MIN_VALUE : (int) count;
|
|
|
|
return context.getResources().getQuantityString(pluralId, safeCount, formattedCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getDurationString(long duration) {
|
|
|
|
if (duration < 0) {
|
|
|
|
duration = 0;
|
|
|
|
}
|
|
|
|
String output;
|
|
|
|
long days = duration / (24 * 60 * 60L); /* greater than a day */
|
|
|
|
duration %= (24 * 60 * 60L);
|
|
|
|
long hours = duration / (60 * 60L); /* greater than an hour */
|
|
|
|
duration %= (60 * 60L);
|
|
|
|
long minutes = duration / 60L;
|
|
|
|
long seconds = duration % 60L;
|
|
|
|
|
|
|
|
//handle days
|
|
|
|
if (days > 0) {
|
|
|
|
output = String.format(Locale.US, "%d:%02d:%02d:%02d", days, hours, minutes, seconds);
|
|
|
|
} else if (hours > 0) {
|
|
|
|
output = String.format(Locale.US, "%d:%02d:%02d", hours, minutes, seconds);
|
|
|
|
} else {
|
|
|
|
output = String.format(Locale.US, "%d:%02d", minutes, seconds);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
2015-12-29 14:35:51 +00:00
|
|
|
}
|