2017-04-22 07:13:48 +02: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-27 19:47:00 +02:00
|
|
|
import android.app.Activity;
|
2017-04-22 07:13:48 +02:00
|
|
|
import android.app.Application;
|
2018-04-13 22:37:21 +02:00
|
|
|
import android.app.Service;
|
2017-06-28 19:33:54 +02:00
|
|
|
import android.arch.persistence.room.Room;
|
2018-04-22 17:20:01 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
2018-01-20 13:39:01 +01:00
|
|
|
import android.content.Context;
|
2018-05-10 11:16:56 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2018-03-09 22:02:32 +01:00
|
|
|
import android.support.annotation.NonNull;
|
2018-05-10 11:16:56 +02:00
|
|
|
import android.support.text.emoji.EmojiCompat;
|
2017-04-22 07:13:48 +02:00
|
|
|
|
2017-11-01 21:02:44 +01:00
|
|
|
import com.evernote.android.job.JobManager;
|
2017-06-28 19:33:54 +02:00
|
|
|
import com.jakewharton.picasso.OkHttp3Downloader;
|
2018-02-03 22:45:14 +01:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager;
|
2017-06-28 19:33:54 +02:00
|
|
|
import com.keylesspalace.tusky.db.AppDatabase;
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.di.AppInjector;
|
2018-05-10 11:16:56 +02:00
|
|
|
import com.keylesspalace.tusky.util.EmojiCompatFont;
|
2017-04-22 07:13:48 +02:00
|
|
|
import com.squareup.picasso.Picasso;
|
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
import dagger.android.AndroidInjector;
|
|
|
|
import dagger.android.DispatchingAndroidInjector;
|
|
|
|
import dagger.android.HasActivityInjector;
|
2018-04-22 17:20:01 +02:00
|
|
|
import dagger.android.HasBroadcastReceiverInjector;
|
2018-04-13 22:37:21 +02:00
|
|
|
import dagger.android.HasServiceInjector;
|
2018-04-06 22:02:46 +02:00
|
|
|
import okhttp3.OkHttpClient;
|
2018-03-27 19:47:00 +02:00
|
|
|
|
2018-04-22 17:20:01 +02:00
|
|
|
public class TuskyApplication extends Application implements HasActivityInjector, HasServiceInjector, HasBroadcastReceiverInjector {
|
2018-03-27 19:47:00 +02:00
|
|
|
@Inject
|
|
|
|
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
|
|
|
|
@Inject
|
2018-04-13 22:37:21 +02:00
|
|
|
DispatchingAndroidInjector<Service> dispatchingServiceInjector;
|
|
|
|
@Inject
|
2018-04-22 17:20:01 +02:00
|
|
|
DispatchingAndroidInjector<BroadcastReceiver> dispatchingBroadcastReceiverInjector;
|
|
|
|
@Inject
|
2018-03-27 19:47:00 +02:00
|
|
|
NotificationPullJobCreator notificationPullJobCreator;
|
2018-07-23 21:59:10 +02:00
|
|
|
@Inject
|
|
|
|
OkHttpClient okHttpClient;
|
2017-06-28 19:33:54 +02:00
|
|
|
|
2018-07-23 21:59:10 +02:00
|
|
|
private AppDatabase appDatabase;
|
|
|
|
private AccountManager accountManager;
|
|
|
|
|
|
|
|
private ServiceLocator serviceLocator;
|
2017-07-07 00:27:51 +02:00
|
|
|
|
2018-03-09 22:02:32 +01:00
|
|
|
public static TuskyApplication getInstance(@NonNull Context context) {
|
|
|
|
return (TuskyApplication) context.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
2017-04-22 07:13:48 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2017-05-01 08:02:32 +02:00
|
|
|
super.onCreate();
|
2018-03-09 22:02:32 +01:00
|
|
|
|
2018-07-23 21:59:10 +02:00
|
|
|
appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB")
|
2018-03-27 19:47:00 +02:00
|
|
|
.allowMainThreadQueries()
|
2018-07-30 15:43:27 +02:00
|
|
|
.addMigrations(AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5,
|
|
|
|
AppDatabase.MIGRATION_5_6, AppDatabase.MIGRATION_6_7, AppDatabase.MIGRATION_7_8)
|
2018-03-27 19:47:00 +02:00
|
|
|
.build();
|
2018-07-23 21:59:10 +02:00
|
|
|
accountManager = new AccountManager(appDatabase);
|
2018-03-09 22:02:32 +01:00
|
|
|
serviceLocator = new ServiceLocator() {
|
|
|
|
@Override
|
|
|
|
public <T> T get(Class<T> clazz) {
|
|
|
|
if (clazz.equals(AccountManager.class)) {
|
|
|
|
//noinspection unchecked
|
|
|
|
return (T) accountManager;
|
2018-07-23 21:59:10 +02:00
|
|
|
} else if (clazz.equals(AppDatabase.class)) {
|
|
|
|
//noinspection unchecked
|
|
|
|
return (T) appDatabase;
|
2018-03-09 22:02:32 +01:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Unknown service " + clazz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-04-22 07:13:48 +02:00
|
|
|
|
2018-04-13 22:37:21 +02:00
|
|
|
initAppInjector();
|
2018-03-27 19:47:00 +02:00
|
|
|
initPicasso();
|
2018-05-10 11:16:56 +02:00
|
|
|
initEmojiCompat();
|
2017-12-02 12:22:52 +01:00
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
JobManager.create(this).addJobCreator(notificationPullJobCreator);
|
2018-01-20 13:39:01 +01:00
|
|
|
|
2018-02-03 22:45:14 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 11:16:56 +02:00
|
|
|
/**
|
|
|
|
* This method will load the EmojiCompat font which has been selected.
|
|
|
|
* If this font does not work or if the user hasn't selected one (yet), it will use a
|
|
|
|
* fallback solution instead which won't make any visible difference to using no EmojiCompat at all.
|
|
|
|
*/
|
|
|
|
private void initEmojiCompat() {
|
|
|
|
int emojiSelection = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(getApplicationContext())
|
|
|
|
.getInt(EmojiPreference.FONT_PREFERENCE, 0);
|
|
|
|
EmojiCompatFont font = EmojiCompatFont.byId(emojiSelection);
|
|
|
|
// FileEmojiCompat will handle any non-existing font and provide a fallback solution.
|
|
|
|
EmojiCompat.Config config = font.getConfig(getApplicationContext())
|
|
|
|
// The user probably wants to get a consistent experience
|
|
|
|
.setReplaceAll(true);
|
|
|
|
EmojiCompat.init(config);
|
|
|
|
}
|
|
|
|
|
2018-04-13 22:37:21 +02:00
|
|
|
protected void initAppInjector() {
|
|
|
|
AppInjector.INSTANCE.init(this);
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:02:32 +01:00
|
|
|
protected void initPicasso() {
|
|
|
|
// Initialize Picasso configuration
|
|
|
|
Picasso.Builder builder = new Picasso.Builder(this);
|
2018-04-22 17:20:01 +02:00
|
|
|
builder.downloader(new OkHttp3Downloader(okHttpClient));
|
2018-03-09 22:02:32 +01:00
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
builder.listener((picasso, uri, exception) -> exception.printStackTrace());
|
|
|
|
}
|
2018-04-06 22:02:46 +02:00
|
|
|
|
|
|
|
Picasso.setSingletonInstance(builder.build());
|
2018-03-09 22:02:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public ServiceLocator getServiceLocator() {
|
|
|
|
return serviceLocator;
|
2017-04-22 07:13:48 +02:00
|
|
|
}
|
2018-02-03 22:45:14 +01:00
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
@Override
|
|
|
|
public AndroidInjector<Activity> activityInjector() {
|
|
|
|
return dispatchingAndroidInjector;
|
|
|
|
}
|
|
|
|
|
2018-04-13 22:37:21 +02:00
|
|
|
@Override
|
|
|
|
public AndroidInjector<Service> serviceInjector() {
|
|
|
|
return dispatchingServiceInjector;
|
|
|
|
}
|
|
|
|
|
2018-04-22 17:20:01 +02:00
|
|
|
@Override
|
|
|
|
public AndroidInjector<BroadcastReceiver> broadcastReceiverInjector() {
|
|
|
|
return dispatchingBroadcastReceiverInjector;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:02:32 +01:00
|
|
|
public interface ServiceLocator {
|
|
|
|
<T> T get(Class<T> clazz);
|
|
|
|
}
|
|
|
|
}
|