tempus/app/src/main/java/com/cappielloantonio/play/App.java

80 lines
2.9 KiB
Java
Raw Normal View History

2020-11-20 15:38:08 +01:00
package com.cappielloantonio.play;
import android.app.Application;
import android.content.Context;
2021-04-12 17:56:09 +02:00
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
2020-11-20 15:38:08 +01:00
import com.cappielloantonio.play.helper.ThemeHelper;
2021-07-24 14:49:13 +02:00
import com.cappielloantonio.play.subsonic.Subsonic;
import com.cappielloantonio.play.subsonic.SubsonicPreferences;
import com.cappielloantonio.play.util.Constants;
import com.cappielloantonio.play.util.Preferences;
2022-01-07 09:40:39 +01:00
import com.google.android.material.color.DynamicColors;
2020-11-20 15:38:08 +01:00
public class App extends Application {
private static App instance;
2021-07-24 14:49:13 +02:00
private static Subsonic subsonic;
private static SharedPreferences preferences;
2020-11-20 15:38:08 +01:00
@Override
public void onCreate() {
super.onCreate();
2022-01-07 09:40:39 +01:00
DynamicColors.applyToActivitiesIfAvailable(this);
2021-04-12 17:56:09 +02:00
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String themePref = sharedPreferences.getString(Preferences.THEME, ThemeHelper.DEFAULT_MODE);
2021-04-12 17:56:09 +02:00
ThemeHelper.applyTheme(themePref);
preferences = getSharedPreferences(Constants.SHARED_PREF_KEY, Context.MODE_PRIVATE);
2020-11-20 15:38:08 +01:00
}
public static App getInstance() {
if (instance == null) {
instance = new App();
}
return instance;
}
2021-07-24 19:58:17 +02:00
public static Subsonic getSubsonicClientInstance(Context context, boolean override) {
if (subsonic == null || override) {
2021-07-24 14:49:13 +02:00
subsonic = getSubsonicClient(context);
}
return subsonic;
}
public SharedPreferences getPreferences() {
if (preferences == null) {
preferences = getSharedPreferences(Constants.SHARED_PREF_KEY, Context.MODE_PRIVATE);
}
return preferences;
}
2021-07-24 14:49:13 +02:00
private static Subsonic getSubsonicClient(Context context) {
String server = Preferences.getServer();
String username = Preferences.getUser();
String password = Preferences.getPassword();
String token = Preferences.getToken();
String salt = Preferences.getSalt();
boolean isLowSecurity = Preferences.isLowScurity();
2021-07-24 14:49:13 +02:00
2021-07-28 18:28:47 +02:00
SubsonicPreferences preferences = new SubsonicPreferences();
preferences.setServerUrl(server);
preferences.setUsername(username);
preferences.setAuthentication(password, token, salt, isLowSecurity);
if (preferences.getAuthentication() != null) {
if (preferences.getAuthentication().getPassword() != null)
Preferences.setPassword(preferences.getAuthentication().getPassword());
if (preferences.getAuthentication().getToken() != null)
Preferences.setToken(preferences.getAuthentication().getToken());
if (preferences.getAuthentication().getSalt() != null)
Preferences.setSalt(preferences.getAuthentication().getSalt());
}
2021-07-24 14:49:13 +02:00
return new Subsonic(context, preferences);
2021-07-24 14:49:13 +02:00
}
2020-11-20 15:38:08 +01:00
}