2015-05-01 09:36:10 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
import android.content.Context;
|
2015-05-23 00:45:12 +02:00
|
|
|
import android.content.SharedPreferences;
|
2015-06-06 00:10:38 +02:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Build.VERSION;
|
2015-05-23 00:45:12 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2015-06-21 10:18:41 +02:00
|
|
|
import android.util.Log;
|
2015-05-23 00:45:12 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-01 09:36:10 +02:00
|
|
|
|
2015-05-13 23:15:20 +02:00
|
|
|
import java.io.File;
|
2015-07-08 23:03:34 +02:00
|
|
|
import java.io.IOException;
|
2015-08-03 01:17:02 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
2015-05-13 23:15:20 +02:00
|
|
|
|
2015-05-30 17:28:03 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler;
|
2015-08-03 01:17:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
2015-07-08 23:03:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
2015-08-22 01:08:46 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-05-01 09:36:10 +02:00
|
|
|
public class GBApplication extends Application {
|
2015-08-03 01:17:02 +02:00
|
|
|
// Since this class must not log to slf4j, we use plain android.util.Log
|
|
|
|
private static final String TAG = "GBApplication";
|
2015-05-01 09:36:10 +02:00
|
|
|
private static GBApplication context;
|
2015-05-30 17:28:03 +02:00
|
|
|
private static ActivityDatabaseHandler mActivityDatabaseHandler;
|
2015-08-16 00:17:16 +02:00
|
|
|
private static final Lock dbLock = new ReentrantLock();
|
2015-08-21 00:58:18 +02:00
|
|
|
private static DeviceService deviceService;
|
2015-05-01 09:36:10 +02:00
|
|
|
|
|
|
|
public GBApplication() {
|
|
|
|
context = this;
|
2015-08-21 00:58:18 +02:00
|
|
|
deviceService = createDeviceService();
|
2015-07-25 00:07:33 +02:00
|
|
|
// don't do anything here, add it to onCreate instead
|
2015-05-01 09:36:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
protected DeviceService createDeviceService() {
|
2015-08-22 01:08:46 +02:00
|
|
|
return new GBDeviceService(this);
|
2015-08-21 00:58:18 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 23:15:20 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
|
2015-07-25 00:07:33 +02:00
|
|
|
// don't do anything here before we set up logging, otherwise
|
|
|
|
// slf4j may be implicitly initialized before we properly configured it.
|
2015-05-23 00:45:12 +02:00
|
|
|
setupLogging();
|
2015-05-13 23:15:20 +02:00
|
|
|
// For debugging problems with the logback configuration
|
|
|
|
// LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
|
|
|
|
// print logback's internal status
|
|
|
|
// StatusPrinter.print(lc);
|
|
|
|
// Logger logger = LoggerFactory.getLogger(GBApplication.class);
|
2015-06-05 21:46:56 +02:00
|
|
|
|
2015-08-22 01:08:46 +02:00
|
|
|
GB.environment = GBEnvironment.createDeviceEnvironment();
|
2015-07-25 00:07:33 +02:00
|
|
|
mActivityDatabaseHandler = new ActivityDatabaseHandler(context);
|
2015-06-05 21:46:56 +02:00
|
|
|
// for testing DB stuff
|
|
|
|
// SQLiteDatabase db = mActivityDatabaseHandler.getWritableDatabase();
|
|
|
|
// db.close();
|
2015-05-13 23:15:20 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 00:45:12 +02:00
|
|
|
public static boolean isFileLoggingEnabled() {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
|
2015-06-13 01:12:08 +02:00
|
|
|
return prefs.getBoolean("log_to_file", false);
|
2015-05-23 00:45:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setupLogging() {
|
2015-06-12 22:30:14 +02:00
|
|
|
if (isFileLoggingEnabled()) {
|
2015-07-08 23:03:34 +02:00
|
|
|
try {
|
|
|
|
File dir = FileUtils.getExternalFilesDir();
|
2015-06-21 10:18:41 +02:00
|
|
|
// used by assets/logback.xml since the location cannot be statically determined
|
|
|
|
System.setProperty("GB_LOGFILES_DIR", dir.getAbsolutePath());
|
2015-07-08 23:03:34 +02:00
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e("GBApplication", "External files dir not available, cannot log to file, ex");
|
2015-06-21 10:18:41 +02:00
|
|
|
System.setProperty("GB_LOGFILES_DIR", "/dev/null");
|
2015-06-12 22:30:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-23 00:45:12 +02:00
|
|
|
try {
|
|
|
|
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
|
|
|
root.detachAppender("FILE");
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
System.out.println("Error removing logger FILE appender");
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2015-05-13 23:15:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 09:36:10 +02:00
|
|
|
public static Context getContext() {
|
|
|
|
return context;
|
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
public static DeviceService deviceService() {
|
|
|
|
return deviceService;
|
|
|
|
}
|
|
|
|
|
2015-08-03 01:17:02 +02:00
|
|
|
/**
|
|
|
|
* Returns the DBHandler instance for reading/writing or throws GBException
|
|
|
|
* when that was not successful
|
|
|
|
* If acquiring was successful, callers must call #releaseDB when they
|
|
|
|
* are done (from the same thread that acquired the lock!
|
|
|
|
* @return the DBHandler
|
|
|
|
* @see #releaseDB()
|
|
|
|
* @throws GBException
|
|
|
|
*/
|
|
|
|
public static DBHandler acquireDB() throws GBException {
|
|
|
|
try {
|
|
|
|
if (dbLock.tryLock(30, TimeUnit.SECONDS)) {
|
|
|
|
return mActivityDatabaseHandler;
|
|
|
|
}
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.i(TAG, "Interrupted while waiting for DB lock");
|
|
|
|
}
|
|
|
|
throw new GBException("Unable to access the database.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the database lock.
|
|
|
|
* @throws IllegalMonitorStateException if the current thread is not owning the lock
|
|
|
|
* @see #acquireDB()
|
|
|
|
*/
|
|
|
|
public static void releaseDB() {
|
|
|
|
dbLock.unlock();
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
2015-06-06 00:10:38 +02:00
|
|
|
|
|
|
|
public static boolean isRunningLollipopOrLater() {
|
|
|
|
return VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
|
|
|
|
}
|
2015-05-01 09:36:10 +02:00
|
|
|
}
|