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-04-14 06:37:21 +10:00
|
|
|
import android.app.Service;
|
2017-06-29 03:33:54 +10:00
|
|
|
import android.arch.persistence.room.Room;
|
2018-04-23 01:20:01 +10:00
|
|
|
import android.content.BroadcastReceiver;
|
2018-01-20 23:39:01 +11:00
|
|
|
import android.content.Context;
|
2018-05-10 19:16:56 +10:00
|
|
|
import android.preference.PreferenceManager;
|
2018-03-10 08:02:32 +11:00
|
|
|
import android.support.annotation.NonNull;
|
2018-05-10 19:16:56 +10:00
|
|
|
import android.support.text.emoji.EmojiCompat;
|
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;
|
2018-05-10 19:16:56 +10:00
|
|
|
import com.keylesspalace.tusky.util.EmojiCompatFont;
|
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;
|
2018-04-23 01:20:01 +10:00
|
|
|
import dagger.android.HasBroadcastReceiverInjector;
|
2018-04-14 06:37:21 +10:00
|
|
|
import dagger.android.HasServiceInjector;
|
2018-04-07 06:02:46 +10:00
|
|
|
import okhttp3.OkHttpClient;
|
2018-03-28 04:47:00 +11:00
|
|
|
|
2018-04-23 01:20:01 +10:00
|
|
|
public class TuskyApplication extends Application implements HasActivityInjector, HasServiceInjector, HasBroadcastReceiverInjector {
|
2018-03-28 04:47:00 +11:00
|
|
|
@Inject
|
|
|
|
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
|
|
|
|
@Inject
|
2018-04-14 06:37:21 +10:00
|
|
|
DispatchingAndroidInjector<Service> dispatchingServiceInjector;
|
|
|
|
@Inject
|
2018-04-23 01:20:01 +10:00
|
|
|
DispatchingAndroidInjector<BroadcastReceiver> dispatchingBroadcastReceiverInjector;
|
|
|
|
@Inject
|
2018-03-28 04:47:00 +11:00
|
|
|
NotificationPullJobCreator notificationPullJobCreator;
|
2018-07-24 05:59:10 +10:00
|
|
|
@Inject
|
|
|
|
OkHttpClient okHttpClient;
|
2017-06-29 03:33:54 +10:00
|
|
|
|
2018-07-24 05:59:10 +10:00
|
|
|
private AppDatabase appDatabase;
|
|
|
|
private AccountManager accountManager;
|
|
|
|
|
|
|
|
private ServiceLocator serviceLocator;
|
2017-07-07 08:27:51 +10:00
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
public static TuskyApplication getInstance(@NonNull Context context) {
|
|
|
|
return (TuskyApplication) context.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
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-07-24 05:59:10 +10:00
|
|
|
appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tuskyDB")
|
2018-03-28 04:47:00 +11:00
|
|
|
.allowMainThreadQueries()
|
2018-04-22 18:35:46 +10:00
|
|
|
.addMigrations(AppDatabase.MIGRATION_2_3, AppDatabase.MIGRATION_3_4, AppDatabase.MIGRATION_4_5, AppDatabase.MIGRATION_5_6, AppDatabase.MIGRATION_6_7)
|
2018-03-28 04:47:00 +11:00
|
|
|
.build();
|
2018-07-24 05:59:10 +10:00
|
|
|
accountManager = new AccountManager(appDatabase);
|
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;
|
2018-07-24 05:59:10 +10:00
|
|
|
} else if (clazz.equals(AppDatabase.class)) {
|
|
|
|
//noinspection unchecked
|
|
|
|
return (T) appDatabase;
|
2018-03-10 08:02:32 +11:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Unknown service " + clazz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-04-22 15:13:48 +10:00
|
|
|
|
2018-04-14 06:37:21 +10:00
|
|
|
initAppInjector();
|
2018-03-28 04:47:00 +11:00
|
|
|
initPicasso();
|
2018-05-10 19:16:56 +10:00
|
|
|
initEmojiCompat();
|
2017-12-02 22:22:52 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
JobManager.create(this).addJobCreator(notificationPullJobCreator);
|
2018-01-20 23:39:01 +11:00
|
|
|
|
2018-05-10 19:16:56 +10:00
|
|
|
//necessary for Android < API 21
|
2017-12-02 22:22:52 +11:00
|
|
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
2018-02-04 08:45:14 +11:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:16:56 +10: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-14 06:37:21 +10:00
|
|
|
protected void initAppInjector() {
|
|
|
|
AppInjector.INSTANCE.init(this);
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
protected void initPicasso() {
|
|
|
|
// Initialize Picasso configuration
|
|
|
|
Picasso.Builder builder = new Picasso.Builder(this);
|
2018-04-23 01:20:01 +10:00
|
|
|
builder.downloader(new OkHttp3Downloader(okHttpClient));
|
2018-03-10 08:02:32 +11:00
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
builder.listener((picasso, uri, exception) -> exception.printStackTrace());
|
|
|
|
}
|
2018-04-07 06:02:46 +10:00
|
|
|
|
|
|
|
Picasso.setSingletonInstance(builder.build());
|
2018-03-10 08:02:32 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-14 06:37:21 +10:00
|
|
|
@Override
|
|
|
|
public AndroidInjector<Service> serviceInjector() {
|
|
|
|
return dispatchingServiceInjector;
|
|
|
|
}
|
|
|
|
|
2018-04-23 01:20:01 +10:00
|
|
|
@Override
|
|
|
|
public AndroidInjector<BroadcastReceiver> broadcastReceiverInjector() {
|
|
|
|
return dispatchingBroadcastReceiverInjector;
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:02:32 +11:00
|
|
|
public interface ServiceLocator {
|
|
|
|
<T> T get(Class<T> clazz);
|
|
|
|
}
|
|
|
|
}
|