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

91 lines
3 KiB
Java
Raw Normal View History

2023-06-17 15:30:23 +02:00
package com.cappielloantonio.tempo;
2020-11-20 15:38:08 +01:00
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
2023-06-17 15:30:23 +02:00
import com.cappielloantonio.tempo.helper.ThemeHelper;
import com.cappielloantonio.tempo.subsonic.Subsonic;
import com.cappielloantonio.tempo.subsonic.SubsonicPreferences;
import com.cappielloantonio.tempo.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;
2023-03-10 15:21:02 +01:00
private static Context context;
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);
2023-03-10 15:21:02 +01:00
instance = new App();
context = getApplicationContext();
2023-03-11 18:42:50 +01:00
preferences = PreferenceManager.getDefaultSharedPreferences(context);
2020-11-20 15:38:08 +01:00
}
public static App getInstance() {
if (instance == null) {
instance = new App();
}
2023-03-10 15:21:02 +01:00
2020-11-20 15:38:08 +01:00
return instance;
}
2023-03-10 15:21:02 +01:00
public static Context getContext() {
if (context == null) {
context = getInstance();
}
return context;
}
public static Subsonic getSubsonicClientInstance(boolean override) {
2021-07-24 19:58:17 +02:00
if (subsonic == null || override) {
2023-03-10 15:21:02 +01:00
subsonic = getSubsonicClient();
2021-07-24 14:49:13 +02:00
}
return subsonic;
}
public SharedPreferences getPreferences() {
if (preferences == null) {
2023-03-11 18:42:50 +01:00
preferences = PreferenceManager.getDefaultSharedPreferences(context);
}
return preferences;
}
2023-03-10 15:21:02 +01:00
private static Subsonic getSubsonicClient() {
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
2023-03-10 15:21:02 +01:00
return new Subsonic(preferences);
2021-07-24 14:49:13 +02:00
}
2020-11-20 15:38:08 +01:00
}