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;
|
|
|
|
|
2015-08-28 12:16:41 +02:00
|
|
|
import android.app.Application;
|
2015-08-13 14:59:03 +02:00
|
|
|
import android.content.Context;
|
2015-08-20 04:19:02 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.text.TextUtils;
|
2016-01-03 05:43:08 +01:00
|
|
|
import android.util.Log;
|
2015-08-20 04:19:02 +02:00
|
|
|
|
2016-01-03 05:43:08 +01:00
|
|
|
import org.mariotaku.twidere.BuildConfig;
|
2015-08-20 04:19:02 +02:00
|
|
|
import org.mariotaku.twidere.Constants;
|
2015-12-31 09:32:27 +01:00
|
|
|
import org.mariotaku.twidere.util.dagger.DependencyHolder;
|
2015-08-13 14:59:03 +02:00
|
|
|
|
2015-08-28 12:16:41 +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;
|
2015-08-20 04:19:02 +02:00
|
|
|
import java.util.UUID;
|
2015-08-13 05:27:53 +02:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executors;
|
2015-08-28 12:16:41 +02:00
|
|
|
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
|
|
|
|
2015-08-20 04:19:02 +02:00
|
|
|
public static final long ACCOUNT_ID_NOT_NEEDED = -1;
|
|
|
|
|
2015-08-28 12:16:41 +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"));
|
|
|
|
}
|
2015-08-20 04:19:02 +02:00
|
|
|
|
2015-08-28 12:16:41 +02:00
|
|
|
private final Application mApplication;
|
2015-08-13 05:27:53 +02:00
|
|
|
private final Executor mExecutor;
|
|
|
|
|
2015-08-28 12:16:41 +02:00
|
|
|
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
|
|
|
|
2015-08-20 04:19:02 +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-01-01 03:49:00 +01:00
|
|
|
return DependencyHolder.get(context).getHotMobiLogger();
|
2015-08-13 14:59:03 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 12:16:41 +02:00
|
|
|
public static File getLogFile(Context context, long accountId, 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;
|
|
|
|
if (accountId > 0) {
|
|
|
|
logFilename = type + "_" + accountId + ".log";
|
|
|
|
} else {
|
|
|
|
logFilename = type + ".log";
|
|
|
|
}
|
|
|
|
return new File(todayLogDir, logFilename);
|
|
|
|
}
|
2015-08-20 04:19:02 +02:00
|
|
|
|
2015-08-28 12:16:41 +02:00
|
|
|
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);
|
2016-02-04 09:42:42 +01:00
|
|
|
return prefs.getLong(KEY_LAST_UPLOAD_TIME, -1);
|
2015-08-28 12:16:41 +02:00
|
|
|
}
|
|
|
|
|
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-01-29 15:46:22 +01:00
|
|
|
public <T extends LogModel> void log(long accountId, final T event, final PreProcessing<T> preProcessing) {
|
2015-12-25 10:54:13 +01:00
|
|
|
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, event, preProcessing));
|
2015-08-20 04:19:02 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 15:46:22 +01:00
|
|
|
public <T extends LogModel> void log(long accountId, final T event) {
|
2015-12-25 10:54:13 +01:00
|
|
|
log(accountId, event, null);
|
2015-08-20 04:19:02 +02:00
|
|
|
}
|
2015-08-28 12:16:41 +02:00
|
|
|
|
2016-01-29 15:46:22 +01:00
|
|
|
public <T extends LogModel> void log(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) {
|
2015-12-25 10:54:13 +01:00
|
|
|
log(ACCOUNT_ID_NOT_NEEDED, event, preProcessing);
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:46:22 +01:00
|
|
|
public <T extends LogModel> void logList(List<T> events, long accountId, String type) {
|
2015-12-25 10:54:13 +01:00
|
|
|
logList(events, accountId, type, null);
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:46:22 +01:00
|
|
|
public <T extends LogModel> void logList(List<T> events, long accountId, String type, final PreProcessing<T> preProcessing) {
|
2015-12-25 10:54:13 +01:00
|
|
|
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, type, events, preProcessing));
|
2015-08-28 12:16:41 +02:00
|
|
|
}
|
2015-11-11 09:53:27 +01:00
|
|
|
|
2015-08-13 05:27:53 +02:00
|
|
|
}
|