· 4 years ago · Jun 17, 2021, 12:40 PM
1/*
2 * Copyright 2011 Artiom Chilaru (http://flexlabs.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.flexlabs.widgets.dualbattery.storage;
18
19import android.content.ContentValues;
20import android.content.Context;
21import android.database.Cursor;
22import android.database.sqlite.SQLiteDatabase;
23import android.database.sqlite.SQLiteOpenHelper;
24import android.database.sqlite.SQLiteQueryBuilder;
25import android.text.TextUtils;
26
27import java.util.Date;
28
29public class BatteryLevelAdapter {
30 private static final String DB_NAME = "BatteryLevels.db";
31 private static final String DB_TABLE = "BatteryLevels";
32 private static final int DB_VERSION = 2;
33
34 public static final String KEY_ID = "_id";
35 public static final int ORD_ID = 0;
36 public static final String KEY_TIME = "Time";
37 public static final int ORD_TIME = 1;
38 public static final String KEY_STATUS = "Status";
39 public static final int ORD_STATUS = 2;
40 public static final String KEY_LEVEL = "Level";
41 public static final int ORD_LEVEL = 3;
42 public static final String KEY_DOCK_STATUS = "DockStatus";
43 public static final int ORD_DOCK_STATUS = 4;
44 public static final String KEY_DOCK_LEVEL = "DockLevel";
45 public static final int ORD_DOCK_LEVEL = 5;
46 public static final String KEY_SCREEN_STATE = "ScreenState";
47 public static final int ORD_SCREEN_STATE = 6;
48
49 private static final String DB_CREATE = "CREATE TABLE " + DB_TABLE + " (" +
50 KEY_ID + " integer PRIMARY KEY AUTOINCREMENT, " +
51 KEY_TIME + " LONG NOT NULL, " +
52 KEY_STATUS + " INT NOT NULL, " +
53 KEY_LEVEL + " INT NOT NULL, " +
54 KEY_DOCK_STATUS + " INT NOT NULL, " +
55 KEY_DOCK_LEVEL + " INT, " +
56 KEY_SCREEN_STATE + " INT NOT NULL);";
57
58 private SQLiteDatabase db;
59 private final Context context;
60 private DBHelper dbHelper;
61
62 public BatteryLevelAdapter(Context context) {
63 this.context = context;
64 dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
65 }
66
67 public BatteryLevelAdapter open() {
68 db = dbHelper.getWritableDatabase();
69 return this;
70 }
71
72 public BatteryLevelAdapter openRead() {
73 db = dbHelper.getReadableDatabase();
74 return this;
75 }
76
77 public void close() {
78 db.close();
79 }
80
81 public long insertEntry(Entry entry) {
82 ContentValues values = new ContentValues();
83 values.put(KEY_TIME, entry.date.getTime());
84 values.put(KEY_STATUS, entry.status);
85 values.put(KEY_LEVEL, entry.level);
86 values.put(KEY_DOCK_STATUS, entry.dock_status);
87 values.put(KEY_DOCK_LEVEL, entry.dock_level);
88 values.put(KEY_SCREEN_STATE, entry.screenOff ? 0 : 1);
89
90 return db.insert(DB_TABLE, null, values);
91 }
92
93 public boolean removeEntry(long index) {
94 return db.delete(DB_TABLE, KEY_ID + " = " + index, null) > 0;
95 }
96
97 public Cursor getAllEntries() {
98 return db.query(
99 DB_TABLE,
100 new String[] { KEY_ID, KEY_TIME, KEY_STATUS, KEY_LEVEL, KEY_DOCK_STATUS, KEY_DOCK_LEVEL, KEY_SCREEN_STATE },
101 null, null, null, null, null);
102 }
103
104 public Cursor getRecentEntries(int days) {
105 return db.query(
106 DB_TABLE,
107 new String[] { KEY_ID, KEY_TIME, KEY_STATUS, KEY_LEVEL, KEY_DOCK_STATUS, KEY_DOCK_LEVEL, KEY_SCREEN_STATE },
108 KEY_TIME + " > ?",
109 new String[] { String.valueOf(new Date().getTime() - (long)1000 * 60 * 60 * 24 * days) }, null, null, null);
110 }
111
112 public Cursor query(String[] projection, String selection, String[] selectionArgs, String sort) {
113 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
114 qb.setTables(DB_TABLE);
115
116 String orderBy = TextUtils.isEmpty(sort) ? KEY_TIME : sort;
117
118 Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
119 return c;
120 }
121
122 public Entry getEntry(long index) {
123 return null;
124 }
125
126 public static class Entry {
127 public Date date;
128 public int status, level, dock_status;
129 public Integer dock_level;
130 public boolean screenOff;
131
132 public Entry(int status, int level, int dock_status, Integer dock_level, boolean screenOff) {
133 this.status = status;
134 this.level = level;
135 this.dock_status = dock_status;
136 this.dock_level = dock_level;
137 this.date = new Date();
138 this.screenOff = screenOff;
139 }
140 }
141
142 private static class DBHelper extends SQLiteOpenHelper {
143 public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
144 super(context, name, factory, version);
145 }
146
147 @Override
148 public void onCreate(SQLiteDatabase sqLiteDatabase) {
149 sqLiteDatabase.execSQL(DB_CREATE);
150 }
151
152 @Override
153 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
154 sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
155 onCreate(sqLiteDatabase);
156 }
157 }
158}
159