Added a setting to disable Tor.

Also fixed a bug with settings namespaces.
This commit is contained in:
akwizgran
2017-03-07 10:31:50 +00:00
parent a9053808b4
commit e682f31898
12 changed files with 137 additions and 92 deletions

View File

@@ -73,8 +73,6 @@ import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.INTENT_
import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.INTENT_CONTACTS;
import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.INTENT_FORUMS;
import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.INTENT_GROUPS;
import static org.briarproject.briar.android.settings.SettingsFragment.PREF_NOTIFY_BLOG;
import static org.briarproject.briar.android.settings.SettingsFragment.PREF_NOTIFY_GROUP;
import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_NAMESPACE;
@ThreadSafe
@@ -92,13 +90,13 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
// Content URIs to differentiate between pending intents
private static final String CONTACT_URI =
"content://org.briarproject/contact";
"content://org.briarproject.briar/contact";
private static final String GROUP_URI =
"content://org.briarproject/group";
"content://org.briarproject.briar/group";
private static final String FORUM_URI =
"content://org.briarproject/forum";
"content://org.briarproject.briar/forum";
private static final String BLOG_URI =
"content://org.briarproject/blog";
"content://org.briarproject.briar/blog";
// Actions for intents that are broadcast when notifications are dismissed
private static final String CLEAR_PRIVATE_MESSAGE_ACTION =
@@ -325,17 +323,18 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
private void updateContactNotification() {
if (contactTotal == 0) {
clearContactNotification();
} else if (settings.getBoolean("notifyPrivateMessages", true)) {
} else if (settings.getBoolean(PREF_NOTIFY_PRIVATE, true)) {
NotificationCompat.Builder b =
new NotificationCompat.Builder(appContext);
b.setSmallIcon(R.drawable.notification_private_message);
b.setColor(ContextCompat.getColor(appContext, R.color.briar_primary));
b.setColor(ContextCompat.getColor(appContext,
R.color.briar_primary));
b.setContentTitle(appContext.getText(R.string.app_name));
b.setContentText(appContext.getResources().getQuantityString(
R.plurals.private_message_notification_text, contactTotal,
contactTotal));
boolean sound = settings.getBoolean("notifySound", true);
String ringtoneUri = settings.get("notifyRingtoneUri");
boolean sound = settings.getBoolean(PREF_NOTIFY_SOUND, true);
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
if (sound && !StringUtils.isNullOrEmpty(ringtoneUri))
b.setSound(Uri.parse(ringtoneUri));
b.setDefaults(getDefaults());
@@ -381,11 +380,11 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
@UiThread
private int getDefaults() {
int defaults = DEFAULT_LIGHTS;
boolean sound = settings.getBoolean("notifySound", true);
String ringtoneUri = settings.get("notifyRingtoneUri");
boolean sound = settings.getBoolean(PREF_NOTIFY_SOUND, true);
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
if (sound && StringUtils.isNullOrEmpty(ringtoneUri))
defaults |= DEFAULT_SOUND;
if (settings.getBoolean("notifyVibration", true))
if (settings.getBoolean(PREF_NOTIFY_VIBRATION, true))
defaults |= DEFAULT_VIBRATE;
return defaults;
}
@@ -438,12 +437,13 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
NotificationCompat.Builder b =
new NotificationCompat.Builder(appContext);
b.setSmallIcon(R.drawable.notification_private_group);
b.setColor(ContextCompat.getColor(appContext, R.color.briar_primary));
b.setColor(ContextCompat.getColor(appContext,
R.color.briar_primary));
b.setContentTitle(appContext.getText(R.string.app_name));
b.setContentText(appContext.getResources().getQuantityString(
R.plurals.group_message_notification_text, groupTotal,
groupTotal));
String ringtoneUri = settings.get("notifyRingtoneUri");
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
if (!StringUtils.isNullOrEmpty(ringtoneUri))
b.setSound(Uri.parse(ringtoneUri));
b.setDefaults(getDefaults());
@@ -530,16 +530,17 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
private void updateForumPostNotification() {
if (forumTotal == 0) {
clearForumPostNotification();
} else if (settings.getBoolean("notifyForumPosts", true)) {
} else if (settings.getBoolean(PREF_NOTIFY_FORUM, true)) {
NotificationCompat.Builder b =
new NotificationCompat.Builder(appContext);
b.setSmallIcon(R.drawable.notification_forum);
b.setColor(ContextCompat.getColor(appContext, R.color.briar_primary));
b.setColor(ContextCompat.getColor(appContext,
R.color.briar_primary));
b.setContentTitle(appContext.getText(R.string.app_name));
b.setContentText(appContext.getResources().getQuantityString(
R.plurals.forum_post_notification_text, forumTotal,
forumTotal));
String ringtoneUri = settings.get("notifyRingtoneUri");
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
if (!StringUtils.isNullOrEmpty(ringtoneUri))
b.setSound(Uri.parse(ringtoneUri));
b.setDefaults(getDefaults());
@@ -630,12 +631,13 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
NotificationCompat.Builder b =
new NotificationCompat.Builder(appContext);
b.setSmallIcon(R.drawable.notification_blog);
b.setColor(ContextCompat.getColor(appContext, R.color.briar_primary));
b.setColor(ContextCompat.getColor(appContext,
R.color.briar_primary));
b.setContentTitle(appContext.getText(R.string.app_name));
b.setContentText(appContext.getResources().getQuantityString(
R.plurals.blog_post_notification_text, blogTotal,
blogTotal));
String ringtoneUri = settings.get("notifyRingtoneUri");
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
if (!StringUtils.isNullOrEmpty(ringtoneUri))
b.setSound(Uri.parse(ringtoneUri));
b.setDefaults(getDefaults());
@@ -696,7 +698,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
b.setContentText(appContext.getResources().getQuantityString(
R.plurals.introduction_notification_text, introductionTotal,
introductionTotal));
String ringtoneUri = settings.get("notifyRingtoneUri");
String ringtoneUri = settings.get(PREF_NOTIFY_RINGTONE_URI);
if (!StringUtils.isNullOrEmpty(ringtoneUri))
b.setSound(Uri.parse(ringtoneUri));
b.setDefaults(getDefaults());

View File

@@ -22,6 +22,8 @@ import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.event.EventListener;
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
import org.briarproject.bramble.api.plugin.BluetoothConstants;
import org.briarproject.bramble.api.plugin.TorConstants;
import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.api.settings.SettingsManager;
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
@@ -45,7 +47,17 @@ import static android.media.RingtoneManager.TYPE_NOTIFICATION;
import static android.provider.Settings.System.DEFAULT_NOTIFICATION_URI;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENABLE;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK;
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_RINGTONE;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_BLOG;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_FORUM;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_GROUP;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_PRIVATE;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_RINGTONE_NAME;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_RINGTONE_URI;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_SOUND;
import static org.briarproject.briar.api.android.AndroidNotificationManager.PREF_NOTIFY_VIBRATION;
@MethodsNotNullByDefault
@ParametersNotNullByDefault
@@ -53,8 +65,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
implements EventListener, Preference.OnPreferenceChangeListener {
public static final String SETTINGS_NAMESPACE = "android-ui";
public static final String PREF_NOTIFY_GROUP = "notifyGroupMessages";
public static final String PREF_NOTIFY_BLOG = "notifyBlogPosts";
public static final String BT_NAMESPACE = BluetoothConstants.ID.getString();
public static final String TOR_NAMESPACE = TorConstants.ID.getString();
private static final Logger LOG =
Logger.getLogger(SettingsFragment.class.getName());
@@ -62,7 +74,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
private SettingsActivity listener;
private AndroidExecutor androidExecutor;
private ListPreference enableBluetooth;
private ListPreference torOverMobile;
private ListPreference torNetwork;
private CheckBoxPreference notifyPrivateMessages;
private CheckBoxPreference notifyGroupMessages;
private CheckBoxPreference notifyForumPosts;
@@ -74,7 +86,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
private volatile SettingsManager settingsManager;
private volatile EventBus eventBus;
private volatile Settings settings;
private volatile boolean bluetoothSetting = false, torSetting = false;
@Override
public void onAttach(Context context) {
@@ -90,10 +101,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.settings);
enableBluetooth =
(ListPreference) findPreference("pref_key_bluetooth");
torOverMobile =
(ListPreference) findPreference("pref_key_tor_mobile");
enableBluetooth = (ListPreference) findPreference("pref_key_bluetooth");
torNetwork = (ListPreference) findPreference("pref_key_tor_network");
notifyPrivateMessages = (CheckBoxPreference) findPreference(
"pref_key_notify_private_messages");
notifyGroupMessages = (CheckBoxPreference) findPreference(
@@ -107,7 +116,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
notifySound = findPreference("pref_key_notify_sound");
enableBluetooth.setOnPreferenceChangeListener(this);
torOverMobile.setOnPreferenceChangeListener(this);
torNetwork.setOnPreferenceChangeListener(this);
notifyPrivateMessages.setOnPreferenceChangeListener(this);
notifyGroupMessages.setOnPreferenceChangeListener(this);
notifyForumPosts.setOnPreferenceChangeListener(this);
@@ -126,10 +135,10 @@ public class SettingsFragment extends PreferenceFragmentCompat
i.putExtra(EXTRA_RINGTONE_DEFAULT_URI,
DEFAULT_NOTIFICATION_URI);
i.putExtra(EXTRA_RINGTONE_SHOW_SILENT, true);
if (settings.getBoolean("notifySound", true)) {
if (settings.getBoolean(PREF_NOTIFY_SOUND, true)) {
Uri uri;
String ringtoneUri =
settings.get("notifyRingtoneUri");
settings.get(PREF_NOTIFY_RINGTONE_URI);
if (StringUtils.isNullOrEmpty(ringtoneUri))
uri = DEFAULT_NOTIFICATION_URI;
else uri = Uri.parse(ringtoneUri);
@@ -181,14 +190,17 @@ public class SettingsFragment extends PreferenceFragmentCompat
try {
long now = System.currentTimeMillis();
settings = settingsManager.getSettings(SETTINGS_NAMESPACE);
Settings btSettings = settingsManager.getSettings("bt");
Settings torSettings = settingsManager.getSettings("tor");
Settings btSettings =
settingsManager.getSettings(BT_NAMESPACE);
Settings torSettings =
settingsManager.getSettings(TOR_NAMESPACE);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Loading settings took " + duration + " ms");
bluetoothSetting = btSettings.getBoolean("enable", false);
torSetting = torSettings.getBoolean("torOverMobile", true);
displaySettings();
boolean btSetting =
btSettings.getBoolean(PREF_BT_ENABLE, false);
int torSetting = torSettings.getInt(PREF_TOR_NETWORK, 2);
displaySettings(btSetting, torSetting);
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
@@ -197,31 +209,33 @@ public class SettingsFragment extends PreferenceFragmentCompat
});
}
private void displaySettings() {
private void displaySettings(final boolean btSetting,
final int torSetting) {
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
@Override
public void run() {
enableBluetooth.setValue(Boolean.toString(bluetoothSetting));
torOverMobile.setValue(Boolean.toString(torSetting));
enableBluetooth.setValue(Boolean.toString(btSetting));
torNetwork.setValue(Integer.toString(torSetting));
notifyPrivateMessages.setChecked(settings.getBoolean(
"notifyPrivateMessages", true));
PREF_NOTIFY_PRIVATE, true));
notifyGroupMessages.setChecked(settings.getBoolean(
PREF_NOTIFY_GROUP, true));
notifyForumPosts.setChecked(settings.getBoolean(
"notifyForumPosts", true));
PREF_NOTIFY_FORUM, true));
notifyBlogPosts.setChecked(settings.getBoolean(
PREF_NOTIFY_BLOG, true));
notifyVibration.setChecked(settings.getBoolean(
"notifyVibration", true));
PREF_NOTIFY_VIBRATION, true));
String text;
if (settings.getBoolean("notifySound", true)) {
String ringtoneName = settings.get("notifyRingtoneName");
if (settings.getBoolean(PREF_NOTIFY_SOUND, true)) {
String ringtoneName =
settings.get(PREF_NOTIFY_RINGTONE_NAME);
if (StringUtils.isNullOrEmpty(ringtoneName)) {
text = getString(R.string.notify_sound_setting_default);
} else {
@@ -248,15 +262,15 @@ public class SettingsFragment extends PreferenceFragmentCompat
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
if (preference == enableBluetooth) {
bluetoothSetting = Boolean.valueOf((String) o);
enableOrDisableBluetooth(bluetoothSetting);
storeBluetoothSettings();
} else if (preference == torOverMobile) {
torSetting = Boolean.valueOf((String) o);
storeTorSettings();
boolean btSetting = Boolean.valueOf((String) o);
enableOrDisableBluetooth(btSetting);
storeBluetoothSettings(btSetting);
} else if (preference == torNetwork) {
int torSetting = Integer.valueOf((String) o);
storeTorSettings(torSetting);
} else if (preference == notifyPrivateMessages) {
Settings s = new Settings();
s.putBoolean("notifyPrivateMessages", (Boolean) o);
s.putBoolean(PREF_NOTIFY_PRIVATE, (Boolean) o);
storeSettings(s);
} else if (preference == notifyGroupMessages) {
Settings s = new Settings();
@@ -264,7 +278,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
storeSettings(s);
} else if (preference == notifyForumPosts) {
Settings s = new Settings();
s.putBoolean("notifyForumPosts", (Boolean) o);
s.putBoolean(PREF_NOTIFY_FORUM, (Boolean) o);
storeSettings(s);
} else if (preference == notifyBlogPosts) {
Settings s = new Settings();
@@ -272,7 +286,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
storeSettings(s);
} else if (preference == notifyVibration) {
Settings s = new Settings();
s.putBoolean("notifyVibration", (Boolean) o);
s.putBoolean(PREF_NOTIFY_VIBRATION, (Boolean) o);
storeSettings(s);
}
return true;
@@ -291,15 +305,15 @@ public class SettingsFragment extends PreferenceFragmentCompat
}
}
private void storeTorSettings() {
private void storeTorSettings(final int torSetting) {
listener.runOnDbThread(new Runnable() {
@Override
public void run() {
try {
Settings s = new Settings();
s.putBoolean("torOverMobile", torSetting);
s.putInt(PREF_TOR_NETWORK, torSetting);
long now = System.currentTimeMillis();
settingsManager.mergeSettings(s, "tor");
settingsManager.mergeSettings(s, TOR_NAMESPACE);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Merging settings took " + duration + " ms");
@@ -311,15 +325,15 @@ public class SettingsFragment extends PreferenceFragmentCompat
});
}
private void storeBluetoothSettings() {
private void storeBluetoothSettings(final boolean btSetting) {
listener.runOnDbThread(new Runnable() {
@Override
public void run() {
try {
Settings s = new Settings();
s.putBoolean("enable", bluetoothSetting);
s.putBoolean(PREF_BT_ENABLE, btSetting);
long now = System.currentTimeMillis();
settingsManager.mergeSettings(s, "bt");
settingsManager.mergeSettings(s, BT_NAMESPACE);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Merging settings took " + duration + " ms");
@@ -357,21 +371,21 @@ public class SettingsFragment extends PreferenceFragmentCompat
Uri uri = data.getParcelableExtra(EXTRA_RINGTONE_PICKED_URI);
if (uri == null) {
// The user chose silence
s.putBoolean("notifySound", false);
s.put("notifyRingtoneName", "");
s.put("notifyRingtoneUri", "");
s.putBoolean(PREF_NOTIFY_SOUND, false);
s.put(PREF_NOTIFY_RINGTONE_NAME, "");
s.put(PREF_NOTIFY_RINGTONE_URI, "");
} else if (RingtoneManager.isDefault(uri)) {
// The user chose the default
s.putBoolean("notifySound", true);
s.put("notifyRingtoneName", "");
s.put("notifyRingtoneUri", "");
s.putBoolean(PREF_NOTIFY_SOUND, true);
s.put(PREF_NOTIFY_RINGTONE_NAME, "");
s.put(PREF_NOTIFY_RINGTONE_URI, "");
} else {
// The user chose a ringtone other than the default
Ringtone r = RingtoneManager.getRingtone(getContext(), uri);
String name = r.getTitle(getContext());
s.putBoolean("notifySound", true);
s.put("notifyRingtoneName", name);
s.put("notifyRingtoneUri", uri.toString());
s.putBoolean(PREF_NOTIFY_SOUND, true);
s.put(PREF_NOTIFY_RINGTONE_NAME, name);
s.put(PREF_NOTIFY_RINGTONE_URI, uri.toString());
}
storeSettings(s);
}
@@ -381,7 +395,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
public void eventOccurred(Event e) {
if (e instanceof SettingsUpdatedEvent) {
String namespace = ((SettingsUpdatedEvent) e).getNamespace();
if (namespace.equals("bt") || namespace.equals("tor")
if (namespace.equals(BT_NAMESPACE)
|| namespace.equals(TOR_NAMESPACE)
|| namespace.equals(SETTINGS_NAMESPACE)) {
LOG.info("Settings updated");
loadSettings();

View File

@@ -9,6 +9,16 @@ import org.briarproject.bramble.api.sync.GroupId;
*/
public interface AndroidNotificationManager {
String PREF_NOTIFY_PRIVATE = "notifyPrivateMessages";
String PREF_NOTIFY_GROUP = "notifyGroupMessages";
String PREF_NOTIFY_FORUM = "notifyForumPosts";
String PREF_NOTIFY_BLOG = "notifyBlogPosts";
String PREF_NOTIFY_SOUND = "notifySound";
String PREF_NOTIFY_RINGTONE_NAME = "notifyRingtoneName";
String PREF_NOTIFY_RINGTONE_URI = "notifyRingtoneUri";
String PREF_NOTIFY_VIBRATION = "notifyVibration";
void clearContactNotification(ContactId c);
void clearAllContactNotifications();