Twidere-App-Android-Twitter.../twidere/src/main/java/edu/tsinghua/hotmobi/HotMobiLogger.java

158 lines
5.6 KiB
Java
Raw Normal View History

2015-08-13 05:27:53 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package edu.tsinghua.hotmobi;
import android.app.Application;
2015-08-13 14:59:03 +02:00
import android.content.Context;
import android.content.SharedPreferences;
2016-10-10 03:30:00 +02:00
import android.support.annotation.NonNull;
2016-03-06 13:59:24 +01:00
import android.support.annotation.Nullable;
import android.text.TextUtils;
2016-01-03 05:43:08 +01:00
import android.util.Log;
2016-01-03 05:43:08 +01:00
import org.mariotaku.twidere.BuildConfig;
import org.mariotaku.twidere.Constants;
2016-03-07 18:25:18 +01:00
import org.mariotaku.twidere.model.UserKey;
2015-12-31 09:32:27 +01:00
import org.mariotaku.twidere.util.dagger.DependencyHolder;
2015-08-13 14:59:03 +02:00
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.UUID;
2015-08-13 05:27:53 +02:00
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
2015-08-13 05:27:53 +02:00
2015-12-31 09:32:27 +01:00
import javax.inject.Singleton;
2016-01-29 15:46:22 +01:00
import edu.tsinghua.hotmobi.model.LogModel;
2015-08-13 05:27:53 +02:00
/**
* Created by mariotaku on 15/8/10.
*/
2015-12-31 09:32:27 +01:00
@Singleton
2016-01-29 15:46:22 +01:00
public class HotMobiLogger implements HotMobiConstants {
2015-08-13 05:27:53 +02:00
public static final String LOGTAG = "HotMobiLogger";
public static final long UPLOAD_INTERVAL_MILLIS = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
final static SimpleDateFormat DATE_FORMAT;
static {
DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
private final Application mApplication;
2015-08-13 05:27:53 +02:00
private final Executor mExecutor;
public HotMobiLogger(Application application) {
mApplication = application;
2015-08-13 05:27:53 +02:00
mExecutor = Executors.newSingleThreadExecutor();
}
2016-01-29 15:46:22 +01:00
public static String getLogFilename(LogModel logModel) {
return logModel.getLogFileName();
2015-08-13 05:27:53 +02:00
}
2015-08-13 14:59:03 +02:00
public static String getInstallationSerialId(Context context) {
final SharedPreferences prefs = context.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME,
Context.MODE_PRIVATE);
final String persistedDeviceId = prefs.getString(Constants.KEY_DEVICE_SERIAL, null);
final String uuid;
if (!TextUtils.isEmpty(persistedDeviceId)) {
uuid = persistedDeviceId.replaceAll("[^\\w\\d]", "");
} else {
uuid = UUID.randomUUID().toString().replaceAll("[^\\w\\d]", "");
prefs.edit().putString(Constants.KEY_DEVICE_SERIAL, uuid).apply();
}
return uuid;
2015-08-13 14:59:03 +02:00
}
public static HotMobiLogger getInstance(Context context) {
2016-09-09 05:58:26 +02:00
return DependencyHolder.Companion.get(context).getHotMobiLogger();
2015-08-13 14:59:03 +02:00
}
2016-03-07 18:25:18 +01:00
public static File getLogFile(Context context, @Nullable UserKey accountKey, String type) {
final File logsDir = getLogsDir(context);
final File todayLogDir = new File(logsDir, DATE_FORMAT.format(new Date()));
if (!todayLogDir.exists()) {
todayLogDir.mkdirs();
}
final String logFilename;
2016-03-06 13:59:24 +01:00
if (accountKey != null) {
logFilename = type + "_" + accountKey + ".log";
} else {
logFilename = type + ".log";
}
return new File(todayLogDir, logFilename);
}
public static File getLogsDir(Context context) {
return new File(context.getFilesDir(), "hotmobi");
}
public static long getLastUploadTime(final Context context) {
2016-01-29 15:46:22 +01:00
final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
return prefs.getLong(KEY_LAST_UPLOAD_TIME, -1);
}
2016-02-11 16:33:38 +01:00
public static boolean printLog(final String msg) {
2016-01-03 05:43:08 +01:00
if (BuildConfig.DEBUG) {
final StackTraceElement ste = new Throwable().fillInStackTrace().getStackTrace()[1];
final String fullName = ste.getClassName();
final String name = fullName.substring(fullName.lastIndexOf('.'));
final String tag = name + "." + ste.getMethodName();
Log.d(tag, msg);
return true;
} else
return false;
}
2016-10-10 03:30:00 +02:00
public <T extends LogModel> void log(UserKey accountId, @NonNull final T event, final PreProcessing<T> preProcessing) {
if (!BuildConfig.HOTMOBI_LOG_ENABLED || !event.isEnabled()) return;
2015-12-25 10:54:13 +01:00
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, event, preProcessing));
}
2016-10-10 03:30:00 +02:00
public <T extends LogModel> void log(UserKey accountId, @NonNull final T event) {
2015-12-25 10:54:13 +01:00
log(accountId, event, null);
}
2016-10-10 03:30:00 +02:00
public <T extends LogModel> void log(@NonNull final T event) {
2015-12-25 10:54:13 +01:00
log(event, null);
}
2016-01-29 15:46:22 +01:00
public void log(final LogModel event, final PreProcessing preProcessing) {
2016-03-06 13:59:24 +01:00
log(null, event, preProcessing);
2015-12-25 10:54:13 +01:00
}
2016-03-07 18:25:18 +01:00
public <T extends LogModel> void logList(List<T> events, UserKey accountId, String type) {
2015-12-25 10:54:13 +01:00
logList(events, accountId, type, null);
}
2016-03-07 18:25:18 +01:00
public <T extends LogModel> void logList(List<T> events, UserKey accountId, String type, final PreProcessing<T> preProcessing) {
2016-08-18 15:51:12 +02:00
if (!BuildConfig.HOTMOBI_LOG_ENABLED) return;
2015-12-25 10:54:13 +01:00
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, type, events, preProcessing));
}
2015-11-11 09:53:27 +01:00
2015-08-13 05:27:53 +02:00
}