Add statics:
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package com.stardust.scriptdroid.statics;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import com.pushtorefresh.storio.sqlite.SQLiteTypeMapping;
|
||||
import com.pushtorefresh.storio.sqlite.StorIOSQLite;
|
||||
import com.pushtorefresh.storio.sqlite.impl.DefaultStorIOSQLite;
|
||||
import com.pushtorefresh.storio.sqlite.queries.DeleteQuery;
|
||||
import com.pushtorefresh.storio.sqlite.queries.Query;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/5.
|
||||
*/
|
||||
|
||||
public class SQLiteStaticsStorage implements ScriptStaticsStorage {
|
||||
|
||||
private static final int VERSION = 1;
|
||||
private static final String DATABASE_NAME = "Ever.db";
|
||||
static final String TABLE_NAME = "FinalEating";
|
||||
|
||||
private StorIOSQLite mStorIOSQLite;
|
||||
|
||||
public SQLiteStaticsStorage(Context context) {
|
||||
mStorIOSQLite = DefaultStorIOSQLite.builder()
|
||||
.sqliteOpenHelper(new SQLiteOpenHelper(context))
|
||||
.addTypeMapping(ScriptStaticsRecord.class, new ScriptStaticsRecordSQLiteTypeMapping())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void record(ScriptSource source) {
|
||||
int times = getTimes(source) + 1;
|
||||
mStorIOSQLite.put()
|
||||
.object(new ScriptStaticsRecord(source.toString(), times))
|
||||
.prepare()
|
||||
.executeAsBlocking();
|
||||
|
||||
}
|
||||
|
||||
public int getTimes(ScriptSource source) {
|
||||
ScriptStaticsRecord record = mStorIOSQLite.get()
|
||||
.object(ScriptStaticsRecord.class)
|
||||
.withQuery(Query.builder()
|
||||
.table(TABLE_NAME)
|
||||
.where("name = ?")
|
||||
.whereArgs(source.toString())
|
||||
.build())
|
||||
.prepare()
|
||||
.executeAsBlocking();
|
||||
if (record != null) {
|
||||
return record.times;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getAll() {
|
||||
List<ScriptStaticsRecord> records = mStorIOSQLite.get()
|
||||
.listOfObjects(ScriptStaticsRecord.class)
|
||||
.withQuery(Query.builder()
|
||||
.table(TABLE_NAME)
|
||||
.orderBy("times")
|
||||
.build())
|
||||
.prepare()
|
||||
.executeAsBlocking();
|
||||
return toMap(records);
|
||||
}
|
||||
|
||||
private Map<String, String> toMap(List<ScriptStaticsRecord> records) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
for (ScriptStaticsRecord record : records) {
|
||||
map.put(record.name, String.valueOf(record.times));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMax(int size) {
|
||||
List<ScriptStaticsRecord> records = mStorIOSQLite.get()
|
||||
.listOfObjects(ScriptStaticsRecord.class)
|
||||
.withQuery(Query.builder()
|
||||
.table(TABLE_NAME)
|
||||
.orderBy("times DESC")
|
||||
.limit(size)
|
||||
.build())
|
||||
.prepare()
|
||||
.executeAsBlocking();
|
||||
return toMap(records);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
mStorIOSQLite.delete()
|
||||
.byQuery(DeleteQuery.builder()
|
||||
.table(TABLE_NAME)
|
||||
.build())
|
||||
.prepare()
|
||||
.executeAsBlocking();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
mStorIOSQLite.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static class SQLiteOpenHelper extends android.database.sqlite.SQLiteOpenHelper {
|
||||
|
||||
|
||||
SQLiteOpenHelper(Context context) {
|
||||
super(context, DATABASE_NAME, null, VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(\n"
|
||||
+ "name TEXT NOT NULL PRIMARY KEY, "
|
||||
+ "times INTEGER"
|
||||
+ ");");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.stardust.scriptdroid.statics;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.flurry.android.FlurryAgent;
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
import com.stardust.scriptdroid.BuildConfig;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/5.
|
||||
*/
|
||||
|
||||
public class ScriptStatics {
|
||||
|
||||
|
||||
private static final String KEY_MILLIS = "Sorry, I should have left";
|
||||
private static ScriptStaticsStorage storage;
|
||||
private static SharedPreferences preferences;
|
||||
|
||||
public static void init(Context context) {
|
||||
storage = new SQLiteStaticsStorage(context);
|
||||
new FlurryAgent.Builder()
|
||||
.withLogEnabled(BuildConfig.DEBUG)
|
||||
.build(context, "D42MH48ZN4PJC5TKNYZD");
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
}
|
||||
|
||||
public static void recordScript(ScriptSource source) {
|
||||
storage.record(source);
|
||||
sendStaticsIfNeeded();
|
||||
}
|
||||
|
||||
private static void sendStaticsIfNeeded() {
|
||||
long millis = preferences.getLong(KEY_MILLIS, 0);
|
||||
if (!DateUtils.isToday(millis)) {
|
||||
preferences.edit().putLong(KEY_MILLIS, System.currentTimeMillis()).apply();
|
||||
FlurryAgent.logEvent("ScriptStatics", storage.getMax(10));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
storage.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.stardust.scriptdroid.statics;
|
||||
|
||||
import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn;
|
||||
import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteCreator;
|
||||
import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/5.
|
||||
*/
|
||||
|
||||
@StorIOSQLiteType(table = SQLiteStaticsStorage.TABLE_NAME)
|
||||
|
||||
public class ScriptStaticsRecord {
|
||||
|
||||
@StorIOSQLiteColumn(name = "name", key = true)
|
||||
public String name;
|
||||
|
||||
@StorIOSQLiteColumn(name = "times")
|
||||
public int times;
|
||||
|
||||
public ScriptStaticsRecord(String name, int times) {
|
||||
this.name = name;
|
||||
this.times = times;
|
||||
}
|
||||
|
||||
public ScriptStaticsRecord() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.stardust.scriptdroid.statics;
|
||||
|
||||
import com.stardust.autojs.script.ScriptSource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/5/5.
|
||||
*/
|
||||
|
||||
public interface ScriptStaticsStorage {
|
||||
|
||||
void record(ScriptSource source);
|
||||
|
||||
Map<String, String> getAll();
|
||||
|
||||
Map<String, String> getMax(int size);
|
||||
|
||||
void clear();
|
||||
|
||||
void close();
|
||||
}
|
||||
Reference in New Issue
Block a user