Simpler forum sharing UX.

This commit is contained in:
akwizgran
2015-04-29 14:44:05 +01:00
parent ec7eb386f5
commit dea5a44724
28 changed files with 548 additions and 830 deletions

View File

@@ -132,9 +132,9 @@
/> />
</activity> </activity>
<activity <activity
android:name=".android.groups.ConfigureGroupActivity" android:name=".android.groups.AvailableGroupsActivity"
android:logo="@drawable/logo" android:logo="@drawable/logo"
android:label="@string/app_name" android:label="@string/available_forums_title"
android:parentActivityName=".android.groups.GroupListActivity" > android:parentActivityName=".android.groups.GroupListActivity" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
@@ -173,9 +173,9 @@
/> />
</activity> </activity>
<activity <activity
android:name=".android.groups.ManageGroupsActivity" android:name=".android.groups.ReadGroupPostActivity"
android:logo="@drawable/logo" android:logo="@drawable/logo"
android:label="@string/manage_forums_title" android:label="@string/app_name"
android:parentActivityName=".android.groups.GroupListActivity" > android:parentActivityName=".android.groups.GroupListActivity" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
@@ -183,7 +183,7 @@
/> />
</activity> </activity>
<activity <activity
android:name=".android.groups.ReadGroupPostActivity" android:name=".android.groups.ShareGroupActivity"
android:logo="@drawable/logo" android:logo="@drawable/logo"
android:label="@string/app_name" android:label="@string/app_name"
android:parentActivityName=".android.groups.GroupListActivity" > android:parentActivityName=".android.groups.GroupListActivity" >

View File

@@ -9,7 +9,7 @@
<color name="private_message_date">#AAAAAA</color> <color name="private_message_date">#AAAAAA</color>
<color name="unread_background">#FFFFFF</color> <color name="unread_background">#FFFFFF</color>
<color name="horizontal_border">#CCCCCC</color> <color name="horizontal_border">#CCCCCC</color>
<color name="groups_available_background">#FCCF1C</color> <color name="forums_available_background">#FCCF1C</color>
<color name="no_private_messages">#AAAAAA</color> <color name="no_private_messages">#AAAAAA</color>
<color name="no_posts">#AAAAAA</color> <color name="no_posts">#AAAAAA</color>
<color name="settings_title_text">#2D3E50</color> <color name="settings_title_text">#2D3E50</color>

View File

@@ -11,8 +11,10 @@
<string name="name_too_long">Name is too long</string> <string name="name_too_long">Name is too long</string>
<string name="password_too_weak">Password is too weak</string> <string name="password_too_weak">Password is too weak</string>
<string name="passwords_do_not_match">Passwords do not match</string> <string name="passwords_do_not_match">Passwords do not match</string>
<string name="create_account_button">Create Account</string>
<string name="enter_password">Enter your password:</string> <string name="enter_password">Enter your password:</string>
<string name="try_again">Wrong password, try again:</string> <string name="try_again">Wrong password, try again:</string>
<string name="sign_in_button">Sign In</string>
<string name="startup_failed_notification_title">Briar could not start</string> <string name="startup_failed_notification_title">Briar could not start</string>
<string name="startup_failed_notification_text">You may need to reinstall Briar.</string> <string name="startup_failed_notification_text">You may need to reinstall Briar.</string>
<string name="expiry_warning">This software has expired.\nPlease install a newer version.</string> <string name="expiry_warning">This software has expired.\nPlease install a newer version.</string>
@@ -33,7 +35,7 @@
<string name="searching_format">Searching for %06d\u2026</string> <string name="searching_format">Searching for %06d\u2026</string>
<string name="connection_failed">Connection failed</string> <string name="connection_failed">Connection failed</string>
<string name="could_not_find_contact">Briar could not find your contact nearby</string> <string name="could_not_find_contact">Briar could not find your contact nearby</string>
<string name="try_again_button">Try again</string> <string name="try_again_button">Try Again</string>
<string name="connected_to_contact">Connected to contact</string> <string name="connected_to_contact">Connected to contact</string>
<string name="calculating_confirmation_code">Calculating confirmation code\u2026</string> <string name="calculating_confirmation_code">Calculating confirmation code\u2026</string>
<string name="your_confirmation_code">Your confirmation code is</string> <string name="your_confirmation_code">Your confirmation code is</string>
@@ -49,45 +51,40 @@
<string name="format_from">From: %s</string> <string name="format_from">From: %s</string>
<string name="forums_title">Forums</string> <string name="forums_title">Forums</string>
<string name="no_forums">No forums</string> <string name="no_forums">No forums</string>
<plurals name="forums_available"> <plurals name="forums_shared">
<item quantity="one">%d forum available from contacts</item> <item quantity="one">%d forum shared by contacts</item>
<item quantity="other">%d forums available from contacts</item> <item quantity="other">%d forums shared by contacts</item>
</plurals> </plurals>
<string name="no_posts">No posts</string> <string name="unsubscribe">Unsubscribe</string>
<string name="subscribe_to_this_forum">Subscribe to this forum</string> <string name="unsubscribed_toast">Unsubscribed</string>
<string name="no_subscribers">No contacts subscribe to this forum</string> <string name="no_forum_posts">No posts</string>
<plurals name="subscribers">
<item quantity="one">%d contact subscribes to this forum:</item>
<item quantity="other">%d contacts subscribe to this forum:</item>
</plurals>
<string name="public_space_warning">Forums are public spaces. There may be other subscribers who are not your contacts.</string>
<string name="create_forum_title">New Forum</string> <string name="create_forum_title">New Forum</string>
<string name="choose_forum_name">Choose a name for your forum:</string> <string name="choose_forum_name">Choose a name for your forum:</string>
<string name="forum_visible_to_all">Share this forum with all contacts</string> <string name="create_forum_button">Create Forum</string>
<string name="forum_visible_to_some">Share this forum with chosen contacts</string>
<string name="done_button">Done</string>
<string name="forum_created_toast">Forum created</string> <string name="forum_created_toast">Forum created</string>
<string name="forum_share_with_all">Share this forum with all contacts</string>
<string name="forum_share_with_some">Share this forum with chosen contacts</string>
<string name="share_button">Share Forum</string>
<string name="from">From:</string> <string name="from">From:</string>
<string name="anonymous">Anonymous</string> <string name="anonymous">Anonymous</string>
<string name="new_identity_item">New identity\u2026</string> <string name="new_identity_item">New identity\u2026</string>
<string name="group_post_hint">Type forum post</string>
<string name="manage_forums_title">Available Forums</string>
<string name="no_forums_available">No forums available</string>
<string name="subscribed_all">Subscribed, shared with all contacts</string>
<string name="subscribed_some">Subscribed, shared with chosen contacts</string>
<string name="not_subscribed">Not subscribed</string>
<string name="new_identity_title">New Identity</string> <string name="new_identity_title">New Identity</string>
<string name="create_button">Create</string> <string name="create_identity_button">Create Identity</string>
<string name="identity_created_toast">Identity created</string> <string name="identity_created_toast">Identity created</string>
<string name="forum_post_hint">Type forum post</string>
<string name="available_forums_title">Available Forums</string>
<string name="subscribed_toast">Subscribed</string>
<string name="shared_by_format">Shared by %s</string>
<string name="no_contacts_prompt">You don\'t have any contacts. Add a contact now?</string> <string name="no_contacts_prompt">You don\'t have any contacts. Add a contact now?</string>
<string name="add_button">Add</string> <string name="add_button">Add</string>
<string name="cancel_button">Cancel</string> <string name="cancel_button">Cancel</string>
<string name="post_sent_toast">Post sent</string> <string name="done_button">Done</string>
<string name="post_sent_toast">Forum post sent</string>
<plurals name="private_message_notification_text"> <plurals name="private_message_notification_text">
<item quantity="one">New private message.</item> <item quantity="one">New private message.</item>
<item quantity="other">%d new private messages.</item> <item quantity="other">%d new private messages.</item>
</plurals> </plurals>
<plurals name="group_post_notification_text"> <plurals name="forum_post_notification_text">
<item quantity="one">New forum post.</item> <item quantity="one">New forum post.</item>
<item quantity="other">%d new forum posts.</item> <item quantity="other">%d new forum posts.</item>
</plurals> </plurals>

View File

@@ -236,7 +236,7 @@ Service, EventListener {
b.setSmallIcon(R.drawable.message_notification_icon); b.setSmallIcon(R.drawable.message_notification_icon);
b.setContentTitle(appContext.getText(R.string.app_name)); b.setContentTitle(appContext.getText(R.string.app_name));
b.setContentText(appContext.getResources().getQuantityString( b.setContentText(appContext.getResources().getQuantityString(
R.plurals.group_post_notification_text, groupTotal, R.plurals.forum_post_notification_text, groupTotal,
groupTotal)); groupTotal));
String ringtoneUri = settings.get("notifyRingtoneUri"); String ringtoneUri = settings.get("notifyRingtoneUri");
if(!StringUtils.isNullOrEmpty(ringtoneUri)) if(!StringUtils.isNullOrEmpty(ringtoneUri))

View File

@@ -47,7 +47,7 @@ public class PasswordActivity extends RoboActivity {
@Inject @CryptoExecutor private Executor cryptoExecutor; @Inject @CryptoExecutor private Executor cryptoExecutor;
private TextView enterPassword = null; private TextView enterPassword = null;
private Button continueButton = null; private Button signInButton = null;
private ProgressBar progress = null; private ProgressBar progress = null;
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@@ -102,15 +102,15 @@ public class PasswordActivity extends RoboActivity {
// Adjusting the padding of buttons and EditTexts has the wrong results // Adjusting the padding of buttons and EditTexts has the wrong results
layout.addView(new FixedVerticalSpace(this)); layout.addView(new FixedVerticalSpace(this));
continueButton = new Button(this); signInButton = new Button(this);
continueButton.setLayoutParams(WRAP_WRAP); signInButton.setLayoutParams(WRAP_WRAP);
continueButton.setText(R.string.continue_button); signInButton.setText(R.string.sign_in_button);
continueButton.setOnClickListener(new OnClickListener() { signInButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
validatePassword(encrypted, passwordEntry.getText()); validatePassword(encrypted, passwordEntry.getText());
} }
}); });
layout.addView(continueButton); layout.addView(signInButton);
progress = new ProgressBar(this); progress = new ProgressBar(this);
progress.setLayoutParams(WRAP_WRAP); progress.setLayoutParams(WRAP_WRAP);
@@ -131,7 +131,7 @@ public class PasswordActivity extends RoboActivity {
Object o = getSystemService(INPUT_METHOD_SERVICE); Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0); ((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
// Replace the button with a progress bar // Replace the button with a progress bar
continueButton.setVisibility(GONE); signInButton.setVisibility(GONE);
progress.setVisibility(VISIBLE); progress.setVisibility(VISIBLE);
// Decrypt the database key in a background thread // Decrypt the database key in a background thread
final String password = e.toString(); final String password = e.toString();
@@ -152,7 +152,7 @@ public class PasswordActivity extends RoboActivity {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {
enterPassword.setText(R.string.try_again); enterPassword.setText(R.string.try_again);
continueButton.setVisibility(VISIBLE); signInButton.setVisibility(VISIBLE);
progress.setVisibility(GONE); progress.setVisibility(GONE);
} }
}); });

View File

@@ -67,7 +67,7 @@ OnEditorActionListener {
private EditText passwordEntry = null, passwordConfirmation = null; private EditText passwordEntry = null, passwordConfirmation = null;
private StrengthMeter strengthMeter = null; private StrengthMeter strengthMeter = null;
private TextView feedback = null; private TextView feedback = null;
private Button continueButton = null; private Button createAccountButton = null;
private ProgressBar progress = null; private ProgressBar progress = null;
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@@ -160,12 +160,12 @@ OnEditorActionListener {
feedback.setText(""); feedback.setText("");
layout.addView(feedback); layout.addView(feedback);
continueButton = new Button(this); createAccountButton = new Button(this);
continueButton.setLayoutParams(WRAP_WRAP); createAccountButton.setLayoutParams(WRAP_WRAP);
continueButton.setText(R.string.continue_button); createAccountButton.setText(R.string.create_account_button);
continueButton.setEnabled(false); createAccountButton.setEnabled(false);
continueButton.setOnClickListener(this); createAccountButton.setOnClickListener(this);
layout.addView(continueButton); layout.addView(createAccountButton);
progress = new ProgressBar(this); progress = new ProgressBar(this);
progress.setLayoutParams(WRAP_WRAP); progress.setLayoutParams(WRAP_WRAP);
@@ -205,7 +205,7 @@ OnEditorActionListener {
} else { } else {
feedback.setText(""); feedback.setText("");
} }
continueButton.setEnabled(nicknameLength > 0 createAccountButton.setEnabled(nicknameLength > 0
&& nicknameLength <= MAX_AUTHOR_NAME_LENGTH && nicknameLength <= MAX_AUTHOR_NAME_LENGTH
&& passwordsMatch && strength >= WEAK); && passwordsMatch && strength >= WEAK);
} }
@@ -220,7 +220,7 @@ OnEditorActionListener {
public void onClick(View view) { public void onClick(View view) {
// Replace the feedback text and button with a progress bar // Replace the feedback text and button with a progress bar
feedback.setVisibility(GONE); feedback.setVisibility(GONE);
continueButton.setVisibility(GONE); createAccountButton.setVisibility(GONE);
progress.setVisibility(VISIBLE); progress.setVisibility(VISIBLE);
final String nickname = nicknameEntry.getText().toString(); final String nickname = nicknameEntry.getText().toString();
final String password = passwordEntry.getText().toString(); final String password = passwordEntry.getText().toString();

View File

@@ -1,10 +1,11 @@
package org.briarproject.android.groups; package org.briarproject.android.groups;
import static android.view.Gravity.CENTER; import static android.widget.Toast.LENGTH_SHORT;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH; import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -13,8 +14,11 @@ import javax.inject.Inject;
import org.briarproject.R; import org.briarproject.R;
import org.briarproject.android.BriarActivity; import org.briarproject.android.BriarActivity;
import org.briarproject.android.util.ListLoadingProgressBar; import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.Contact;
import org.briarproject.api.ContactId;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.event.Event; import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener; import org.briarproject.api.event.EventListener;
@@ -22,24 +26,22 @@ import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
import org.briarproject.api.event.SubscriptionAddedEvent; import org.briarproject.api.event.SubscriptionAddedEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent; import org.briarproject.api.event.SubscriptionRemovedEvent;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupStatus; import org.briarproject.api.messaging.GroupId;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.Toast;
public class ManageGroupsActivity extends BriarActivity public class AvailableGroupsActivity extends BriarActivity
implements EventListener, OnItemClickListener { implements EventListener, OnItemClickListener {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(ManageGroupsActivity.class.getName()); Logger.getLogger(AvailableGroupsActivity.class.getName());
private TextView empty = null; private AvailableGroupsAdapter adapter = null;
private ManageGroupsAdapter adapter = null;
private ListView list = null; private ListView list = null;
private ListLoadingProgressBar loading = null; private ListLoadingProgressBar loading = null;
@@ -51,13 +53,7 @@ implements EventListener, OnItemClickListener {
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
super.onCreate(state); super.onCreate(state);
empty = new TextView(this); adapter = new AvailableGroupsAdapter(this);
empty.setLayoutParams(MATCH_MATCH);
empty.setGravity(CENTER);
empty.setTextSize(18);
empty.setText(R.string.no_forums_available);
adapter = new ManageGroupsAdapter(this);
list = new ListView(this); list = new ListView(this);
list.setLayoutParams(MATCH_MATCH); list.setLayoutParams(MATCH_MATCH);
list.setAdapter(adapter); list.setAdapter(adapter);
@@ -79,8 +75,18 @@ implements EventListener, OnItemClickListener {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
Collection<GroupContacts> available =
new ArrayList<GroupContacts>();
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
Collection<GroupStatus> available = db.getAvailableGroups(); for(Group g : db.getAvailableGroups()) {
try {
GroupId id = g.getId();
Collection<Contact> c = db.getSubscribers(id);
available.add(new GroupContacts(g, c));
} catch(NoSuchSubscriptionException e) {
continue;
}
}
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms"); LOG.info("Load took " + duration + " ms");
@@ -93,17 +99,18 @@ implements EventListener, OnItemClickListener {
}); });
} }
private void displayGroups(final Collection<GroupStatus> available) { private void displayGroups(final Collection<GroupContacts> available) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {
if(available.isEmpty()) { if(available.isEmpty()) {
setContentView(empty); LOG.info("No groups available, finishing");
finish();
} else { } else {
setContentView(list); setContentView(list);
adapter.clear(); adapter.clear();
for(GroupStatus s : available) for(GroupContacts g : available)
adapter.add(new ManageGroupsItem(s)); adapter.add(new AvailableGroupsItem(g));
adapter.sort(ManageGroupsItemComparator.INSTANCE); adapter.sort(AvailableGroupsItemComparator.INSTANCE);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} }
} }
@@ -121,25 +128,36 @@ implements EventListener, OnItemClickListener {
LOG.info("Remote subscriptions changed, reloading"); LOG.info("Remote subscriptions changed, reloading");
loadGroups(); loadGroups();
} else if(e instanceof SubscriptionAddedEvent) { } else if(e instanceof SubscriptionAddedEvent) {
LOG.info("Group added, reloading"); LOG.info("Subscription added, reloading");
loadGroups(); loadGroups();
} else if(e instanceof SubscriptionRemovedEvent) { } else if(e instanceof SubscriptionRemovedEvent) {
LOG.info("Group removed, reloading"); LOG.info("Subscription removed, reloading");
loadGroups(); loadGroups();
} }
} }
public void onItemClick(AdapterView<?> parent, View view, int position, public void onItemClick(AdapterView<?> parent, View view, int position,
long id) { long id) {
ManageGroupsItem item = adapter.getItem(position); AvailableGroupsItem item = adapter.getItem(position);
GroupStatus s = item.getGroupStatus(); Collection<ContactId> visible = new ArrayList<ContactId>();
Group g = s.getGroup(); for(Contact c : item.getContacts()) visible.add(c.getId());
Intent i = new Intent(this, ConfigureGroupActivity.class); addSubscription(item.getGroup(), visible);
i.putExtra("briar.GROUP_ID", g.getId().getBytes()); String subscribed = getString(R.string.subscribed_toast);
i.putExtra("briar.GROUP_NAME", g.getName()); Toast.makeText(this, subscribed, LENGTH_SHORT).show();
i.putExtra("briar.GROUP_SALT", g.getSalt()); }
i.putExtra("briar.SUBSCRIBED", s.isSubscribed());
i.putExtra("briar.VISIBLE_TO_ALL", s.isVisibleToAll()); private void addSubscription(final Group g,
startActivity(i); final Collection<ContactId> visible) {
runOnDbThread(new Runnable() {
public void run() {
try {
db.addGroup(g);
db.setVisibility(g.getId(), visible);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
} }
} }

View File

@@ -0,0 +1,57 @@
package org.briarproject.android.groups;
import static android.text.TextUtils.TruncateAt.END;
import static android.widget.LinearLayout.VERTICAL;
import java.util.ArrayList;
import java.util.Collection;
import org.briarproject.R;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.Contact;
import org.briarproject.util.StringUtils;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
class AvailableGroupsAdapter extends ArrayAdapter<AvailableGroupsItem> {
private final int pad;
AvailableGroupsAdapter(Context ctx) {
super(ctx, android.R.layout.simple_expandable_list_item_1,
new ArrayList<AvailableGroupsItem>());
pad = LayoutUtils.getPadding(ctx);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AvailableGroupsItem item = getItem(position);
Context ctx = getContext();
LinearLayout layout = new LinearLayout(ctx);
layout.setOrientation(VERTICAL);
TextView name = new TextView(ctx);
name.setTextSize(18);
name.setSingleLine();
name.setEllipsize(END);
name.setPadding(pad, pad, pad, pad);
name.setText(item.getGroup().getName());
layout.addView(name);
TextView status = new TextView(ctx);
status.setPadding(pad, 0, pad, pad);
Collection<String> names = new ArrayList<String>();
for(Contact c : item.getContacts()) names.add(c.getAuthor().getName());
String format = ctx.getString(R.string.shared_by_format);
status.setText(String.format(format, StringUtils.join(names, ", ")));
layout.addView(status);
return layout;
}
}

View File

@@ -0,0 +1,23 @@
package org.briarproject.android.groups;
import java.util.Collection;
import org.briarproject.api.Contact;
import org.briarproject.api.messaging.Group;
class AvailableGroupsItem {
private final GroupContacts groupContacts;
AvailableGroupsItem(GroupContacts groupContacts) {
this.groupContacts = groupContacts;
}
Group getGroup() {
return groupContacts.getGroup();
}
Collection<Contact> getContacts() {
return groupContacts.getContacts();
}
}

View File

@@ -0,0 +1,16 @@
package org.briarproject.android.groups;
import java.util.Comparator;
class AvailableGroupsItemComparator implements Comparator<AvailableGroupsItem> {
static final AvailableGroupsItemComparator INSTANCE =
new AvailableGroupsItemComparator();
public int compare(AvailableGroupsItem a, AvailableGroupsItem b) {
if(a == b) return 0;
String aName = a.getGroup().getName();
String bName = b.getGroup().getName();
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
}
}

View File

@@ -1,356 +0,0 @@
package org.briarproject.android.groups;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.view.Gravity.CENTER;
import static android.view.Gravity.CENTER_HORIZONTAL;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.BriarActivity;
import org.briarproject.android.contact.SelectContactsDialog;
import org.briarproject.android.invitation.AddContactActivity;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.Contact;
import org.briarproject.api.ContactId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.LocalSubscriptionsUpdatedEvent;
import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class ConfigureGroupActivity extends BriarActivity
implements OnClickListener, EventListener, NoContactsDialog.Listener,
SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(ConfigureGroupActivity.class.getName());
private String groupName = null;
private CheckBox subscribeCheckBox = null;
private RadioGroup radioGroup = null;
private RadioButton visibleToAll = null, visibleToSome = null;
private Button doneButton = null;
private TextView subscribers = null;
private TextView subscriberNames = null;
private ProgressBar progress = null;
private boolean changed = false;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject private volatile EventBus eventBus;
private volatile GroupId groupId = null;
private volatile Group group = null;
private volatile boolean subscribed = false;
private volatile Collection<Contact> contacts = null;
private volatile Collection<ContactId> selected = null;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
byte[] b = i.getByteArrayExtra("briar.GROUP_ID");
if(b == null) throw new IllegalStateException();
groupId = new GroupId(b);
groupName = i.getStringExtra("briar.GROUP_NAME");
if(groupName == null) throw new IllegalStateException();
setTitle(groupName);
b = i.getByteArrayExtra("briar.GROUP_SALT");
if(b == null) throw new IllegalStateException();
group = new Group(groupId, groupName, b);
subscribed = i.getBooleanExtra("briar.SUBSCRIBED", false);
boolean all = i.getBooleanExtra("briar.VISIBLE_TO_ALL", false);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_MATCH);
layout.setOrientation(VERTICAL);
layout.setGravity(CENTER_HORIZONTAL);
int pad = LayoutUtils.getPadding(this);
layout.setPadding(pad, pad, pad, pad);
subscribers = new TextView(this);
subscribers.setGravity(CENTER);
subscribers.setTextSize(18);
subscribers.setText("\u2026");
layout.addView(subscribers);
subscriberNames = new TextView(this);
subscriberNames.setGravity(CENTER);
subscriberNames.setTextSize(18);
subscriberNames.setVisibility(GONE);
layout.addView(subscriberNames);
LinearLayout warning = new LinearLayout(this);
warning.setOrientation(HORIZONTAL);
warning.setPadding(pad, pad, pad, pad);
ImageView icon = new ImageView(this);
icon.setImageResource(R.drawable.action_about);
warning.addView(icon);
TextView publicSpace = new TextView(this);
publicSpace.setPadding(pad, 0, 0, 0);
publicSpace.setText(R.string.public_space_warning);
warning.addView(publicSpace);
layout.addView(warning);
subscribeCheckBox = new CheckBox(this);
subscribeCheckBox.setId(1);
subscribeCheckBox.setText(R.string.subscribe_to_this_forum);
subscribeCheckBox.setChecked(subscribed);
subscribeCheckBox.setOnClickListener(this);
layout.addView(subscribeCheckBox);
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(VERTICAL);
visibleToAll = new RadioButton(this);
visibleToAll.setId(2);
visibleToAll.setText(R.string.forum_visible_to_all);
visibleToAll.setEnabled(subscribed);
visibleToAll.setOnClickListener(this);
radioGroup.addView(visibleToAll);
visibleToSome = new RadioButton(this);
visibleToSome.setId(3);
visibleToSome.setText(R.string.forum_visible_to_some);
visibleToSome.setEnabled(subscribed);
visibleToSome.setOnClickListener(this);
radioGroup.addView(visibleToSome);
if(!subscribed || all) radioGroup.check(visibleToAll.getId());
else radioGroup.check(visibleToSome.getId());
layout.addView(radioGroup);
doneButton = new Button(this);
doneButton.setLayoutParams(WRAP_WRAP);
doneButton.setText(R.string.done_button);
doneButton.setOnClickListener(this);
layout.addView(doneButton);
progress = new ProgressBar(this);
progress.setLayoutParams(WRAP_WRAP);
progress.setIndeterminate(true);
progress.setVisibility(GONE);
layout.addView(progress);
setContentView(layout);
}
@Override
public void onResume() {
super.onResume();
eventBus.addListener(this);
loadSubscribers();
}
private void loadSubscribers() {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getSubscribers(groupId);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displaySubscribers(contacts);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void displaySubscribers(final Collection<Contact> contacts) {
runOnUiThread(new Runnable() {
public void run() {
if(contacts.isEmpty()) {
subscribers.setText(R.string.no_subscribers);
subscriberNames.setVisibility(GONE);
} else {
int count = contacts.size();
Resources res = getResources();
String title = res.getQuantityString(R.plurals.subscribers,
count, count);
subscribers.setText(title);
List<String> names = new ArrayList<String>();
for(Contact c : contacts)
names.add(c.getAuthor().getName());
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
StringBuilder s = new StringBuilder();
for(int i = 0; i < count; i++) {
s.append(names.get(i));
if(i + 1 < count) s.append(", ");
}
subscriberNames.setText(s.toString());
subscriberNames.setVisibility(VISIBLE);
}
}
});
}
@Override
public void onPause() {
super.onPause();
eventBus.removeListener(this);
}
public void onClick(View view) {
if(view == subscribeCheckBox) {
changed = true;
boolean subscribe = subscribeCheckBox.isChecked();
visibleToAll.setEnabled(subscribe);
visibleToSome.setEnabled(subscribe);
} else if(view == visibleToAll) {
changed = true;
} else if(view == visibleToSome) {
changed = true;
if(contacts == null) loadVisibleContacts();
else displayVisibleContacts();
} else if(view == doneButton) {
if(changed) {
// Replace the button with a progress bar
doneButton.setVisibility(GONE);
progress.setVisibility(VISIBLE);
// Update the group in a background thread
boolean subscribe = subscribeCheckBox.isChecked();
boolean all = visibleToAll.isChecked();
updateGroup(subscribe, all);
} else {
finish();
}
}
}
private void loadVisibleContacts() {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
contacts = db.getContacts();
selected = db.getVisibility(groupId);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayVisibleContacts();
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void displayVisibleContacts() {
runOnUiThread(new Runnable() {
public void run() {
if(contacts.isEmpty()) {
NoContactsDialog builder = new NoContactsDialog();
builder.setListener(ConfigureGroupActivity.this);
builder.build(ConfigureGroupActivity.this).show();
} else {
SelectContactsDialog builder = new SelectContactsDialog();
builder.setListener(ConfigureGroupActivity.this);
builder.setContacts(contacts);
builder.setSelected(selected);
builder.build(ConfigureGroupActivity.this).show();
}
}
});
}
private void updateGroup(final boolean subscribe, final boolean all) {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
if(subscribe) {
if(!subscribed) db.addGroup(group);
db.setVisibleToAll(groupId, all);
if(!all) db.setVisibility(groupId, selected);
} else if(subscribed) {
db.removeGroup(group);
}
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Update took " + duration + " ms");
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
if(subscribe) showGroup();
else finishOnUiThread();
}
});
}
private void showGroup() {
runOnUiThread(new Runnable() {
public void run() {
Intent i = new Intent(ConfigureGroupActivity.this,
GroupActivity.class);
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("briar.GROUP_ID", groupId.getBytes());
i.putExtra("briar.GROUP_NAME", groupName);
startActivity(i);
finish();
}
});
}
public void eventOccurred(Event e) {
if(e instanceof LocalSubscriptionsUpdatedEvent) {
LOG.info("Local subscriptions updated, reloading");
loadSubscribers();
} else if(e instanceof RemoteSubscriptionsUpdatedEvent) {
LOG.info("Remote subscriptions updated, reloading");
loadSubscribers();
}
}
public void contactCreationSelected() {
startActivity(new Intent(this, AddContactActivity.class));
}
public void contactCreationCancelled() {}
public void contactsSelected(Collection<ContactId> selected) {
this.selected = Collections.unmodifiableCollection(selected);
}
public void contactSelectionCancelled() {}
}

View File

@@ -14,19 +14,13 @@ import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP; import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP;
import static org.briarproject.api.messaging.MessagingConstants.MAX_GROUP_NAME_LENGTH; import static org.briarproject.api.messaging.MessagingConstants.MAX_GROUP_NAME_LENGTH;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.inject.Inject; import javax.inject.Inject;
import org.briarproject.R; import org.briarproject.R;
import org.briarproject.android.BriarActivity; import org.briarproject.android.BriarActivity;
import org.briarproject.android.contact.SelectContactsDialog;
import org.briarproject.android.invitation.AddContactActivity;
import org.briarproject.android.util.LayoutUtils; import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.Contact;
import org.briarproject.api.ContactId;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
@@ -42,31 +36,24 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView; import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener; import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast; import android.widget.Toast;
public class CreateGroupActivity extends BriarActivity public class CreateGroupActivity extends BriarActivity
implements OnEditorActionListener, OnClickListener, NoContactsDialog.Listener, implements OnEditorActionListener, OnClickListener {
SelectContactsDialog.Listener {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(CreateGroupActivity.class.getName()); Logger.getLogger(CreateGroupActivity.class.getName());
private EditText nameEntry = null; private EditText nameEntry = null;
private RadioGroup radioGroup = null; private Button createForumButton = null;
private RadioButton visibleToAll = null, visibleToSome = null;
private Button createButton = null;
private ProgressBar progress = null; private ProgressBar progress = null;
private TextView feedback = null; private TextView feedback = null;
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject private volatile GroupFactory groupFactory; @Inject private volatile GroupFactory groupFactory;
@Inject private volatile DatabaseComponent db; @Inject private volatile DatabaseComponent db;
private volatile Collection<Contact> contacts = null;
private volatile Collection<ContactId> selected = Collections.emptySet();
@Override @Override
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
@@ -97,32 +84,16 @@ SelectContactsDialog.Listener {
nameEntry.setOnEditorActionListener(this); nameEntry.setOnEditorActionListener(this);
layout.addView(nameEntry); layout.addView(nameEntry);
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(VERTICAL);
visibleToAll = new RadioButton(this);
visibleToAll.setId(2);
visibleToAll.setText(R.string.forum_visible_to_all);
visibleToAll.setOnClickListener(this);
radioGroup.addView(visibleToAll);
visibleToSome = new RadioButton(this);
visibleToSome.setId(3);
visibleToSome.setText(R.string.forum_visible_to_some);
visibleToSome.setOnClickListener(this);
radioGroup.addView(visibleToSome);
layout.addView(radioGroup);
feedback = new TextView(this); feedback = new TextView(this);
feedback.setGravity(CENTER); feedback.setGravity(CENTER);
feedback.setPadding(0, pad, 0, pad); feedback.setPadding(0, pad, 0, pad);
layout.addView(feedback); layout.addView(feedback);
createButton = new Button(this); createForumButton = new Button(this);
createButton.setLayoutParams(WRAP_WRAP); createForumButton.setLayoutParams(WRAP_WRAP);
createButton.setText(R.string.create_button); createForumButton.setText(R.string.create_forum_button);
createButton.setOnClickListener(this); createForumButton.setOnClickListener(this);
layout.addView(createButton); layout.addView(createForumButton);
progress = new ProgressBar(this); progress = new ProgressBar(this);
progress.setLayoutParams(WRAP_WRAP); progress.setLayoutParams(WRAP_WRAP);
@@ -135,9 +106,7 @@ SelectContactsDialog.Listener {
private void enableOrDisableCreateButton() { private void enableOrDisableCreateButton() {
if(progress == null) return; // Not created yet if(progress == null) return; // Not created yet
boolean nameValid = validateName(); createForumButton.setEnabled(validateName());
boolean visibilitySelected = radioGroup.getCheckedRadioButtonId() != -1;
createButton.setEnabled(nameValid && visibilitySelected);
} }
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) { public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
@@ -156,67 +125,22 @@ SelectContactsDialog.Listener {
} }
public void onClick(View view) { public void onClick(View view) {
if(view == visibleToAll) { if(view == createForumButton) {
enableOrDisableCreateButton();
} else if(view == visibleToSome) {
if(contacts == null) loadContacts();
else displayContacts();
} else if(view == createButton) {
hideSoftKeyboard(); hideSoftKeyboard();
if(!validateName()) return; if(!validateName()) return;
createButton.setVisibility(GONE); createForumButton.setVisibility(GONE);
progress.setVisibility(VISIBLE); progress.setVisibility(VISIBLE);
String name = nameEntry.getText().toString(); storeGroup(nameEntry.getText().toString());
boolean all = visibleToAll.isChecked();
storeGroup(name, all);
} }
} }
private void loadContacts() { private void storeGroup(final String name) {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayContacts();
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void displayContacts() {
runOnUiThread(new Runnable() {
public void run() {
if(contacts.isEmpty()) {
NoContactsDialog builder = new NoContactsDialog();
builder.setListener(CreateGroupActivity.this);
builder.build(CreateGroupActivity.this).show();
} else {
SelectContactsDialog builder = new SelectContactsDialog();
builder.setListener(CreateGroupActivity.this);
builder.setContacts(contacts);
builder.setSelected(selected);
builder.build(CreateGroupActivity.this).show();
}
}
});
}
private void storeGroup(final String name, final boolean all) {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
Group g = groupFactory.createGroup(name); Group g = groupFactory.createGroup(name);
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
db.addGroup(g); db.addGroup(g);
if(all) db.setVisibleToAll(g.getId(), true);
else db.setVisibility(g.getId(), selected);
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Storing group took " + duration + " ms"); LOG.info("Storing group took " + duration + " ms");
@@ -244,21 +168,4 @@ SelectContactsDialog.Listener {
} }
}); });
} }
public void contactCreationSelected() {
startActivity(new Intent(this, AddContactActivity.class));
}
public void contactCreationCancelled() {
enableOrDisableCreateButton();
}
public void contactsSelected(Collection<ContactId> selected) {
this.selected = Collections.unmodifiableCollection(selected);
enableOrDisableCreateButton();
}
public void contactSelectionCancelled() {
enableOrDisableCreateButton();
}
} }

View File

@@ -69,7 +69,7 @@ OnClickListener, OnItemClickListener {
private GroupAdapter adapter = null; private GroupAdapter adapter = null;
private ListView list = null; private ListView list = null;
private ListLoadingProgressBar loading = null; private ListLoadingProgressBar loading = null;
private ImageButton composeButton = null, configureButton = null; private ImageButton composeButton = null, shareButton = null;
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db; @Inject private volatile DatabaseComponent db;
@@ -85,6 +85,8 @@ OnClickListener, OnItemClickListener {
byte[] b = i.getByteArrayExtra("briar.GROUP_ID"); byte[] b = i.getByteArrayExtra("briar.GROUP_ID");
if(b == null) throw new IllegalStateException(); if(b == null) throw new IllegalStateException();
groupId = new GroupId(b); groupId = new GroupId(b);
String name = i.getStringExtra("briar.GROUP_NAME");
if(name != null) setTitle(name);
LinearLayout layout = new LinearLayout(this); LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_MATCH); layout.setLayoutParams(MATCH_MATCH);
@@ -95,7 +97,7 @@ OnClickListener, OnItemClickListener {
empty.setLayoutParams(MATCH_WRAP_1); empty.setLayoutParams(MATCH_WRAP_1);
empty.setGravity(CENTER); empty.setGravity(CENTER);
empty.setTextSize(18); empty.setTextSize(18);
empty.setText(R.string.no_posts); empty.setText(R.string.no_forum_posts);
empty.setVisibility(GONE); empty.setVisibility(GONE);
layout.addView(empty); layout.addView(empty);
@@ -127,11 +129,11 @@ OnClickListener, OnItemClickListener {
footer.addView(composeButton); footer.addView(composeButton);
footer.addView(new ElasticHorizontalSpace(this)); footer.addView(new ElasticHorizontalSpace(this));
configureButton = new ImageButton(this); shareButton = new ImageButton(this);
configureButton.setBackgroundResource(0); shareButton.setBackgroundResource(0);
configureButton.setImageResource(R.drawable.action_settings); shareButton.setImageResource(R.drawable.social_share);
configureButton.setOnClickListener(this); shareButton.setOnClickListener(this);
footer.addView(configureButton); footer.addView(shareButton);
footer.addView(new ElasticHorizontalSpace(this)); footer.addView(new ElasticHorizontalSpace(this));
layout.addView(footer); layout.addView(footer);
@@ -334,12 +336,10 @@ OnClickListener, OnItemClickListener {
i.putExtra("briar.GROUP_NAME", group.getName()); i.putExtra("briar.GROUP_NAME", group.getName());
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage()); i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage());
startActivity(i); startActivity(i);
} else if(view == configureButton) { } else if(view == shareButton) {
Intent i = new Intent(this, ConfigureGroupActivity.class); Intent i = new Intent(this, ShareGroupActivity.class);
i.putExtra("briar.GROUP_ID", groupId.getBytes()); i.putExtra("briar.GROUP_ID", groupId.getBytes());
i.putExtra("briar.GROUP_NAME", group.getName()); i.putExtra("briar.GROUP_NAME", group.getName());
i.putExtra("briar.GROUP_SALT", group.getSalt());
i.putExtra("briar.SUBSCRIBED", true);
startActivity(i); startActivity(i);
} }
} }

View File

@@ -0,0 +1,25 @@
package org.briarproject.android.groups;
import java.util.Collection;
import org.briarproject.api.Contact;
import org.briarproject.api.messaging.Group;
class GroupContacts {
private final Group group;
private final Collection<Contact> contacts;
GroupContacts(Group group, Collection<Contact> contacts) {
this.group = group;
this.contacts = contacts;
}
Group getGroup() {
return group;
}
Collection<Contact> getContacts() {
return contacts;
}
}

View File

@@ -2,10 +2,12 @@ package org.briarproject.android.groups;
import static android.view.Gravity.CENTER; import static android.view.Gravity.CENTER;
import static android.view.Gravity.CENTER_HORIZONTAL; import static android.view.Gravity.CENTER_HORIZONTAL;
import static android.view.Menu.NONE;
import static android.view.View.GONE; import static android.view.View.GONE;
import static android.view.View.VISIBLE; import static android.view.View.VISIBLE;
import static android.widget.LinearLayout.HORIZONTAL; import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL; import static android.widget.LinearLayout.VERTICAL;
import static android.widget.Toast.LENGTH_SHORT;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH; import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
@@ -21,7 +23,6 @@ import javax.inject.Inject;
import org.briarproject.R; import org.briarproject.R;
import org.briarproject.android.BriarActivity; import org.briarproject.android.BriarActivity;
import org.briarproject.android.util.ElasticHorizontalSpace;
import org.briarproject.android.util.HorizontalBorder; import org.briarproject.android.util.HorizontalBorder;
import org.briarproject.android.util.LayoutUtils; import org.briarproject.android.util.LayoutUtils;
import org.briarproject.android.util.ListLoadingProgressBar; import org.briarproject.android.util.ListLoadingProgressBar;
@@ -39,23 +40,30 @@ import org.briarproject.api.event.SubscriptionAddedEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent; import org.briarproject.api.event.SubscriptionRemovedEvent;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId; import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.GroupStatus;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
public class GroupListActivity extends BriarActivity public class GroupListActivity extends BriarActivity
implements EventListener, OnClickListener, OnItemClickListener { implements EventListener, OnClickListener, OnItemClickListener,
OnCreateContextMenuListener {
private static final int MENU_ITEM_UNSUBSCRIBE = 1;
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(GroupListActivity.class.getName()); Logger.getLogger(GroupListActivity.class.getName());
@@ -67,7 +75,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
private ListView list = null; private ListView list = null;
private ListLoadingProgressBar loading = null; private ListLoadingProgressBar loading = null;
private TextView available = null; private TextView available = null;
private ImageButton newGroupButton = null, manageGroupsButton = null; private ImageButton newGroupButton = null;
// Fields that are accessed from background threads must be volatile // Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db; @Inject private volatile DatabaseComponent db;
@@ -96,6 +104,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
list.setLayoutParams(MATCH_WRAP_1); list.setLayoutParams(MATCH_WRAP_1);
list.setAdapter(adapter); list.setAdapter(adapter);
list.setOnItemClickListener(this); list.setOnItemClickListener(this);
list.setOnCreateContextMenuListener(this);
list.setVisibility(GONE); list.setVisibility(GONE);
layout.addView(list); layout.addView(list);
@@ -107,10 +116,10 @@ implements EventListener, OnClickListener, OnItemClickListener {
available.setLayoutParams(MATCH_WRAP); available.setLayoutParams(MATCH_WRAP);
available.setGravity(CENTER); available.setGravity(CENTER);
available.setTextSize(18); available.setTextSize(18);
Resources res = getResources();
int background = res.getColor(R.color.groups_available_background);
available.setBackgroundColor(background);
available.setPadding(pad, pad, pad, pad); available.setPadding(pad, pad, pad, pad);
Resources res = getResources();
int background = res.getColor(R.color.forums_available_background);
available.setBackgroundColor(background);
available.setOnClickListener(this); available.setOnClickListener(this);
available.setVisibility(GONE); available.setVisibility(GONE);
layout.addView(available); layout.addView(available);
@@ -122,21 +131,11 @@ implements EventListener, OnClickListener, OnItemClickListener {
footer.setOrientation(HORIZONTAL); footer.setOrientation(HORIZONTAL);
footer.setGravity(CENTER); footer.setGravity(CENTER);
footer.setBackgroundColor(res.getColor(R.color.button_bar_background)); footer.setBackgroundColor(res.getColor(R.color.button_bar_background));
footer.addView(new ElasticHorizontalSpace(this));
newGroupButton = new ImageButton(this); newGroupButton = new ImageButton(this);
newGroupButton.setBackgroundResource(0); newGroupButton.setBackgroundResource(0);
newGroupButton.setImageResource(R.drawable.social_new_chat); newGroupButton.setImageResource(R.drawable.social_new_chat);
newGroupButton.setOnClickListener(this); newGroupButton.setOnClickListener(this);
footer.addView(newGroupButton); footer.addView(newGroupButton);
footer.addView(new ElasticHorizontalSpace(this));
manageGroupsButton = new ImageButton(this);
manageGroupsButton.setBackgroundResource(0);
manageGroupsButton.setImageResource(R.drawable.action_settings);
manageGroupsButton.setOnClickListener(this);
footer.addView(manageGroupsButton);
footer.addView(new ElasticHorizontalSpace(this));
layout.addView(footer); layout.addView(footer);
setContentView(layout); setContentView(layout);
@@ -154,26 +153,19 @@ implements EventListener, OnClickListener, OnItemClickListener {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
int availableCount = 0;
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
for(GroupStatus s : db.getAvailableGroups()) { for(Group g : db.getGroups()) {
Group g = s.getGroup(); try {
if(s.isSubscribed()) { displayHeaders(g, db.getMessageHeaders(g.getId()));
try { } catch(NoSuchSubscriptionException e) {
Collection<MessageHeader> headers = // Continue
db.getMessageHeaders(g.getId());
displayHeaders(g, headers);
} catch(NoSuchSubscriptionException e) {
// Continue
}
} else {
availableCount++;
} }
} }
int available = db.getAvailableGroups().size();
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Full load took " + duration + " ms"); LOG.info("Full load took " + duration + " ms");
displayAvailable(availableCount); displayAvailable(available);
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(WARNING)) if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e); LOG.log(WARNING, e.toString(), e);
@@ -226,7 +218,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
} else { } else {
available.setVisibility(VISIBLE); available.setVisibility(VISIBLE);
available.setText(getResources().getQuantityString( available.setText(getResources().getQuantityString(
R.plurals.forums_available, availableCount, R.plurals.forums_shared, availableCount,
availableCount)); availableCount));
} }
} }
@@ -279,9 +271,8 @@ implements EventListener, OnClickListener, OnItemClickListener {
} else if(e instanceof SubscriptionRemovedEvent) { } else if(e instanceof SubscriptionRemovedEvent) {
Group g = ((SubscriptionRemovedEvent) e).getGroup(); Group g = ((SubscriptionRemovedEvent) e).getGroup();
if(groups.containsKey(g.getId())) { if(groups.containsKey(g.getId())) {
// Reload the group, expecting NoSuchSubscriptionException
LOG.info("Group removed, reloading"); LOG.info("Group removed, reloading");
loadHeaders(g); loadHeaders();
} }
} }
} }
@@ -330,10 +321,8 @@ implements EventListener, OnClickListener, OnItemClickListener {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
int available = 0;
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
for(GroupStatus s : db.getAvailableGroups()) int available = db.getAvailableGroups().size();
if(!s.isSubscribed()) available++;
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Loading available took " + duration + " ms"); LOG.info("Loading available took " + duration + " ms");
@@ -348,11 +337,9 @@ implements EventListener, OnClickListener, OnItemClickListener {
public void onClick(View view) { public void onClick(View view) {
if(view == available) { if(view == available) {
startActivity(new Intent(this, ManageGroupsActivity.class)); startActivity(new Intent(this, AvailableGroupsActivity.class));
} else if(view == newGroupButton) { } else if(view == newGroupButton) {
startActivity(new Intent(this, CreateGroupActivity.class)); startActivity(new Intent(this, CreateGroupActivity.class));
} else if(view == manageGroupsButton) {
startActivity(new Intent(this, ManageGroupsActivity.class));
} }
} }
@@ -364,4 +351,41 @@ implements EventListener, OnClickListener, OnItemClickListener {
i.putExtra("briar.GROUP_NAME", g.getName()); i.putExtra("briar.GROUP_NAME", g.getName());
startActivity(i); startActivity(i);
} }
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenu.ContextMenuInfo info) {
String delete = getString(R.string.unsubscribe);
menu.add(NONE, MENU_ITEM_UNSUBSCRIBE, NONE, delete);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
if(menuItem.getItemId() == MENU_ITEM_UNSUBSCRIBE) {
ContextMenuInfo info = menuItem.getMenuInfo();
int position = ((AdapterContextMenuInfo) info).position;
GroupListItem item = adapter.getItem(position);
removeSubscription(item.getGroup());
String unsubscribed = getString(R.string.unsubscribed_toast);
Toast.makeText(this, unsubscribed, LENGTH_SHORT).show();
}
return true;
}
private void removeSubscription(final Group g) {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
db.removeGroup(g);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Removing group took " + duration + " ms");
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
} }

View File

@@ -55,7 +55,7 @@ class GroupListAdapter extends ArrayAdapter<GroupListItem> {
TextView noPosts = new TextView(ctx); TextView noPosts = new TextView(ctx);
noPosts.setPadding(pad, 0, pad, pad); noPosts.setPadding(pad, 0, pad, pad);
noPosts.setTextColor(res.getColor(R.color.no_posts)); noPosts.setTextColor(res.getColor(R.color.no_posts));
noPosts.setText(R.string.no_posts); noPosts.setText(R.string.no_forum_posts);
layout.addView(noPosts); layout.addView(noPosts);
} else { } else {
TextView date = new TextView(ctx); TextView date = new TextView(ctx);

View File

@@ -1,72 +0,0 @@
package org.briarproject.android.groups;
import static android.text.TextUtils.TruncateAt.END;
import static android.view.View.INVISIBLE;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import java.util.ArrayList;
import org.briarproject.R;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.messaging.GroupStatus;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
class ManageGroupsAdapter extends ArrayAdapter<ManageGroupsItem> {
private final int pad;
ManageGroupsAdapter(Context ctx) {
super(ctx, android.R.layout.simple_expandable_list_item_1,
new ArrayList<ManageGroupsItem>());
pad = LayoutUtils.getPadding(ctx);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ManageGroupsItem item = getItem(position);
GroupStatus groupStatus = item.getGroupStatus();
Context ctx = getContext();
LinearLayout layout = new LinearLayout(ctx);
layout.setOrientation(HORIZONTAL);
ImageView subscribed = new ImageView(ctx);
subscribed.setPadding(pad, pad, pad, pad);
subscribed.setImageResource(R.drawable.navigation_accept);
if(!groupStatus.isSubscribed()) subscribed.setVisibility(INVISIBLE);
layout.addView(subscribed);
LinearLayout innerLayout = new LinearLayout(ctx);
innerLayout.setOrientation(VERTICAL);
TextView name = new TextView(ctx);
name.setTextSize(18);
name.setSingleLine();
name.setEllipsize(END);
name.setPadding(0, pad, pad, pad);
name.setText(groupStatus.getGroup().getName());
innerLayout.addView(name);
TextView status = new TextView(ctx);
status.setPadding(0, 0, pad, pad);
if(groupStatus.isSubscribed()) {
if(groupStatus.isVisibleToAll())
status.setText(R.string.subscribed_all);
else status.setText(R.string.subscribed_some);
} else {
status.setText(R.string.not_subscribed);
}
innerLayout.addView(status);
layout.addView(innerLayout);
return layout;
}
}

View File

@@ -1,16 +0,0 @@
package org.briarproject.android.groups;
import org.briarproject.api.messaging.GroupStatus;
class ManageGroupsItem {
private final GroupStatus status;
ManageGroupsItem(GroupStatus status) {
this.status = status;
}
GroupStatus getGroupStatus() {
return status;
}
}

View File

@@ -1,16 +0,0 @@
package org.briarproject.android.groups;
import java.util.Comparator;
class ManageGroupsItemComparator implements Comparator<ManageGroupsItem> {
static final ManageGroupsItemComparator INSTANCE =
new ManageGroupsItemComparator();
public int compare(ManageGroupsItem a, ManageGroupsItem b) {
if(a == b) return 0;
String aName = a.getGroupStatus().getGroup().getName();
String bName = b.getGroupStatus().getGroup().getName();
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
}
}

View File

@@ -0,0 +1,202 @@
package org.briarproject.android.groups;
import static android.view.Gravity.CENTER_HORIZONTAL;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static android.widget.LinearLayout.VERTICAL;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.BriarActivity;
import org.briarproject.android.contact.SelectContactsDialog;
import org.briarproject.android.invitation.AddContactActivity;
import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.Contact;
import org.briarproject.api.ContactId;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.messaging.GroupId;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class ShareGroupActivity extends BriarActivity
implements OnClickListener, NoContactsDialog.Listener,
SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(ShareGroupActivity.class.getName());
private String groupName = null;
private RadioGroup radioGroup = null;
private RadioButton shareWithAll = null, shareWithSome = null;
private Button shareButton = null;
private ProgressBar progress = null;
private boolean changed = false;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
private volatile GroupId groupId = null;
private volatile Collection<Contact> contacts = null;
private volatile Collection<ContactId> selected = null;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
byte[] b = i.getByteArrayExtra("briar.GROUP_ID");
if(b == null) throw new IllegalStateException();
groupId = new GroupId(b);
groupName = i.getStringExtra("briar.GROUP_NAME");
if(groupName == null) throw new IllegalStateException();
setTitle(groupName);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_MATCH);
layout.setOrientation(VERTICAL);
layout.setGravity(CENTER_HORIZONTAL);
int pad = LayoutUtils.getPadding(this);
layout.setPadding(pad, pad, pad, pad);
radioGroup = new RadioGroup(this);
radioGroup.setOrientation(VERTICAL);
radioGroup.setPadding(0, 0, 0, pad);
shareWithAll = new RadioButton(this);
shareWithAll.setId(2);
shareWithAll.setText(R.string.forum_share_with_all);
shareWithAll.setOnClickListener(this);
radioGroup.addView(shareWithAll);
shareWithSome = new RadioButton(this);
shareWithSome.setId(3);
shareWithSome.setText(R.string.forum_share_with_some);
shareWithSome.setOnClickListener(this);
radioGroup.addView(shareWithSome);
layout.addView(radioGroup);
shareButton = new Button(this);
shareButton.setLayoutParams(WRAP_WRAP);
shareButton.setText(R.string.share_button);
shareButton.setOnClickListener(this);
layout.addView(shareButton);
progress = new ProgressBar(this);
progress.setLayoutParams(WRAP_WRAP);
progress.setIndeterminate(true);
progress.setVisibility(GONE);
layout.addView(progress);
setContentView(layout);
}
public void onClick(View view) {
if(view == shareWithAll) {
changed = true;
} else if(view == shareWithSome) {
changed = true;
if(contacts == null) loadVisibility();
else displayVisibility();
} else if(view == shareButton) {
if(changed) {
// Replace the button with a progress bar
shareButton.setVisibility(GONE);
progress.setVisibility(VISIBLE);
// Update the group in a background thread
storeVisibility(shareWithAll.isChecked());
} else {
finish();
}
}
}
private void loadVisibility() {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
contacts = db.getContacts();
selected = db.getVisibility(groupId);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayVisibility();
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void displayVisibility() {
runOnUiThread(new Runnable() {
public void run() {
if(contacts.isEmpty()) {
NoContactsDialog builder = new NoContactsDialog();
builder.setListener(ShareGroupActivity.this);
builder.build(ShareGroupActivity.this).show();
} else {
SelectContactsDialog builder = new SelectContactsDialog();
builder.setListener(ShareGroupActivity.this);
builder.setContacts(contacts);
builder.setSelected(selected);
builder.build(ShareGroupActivity.this).show();
}
}
});
}
private void storeVisibility(final boolean all) {
runOnDbThread(new Runnable() {
public void run() {
try {
long now = System.currentTimeMillis();
db.setVisibleToAll(groupId, all);
if(!all) db.setVisibility(groupId, selected);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Update took " + duration + " ms");
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
finishOnUiThread();
}
});
}
public void contactCreationSelected() {
startActivity(new Intent(this, AddContactActivity.class));
}
public void contactCreationCancelled() {
radioGroup.clearCheck();
}
public void contactsSelected(Collection<ContactId> selected) {
this.selected = Collections.unmodifiableCollection(selected);
}
public void contactSelectionCancelled() {
radioGroup.clearCheck();
}
}

View File

@@ -149,7 +149,7 @@ implements OnItemSelectedListener, OnClickListener {
int inputType = TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE int inputType = TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE
| TYPE_TEXT_FLAG_CAP_SENTENCES; | TYPE_TEXT_FLAG_CAP_SENTENCES;
content.setInputType(inputType); content.setInputType(inputType);
content.setHint(R.string.group_post_hint); content.setHint(R.string.forum_post_hint);
layout.addView(content); layout.addView(content);
setContentView(layout); setContentView(layout);

View File

@@ -52,7 +52,7 @@ implements OnEditorActionListener, OnClickListener {
@Inject @CryptoExecutor private Executor cryptoExecutor; @Inject @CryptoExecutor private Executor cryptoExecutor;
private EditText nicknameEntry = null; private EditText nicknameEntry = null;
private Button createButton = null; private Button createIdentityButton = null;
private ProgressBar progress = null; private ProgressBar progress = null;
private TextView feedback = null; private TextView feedback = null;
@@ -96,12 +96,12 @@ implements OnEditorActionListener, OnClickListener {
feedback.setPadding(0, pad, 0, pad); feedback.setPadding(0, pad, 0, pad);
layout.addView(feedback); layout.addView(feedback);
createButton = new Button(this); createIdentityButton = new Button(this);
createButton.setLayoutParams(WRAP_WRAP); createIdentityButton.setLayoutParams(WRAP_WRAP);
createButton.setText(R.string.create_button); createIdentityButton.setText(R.string.create_identity_button);
createButton.setEnabled(false); createIdentityButton.setEnabled(false);
createButton.setOnClickListener(this); createIdentityButton.setOnClickListener(this);
layout.addView(createButton); layout.addView(createIdentityButton);
progress = new ProgressBar(this); progress = new ProgressBar(this);
progress.setLayoutParams(WRAP_WRAP); progress.setLayoutParams(WRAP_WRAP);
@@ -114,7 +114,7 @@ implements OnEditorActionListener, OnClickListener {
private void enableOrDisableCreateButton() { private void enableOrDisableCreateButton() {
if(progress == null) return; // Not created yet if(progress == null) return; // Not created yet
createButton.setEnabled(validateNickname()); createIdentityButton.setEnabled(validateNickname());
} }
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) { public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
@@ -137,7 +137,7 @@ implements OnEditorActionListener, OnClickListener {
hideSoftKeyboard(); hideSoftKeyboard();
if(!validateNickname()) return; if(!validateNickname()) return;
// Replace the button with a progress bar // Replace the button with a progress bar
createButton.setVisibility(GONE); createIdentityButton.setVisibility(GONE);
progress.setVisibility(VISIBLE); progress.setVisibility(VISIBLE);
// Create the identity in a background thread // Create the identity in a background thread
final String nickname = nicknameEntry.getText().toString(); final String nickname = nicknameEntry.getText().toString();

View File

@@ -16,7 +16,6 @@ import org.briarproject.api.TransportProperties;
import org.briarproject.api.messaging.Ack; import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId; import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.GroupStatus;
import org.briarproject.api.messaging.Message; import org.briarproject.api.messaging.Message;
import org.briarproject.api.messaging.MessageId; import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.Offer; import org.briarproject.api.messaging.Offer;
@@ -157,11 +156,8 @@ public interface DatabaseComponent {
Collection<TransportUpdate> generateTransportUpdates(ContactId c, Collection<TransportUpdate> generateTransportUpdates(ContactId c,
int maxLatency) throws DbException; int maxLatency) throws DbException;
/** /** Returns all groups to which the user could subscribe. */
* Returns the status of all groups to which the user subscribes or can Collection<Group> getAvailableGroups() throws DbException;
* subscribe, excluding inbox groups.
*/
Collection<GroupStatus> getAvailableGroups() throws DbException;
/** Returns the configuration for the given transport. */ /** Returns the configuration for the given transport. */
TransportConfig getConfig(TransportId t) throws DbException; TransportConfig getConfig(TransportId t) throws DbException;
@@ -175,7 +171,7 @@ public interface DatabaseComponent {
/** Returns the group with the given ID, if the user subscribes to it. */ /** Returns the group with the given ID, if the user subscribes to it. */
Group getGroup(GroupId g) throws DbException; Group getGroup(GroupId g) throws DbException;
/** Returns all groups to which the user subscribes. */ /** Returns all groups to which the user subscribes, excluding inboxes. */
Collection<Group> getGroups() throws DbException; Collection<Group> getGroups() throws DbException;
/** /**

View File

@@ -1,25 +0,0 @@
package org.briarproject.api.messaging;
public class GroupStatus {
private final Group group;
private final boolean subscribed, visibleToAll;
public GroupStatus(Group group, boolean subscribed, boolean visibleToAll) {
this.group = group;
this.subscribed = subscribed;
this.visibleToAll = visibleToAll;
}
public Group getGroup() {
return group;
}
public boolean isSubscribed() {
return subscribed;
}
public boolean isVisibleToAll() {
return visibleToAll;
}
}

View File

@@ -17,7 +17,6 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.MessageHeader; import org.briarproject.api.db.MessageHeader;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId; import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.GroupStatus;
import org.briarproject.api.messaging.Message; import org.briarproject.api.messaging.Message;
import org.briarproject.api.messaging.MessageId; import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.RetentionAck; import org.briarproject.api.messaging.RetentionAck;
@@ -230,12 +229,11 @@ interface Database<T> {
int countOfferedMessages(T txn, ContactId c) throws DbException; int countOfferedMessages(T txn, ContactId c) throws DbException;
/** /**
* Returns the status of all groups to which the user subscribes or can * Returns all groups to which the user could subscribe.
* subscribe, excluding inbox groups.
* <p> * <p>
* Locking: read. * Locking: read.
*/ */
Collection<GroupStatus> getAvailableGroups(T txn) throws DbException; Collection<Group> getAvailableGroups(T txn) throws DbException;
/** /**
* Returns the configuration for the given transport. * Returns the configuration for the given transport.
@@ -294,7 +292,7 @@ interface Database<T> {
Group getGroup(T txn, GroupId g) throws DbException; Group getGroup(T txn, GroupId g) throws DbException;
/** /**
* Returns all groups to which the user subscribes. * Returns all groups to which the user subscribes, excluding inboxes.
* <p> * <p>
* Locking: read. * Locking: read.
*/ */

View File

@@ -64,7 +64,6 @@ import org.briarproject.api.lifecycle.ShutdownManager;
import org.briarproject.api.messaging.Ack; import org.briarproject.api.messaging.Ack;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId; import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.GroupStatus;
import org.briarproject.api.messaging.Message; import org.briarproject.api.messaging.Message;
import org.briarproject.api.messaging.MessageId; import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.Offer; import org.briarproject.api.messaging.Offer;
@@ -580,12 +579,12 @@ DatabaseCleaner.Callback {
} }
} }
public Collection<GroupStatus> getAvailableGroups() throws DbException { public Collection<Group> getAvailableGroups() throws DbException {
lock.readLock().lock(); lock.readLock().lock();
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
Collection<GroupStatus> groups = db.getAvailableGroups(txn); Collection<Group> groups = db.getAvailableGroups(txn);
db.commitTransaction(txn); db.commitTransaction(txn);
return groups; return groups;
} catch(DbException e) { } catch(DbException e) {

View File

@@ -46,7 +46,6 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.MessageHeader; import org.briarproject.api.db.MessageHeader;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId; import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.GroupStatus;
import org.briarproject.api.messaging.Message; import org.briarproject.api.messaging.Message;
import org.briarproject.api.messaging.MessageId; import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.messaging.RetentionAck; import org.briarproject.api.messaging.RetentionAck;
@@ -1174,35 +1173,12 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public Collection<GroupStatus> getAvailableGroups(Connection txn) public Collection<Group> getAvailableGroups(Connection txn)
throws DbException { throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
// Add all subscribed groups to the list, except inbox groups String sql = "SELECT DISTINCT cg.groupId, cg.name, cg.salt"
String sql = "SELECT DISTINCT g.groupId, name, salt, visibleToAll"
+ " FROM groups AS g"
+ " LEFT OUTER JOIN groupVisibilities AS gv"
+ " ON g.groupId = gv.groupId"
+ " WHERE inbox = FALSE OR inbox IS NULL"
+ " GROUP BY g.groupId";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
List<GroupStatus> groups = new ArrayList<GroupStatus>();
Set<GroupId> ids = new HashSet<GroupId>();
while(rs.next()) {
GroupId id = new GroupId(rs.getBytes(1));
if(!ids.add(id)) throw new DbStateException();
String name = rs.getString(2);
byte[] salt = rs.getBytes(3);
Group group = new Group(id, name, salt);
boolean visibleToAll = rs.getBoolean(4);
groups.add(new GroupStatus(group, true, visibleToAll));
}
rs.close();
ps.close();
// Add all unsubscribed groups to the list
sql = "SELECT DISTINCT cg.groupId, cg.name, cg.salt"
+ " FROM contactGroups AS cg" + " FROM contactGroups AS cg"
+ " LEFT OUTER JOIN groups AS g" + " LEFT OUTER JOIN groups AS g"
+ " ON cg.groupId = g.groupId" + " ON cg.groupId = g.groupId"
@@ -1210,13 +1186,14 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " GROUP BY cg.groupId"; + " GROUP BY cg.groupId";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
rs = ps.executeQuery(); rs = ps.executeQuery();
List<Group> groups = new ArrayList<Group>();
Set<GroupId> ids = new HashSet<GroupId>();
while(rs.next()) { while(rs.next()) {
GroupId id = new GroupId(rs.getBytes(1)); GroupId id = new GroupId(rs.getBytes(1));
if(!ids.add(id)) throw new DbStateException(); if(!ids.add(id)) throw new DbStateException();
String name = rs.getString(2); String name = rs.getString(2);
byte[] salt = rs.getBytes(3); byte[] salt = rs.getBytes(3);
Group group = new Group(id, name, salt); groups.add(new Group(id, name, salt));
groups.add(new GroupStatus(group, false, false));
} }
rs.close(); rs.close();
ps.close(); ps.close();
@@ -1399,7 +1376,12 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT groupId, name, salt FROM groups"; String sql = "SELECT DISTINCT g.groupId, name, salt"
+ " FROM groups AS g"
+ " LEFT OUTER JOIN groupVisibilities AS gv"
+ " ON g.groupId = gv.groupId"
+ " WHERE gv.inbox IS NULL OR gv.inbox = FALSE"
+ " GROUP BY g.groupId";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
rs = ps.executeQuery(); rs = ps.executeQuery();
List<Group> groups = new ArrayList<Group>(); List<Group> groups = new ArrayList<Group>();

View File

@@ -35,7 +35,6 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.MessageHeader; import org.briarproject.api.db.MessageHeader;
import org.briarproject.api.messaging.Group; import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId; import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.GroupStatus;
import org.briarproject.api.messaging.Message; import org.briarproject.api.messaging.Message;
import org.briarproject.api.messaging.MessageId; import org.briarproject.api.messaging.MessageId;
import org.briarproject.api.transport.Endpoint; import org.briarproject.api.transport.Endpoint;
@@ -1430,66 +1429,26 @@ public class H2DatabaseTest extends BriarTestCase {
db.setGroups(txn, contactId, Arrays.asList(group), 1); db.setGroups(txn, contactId, Arrays.asList(group), 1);
db.setGroups(txn, contactId1, Arrays.asList(group), 1); db.setGroups(txn, contactId1, Arrays.asList(group), 1);
// The group should be available, not subscribed, not visible to all // The group should be available
assertEquals(Collections.emptyList(), db.getGroups(txn)); assertEquals(Collections.emptyList(), db.getGroups(txn));
Iterator<GroupStatus> it = db.getAvailableGroups(txn).iterator(); assertEquals(Arrays.asList(group), db.getAvailableGroups(txn));
assertTrue(it.hasNext());
GroupStatus status = it.next();
assertEquals(group, status.getGroup());
assertFalse(status.isSubscribed());
assertFalse(status.isVisibleToAll());
assertFalse(it.hasNext());
// Subscribe to the group - it should be available, subscribed, // Subscribe to the group - it should no longer be available
// not visible to all
db.addGroup(txn, group); db.addGroup(txn, group);
assertEquals(Arrays.asList(group), db.getGroups(txn)); assertEquals(Arrays.asList(group), db.getGroups(txn));
it = db.getAvailableGroups(txn).iterator(); assertEquals(Collections.emptyList(), db.getAvailableGroups(txn));
assertTrue(it.hasNext());
status = it.next();
assertEquals(group, status.getGroup());
assertTrue(status.isSubscribed());
assertFalse(status.isVisibleToAll());
assertFalse(it.hasNext());
// The first contact unsubscribes - the group should be available, // Unsubscribe from the group - it should be available again
// subscribed, not visible to all
db.setGroups(txn, contactId, Collections.<Group>emptyList(), 2);
assertEquals(Arrays.asList(group), db.getGroups(txn));
it = db.getAvailableGroups(txn).iterator();
assertTrue(it.hasNext());
status = it.next();
assertEquals(group, status.getGroup());
assertTrue(status.isSubscribed());
assertFalse(status.isVisibleToAll());
assertFalse(it.hasNext());
// Make the group visible to all contacts - it should be available,
// subscribed, visible to all
db.setVisibleToAll(txn, groupId, true);
assertEquals(Arrays.asList(group), db.getGroups(txn));
it = db.getAvailableGroups(txn).iterator();
assertTrue(it.hasNext());
status = it.next();
assertEquals(group, status.getGroup());
assertTrue(status.isSubscribed());
assertTrue(status.isVisibleToAll());
assertFalse(it.hasNext());
// Unsubscribe from the group - it should be available, not subscribed,
// not visible to all
db.removeGroup(txn, groupId); db.removeGroup(txn, groupId);
assertEquals(Collections.emptyList(), db.getGroups(txn)); assertEquals(Collections.emptyList(), db.getGroups(txn));
it = db.getAvailableGroups(txn).iterator(); assertEquals(Arrays.asList(group), db.getAvailableGroups(txn));
assertTrue(it.hasNext());
status = it.next();
assertEquals(group, status.getGroup());
assertFalse(status.isSubscribed());
assertFalse(status.isVisibleToAll());
assertFalse(it.hasNext());
// The second contact unsubscribes - the group should no longer be // The first contact unsubscribes - it should still be available
// available db.setGroups(txn, contactId, Collections.<Group>emptyList(), 2);
assertEquals(Collections.emptyList(), db.getGroups(txn));
assertEquals(Arrays.asList(group), db.getAvailableGroups(txn));
// The second contact unsubscribes - it should no longer be available
db.setGroups(txn, contactId1, Collections.<Group>emptyList(), 2); db.setGroups(txn, contactId1, Collections.<Group>emptyList(), 2);
assertEquals(Collections.emptyList(), db.getGroups(txn)); assertEquals(Collections.emptyList(), db.getGroups(txn));
assertEquals(Collections.emptyList(), db.getAvailableGroups(txn)); assertEquals(Collections.emptyList(), db.getAvailableGroups(txn));