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-09-30 12:24:29 +02:00
|
|
|
import android.content.Intent;
|
2015-09-30 15:42:26 +02:00
|
|
|
import android.content.IntentFilter;
|
2015-08-20 04:19:02 +02:00
|
|
|
import android.content.SharedPreferences;
|
2015-08-13 14:59:03 +02:00
|
|
|
import android.location.Location;
|
2015-09-30 12:24:29 +02:00
|
|
|
import android.os.BatteryManager;
|
2015-08-20 04:19:02 +02:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import org.mariotaku.twidere.Constants;
|
2015-08-13 14:59:03 +02:00
|
|
|
import org.mariotaku.twidere.app.TwidereApplication;
|
|
|
|
import org.mariotaku.twidere.util.Utils;
|
|
|
|
|
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-09-30 12:24:29 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.BatteryRecord;
|
2015-08-13 14:59:03 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.LatLng;
|
2015-08-28 12:16:41 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.LinkEvent;
|
2015-08-20 04:19:02 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.MediaEvent;
|
2015-08-28 12:16:41 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.NetworkEvent;
|
2015-08-13 05:27:53 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.RefreshEvent;
|
2015-08-28 12:16:41 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.ScrollRecord;
|
2015-08-13 05:27:53 +02:00
|
|
|
import edu.tsinghua.hotmobi.model.SessionEvent;
|
|
|
|
import edu.tsinghua.hotmobi.model.TweetEvent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mariotaku on 15/8/10.
|
|
|
|
*/
|
|
|
|
public class HotMobiLogger {
|
|
|
|
|
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);
|
|
|
|
public static final String LAST_UPLOAD_TIME = "last_upload_time";
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-08-28 12:16:41 +02:00
|
|
|
public static String getLogFilename(Object event) {
|
2015-08-20 04:19:02 +02:00
|
|
|
if (event instanceof RefreshEvent) {
|
2015-08-13 05:27:53 +02:00
|
|
|
return "refresh";
|
|
|
|
} else if (event instanceof SessionEvent) {
|
|
|
|
return "session";
|
|
|
|
} else if (event instanceof TweetEvent) {
|
|
|
|
return "tweet";
|
2015-08-20 04:19:02 +02:00
|
|
|
} else if (event instanceof MediaEvent) {
|
|
|
|
return "media";
|
2015-08-28 12:16:41 +02:00
|
|
|
} else if (event instanceof LinkEvent) {
|
|
|
|
return "link";
|
|
|
|
} else if (event instanceof NetworkEvent) {
|
|
|
|
return "network";
|
|
|
|
} else if (event instanceof ScrollRecord) {
|
|
|
|
return "scroll";
|
2015-09-30 12:24:29 +02:00
|
|
|
} else if (event instanceof BatteryRecord) {
|
|
|
|
return "battery";
|
2015-08-13 05:27:53 +02:00
|
|
|
}
|
2015-08-28 12:16:41 +02:00
|
|
|
throw new UnsupportedOperationException("Unknown event type " + event);
|
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) {
|
|
|
|
return ((TwidereApplication) context.getApplicationContext()).getHotMobiLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static LatLng getCachedLatLng(Context context) {
|
|
|
|
final Location location = Utils.getCachedLocation(context);
|
|
|
|
if (location == null) return null;
|
|
|
|
return new LatLng(location.getLatitude(), location.getLongitude());
|
|
|
|
}
|
2015-08-20 04:19:02 +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) {
|
|
|
|
final SharedPreferences prefs = context.getSharedPreferences("spice_data_profiling", Context.MODE_PRIVATE);
|
|
|
|
return prefs.getLong(LAST_UPLOAD_TIME, -1);
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:42:26 +02:00
|
|
|
public static void logPowerBroadcast(Context context) {
|
|
|
|
logPowerBroadcast(context, context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)));
|
|
|
|
}
|
|
|
|
|
2015-09-30 12:24:29 +02:00
|
|
|
public static void logPowerBroadcast(Context context, Intent intent) {
|
2015-09-30 15:42:26 +02:00
|
|
|
if (intent == null) return;
|
2015-09-30 12:24:29 +02:00
|
|
|
if (!intent.hasExtra(BatteryManager.EXTRA_LEVEL) || !intent.hasExtra(BatteryManager.EXTRA_SCALE) ||
|
|
|
|
!intent.hasExtra(BatteryManager.EXTRA_STATUS)) return;
|
|
|
|
final BatteryRecord record = new BatteryRecord();
|
|
|
|
record.setLevel(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) / (float)
|
|
|
|
intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1));
|
|
|
|
record.setState(intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1));
|
|
|
|
record.setTimestamp(System.currentTimeMillis());
|
|
|
|
record.setTimeOffset(TimeZone.getDefault().getRawOffset());
|
|
|
|
getInstance(context).log(record);
|
|
|
|
}
|
|
|
|
|
2015-08-28 12:16:41 +02:00
|
|
|
public void log(long accountId, final Object event) {
|
|
|
|
mExecutor.execute(new WriteLogTask(mApplication, accountId, event));
|
2015-08-20 04:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void log(Object event) {
|
|
|
|
log(ACCOUNT_ID_NOT_NEEDED, event);
|
|
|
|
}
|
2015-08-28 12:16:41 +02:00
|
|
|
|
|
|
|
public void logList(List<?> events, long accountId, String type) {
|
|
|
|
mExecutor.execute(new WriteLogTask(mApplication, accountId, type, events));
|
|
|
|
}
|
2015-08-13 05:27:53 +02:00
|
|
|
}
|