mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-26 10:35:50 +01:00
Use try-with-resources for db access
This commit is contained in:
parent
f24dc0c1cb
commit
7618ec39a2
@ -206,7 +206,7 @@ public class ConfigActivity extends AbstractGBActivity {
|
||||
try {
|
||||
helper = new PackageConfigHelper(getApplicationContext());
|
||||
list = helper.getNotificationConfigurations();
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.toast("error getting configurations", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
@ -233,7 +233,7 @@ public class ConfigActivity extends AbstractGBActivity {
|
||||
try {
|
||||
helper.saveNotificationConfiguration(config);
|
||||
LocalBroadcastManager.getInstance(ConfigActivity.this).sendBroadcast(new Intent(QHybridSupport.QHYBRID_COMMAND_NOTIFICATION_CONFIG_CHANGED));
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.toast("error saving notification", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
}
|
||||
refreshList();
|
||||
@ -259,7 +259,7 @@ public class ConfigActivity extends AbstractGBActivity {
|
||||
try {
|
||||
helper.deleteNotificationConfiguration((NotificationConfiguration) adapterView.getItemAtPosition(i));
|
||||
LocalBroadcastManager.getInstance(ConfigActivity.this).sendBroadcast(new Intent(QHybridSupport.QHYBRID_COMMAND_NOTIFICATION_CONFIG_CHANGED));
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.toast("error deleting setting", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
}
|
||||
refreshList();
|
||||
@ -508,7 +508,7 @@ public class ConfigActivity extends AbstractGBActivity {
|
||||
list.clear();
|
||||
try {
|
||||
list.addAll(helper.getNotificationConfigurations());
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.toast("error getting configurations", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
}
|
||||
// null is added to indicate the plus button added handled in PackageAdapter#getView
|
||||
|
@ -25,9 +25,7 @@ import android.util.Log;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.misfit.PlayNotificationRequest;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
|
||||
@ -43,11 +41,15 @@ public class PackageConfigHelper {
|
||||
public static final String DB_RESPECT_SILENT = "respectSilent";
|
||||
|
||||
|
||||
public PackageConfigHelper(Context context) throws GBException {
|
||||
initDB();
|
||||
public PackageConfigHelper(Context context) {
|
||||
try {
|
||||
initDB();
|
||||
} catch (Exception e) {
|
||||
GB.log("error getting database", GB.ERROR, e);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveNotificationConfiguration(NotificationConfiguration settings) throws GBException {
|
||||
public void saveNotificationConfiguration(NotificationConfiguration settings) throws Exception {
|
||||
ContentValues values = new ContentValues(6);
|
||||
values.put(DB_PACKAGE, settings.getPackageName());
|
||||
values.put(DB_APPNAME, settings.getAppName());
|
||||
@ -56,103 +58,99 @@ public class PackageConfigHelper {
|
||||
values.put(DB_VIBRATION, settings.getVibration().getValue());
|
||||
values.put(DB_RESPECT_SILENT, settings.getRespectSilentMode());
|
||||
|
||||
SQLiteDatabase database = GBApplication.acquireDB().getDatabase();
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
SQLiteDatabase database = db.getDatabase();
|
||||
|
||||
if(settings.getId() == -1) {
|
||||
settings.setId(database.insert(DB_TABLE, null, values));
|
||||
}else{
|
||||
database.update(DB_TABLE, values, DB_ID + "=?", new String[]{String.valueOf(settings.getId())});
|
||||
if (settings.getId() == -1) {
|
||||
settings.setId(database.insert(DB_TABLE, null, values));
|
||||
} else {
|
||||
database.update(DB_TABLE, values, DB_ID + "=?", new String[]{String.valueOf(settings.getId())});
|
||||
}
|
||||
}
|
||||
|
||||
GBApplication.releaseDB();
|
||||
//LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent());
|
||||
}
|
||||
|
||||
public ArrayList<NotificationConfiguration> getNotificationConfigurations() throws GBException {
|
||||
SQLiteDatabase database = GBApplication.acquireDB().getDatabase();
|
||||
public ArrayList<NotificationConfiguration> getNotificationConfigurations() throws Exception {
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
SQLiteDatabase database = db.getDatabase();
|
||||
|
||||
Cursor cursor = database.query(DB_TABLE, new String[]{"*"}, null, null, null, null, null);
|
||||
|
||||
GBApplication.releaseDB();
|
||||
|
||||
int size = cursor.getCount();
|
||||
ArrayList<NotificationConfiguration> list = new ArrayList<>(size);
|
||||
if(size > 0){
|
||||
int appNamePos = cursor.getColumnIndex(DB_APPNAME);
|
||||
int packageNamePos = cursor.getColumnIndex(DB_PACKAGE);
|
||||
int hourPos = cursor.getColumnIndex(DB_HOUR);
|
||||
int minPos = cursor.getColumnIndex(DB_MINUTE);
|
||||
int silentPos = cursor.getColumnIndex(DB_RESPECT_SILENT);
|
||||
int vibrationPos = cursor.getColumnIndex(DB_VIBRATION);
|
||||
int idPos = cursor.getColumnIndex(DB_ID);
|
||||
cursor.moveToFirst();
|
||||
do {
|
||||
list.add(new NotificationConfiguration(
|
||||
(short)cursor.getInt(minPos),
|
||||
(short)cursor.getInt(hourPos),
|
||||
cursor.getString(packageNamePos),
|
||||
cursor.getString(appNamePos),
|
||||
cursor.getInt(silentPos) == 1,
|
||||
PlayNotificationRequest.VibrationType.fromValue((byte)cursor.getInt(vibrationPos)),
|
||||
cursor.getInt(idPos)
|
||||
));
|
||||
Log.d("Settings", "setting #" + cursor.getPosition() + ": " + cursor.getInt(silentPos));
|
||||
}while (cursor.moveToNext());
|
||||
try (Cursor cursor = database.query(DB_TABLE, new String[]{"*"}, null, null, null, null, null)) {
|
||||
int size = cursor.getCount();
|
||||
ArrayList<NotificationConfiguration> list = new ArrayList<>(size);
|
||||
if (size > 0) {
|
||||
int appNamePos = cursor.getColumnIndex(DB_APPNAME);
|
||||
int packageNamePos = cursor.getColumnIndex(DB_PACKAGE);
|
||||
int hourPos = cursor.getColumnIndex(DB_HOUR);
|
||||
int minPos = cursor.getColumnIndex(DB_MINUTE);
|
||||
int silentPos = cursor.getColumnIndex(DB_RESPECT_SILENT);
|
||||
int vibrationPos = cursor.getColumnIndex(DB_VIBRATION);
|
||||
int idPos = cursor.getColumnIndex(DB_ID);
|
||||
cursor.moveToFirst();
|
||||
do {
|
||||
list.add(new NotificationConfiguration(
|
||||
(short) cursor.getInt(minPos),
|
||||
(short) cursor.getInt(hourPos),
|
||||
cursor.getString(packageNamePos),
|
||||
cursor.getString(appNamePos),
|
||||
cursor.getInt(silentPos) == 1,
|
||||
PlayNotificationRequest.VibrationType.fromValue((byte) cursor.getInt(vibrationPos)),
|
||||
cursor.getInt(idPos)
|
||||
));
|
||||
Log.d("Settings", "setting #" + cursor.getPosition() + ": " + cursor.getInt(silentPos));
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
return list;
|
||||
}
|
||||
|
||||
public NotificationConfiguration getNotificationConfiguration(String appName) throws GBException {
|
||||
public NotificationConfiguration getNotificationConfiguration(String appName) throws Exception {
|
||||
if(appName == null) return null;
|
||||
|
||||
SQLiteDatabase database = GBApplication.acquireDB().getDatabase();
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
SQLiteDatabase database = db.getDatabase();
|
||||
|
||||
Cursor c = database.query(DB_TABLE, new String[]{"*"}, DB_APPNAME + "=?", new String[]{appName}, null, null, null);
|
||||
|
||||
GBApplication.releaseDB();
|
||||
|
||||
if(c.getCount() == 0){
|
||||
c.close();
|
||||
return null;
|
||||
try (Cursor c = database.query(DB_TABLE, new String[]{"*"}, DB_APPNAME + "=?", new String[]{appName}, null, null, null)) {
|
||||
if (c.getCount() == 0) {
|
||||
return null;
|
||||
}
|
||||
c.moveToFirst();
|
||||
NotificationConfiguration settings = new NotificationConfiguration(
|
||||
(short) c.getInt(c.getColumnIndex(DB_MINUTE)),
|
||||
(short) c.getInt(c.getColumnIndex(DB_HOUR)),
|
||||
c.getString(c.getColumnIndex(DB_PACKAGE)),
|
||||
c.getString(c.getColumnIndex(DB_APPNAME)),
|
||||
c.getInt(c.getColumnIndex(DB_RESPECT_SILENT)) == 1,
|
||||
PlayNotificationRequest.VibrationType.fromValue((byte) c.getInt(c.getColumnIndex(DB_VIBRATION))),
|
||||
c.getInt(c.getColumnIndex(DB_ID))
|
||||
);
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
c.moveToFirst();
|
||||
NotificationConfiguration settings = new NotificationConfiguration(
|
||||
(short)c.getInt(c.getColumnIndex(DB_MINUTE)),
|
||||
(short)c.getInt(c.getColumnIndex(DB_HOUR)),
|
||||
c.getString(c.getColumnIndex(DB_PACKAGE)),
|
||||
c.getString(c.getColumnIndex(DB_APPNAME)),
|
||||
c.getInt(c.getColumnIndex(DB_RESPECT_SILENT)) == 1,
|
||||
PlayNotificationRequest.VibrationType.fromValue((byte)c.getInt(c.getColumnIndex(DB_VIBRATION))),
|
||||
c.getInt(c.getColumnIndex(DB_ID))
|
||||
);
|
||||
c.close();
|
||||
return settings;
|
||||
}
|
||||
|
||||
private void initDB() throws GBException {
|
||||
SQLiteDatabase database = GBApplication.acquireDB().getDatabase();
|
||||
private void initDB() throws Exception {
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
SQLiteDatabase database = db.getDatabase();
|
||||
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS notifications(" +
|
||||
DB_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
|
||||
DB_PACKAGE + " TEXT, " +
|
||||
DB_VIBRATION + " INTEGER, " +
|
||||
DB_MINUTE + " INTEGER DEFAULT -1, " +
|
||||
DB_APPNAME + " TEXT," +
|
||||
DB_RESPECT_SILENT + " INTEGER," +
|
||||
DB_HOUR + " INTEGER DEFAULT -1);");
|
||||
|
||||
GBApplication.releaseDB();
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS notifications(" +
|
||||
DB_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
|
||||
DB_PACKAGE + " TEXT, " +
|
||||
DB_VIBRATION + " INTEGER, " +
|
||||
DB_MINUTE + " INTEGER DEFAULT -1, " +
|
||||
DB_APPNAME + " TEXT," +
|
||||
DB_RESPECT_SILENT + " INTEGER," +
|
||||
DB_HOUR + " INTEGER DEFAULT -1);");
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteNotificationConfiguration(NotificationConfiguration packageSettings) throws GBException {
|
||||
public void deleteNotificationConfiguration(NotificationConfiguration packageSettings) throws Exception {
|
||||
Log.d("DB", "deleting id " + packageSettings.getId());
|
||||
if(packageSettings.getId() == -1) return;
|
||||
|
||||
SQLiteDatabase database = GBApplication.acquireDB().getDatabase();
|
||||
|
||||
database.delete(DB_TABLE, DB_ID + "=?", new String[]{String.valueOf(packageSettings.getId())});
|
||||
|
||||
GBApplication.releaseDB();
|
||||
try (DBHandler db = GBApplication.acquireDB()) {
|
||||
SQLiteDatabase database = db.getDatabase();
|
||||
database.delete(DB_TABLE, DB_ID + "=?", new String[]{String.valueOf(packageSettings.getId())});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,16 +33,15 @@ import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.QHybridSupport;
|
||||
@ -53,20 +52,14 @@ import static android.view.View.GONE;
|
||||
public class QHybridAppChoserActivity extends AbstractGBActivity {
|
||||
boolean hasControl = false;
|
||||
|
||||
PackageConfigHelper helper;
|
||||
private PackageConfigHelper helper;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_qhybrid_app_choser);
|
||||
|
||||
try {
|
||||
helper = new PackageConfigHelper(getApplicationContext());
|
||||
} catch (GBException e) {
|
||||
GB.toast("error getting database helper", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
helper = new PackageConfigHelper(getApplicationContext());
|
||||
|
||||
final ListView appList = findViewById(R.id.qhybrid_appChooserList);
|
||||
final PackageManager manager = getPackageManager();
|
||||
@ -145,7 +138,7 @@ public class QHybridAppChoserActivity extends AbstractGBActivity {
|
||||
try {
|
||||
helper.saveNotificationConfiguration(config);
|
||||
LocalBroadcastManager.getInstance(QHybridAppChoserActivity.this).sendBroadcast(new Intent(QHybridSupport.QHYBRID_COMMAND_NOTIFICATION_CONFIG_CHANGED));
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.toast("error saving configuration", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
}
|
||||
finish();
|
||||
|
@ -231,11 +231,7 @@ public class QHybridSupport extends QHybridBaseSupport {
|
||||
};
|
||||
LocalBroadcastManager.getInstance(getContext()).registerReceiver(commandReceiver, commandFilter);
|
||||
|
||||
try {
|
||||
helper = new PackageConfigHelper(GBApplication.getContext());
|
||||
} catch (GBException e) {
|
||||
GB.toast("error getting database", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
}
|
||||
helper = new PackageConfigHelper(GBApplication.getContext());
|
||||
|
||||
IntentFilter globalFilter = new IntentFilter();
|
||||
globalFilter.addAction(QHYBRID_ACTION_SET_ACTIVITY_HAND);
|
||||
@ -403,7 +399,7 @@ public class QHybridSupport extends QHybridBaseSupport {
|
||||
NotificationConfiguration config = null;
|
||||
try {
|
||||
config = helper.getNotificationConfiguration(packageName);
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.toast("error getting notification configuration", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
}
|
||||
if (config == null) return;
|
||||
@ -455,7 +451,7 @@ public class QHybridSupport extends QHybridBaseSupport {
|
||||
for (NotificationConfiguration config : helper.getNotificationConfigurations()) {
|
||||
configs.put(config, false);
|
||||
}
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.toast("error getting notification configs", Toast.LENGTH_SHORT, GB.ERROR, e);
|
||||
}
|
||||
|
||||
|
@ -344,7 +344,7 @@ public class FossilWatchAdapter extends WatchAdapter {
|
||||
getDeviceSupport().getDevice().sendDeviceUpdateIntent(getContext());
|
||||
}
|
||||
}, false);
|
||||
} catch (GBException e) {
|
||||
} catch (Exception e) {
|
||||
GB.log("error", GB.ERROR, e);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user