2016-02-24 14:56:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package at.bitfire.davdroid;
|
|
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
|
import android.content.SharedPreferences;
|
2016-02-24 22:08:19 +00:00
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
2016-02-24 14:56:30 +00:00
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
|
import java.util.logging.Handler;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
|
|
|
|
|
|
|
import at.bitfire.davdroid.log.LogcatHandler;
|
2016-02-24 22:08:19 +00:00
|
|
|
|
import at.bitfire.davdroid.log.PlainTextFormatter;
|
2016-02-24 14:56:30 +00:00
|
|
|
|
import de.duenndns.ssl.MemorizingTrustManager;
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import okhttp3.internal.tls.OkHostnameVerifier;
|
|
|
|
|
|
2016-03-18 16:24:46 +00:00
|
|
|
|
public class App extends Application implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2016-02-24 14:56:30 +00:00
|
|
|
|
public static final String
|
2016-03-18 16:24:46 +00:00
|
|
|
|
PREF_FILE = "davdroid_preferences",
|
2016-02-24 22:08:19 +00:00
|
|
|
|
PREF_LOG_TO_FILE = "log_to_file";
|
2016-02-24 14:56:30 +00:00
|
|
|
|
|
2016-03-18 16:24:46 +00:00
|
|
|
|
@Getter
|
|
|
|
|
private static MemorizingTrustManager memorizingTrustManager;
|
|
|
|
|
|
2016-02-24 14:56:30 +00:00
|
|
|
|
@Getter
|
|
|
|
|
private static SSLSocketFactoryCompat sslSocketFactoryCompat;
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
private static HostnameVerifier hostnameVerifier;
|
|
|
|
|
|
|
|
|
|
public final static Logger log = Logger.getLogger("davdroid");
|
|
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
|
private SharedPreferences preferences;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
super.onCreate();
|
|
|
|
|
|
|
|
|
|
preferences = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
|
2016-03-18 16:24:46 +00:00
|
|
|
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
2016-02-24 14:56:30 +00:00
|
|
|
|
|
|
|
|
|
// initialize MemorizingTrustManager
|
2016-03-18 16:24:46 +00:00
|
|
|
|
memorizingTrustManager = new MemorizingTrustManager(this);
|
|
|
|
|
sslSocketFactoryCompat = new SSLSocketFactoryCompat(memorizingTrustManager);
|
|
|
|
|
hostnameVerifier = memorizingTrustManager.wrapHostnameVerifier(OkHostnameVerifier.INSTANCE);
|
2016-02-24 14:56:30 +00:00
|
|
|
|
|
|
|
|
|
reinitLogger();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-18 16:24:46 +00:00
|
|
|
|
// won't be called in production
|
|
|
|
|
@Override
|
|
|
|
|
public void onTerminate() {
|
|
|
|
|
super.onTerminate();
|
|
|
|
|
preferences.unregisterOnSharedPreferenceChangeListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
|
|
|
if (PREF_LOG_TO_FILE.equals(key)) {
|
|
|
|
|
log.info("Logging preferences changed, initializing logger again");
|
|
|
|
|
reinitLogger();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-24 14:56:30 +00:00
|
|
|
|
|
2016-03-18 16:24:46 +00:00
|
|
|
|
private void reinitLogger() {
|
2016-02-24 14:56:30 +00:00
|
|
|
|
// don't use Android default logging, we have our own handlers
|
|
|
|
|
log.setUseParentHandlers(false);
|
|
|
|
|
|
2016-02-24 22:08:19 +00:00
|
|
|
|
boolean logToFile = preferences.getBoolean(PREF_LOG_TO_FILE, false),
|
|
|
|
|
logVerbose = logToFile || Log.isLoggable(log.getName(), Log.DEBUG);
|
|
|
|
|
|
2016-02-24 14:56:30 +00:00
|
|
|
|
// set logging level according to preferences
|
2016-02-24 22:08:19 +00:00
|
|
|
|
log.setLevel(logVerbose ? Level.ALL : Level.INFO);
|
2016-02-24 14:56:30 +00:00
|
|
|
|
|
|
|
|
|
// remove all handlers
|
|
|
|
|
for (Handler handler : log.getHandlers())
|
|
|
|
|
log.removeHandler(handler);
|
|
|
|
|
|
|
|
|
|
// add logcat handler
|
|
|
|
|
log.addHandler(LogcatHandler.INSTANCE);
|
|
|
|
|
|
|
|
|
|
// log to external file according to preferences
|
2016-02-24 22:08:19 +00:00
|
|
|
|
if (logToFile) {
|
2016-02-24 14:56:30 +00:00
|
|
|
|
File dir = getExternalFilesDir(null);
|
|
|
|
|
if (dir != null)
|
|
|
|
|
try {
|
2016-02-24 22:08:19 +00:00
|
|
|
|
String pattern = new File(dir, "davdroid%u-" + DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMdd-HHmmss") + ".txt").toString();
|
2016-02-24 14:56:30 +00:00
|
|
|
|
log.info("Logging to external file: " + pattern);
|
|
|
|
|
|
|
|
|
|
FileHandler fileHandler = new FileHandler(pattern);
|
2016-02-24 22:08:19 +00:00
|
|
|
|
fileHandler.setFormatter(PlainTextFormatter.DEFAULT);
|
2016-02-24 14:56:30 +00:00
|
|
|
|
log.addHandler(fileHandler);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.log(Level.SEVERE, "Can't create external log file", e);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
log.severe("No external media found, can't create external log file");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|