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

216 lines
8.5 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;
2015-09-30 12:24:29 +02:00
import android.content.Intent;
2015-09-30 15:42:26 +02:00
import android.content.IntentFilter;
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;
import android.text.TextUtils;
import org.mariotaku.twidere.Constants;
2015-10-19 11:28:16 +02:00
import org.mariotaku.twidere.app.TwidereApplication;
2015-11-03 10:52:23 +01:00
import org.mariotaku.twidere.util.JsonSerializer;
2015-08-13 14:59:03 +02:00
import org.mariotaku.twidere.util.Utils;
2015-10-09 09:58:36 +02:00
import org.mariotaku.twidere.util.dagger.ApplicationModule;
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-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;
import edu.tsinghua.hotmobi.model.LinkEvent;
import edu.tsinghua.hotmobi.model.MediaEvent;
import edu.tsinghua.hotmobi.model.NetworkEvent;
import edu.tsinghua.hotmobi.model.NotificationEvent;
2015-08-13 05:27:53 +02:00
import edu.tsinghua.hotmobi.model.RefreshEvent;
2015-11-11 09:53:27 +01:00
import edu.tsinghua.hotmobi.model.ScreenEvent;
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 {
public static final long ACCOUNT_ID_NOT_NEEDED = -1;
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";
2015-11-03 10:52:23 +01:00
public static final String FALLBACK_CACHED_LOCATION = "fallback_cached_location";
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();
}
public static String getLogFilename(Object event) {
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";
} else if (event instanceof MediaEvent) {
return "media";
} 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";
} else if (event instanceof NotificationEvent) {
return "notification";
2015-11-11 10:49:05 +01:00
} else if (event instanceof ScreenEvent) {
return "screen";
2015-08-13 05:27:53 +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
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) {
2015-10-09 09:58:36 +02:00
return ApplicationModule.get(context).getHotMobiLogger();
2015-08-13 14:59:03 +02:00
}
public static LatLng getCachedLatLng(Context context) {
final Location location = Utils.getCachedLocation(context);
2015-11-03 10:52:23 +01:00
if (location == null) {
return getFallbackCachedLocation(context);
}
final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
final SharedPreferences prefs = context.getSharedPreferences("spice_data_profiling", Context.MODE_PRIVATE);
prefs.edit().putString(FALLBACK_CACHED_LOCATION, JsonSerializer.serialize(latLng, LatLng.class)).apply();
return latLng;
}
private static LatLng getFallbackCachedLocation(Context context) {
final SharedPreferences prefs = context.getSharedPreferences("spice_data_profiling", Context.MODE_PRIVATE);
return JsonSerializer.parse(prefs.getString(FALLBACK_CACHED_LOCATION, null), LatLng.class);
2015-08-13 14:59:03 +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);
}
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) {
2015-10-19 11:28:16 +02:00
final TwidereApplication app = TwidereApplication.getInstance(context);
logPowerBroadcast(context, app.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)));
2015-09-30 15:42:26 +02:00
}
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());
2015-12-25 10:54:13 +01:00
getInstance(context).log(record, null);
2015-09-30 12:24:29 +02:00
}
2015-12-25 10:54:13 +01:00
public <T> void log(long accountId, final T event, final PreProcessing<T> preProcessing) {
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, event, preProcessing));
}
2015-12-25 10:54:13 +01:00
public <T> void log(long accountId, final T event) {
log(accountId, event, null);
}
2015-12-25 10:54:13 +01:00
public <T> void log(final T event) {
log(event, null);
}
public void log(final Object event, final PreProcessing preProcessing) {
log(ACCOUNT_ID_NOT_NEEDED, event, preProcessing);
}
public <T> void logList(List<T> events, long accountId, String type) {
logList(events, accountId, type, null);
}
public <T> void logList(List<T> events, long accountId, String type, final PreProcessing<T> preProcessing) {
mExecutor.execute(new WriteLogTask<>(mApplication, accountId, type, events, preProcessing));
}
2015-11-11 09:53:27 +01:00
2015-11-20 09:22:16 +01:00
public static void logScreenEvent(Context context, ScreenEvent.Action action, long presentDuration) {
2015-12-25 10:54:13 +01:00
getInstance(context).log(ScreenEvent.create(context, action, presentDuration), null);
2015-11-11 09:53:27 +01:00
}
2015-12-25 10:54:13 +01:00
2015-08-13 05:27:53 +02:00
}