下面列出了com.google.gson.internal.bind.DateTypeAdapter#com.activeandroid.query.Select 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean toSettings(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<AlertType> alerts = new Select()
.from(AlertType.class)
.execute();
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.serializeSpecialFloatingPointValues()
.create();
String output = gson.toJson(alerts);
Log.e(TAG, "Created the string " + output);
prefs.edit().putString("saved_alerts", output).commit(); // always leave this as commit
return true;
}
public static double max_recent() {
Sensor sensor = Sensor.currentSensor();
Calibration calibration = new Select()
.from(Calibration.class)
.where("Sensor = ? ", sensor.getId())
.where("slope_confidence != 0")
.where("sensor_confidence != 0")
.where("timestamp > ?", (new Date().getTime() - (60000 * 60 * 24 * 4)))
.orderBy("bg desc")
.executeSingle();
if (calibration != null) {
return calibration.bg;
} else {
return 120;
}
}
public static BgReading getForPreciseTimestamp(long timestamp, long precision, boolean lock_to_sensor) {
final Sensor sensor = Sensor.currentSensor();
if ((sensor != null) || !lock_to_sensor) {
final BgReading bgReading = new Select()
.from(BgReading.class)
.where(lock_to_sensor ? "Sensor = ?" : "timestamp > ?", (lock_to_sensor ? sensor.getId() : 0))
.where("timestamp <= ?", (timestamp + precision))
.where("timestamp >= ?", (timestamp - precision))
.orderBy("abs(timestamp - " + timestamp + ") asc")
.executeSingle();
if (bgReading != null && Math.abs(bgReading.timestamp - timestamp) < precision) { //cool, so was it actually within precision of that bg reading?
//Log.d(TAG, "getForPreciseTimestamp: Found a BG timestamp match");
return bgReading;
}
}
Log.d(TAG, "getForPreciseTimestamp: No luck finding a BG timestamp match: " + JoH.dateTimeText((long) timestamp) + " precision:" + precision + " Sensor: " + ((sensor == null) ? "null" : sensor.getId()));
return null;
}
private static int getLegacyCount(Class which, Boolean rest, Boolean mongo, Boolean and) {
try {
String where = "";
if (rest != null) where += " success = " + (rest ? "1 " : "0 ");
if (and != null) where += (and ? " and " : " or ");
if (mongo != null) where += " mongo_success = " + (mongo ? "1 " : "0 ");
final String query = new Select("COUNT(*) as total").from(which).toSql();
final Cursor resultCursor = Cache.openDatabase().rawQuery(query + ((where.length() > 0) ? " where " + where : ""), null);
if (resultCursor.moveToNext()) {
final int total = resultCursor.getInt(0);
resultCursor.close();
return total;
} else {
return 0;
}
} catch (Exception e) {
Log.d(TAG, "Got exception getting count: " + e);
return -1;
}
}
public static boolean toSettings(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<AlertType> alerts = new Select()
.from(AlertType.class)
.execute();
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.serializeSpecialFloatingPointValues()
.create();
String output = gson.toJson(alerts);
Log.e(TAG, "Created the string " + output);
prefs.edit().putString("saved_alerts", output).commit();
return true;
}
public static UserNotification GetNotificationByType(String type) {
if (legacy_types.contains(type)) {
type = type + " = ?";
return new Select()
.from(UserNotification.class)
.where(type, true)
.orderBy("_ID desc")
.executeSingle();
} else {
final String timestamp = PersistentStore.getString("UserNotification:timestamp:" + type);
if (timestamp.equals("")) return null;
final String message = PersistentStore.getString("UserNotification:message:" + type);
if (message.equals("")) return null;
UserNotification userNotification = new UserNotification();
userNotification.timestamp = JoH.tolerantParseDouble(timestamp, -1);
if (userNotification.timestamp == -1) return null; // bad data
userNotification.message = message;
Log.d(TAG, "Workaround for: " + type + " " + userNotification.message + " timestamp: " + userNotification.timestamp);
return userNotification;
}
}
public static Reminder getNextActiveReminder() {
fixUpTable(schema);
final boolean onHomeWifi = !HomeWifi.isSet() || HomeWifi.isConnected();
final long now = JoH.tsl();
final Reminder reminder = new Select()
.from(Reminder.class)
.where("enabled = ?", true)
.where("next_due < ?", now)
.where("snoozed_till < ?", now)
.where("last_fired < (? - (600000 * alerted_times))", now)
// if on home wifi or not set then anything otherwise only home only = false
.where(onHomeWifi ? "homeonly > -1 " : "homeonly = 0")
.orderBy("enabled desc, priority desc, next_due asc")
.executeSingle();
return reminder;
}
public static TransmitterData getForTimestamp(double timestamp) {//KS
try {
Sensor sensor = Sensor.currentSensor();
if (sensor != null) {
TransmitterData bgReading = new Select()
.from(TransmitterData.class)
.where("timestamp <= ?", (timestamp + (60 * 1000))) // 1 minute padding (should never be that far off, but why not)
.orderBy("timestamp desc")
.executeSingle();
if (bgReading != null && Math.abs(bgReading.timestamp - timestamp) < (3 * 60 * 1000)) { //cool, so was it actually within 4 minutes of that bg reading?
Log.i(TAG, "getForTimestamp: Found a BG timestamp match");
return bgReading;
}
}
} catch (Exception e) {
Log.e(TAG,"getForTimestamp() Got exception on Select : "+e.toString());
return null;
}
Log.d(TAG, "getForTimestamp: No luck finding a BG timestamp match");
return null;
}
public static List<Calibration> latestValid(int number, long until) {
Sensor sensor = Sensor.currentSensor();
if (sensor == null) {
return null;
}
// we don't filter invalid intercepts here as they will be filtered in the plugin itself
return new Select()
.from(Calibration.class)
.where("Sensor = ? ", sensor.getId())
.where("slope_confidence != 0")
.where("sensor_confidence != 0")
.where("slope != 0")
.where("timestamp <= ?", until)
.orderBy("timestamp desc")
.limit(number)
.execute();
}
private List<Notification> getAll() {
//Getting all items stored in Inventory table
return new Select()
.from(Notification.class)
.limit(10)
.execute();
}
public void markAsRead(View view) {
List<Notification> listtodel= new Select()
.from(Notification.class)
.limit(10)
.execute();
for (int i=listtodel.size()-1;i>=0;i--){
Notification.delete(Notification.class,i);
}
Intent refresh = new Intent(this, NotificationActivity.class);
startActivity(refresh);//Start the same Activity
finish(); //finish Activity.
}
public static void cleanup(long timestamp) {
patched = fixUpTable(schema, patched);
List<UserError> userErrors = new Select()
.from(UserError.class)
.where("timestamp < ?", timestamp)
.orderBy("timestamp desc")
.execute();
if (userErrors != null) Log.d(TAG, "cleanup UserError size=" + userErrors.size());
new Cleanup().execute(userErrors);
}
public static List<BgReading> latestForGraphAsc(int number, long startTime, long endTime) {//KS
return new Select()
.from(BgReading.class)
.where("timestamp >= " + Math.max(startTime, 0))
.where("timestamp <= " + endTime)
.where("calculated_value != 0")
.where("raw_data != 0")
.orderBy("timestamp asc")
.limit(number)
.execute();
}
public static List<AlertType> getAll(boolean above) {
String order;
if (above) {
order = "threshold asc";
} else {
order = "threshold desc";
}
List<AlertType> alerts = new Select()
.from(AlertType.class)
.where("above = ?", above)
.orderBy(order)
.execute();
return alerts;
}
public static Calibration lastValid() {
Sensor sensor = Sensor.currentSensor();
if (sensor == null) {
return null;
}
return new Select()
.from(Calibration.class)
.where("Sensor = ? ", sensor.getId())
.where("slope_confidence != 0")
.where("sensor_confidence != 0")
.where("slope != 0")
.orderBy("timestamp desc")
.executeSingle();
}
public static LibreBlock getForTimestamp(long timestamp) {
final long margin = (3 * 1000);
return new Select()
.from(LibreBlock.class)
.where("timestamp >= ?", (timestamp - margin))
.where("timestamp <= ?", (timestamp + margin))
.executeSingle();
}
public static List<BgReading> latest(int number, boolean is_follower) {
if (is_follower) {
// exclude sensor information when working as a follower
return new Select()
.from(BgReading.class)
.where("calculated_value != 0")
.where("raw_data != 0")
// .where("timestamp <= ?", JoH.tsl())
.orderBy("timestamp desc")
.limit(number)
.execute();
} else {
Sensor sensor = Sensor.currentSensor();
if (sensor == null) {
return null;
}
return new Select()
.from(BgReading.class)
.where("Sensor = ? ", sensor.getId())
.where("calculated_value != 0")
.where("raw_data != 0")
// .where("timestamp <= ?", JoH.tsl())
.orderBy("timestamp desc")
.limit(number)
.execute();
}
}
public static void remove_all() {
List<AlertType> Alerts = new Select()
.from(AlertType.class)
.execute();
for (AlertType alert : Alerts) {
alert.delete();
}
ActiveBgAlert.ClearData();
}
public static BgReading byUUID(String uuid) {
if (uuid == null) return null;
return new Select()
.from(BgReading.class)
.where("uuid = ?", uuid)
.executeSingle();
}
public static List<Treatments> latest(int num) {
try {
return new Select()
.from(Treatments.class)
.orderBy("timestamp desc")
.limit(num)
.execute();
} catch (android.database.sqlite.SQLiteException e) {
fixUpTable();
return null;
}
}
public static List<Reminder> getAllReminders() {
fixUpTable(schema);
final List<Reminder> reminders = new Select()
.from(Reminder.class)
.orderBy("enabled desc, priority desc, next_due asc")
.execute();
return reminders;
}
public static List<UserError> bySeverity(Integer[] levels) {
String levelsString = " ";
for (int level : levels) {
levelsString += level + ",";
}
Log.d("UserError", "severity in ("+levelsString.substring(0,levelsString.length() - 1)+")");
return new Select()
.from(UserError.class)
.where("severity in ("+levelsString.substring(0,levelsString.length() - 1)+")")
.orderBy("timestamp desc")
.limit(10000)//too many data can kill akp
.execute();
}
public List<GankDaily> getDailyDataFromDB(Observer<List<GankDaily>> observer) {
Observable.create(new ObservableOnSubscribe<List<GankDaily>>() {
@Override
public void subscribe(ObservableEmitter<List<GankDaily>> emitter) throws Exception {
List<GankDaily> list = new Select().from(GankDaily.class)
.orderBy("publishedAt DESC")//DESC ASC
.execute();
emitter.onNext(list);
}
}).subscribeOn(Schedulers.io())
.subscribe(observer);
return mList;
}
public static List<SensorSendQueue> queue() {
return new Select()
.from(SensorSendQueue.class)
.where("success = ?", false)
.orderBy("_ID desc")
.execute();
}
public static List<BgReading> futureReadings() {
double timestamp = new Date().getTime();
return new Select()
.from(BgReading.class)
.where("timestamp > " + timestamp)
.orderBy("timestamp desc")
.execute();
}
public static Sensor currentSensor() {
try {//KS
Sensor sensor = new Select()
.from(Sensor.class)
.where("started_at != 0")
.where("stopped_at = 0")
.orderBy("_ID desc")
.limit(1)
.executeSingle();
return sensor;
}
catch (Exception e) {
return null;
}
}
public static List<BgReading> latestForGraphSensor(int number, long startTime, long endTime) {
Sensor sensor = Sensor.currentSensor();
if (sensor == null) { return null; }
return new Select()
.from(BgReading.class)
.where("Sensor = ? ", sensor.getId())
.where("timestamp >= " + Math.max(startTime, 0))
.where("timestamp <= " + endTime)
.where("calculated_value != 0")
.where("raw_data != 0")
.where("calibration_uuid != \"\"")
.orderBy("timestamp desc")
.limit(number)
.execute();
}
private static List<String> getClasses() {
fixUpTable();
final ArrayList<String> results = new ArrayList<>();
final String query = new Select("distinct otype as otypes").from(UploaderQueue.class).toSql();
final Cursor resultCursor = Cache.openDatabase().rawQuery(query, null);
while (resultCursor.moveToNext()) {
results.add(resultCursor.getString(0));
}
resultCursor.close();
return results;
}
public static List<BloodTest> last(int num) {
try {
return new Select()
.from(BloodTest.class)
.orderBy("timestamp desc")
.limit(num)
.execute();
} catch (android.database.sqlite.SQLiteException e) {
fixUpTable();
return null;
}
}
public static List<UserError> bySeverity(Integer[] levels) {
String levelsString = " ";
for (int level : levels) {
levelsString += level + ",";
}
Log.d("UserError", "severity in ("+levelsString.substring(0,levelsString.length() - 1)+")");
return new Select()
.from(UserError.class)
.where("severity in ("+levelsString.substring(0,levelsString.length() - 1)+")")
.orderBy("timestamp desc")
.execute();
}