2017-04-22 15:13:48 +10:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
import android.app.Activity;
|
2017-04-22 15:13:48 +10:00
|
|
|
import android.app.Application;
|
2018-01-20 23:39:01 +11:00
|
|
|
import android.app.UiModeManager;
|
2017-06-29 03:33:54 +10:00
|
|
|
import android.arch.persistence.room.Room;
|
2018-01-20 23:39:01 +11:00
|
|
|
import android.content.Context;
|
2017-12-27 07:45:08 +11:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
2018-03-10 08:02:32 +11:00
|
|
|
import android.support.annotation.NonNull;
|
2017-12-02 22:22:52 +11:00
|
|
|
import android.support.v7.app.AppCompatDelegate;
|
2017-04-22 15:13:48 +10:00
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
import com.evernote.android.job.JobManager;
|
2017-06-29 03:33:54 +10:00
|
|
|
import com.jakewharton.picasso.OkHttp3Downloader;
|
2018-02-04 08:45:14 +11:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2017-06-29 03:33:54 +10:00
|
|
|
import com.keylesspalace.tusky.db.AppDatabase;
|
2018-03-28 04:47:00 +11:00
|
|
|
import com.keylesspalace.tusky.di.AppInjector;
|
2017-06-19 07:20:54 +10:00
|
|
|
import com.keylesspalace.tusky.util.OkHttpUtils;
|
2018-02-04 09:26:53 +11:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
2017-04-22 15:13:48 +10:00
|
|
|
import com.squareup.picasso.Picasso;
|
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
import dagger.android.AndroidInjector;
|
|
|
|
import dagger.android.DispatchingAndroidInjector;
|
|
|
|
import dagger.android.HasActivityInjector;
|
|
|
|
|
|
|
|
public class TuskyApplication extends Application implements HasActivityInjector {
|
2018-02-04 09:26:53 +11:00
|
|
|
public static final String APP_THEME_DEFAULT = ThemeUtils.THEME_NIGHT;
|
2018-01-20 23:39:01 +11:00
|
|
|
|
2017-06-29 03:33:54 +10:00
|
|
|
private static AppDatabase db;
|
2018-03-10 08:02:32 +11:00
|
|
|
private AccountManager accountManager;
|
2018-03-28 04:47:00 +11:00
|
|
|
@Inject
|
|
|
|
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
|
|
|
|
@Inject
|
|
|
|
NotificationPullJobCreator notificationPullJobCreator;
|
2017-06-29 03:33:54 +10:00
|
|
|
|
2017-07-06 00:34:59 +10:00
|
|
|
public static AppDatabase getDB() {
|
|
|
|
return db;
|
|
|
|
}
|
2017-07-07 08:27:51 +10:00
|
|
|
|
2018-01-20 23:39:01 +11:00
|
|
|
private static UiModeManager uiModeManager;
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
public static UiModeManager getUiModeManager() {
|
|
|
|
return uiModeManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TuskyApplication getInstance(@NonNull Context context) {
|
|
|
|
return (TuskyApplication) context.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private ServiceLocator serviceLocator;
|
2018-01-20 23:39:01 +11:00
|
|
|
|
2017-04-22 15:13:48 +10:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2017-05-01 16:02:32 +10:00
|
|
|
super.onCreate();
|
2018-03-10 08:02:32 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB")
|
|
|
|
.allowMainThreadQueries()
|
|
|
|
.addMigrations(AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5)
|
|
|
|
.build();
|
|
|
|
accountManager = new AccountManager(db);
|
2018-03-10 08:02:32 +11:00
|
|
|
serviceLocator = new ServiceLocator() {
|
|
|
|
@Override
|
|
|
|
public <T> T get(Class<T> clazz) {
|
|
|
|
if (clazz.equals(AccountManager.class)) {
|
|
|
|
//noinspection unchecked
|
|
|
|
return (T) accountManager;
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Unknown service " + clazz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-04-22 15:13:48 +10:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
AppInjector.INSTANCE.init(this);
|
|
|
|
initPicasso();
|
2017-12-02 22:22:52 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
JobManager.create(this).addJobCreator(notificationPullJobCreator);
|
2018-03-10 08:02:32 +11:00
|
|
|
uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
|
2018-01-20 23:39:01 +11:00
|
|
|
|
2017-12-02 22:22:52 +11:00
|
|
|
//necessary for Android < APi 21
|
|
|
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
2018-02-04 08:45:14 +11:00
|
|
|
}
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
protected void initPicasso() {
|
|
|
|
// Initialize Picasso configuration
|
|
|
|
Picasso.Builder builder = new Picasso.Builder(this);
|
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
builder.downloader(new OkHttp3Downloader(OkHttpUtils.getCompatibleClient(preferences)));
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
builder.listener((picasso, uri, exception) -> exception.printStackTrace());
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Picasso.setSingletonInstance(builder.build());
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ServiceLocator getServiceLocator() {
|
|
|
|
return serviceLocator;
|
2017-04-22 15:13:48 +10:00
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
@Override
|
|
|
|
public AndroidInjector<Activity> activityInjector() {
|
|
|
|
return dispatchingAndroidInjector;
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
public interface ServiceLocator {
|
|
|
|
<T> T get(Class<T> clazz);
|
|
|
|
}
|
|
|
|
}
|