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