TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/helper/HelperInstance.java

126 lines
5.0 KiB
Java

package app.fedilab.fedilabtube.helper;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
import app.fedilab.fedilabtube.BuildConfig;
import app.fedilab.fedilabtube.R;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program 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.
*
* TubeLab 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 TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
public class HelperInstance {
/**
* Returns the instance of the authenticated user
*
* @param context Context
* @return String domain instance
*/
public static String getLiveInstance(Context context) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if (BuildConfig.FLAVOR.compareTo("fdroid_full") == 0 || BuildConfig.FLAVOR.compareTo("google_full") == 0) {
return sharedpreferences.getString(Helper.PREF_INSTANCE, getDefaultInstance());
} else {
return sharedpreferences.getString(Helper.PREF_INSTANCE, "tube-institutionnel.apps.education.fr");
}
}
public static void setTheme(AppCompatActivity activity, String instance, boolean noActionBar) {
switch (instance) {
case "tube-institutionnel.apps.education.fr":
activity.setTheme(noActionBar?R.style.InstitutionnelNoActionBar:R.style.Institutionnel);
break;
case "tube-maternelle.apps.education.fr":
activity.setTheme(noActionBar?R.style.MaternelleNoActionBar:R.style.Maternelle);
break;
case "tube-arts-lettres-sciences-humaines.apps.education.fr":
activity.setTheme(noActionBar?R.style.ArtNoActionBar:R.style.Art);
break;
case "tube-sciences-technologies.apps.education.fr":
activity.setTheme(noActionBar?R.style.SciencesNoActionBar:R.style.Sciences);
break;
case "tube-education-physique-et-sportive.apps.education.fr":
activity.setTheme(noActionBar?R.style.EducationNoActionBar:R.style.Education);
break;
case "tube-enseignement-professionnel.apps.education.fr":
activity.setTheme(noActionBar?R.style.EnseignementProNoActionBar:R.style.EnseignementPro);
break;
case "tube-langues-vivantes.apps.education.fr":
activity.setTheme(noActionBar?R.style.LanguesNoActionBar:R.style.Langues);
break;
case "tube-action-educative.apps.education.fr":
activity.setTheme(noActionBar?R.style.ActionEducativeNoActionBar:R.style.ActionEducative);
break;
case "tube-cycle-2.apps.education.fr":
activity.setTheme(noActionBar?R.style.Cycle2NoActionBar:R.style.Cycle2);
break;
case "tube-cycle-3.apps.education.fr":
activity.setTheme(noActionBar?R.style.Cycle3NoActionBar:R.style.Cycle3);
break;
default:
activity.setTheme(noActionBar?R.style.AppThemeNoActionBar:R.style.AppTheme);
}
}
/**
* Get a default instance host name depending of the device locale
*
* @return peertube host String
*/
private static String getDefaultInstance() {
String lang = Locale.getDefault().getLanguage();
if (lang.contains("-")) {
if (!lang.split("-")[0].trim().toLowerCase().startsWith("zh")) {
lang = lang.split("-")[0];
if (lang.split("-")[1].toLowerCase().contains("be")) {
lang = "be";
} else if (lang.split("-")[1].toLowerCase().contains("gb")) {
lang = "gb";
}
} else {
lang = lang.split("-")[0] + "-" + lang.split("-")[1].toUpperCase();
}
}
switch (lang) {
case "it":
return "peertube.uno";
case "be":
return "peertube.be";
case "fr":
return "video.liberta.vip";
case "de":
return "peertube.social";
case "ru":
return "peertube.su";
case "gb":
return "peertube.co.uk";
case "da":
case "sv":
case "nb":
case "fi":
case "is":
return "peertube.dk";
default:
return "peertube.social";
}
}
}