2016-06-05 01:40:35 +02:00
|
|
|
package info.nightscout.androidaps;
|
|
|
|
|
2020-04-02 18:48:37 +02:00
|
|
|
import android.bluetooth.BluetoothDevice;
|
2020-01-03 14:30:39 +01:00
|
|
|
import android.content.Intent;
|
2017-07-24 01:50:27 +02:00
|
|
|
import android.content.IntentFilter;
|
2016-06-24 17:16:17 +02:00
|
|
|
import android.content.res.Resources;
|
2020-03-10 22:20:36 +01:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.wifi.WifiManager;
|
2019-07-05 01:22:45 +02:00
|
|
|
|
2019-12-24 16:07:17 +01:00
|
|
|
import androidx.annotation.StringRes;
|
2019-05-16 13:57:37 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
|
|
|
import com.j256.ormlite.android.apptools.OpenHelperManager;
|
|
|
|
|
2017-12-22 02:55:40 +01:00
|
|
|
import net.danlew.android.joda.JodaTimeAndroid;
|
|
|
|
|
2020-03-25 23:51:04 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-12-23 13:27:15 +01:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-12-13 02:12:19 +01:00
|
|
|
import dagger.android.AndroidInjector;
|
2019-12-21 20:17:08 +01:00
|
|
|
import dagger.android.DaggerApplication;
|
2016-08-05 23:54:03 +02:00
|
|
|
import info.nightscout.androidaps.db.DatabaseHelper;
|
2019-12-13 02:12:19 +01:00
|
|
|
import info.nightscout.androidaps.dependencyInjection.DaggerAppComponent;
|
2020-03-25 23:51:04 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
2019-12-28 01:52:20 +01:00
|
|
|
import info.nightscout.androidaps.logging.AAPSLogger;
|
|
|
|
import info.nightscout.androidaps.logging.LTag;
|
2019-02-28 23:16:50 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
|
2020-03-16 21:40:29 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.PluginStore;
|
2020-01-03 16:29:59 +01:00
|
|
|
import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtils;
|
2019-02-28 23:16:50 +01:00
|
|
|
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
|
2020-04-02 18:48:37 +02:00
|
|
|
import info.nightscout.androidaps.receivers.BTReceiver;
|
2020-03-21 17:04:09 +01:00
|
|
|
import info.nightscout.androidaps.receivers.ChargingStateReceiver;
|
2017-07-24 01:50:27 +02:00
|
|
|
import info.nightscout.androidaps.receivers.DataReceiver;
|
2016-12-16 02:00:53 +01:00
|
|
|
import info.nightscout.androidaps.receivers.KeepAliveReceiver;
|
2020-03-10 22:20:36 +01:00
|
|
|
import info.nightscout.androidaps.receivers.NetworkChangeReceiver;
|
2019-07-18 19:09:49 +02:00
|
|
|
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
|
2018-07-29 15:37:28 +02:00
|
|
|
import info.nightscout.androidaps.services.Intents;
|
2019-12-13 02:12:19 +01:00
|
|
|
import info.nightscout.androidaps.utils.ActivityMonitor;
|
2019-10-25 11:38:23 +02:00
|
|
|
import info.nightscout.androidaps.utils.LocaleHelper;
|
2020-03-16 22:49:34 +01:00
|
|
|
import info.nightscout.androidaps.utils.sharedPreferences.SP;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2019-12-21 20:17:08 +01:00
|
|
|
public class MainApp extends DaggerApplication {
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2019-12-21 20:17:08 +01:00
|
|
|
static MainApp sInstance;
|
2020-01-02 23:25:29 +01:00
|
|
|
private static Resources sResources;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2019-12-21 20:17:08 +01:00
|
|
|
static DatabaseHelper sDatabaseHelper = null;
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2020-03-16 21:40:29 +01:00
|
|
|
@Inject PluginStore pluginStore;
|
2019-12-31 09:41:02 +01:00
|
|
|
@Inject AAPSLogger aapsLogger;
|
|
|
|
@Inject ActivityMonitor activityMonitor;
|
2020-01-03 16:29:59 +01:00
|
|
|
@Inject VersionCheckerUtils versionCheckersUtils;
|
2020-03-16 22:49:34 +01:00
|
|
|
@Inject SP sp;
|
2019-12-27 04:17:49 +01:00
|
|
|
|
2019-12-28 15:00:16 +01:00
|
|
|
@Inject ConfigBuilderPlugin configBuilderPlugin;
|
2020-01-06 15:22:28 +01:00
|
|
|
@Inject KeepAliveReceiver.KeepAliveManager keepAliveManager;
|
2020-03-25 23:51:04 +01:00
|
|
|
@Inject List<PluginBase> plugins;
|
2019-12-23 13:27:15 +01:00
|
|
|
|
2016-06-05 01:40:35 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
2019-12-13 02:12:19 +01:00
|
|
|
|
2020-03-16 22:49:34 +01:00
|
|
|
aapsLogger.debug("onCreate");
|
2018-02-22 13:30:36 +01:00
|
|
|
sInstance = this;
|
|
|
|
sResources = getResources();
|
2019-10-25 11:38:23 +02:00
|
|
|
LocaleHelper.INSTANCE.update(this);
|
2018-03-31 00:36:03 +02:00
|
|
|
sDatabaseHelper = OpenHelperManager.getHelper(sInstance, DatabaseHelper.class);
|
2018-02-22 13:30:36 +01:00
|
|
|
|
2019-11-12 13:44:02 +01:00
|
|
|
Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> {
|
|
|
|
if (ex instanceof InternalError) {
|
|
|
|
// usually the app trying to spawn a thread while being killed
|
|
|
|
return;
|
|
|
|
}
|
2020-04-01 16:13:08 +02:00
|
|
|
aapsLogger.error("Uncaught exception crashing app", ex);
|
2019-11-12 13:44:02 +01:00
|
|
|
});
|
2019-08-22 11:12:15 +02:00
|
|
|
|
2019-12-31 09:41:02 +01:00
|
|
|
registerActivityLifecycleCallbacks(activityMonitor);
|
2019-10-15 23:50:09 +02:00
|
|
|
|
2017-12-22 02:55:40 +01:00
|
|
|
JodaTimeAndroid.init(this);
|
2018-02-22 13:30:36 +01:00
|
|
|
|
2020-03-16 22:49:34 +01:00
|
|
|
aapsLogger.debug("Version: " + BuildConfig.VERSION_NAME);
|
|
|
|
aapsLogger.debug("BuildVersion: " + BuildConfig.BUILDVERSION);
|
|
|
|
aapsLogger.debug("Remote: " + BuildConfig.REMOTE);
|
2016-06-05 01:40:35 +02:00
|
|
|
|
2017-07-24 01:50:27 +02:00
|
|
|
registerLocalBroadcastReceiver();
|
|
|
|
|
2019-04-22 14:10:12 +02:00
|
|
|
//trigger here to see the new version on app start after an update
|
2020-01-03 16:29:59 +01:00
|
|
|
versionCheckersUtils.triggerCheckVersion();
|
2019-04-22 14:10:12 +02:00
|
|
|
|
2020-01-10 23:14:58 +01:00
|
|
|
// Register all tabs in app here
|
2020-03-25 23:51:04 +01:00
|
|
|
pluginStore.setPlugins(plugins);
|
2020-01-10 23:14:58 +01:00
|
|
|
configBuilderPlugin.initialize();
|
2018-03-19 17:06:22 +01:00
|
|
|
|
2018-07-10 17:55:50 +02:00
|
|
|
NSUpload.uploadAppStart();
|
2017-08-23 13:28:07 +02:00
|
|
|
|
2020-01-06 15:22:28 +01:00
|
|
|
new Thread(() -> keepAliveManager.setAlarm(this)).start();
|
2019-12-13 13:12:36 +01:00
|
|
|
doMigrations();
|
|
|
|
}
|
|
|
|
|
2019-12-27 04:17:49 +01:00
|
|
|
|
2019-12-13 13:12:36 +01:00
|
|
|
private void doMigrations() {
|
|
|
|
|
2017-07-24 01:50:27 +02:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:17:08 +01:00
|
|
|
@Override
|
|
|
|
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
|
|
|
|
return DaggerAppComponent
|
|
|
|
.builder()
|
|
|
|
.application(this)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2017-07-24 01:50:27 +02:00
|
|
|
private void registerLocalBroadcastReceiver() {
|
2020-03-31 09:48:46 +02:00
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(Intents.ACTION_NEW_TREATMENT);
|
|
|
|
filter.addAction(Intents.ACTION_CHANGED_TREATMENT);
|
|
|
|
filter.addAction(Intents.ACTION_REMOVED_TREATMENT);
|
|
|
|
filter.addAction(Intents.ACTION_NEW_SGV);
|
|
|
|
filter.addAction(Intents.ACTION_NEW_PROFILE);
|
|
|
|
filter.addAction(Intents.ACTION_NEW_MBG);
|
|
|
|
filter.addAction(Intents.ACTION_NEW_CAL);
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(new DataReceiver(), filter);
|
|
|
|
|
|
|
|
filter = new IntentFilter();
|
|
|
|
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
|
|
|
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
|
|
|
registerReceiver(new TimeDateOrTZChangeReceiver(), filter);
|
|
|
|
|
|
|
|
filter = new IntentFilter();
|
|
|
|
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
|
|
|
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
|
|
|
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
|
|
|
|
registerReceiver(new NetworkChangeReceiver(), filter);
|
|
|
|
|
2020-03-31 20:22:32 +02:00
|
|
|
filter = new IntentFilter();
|
|
|
|
filter.addAction(Intent.ACTION_POWER_CONNECTED);
|
|
|
|
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
|
|
|
|
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
|
|
|
registerReceiver(new ChargingStateReceiver(), filter);
|
2020-04-02 18:48:37 +02:00
|
|
|
|
|
|
|
filter = new IntentFilter();
|
|
|
|
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
|
|
|
|
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
|
|
|
|
registerReceiver(new BTReceiver(), filter);
|
2016-12-16 02:00:53 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
@Deprecated
|
2019-12-24 16:07:17 +01:00
|
|
|
public static String gs(@StringRes int id) {
|
2018-01-14 21:42:36 +01:00
|
|
|
return sResources.getString(id);
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
@Deprecated
|
2016-06-05 01:40:35 +02:00
|
|
|
public static MainApp instance() {
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DatabaseHelper getDbHelper() {
|
2016-07-07 10:34:20 +02:00
|
|
|
return sDatabaseHelper;
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTerminate() {
|
2019-12-28 01:52:20 +01:00
|
|
|
aapsLogger.debug(LTag.CORE, "onTerminate");
|
2019-12-31 09:41:02 +01:00
|
|
|
unregisterActivityLifecycleCallbacks(activityMonitor);
|
2020-01-06 15:22:28 +01:00
|
|
|
keepAliveManager.cancelAlarm(this);
|
2019-12-24 16:07:17 +01:00
|
|
|
super.onTerminate();
|
2016-06-05 01:40:35 +02:00
|
|
|
}
|
2016-12-16 02:00:53 +01:00
|
|
|
}
|