1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-21 04:20:27 +02:00

Updated PullRequest with recommended improvements

This commit is contained in:
abettenburg 2019-01-12 10:00:33 +01:00
parent 51399066a4
commit 86e92130d2
4 changed files with 117 additions and 123 deletions

View File

@ -25,12 +25,10 @@ import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.adapter.AppBlacklistAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AppBlacklistActivity extends AbstractGBActivity {

View File

@ -3,11 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import de.greenrobot.dao.query.Query;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.adapter.AppBlacklistAdapter;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
@ -15,7 +13,7 @@ import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilter;
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilterDao;
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilterEntry;
import nodomain.freeyourgadget.gadgetbridge.entities.NotificationFilterEntryDao;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -25,8 +23,6 @@ import java.util.List;
public class NotificationFilterActivity extends AbstractGBActivity {
private static final String TAG = NotificationFilterActivity.class.getName();
public static final int NOTIFICATION_FILTER_MODE_NONE = 0;
public static final int NOTIFICATION_FILTER_MODE_WHITELIST = 1;
public static final int NOTIFICATION_FILTER_MODE_BLACKLIST = 2;
@ -38,7 +34,6 @@ public class NotificationFilterActivity extends AbstractGBActivity {
private Spinner mSpinnerFilterSubMode;
private NotificationFilter mNotificationFilter;
private EditText mEditTextWords;
private DBHandler db = null;
private List<String> mWordsList = new ArrayList<>();
private List<Long> mFilterEntryIds = new ArrayList<>();
@ -58,49 +53,43 @@ public class NotificationFilterActivity extends AbstractGBActivity {
packageName = packageName.toLowerCase();
try {
db = GBApplication.acquireDB();
} catch (GBException e) {
LOG.error("Could not acquire DB.", e);
this.finish();
}
try (DBHandler db = GBApplication.acquireDB()) {
NotificationFilterDao notificationFilterDao = db.getDaoSession().getNotificationFilterDao();
NotificationFilterEntryDao notificationFilterEntryDao = db.getDaoSession().getNotificationFilterEntryDao();
NotificationFilterDao notificationFilterDao = db.getDaoSession().getNotificationFilterDao();
NotificationFilterEntryDao notificationFilterEntryDao = db.getDaoSession().getNotificationFilterEntryDao();
Query<NotificationFilter> query = notificationFilterDao.queryBuilder().where(NotificationFilterDao.Properties.AppIdentifier.eq(packageName)).build();
mNotificationFilter = query.unique();
Query<NotificationFilter> query = notificationFilterDao.queryBuilder().where(NotificationFilterDao.Properties.AppIdentifier.eq(packageName)).build();
mNotificationFilter = query.unique();
if (mNotificationFilter == null) {
mNotificationFilter = new NotificationFilter();
mNotificationFilter.setAppIdentifier(packageName);
LOG.debug("New Notification Filter");
} else {
LOG.debug("Loaded existing notification filter");
Query<NotificationFilterEntry> queryEntries = notificationFilterEntryDao.queryBuilder().where(NotificationFilterEntryDao.Properties.NotificationFilterId.eq(mNotificationFilter.getId())).build();
List<NotificationFilterEntry> filterEntries = queryEntries.list();
if (!filterEntries.isEmpty()) {
if (mNotificationFilter == null) {
mNotificationFilter = new NotificationFilter();
mNotificationFilter.setAppIdentifier(packageName);
LOG.debug("New Notification Filter");
} else {
LOG.debug("Loaded existing notification filter");
Query<NotificationFilterEntry> queryEntries = notificationFilterEntryDao.queryBuilder().where(NotificationFilterEntryDao.Properties.NotificationFilterId.eq(mNotificationFilter.getId())).build();
List<NotificationFilterEntry> filterEntries = queryEntries.list();
for (NotificationFilterEntry temp : filterEntries) {
mWordsList.add(temp.getNotificationFilterContent());
mFilterEntryIds.add(temp.getId());
LOG.debug("Loaded filter word: " + temp.getNotificationFilterContent());
}
}
}
setupView();
setupView(db);
} catch (Exception e) {
GB.toast(this, "Error accessing the database: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
this.finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (db != null) {
GBApplication.releaseDB();
}
}
private void setupView() {
private void setupView(DBHandler db) {
mSpinnerFilterMode = findViewById(R.id.spinnerFilterMode);
mSpinnerFilterMode.setSelection(mNotificationFilter.getNotificationFilterMode());
@ -145,69 +134,76 @@ public class NotificationFilterActivity extends AbstractGBActivity {
mEditTextWords.setEnabled(mSpinnerFilterMode.getSelectedItemPosition() == NOTIFICATION_FILTER_MODE_NONE);
mButtonSave = findViewById(R.id.buttonSaveFilter);
mButtonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: check for modifications, only save if something changed
String words = mEditTextWords.getText().toString();
if (StringUtils.isBlank(words) && mSpinnerFilterMode.getSelectedItemPosition() != NOTIFICATION_FILTER_MODE_NONE) {
Toast.makeText(NotificationFilterActivity.this, R.string.toast_notification_filter_words_empty_hint, Toast.LENGTH_SHORT).show();
return;
}
try {
db = GBApplication.acquireDB();
NotificationFilterDao notificationFilterDao = db.getDaoSession().getNotificationFilterDao();
NotificationFilterEntryDao notificationFilterEntryDao = db.getDaoSession().getNotificationFilterEntryDao();
debugOutput(notificationFilterDao);
mNotificationFilter.setNotificationFilterMode(mSpinnerFilterMode.getSelectedItemPosition());
mNotificationFilter.setNotificationFilterSubMode(mSpinnerFilterSubMode.getSelectedItemPosition());
notificationFilterEntryDao.deleteByKeyInTx(mFilterEntryIds);
Long filterId = notificationFilterDao.insertOrReplace(mNotificationFilter);
// only save words if filter mode != none
if (mNotificationFilter.getNotificationFilterMode() != NOTIFICATION_FILTER_MODE_NONE) {
String[] wordsSplitted = words.split("\n");
for (String temp : wordsSplitted) {
if (StringUtils.isBlank(temp)) {
continue;
}
temp = temp.trim();
NotificationFilterEntry notificationFilterEntry = new NotificationFilterEntry();
notificationFilterEntry.setNotificationFilterContent(temp);
notificationFilterEntry.setNotificationFilterId(filterId);
notificationFilterEntryDao.insert(notificationFilterEntry);
}
}
Toast.makeText(NotificationFilterActivity.this, R.string.toast_notification_filter_saved_successfully, Toast.LENGTH_SHORT).show();
NotificationFilterActivity.this.finish();
} catch (GBException e) {
LOG.error("Could not acquire DB.", e);
Toast.makeText(NotificationFilterActivity.this, "Database Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
saveFilter();
}
});
}
private void saveFilter() {
// TODO: check for modifications, only save if something changed
String words = mEditTextWords.getText().toString();
if (StringUtils.isBlank(words) && mSpinnerFilterMode.getSelectedItemPosition() != NOTIFICATION_FILTER_MODE_NONE) {
Toast.makeText(NotificationFilterActivity.this, R.string.toast_notification_filter_words_empty_hint, Toast.LENGTH_SHORT).show();
return;
}
try (DBHandler db = GBApplication.acquireDB()) {
NotificationFilterDao notificationFilterDao = db.getDaoSession().getNotificationFilterDao();
NotificationFilterEntryDao notificationFilterEntryDao = db.getDaoSession().getNotificationFilterEntryDao();
debugOutput(notificationFilterDao);
mNotificationFilter.setNotificationFilterMode(mSpinnerFilterMode.getSelectedItemPosition());
mNotificationFilter.setNotificationFilterSubMode(mSpinnerFilterSubMode.getSelectedItemPosition());
notificationFilterEntryDao.deleteByKeyInTx(mFilterEntryIds);
Long filterId = notificationFilterDao.insertOrReplace(mNotificationFilter);
// only save words if filter mode != none
if (mNotificationFilter.getNotificationFilterMode() != NOTIFICATION_FILTER_MODE_NONE) {
String[] wordsSplitted = words.split("\n");
for (String temp : wordsSplitted) {
if (StringUtils.isBlank(temp)) {
continue;
}
temp = temp.trim();
NotificationFilterEntry notificationFilterEntry = new NotificationFilterEntry();
notificationFilterEntry.setNotificationFilterContent(temp);
notificationFilterEntry.setNotificationFilterId(filterId);
notificationFilterEntryDao.insert(notificationFilterEntry);
}
}
Toast.makeText(NotificationFilterActivity.this, R.string.toast_notification_filter_saved_successfully, Toast.LENGTH_SHORT).show();
NotificationFilterActivity.this.finish();
} catch (Exception e) {
GB.toast(NotificationFilterActivity.this, "Error accessing the database: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
}
}
/**
* Only used for debugging purposes
*
* @param notificationFilterDao {@link NotificationFilterDao}
*/
private void debugOutput(NotificationFilterDao notificationFilterDao) {
if (BuildConfig.DEBUG) {
List<NotificationFilter> filters = notificationFilterDao.loadAll();
LOG.info(TAG, "Saved filters");
LOG.info("Saved filters");
for (NotificationFilter temp : filters) {
LOG.info(TAG, "Filter: " + temp.getId() + " " + temp.getAppIdentifier());
LOG.info("Filter: " + temp.getId() + " " + temp.getAppIdentifier());
}
}
}

View File

@ -42,6 +42,7 @@ import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.packageNameToPebbleMsgSender;
@ -136,7 +137,7 @@ public class AppBlacklistAdapter extends RecyclerView.Adapter<AppBlacklistAdapte
public void onClick(View view) {
if (holder.blacklist_checkbox.isChecked()) {
Toast.makeText(mContext, R.string.toast_app_must_not_be_blacklisted, Toast.LENGTH_SHORT).show();
GB.toast(mContext, mContext.getString(R.string.toast_app_must_not_be_blacklisted), Toast.LENGTH_SHORT, GB.INFO);
} else {
Intent intentStartNotificationFilterActivity = new Intent(mContext, NotificationFilterActivity.class);
intentStartNotificationFilterActivity.putExtra(STRING_EXTRA_PACKAGE_NAME, appInfo.packageName);

View File

@ -364,44 +364,43 @@ public class NotificationListener extends NotificationListenerService {
private boolean checkNotificationContentForWhiteAndBlackList(String packageName, String body) {
long start = System.currentTimeMillis();
DBHandler db;
List<String> wordsList = new ArrayList<>();
NotificationFilter notificationFilter;
try {
db = GBApplication.acquireDB();
} catch (GBException e) {
try (DBHandler db = GBApplication.acquireDB()) {
NotificationFilterDao notificationFilterDao = db.getDaoSession().getNotificationFilterDao();
NotificationFilterEntryDao notificationFilterEntryDao = db.getDaoSession().getNotificationFilterEntryDao();
Query<NotificationFilter> query = notificationFilterDao.queryBuilder().where(NotificationFilterDao.Properties.AppIdentifier.eq(packageName.toLowerCase())).build();
notificationFilter = query.unique();
if (notificationFilter == null) {
LOG.debug("No Notification Filter found");
return true;
}
LOG.debug("Loaded notification filter for '{}'", packageName);
Query<NotificationFilterEntry> queryEntries = notificationFilterEntryDao.queryBuilder().where(NotificationFilterEntryDao.Properties.NotificationFilterId.eq(notificationFilter.getId())).build();
List<NotificationFilterEntry> filterEntries = queryEntries.list();
if (BuildConfig.DEBUG) {
LOG.info("Database lookup took '{}' ms", System.currentTimeMillis() - start);
}
if (!filterEntries.isEmpty()) {
for (NotificationFilterEntry temp : filterEntries) {
wordsList.add(temp.getNotificationFilterContent());
LOG.debug("Loaded filter word: " + temp.getNotificationFilterContent());
}
}
} catch (Exception e) {
LOG.error("Could not acquire DB.", e);
return true;
}
List<String> wordsList = new ArrayList<>();
NotificationFilterDao notificationFilterDao = db.getDaoSession().getNotificationFilterDao();
NotificationFilterEntryDao notificationFilterEntryDao = db.getDaoSession().getNotificationFilterEntryDao();
Query<NotificationFilter> query = notificationFilterDao.queryBuilder().where(NotificationFilterDao.Properties.AppIdentifier.eq(packageName.toLowerCase())).build();
NotificationFilter notificationFilter = query.unique();
if (notificationFilter == null) {
LOG.debug("No Notification Filter found");
return true;
}
LOG.debug("Loaded notification filter for '{}'", packageName);
Query<NotificationFilterEntry> queryEntries = notificationFilterEntryDao.queryBuilder().where(NotificationFilterEntryDao.Properties.NotificationFilterId.eq(notificationFilter.getId())).build();
List<NotificationFilterEntry> filterEntries = queryEntries.list();
if (BuildConfig.DEBUG) {
LOG.info("Database lookup took '{}' ms", System.currentTimeMillis() - start);
}
if (!filterEntries.isEmpty()) {
for (NotificationFilterEntry temp : filterEntries) {
wordsList.add(temp.getNotificationFilterContent());
LOG.debug("Loaded filter word: " + temp.getNotificationFilterContent());
}
}
return shouldContinueAfterFilter(body, wordsList, notificationFilter);
}
@ -423,13 +422,13 @@ public class NotificationListener extends NotificationListenerService {
LOG.info("Every word was found, blacklist has effect, processing stops.");
return false;
} else {
boolean notContainsAny = !StringUtils.containsAny(body, wordsList.toArray(new CharSequence[0]));
if (notContainsAny) {
LOG.info("Not matching word was found, blacklist has no effect, processing continues.");
boolean containsAny = StringUtils.containsAny(body, wordsList.toArray(new CharSequence[0]));
if (!containsAny) {
LOG.info("No matching word was found, blacklist has no effect, processing continues.");
} else {
LOG.info("At least one matching word was found, blacklist has effect, processing stops.");
}
return notContainsAny;
return !containsAny;
}
case NOTIFICATION_FILTER_MODE_WHITELIST: