Changed the root package from net.sf.briar to org.briarproject.

This commit is contained in:
akwizgran
2014-01-08 16:18:30 +00:00
parent dce70f487c
commit 832476412c
427 changed files with 2507 additions and 2507 deletions

View File

@@ -0,0 +1,247 @@
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.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.contact.SelectContactsDialog;
import org.briarproject.android.invitation.AddContactActivity;
import org.briarproject.api.Contact;
import org.briarproject.api.ContactId;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId;
import roboguice.activity.RoboFragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class ConfigureGroupActivity extends RoboFragmentActivity
implements OnClickListener, NoContactsDialog.Listener,
SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(ConfigureGroupActivity.class.getName());
private boolean subscribed = false;
private CheckBox subscribeCheckBox = null;
private RadioGroup radioGroup = null;
private RadioButton visibleToAll = null, visibleToSome = null;
private Button doneButton = null;
private ProgressBar progress = null;
private NoContactsDialog noContactsDialog = null;
private SelectContactsDialog selectContactsDialog = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile Group group = null;
private volatile Collection<ContactId> selected = Collections.emptyList();
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
byte[] b = i.getByteArrayExtra("org.briarproject.GROUP_ID");
if(b == null) throw new IllegalStateException();
GroupId id = new GroupId(b);
String name = i.getStringExtra("org.briarproject.GROUP_NAME");
if(name == null) throw new IllegalStateException();
setTitle(name);
b = i.getByteArrayExtra("org.briarproject.GROUP_SALT");
if(b == null) throw new IllegalStateException();
group = new Group(id, name, b);
subscribed = i.getBooleanExtra("org.briarproject.SUBSCRIBED", false);
boolean all = i.getBooleanExtra("org.briarproject.VISIBLE_TO_ALL", false);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_MATCH);
layout.setOrientation(VERTICAL);
layout.setGravity(CENTER_HORIZONTAL);
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);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentByTag("NoContactsDialog");
if(f == null) noContactsDialog = new NoContactsDialog();
else noContactsDialog = (NoContactsDialog) f;
noContactsDialog.setListener(this);
f = fm.findFragmentByTag("SelectContactsDialog");
if(f == null) selectContactsDialog = new SelectContactsDialog();
else selectContactsDialog = (SelectContactsDialog) f;
selectContactsDialog.setListener(this);
}
public void onClick(View view) {
if(view == subscribeCheckBox) {
boolean subscribe = subscribeCheckBox.isChecked();
visibleToAll.setEnabled(subscribe);
visibleToSome.setEnabled(subscribe);
} else if(view == visibleToSome) {
loadContacts();
} else if(view == doneButton) {
boolean subscribe = subscribeCheckBox.isChecked();
boolean all = visibleToAll.isChecked();
Collection<ContactId> visible =
Collections.unmodifiableCollection(selected);
// Replace the button with a progress bar
doneButton.setVisibility(GONE);
progress.setVisibility(VISIBLE);
// Update the blog in a background thread
if(subscribe || subscribed)
updateGroup(subscribe, subscribed, all, visible);
}
}
private void loadContacts() {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayContacts(contacts);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void displayContacts(final Collection<Contact> contacts) {
runOnUiThread(new Runnable() {
public void run() {
FragmentManager fm = getSupportFragmentManager();
if(contacts.isEmpty()) {
noContactsDialog.show(fm, "NoContactsDialog");
} else {
selectContactsDialog.setContacts(contacts);
selectContactsDialog.show(fm, "SelectContactsDialog");
}
}
});
}
private void updateGroup(final boolean subscribe,
final boolean wasSubscribed, final boolean all,
final Collection<ContactId> visible) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
if(subscribe) {
if(!wasSubscribed) db.addGroup(group);
db.setVisibleToAll(group.getId(), all);
if(!all) db.setVisibility(group.getId(), visible);
} else if(wasSubscribed) {
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);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
runOnUiThread(new Runnable() {
public void run() {
finish();
}
});
}
});
}
public void contactCreationSelected() {
startActivity(new Intent(this, AddContactActivity.class));
}
public void contactCreationCancelled() {}
public void contactsSelected(Collection<ContactId> selected) {
this.selected = selected;
}
public void contactSelectionCancelled() {}
}

View File

@@ -0,0 +1,264 @@
package org.briarproject.android.groups;
import static android.text.InputType.TYPE_CLASS_TEXT;
import static android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
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.view.inputmethod.InputMethodManager.HIDE_IMPLICIT_ONLY;
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.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.contact.SelectContactsDialog;
import org.briarproject.android.invitation.AddContactActivity;
import org.briarproject.api.Contact;
import org.briarproject.api.ContactId;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupFactory;
import roboguice.activity.RoboFragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class CreateGroupActivity extends RoboFragmentActivity
implements OnEditorActionListener, OnClickListener, NoContactsDialog.Listener,
SelectContactsDialog.Listener {
private static final Logger LOG =
Logger.getLogger(CreateGroupActivity.class.getName());
private EditText nameEntry = null;
private RadioGroup radioGroup = null;
private RadioButton visibleToAll = null, visibleToSome = null;
private Button createButton = null;
private ProgressBar progress = null;
private NoContactsDialog noContactsDialog = null;
private SelectContactsDialog selectContactsDialog = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile GroupFactory groupFactory;
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile Collection<ContactId> selected = Collections.emptyList();
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_MATCH);
layout.setOrientation(VERTICAL);
layout.setGravity(CENTER_HORIZONTAL);
TextView chooseName = new TextView(this);
chooseName.setGravity(CENTER);
chooseName.setTextSize(18);
chooseName.setPadding(10, 10, 10, 0);
chooseName.setText(R.string.choose_forum_name);
layout.addView(chooseName);
nameEntry = new EditText(this) {
@Override
protected void onTextChanged(CharSequence text, int start,
int lengthBefore, int lengthAfter) {
enableOrDisableCreateButton();
}
};
nameEntry.setId(1);
nameEntry.setMaxLines(1);
nameEntry.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_FLAG_CAP_SENTENCES);
nameEntry.setOnEditorActionListener(this);
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);
createButton = new Button(this);
createButton.setLayoutParams(WRAP_WRAP);
createButton.setText(R.string.create_button);
createButton.setOnClickListener(this);
layout.addView(createButton);
progress = new ProgressBar(this);
progress.setLayoutParams(WRAP_WRAP);
progress.setIndeterminate(true);
progress.setVisibility(GONE);
layout.addView(progress);
setContentView(layout);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentByTag("NoContactsDialog");
if(f == null) noContactsDialog = new NoContactsDialog();
else noContactsDialog = (NoContactsDialog) f;
noContactsDialog.setListener(this);
f = fm.findFragmentByTag("SelectContactsDialog");
if(f == null) selectContactsDialog = new SelectContactsDialog();
else selectContactsDialog = (SelectContactsDialog) f;
selectContactsDialog.setListener(this);
}
private void enableOrDisableCreateButton() {
if(nameEntry == null || radioGroup == null || createButton == null)
return; // Activity not created yet
boolean nameNotEmpty = nameEntry.getText().length() > 0;
boolean visibilitySelected = radioGroup.getCheckedRadioButtonId() != -1;
createButton.setEnabled(nameNotEmpty && visibilitySelected);
}
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
validateName();
return true;
}
public void onClick(View view) {
if(view == visibleToAll) {
enableOrDisableCreateButton();
} else if(view == visibleToSome) {
loadContacts();
} else if(view == createButton) {
if(!validateName()) return;
final String name = nameEntry.getText().toString();
final boolean all = visibleToAll.isChecked();
final Collection<ContactId> visible =
Collections.unmodifiableCollection(selected);
// Replace the button with a progress bar
createButton.setVisibility(GONE);
progress.setVisibility(VISIBLE);
// Create and store the group in a background thread
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
Group g = groupFactory.createGroup(name);
long now = System.currentTimeMillis();
db.addGroup(g);
if(all) db.setVisibleToAll(g.getId(), true);
else db.setVisibility(g.getId(), visible);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Storing group took " + duration + " ms");
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
runOnUiThread(new Runnable() {
public void run() {
finish();
}
});
}
});
}
}
private void loadContacts() {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<Contact> contacts = db.getContacts();
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayContacts(contacts);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void displayContacts(final Collection<Contact> contacts) {
runOnUiThread(new Runnable() {
public void run() {
FragmentManager fm = getSupportFragmentManager();
if(contacts.isEmpty()) {
noContactsDialog.show(fm, "NoContactsDialog");
} else {
selectContactsDialog.setContacts(contacts);
selectContactsDialog.show(fm, "SelectContactsDialog");
}
}
});
}
private boolean validateName() {
if(nameEntry.getText().toString().equals("")) return false;
// Hide the soft keyboard
Object o = getSystemService(INPUT_METHOD_SERVICE);
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
return true;
}
public void contactCreationSelected() {
startActivity(new Intent(this, AddContactActivity.class));
}
public void contactCreationCancelled() {
enableOrDisableCreateButton();
}
public void contactsSelected(Collection<ContactId> selected) {
this.selected = selected;
enableOrDisableCreateButton();
}
public void contactSelectionCancelled() {
enableOrDisableCreateButton();
}
}

View File

@@ -0,0 +1,237 @@
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.groups.ReadGroupPostActivity.RESULT_NEXT;
import static org.briarproject.android.groups.ReadGroupPostActivity.RESULT_PREV;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP_1;
import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.AscendingHeaderComparator;
import org.briarproject.android.util.HorizontalBorder;
import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.Author;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.MessageHeader;
import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.event.MessageExpiredEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.GroupId;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
public class GroupActivity extends RoboActivity implements EventListener,
OnClickListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(GroupActivity.class.getName());
private String groupName = null;
private GroupAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile GroupId groupId = null;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
byte[] b = i.getByteArrayExtra("org.briarproject.GROUP_ID");
if(b == null) throw new IllegalStateException();
groupId = new GroupId(b);
groupName = i.getStringExtra("org.briarproject.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);
adapter = new GroupAdapter(this);
list = new ListView(this);
// Give me all the width and all the unused height
list.setLayoutParams(MATCH_WRAP_1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
layout.addView(list);
// Show a progress bar while the list is loading
list.setVisibility(GONE);
loading = new ListLoadingProgressBar(this);
layout.addView(loading);
layout.addView(new HorizontalBorder(this));
ImageButton composeButton = new ImageButton(this);
composeButton.setBackgroundResource(0);
composeButton.setImageResource(R.drawable.content_new_email);
composeButton.setOnClickListener(this);
layout.addView(composeButton);
setContentView(layout);
}
@Override
public void onResume() {
super.onResume();
db.addListener(this);
loadHeaders();
}
private void loadHeaders() {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<MessageHeader> headers =
db.getMessageHeaders(groupId);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayHeaders(headers);
} catch(NoSuchSubscriptionException e) {
if(LOG.isLoggable(INFO)) LOG.info("Subscription removed");
runOnUiThread(new Runnable() {
public void run() {
finish();
}
});
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void displayHeaders(final Collection<MessageHeader> headers) {
runOnUiThread(new Runnable() {
public void run() {
list.setVisibility(VISIBLE);
loading.setVisibility(GONE);
adapter.clear();
for(MessageHeader h : headers) adapter.add(h);
adapter.sort(AscendingHeaderComparator.INSTANCE);
adapter.notifyDataSetChanged();
selectFirstUnread();
}
});
}
private void selectFirstUnread() {
int firstUnread = -1, count = adapter.getCount();
for(int i = 0; i < count; i++) {
if(!adapter.getItem(i).isRead()) {
firstUnread = i;
break;
}
}
if(firstUnread == -1) list.setSelection(count - 1);
else list.setSelection(firstUnread);
}
@Override
public void onActivityResult(int request, int result, Intent data) {
if(result == RESULT_PREV) {
int position = request - 1;
if(position >= 0 && position < adapter.getCount())
displayMessage(position);
} else if(result == RESULT_NEXT) {
int position = request + 1;
if(position >= 0 && position < adapter.getCount())
displayMessage(position);
}
}
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
}
public void eventOccurred(Event e) {
if(e instanceof MessageAddedEvent) {
if(((MessageAddedEvent) e).getGroup().getId().equals(groupId)) {
if(LOG.isLoggable(INFO)) LOG.info("Message added, reloading");
loadHeaders();
}
} else if(e instanceof MessageExpiredEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Message expired, reloading");
loadHeaders();
} else if(e instanceof SubscriptionRemovedEvent) {
SubscriptionRemovedEvent s = (SubscriptionRemovedEvent) e;
if(s.getGroup().getId().equals(groupId)) {
if(LOG.isLoggable(INFO)) LOG.info("Subscription removed");
runOnUiThread(new Runnable() {
public void run() {
finish();
}
});
}
}
}
public void onClick(View view) {
Intent i = new Intent(this, WriteGroupPostActivity.class);
i.putExtra("org.briarproject.GROUP_ID", groupId.getBytes());
startActivity(i);
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayMessage(position);
}
private void displayMessage(int position) {
MessageHeader item = adapter.getItem(position);
Intent i = new Intent(this, ReadGroupPostActivity.class);
i.putExtra("org.briarproject.GROUP_ID", groupId.getBytes());
i.putExtra("org.briarproject.GROUP_NAME", groupName);
i.putExtra("org.briarproject.MESSAGE_ID", item.getId().getBytes());
Author author = item.getAuthor();
if(author != null) {
i.putExtra("org.briarproject.AUTHOR_ID", author.getId().getBytes());
i.putExtra("org.briarproject.AUTHOR_NAME", author.getName());
}
i.putExtra("org.briarproject.CONTENT_TYPE", item.getContentType());
i.putExtra("org.briarproject.TIMESTAMP", item.getTimestamp());
startActivityForResult(i, position);
}
}

View File

@@ -0,0 +1,63 @@
package org.briarproject.android.groups;
import static android.widget.LinearLayout.HORIZONTAL;
import static java.text.DateFormat.SHORT;
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP_1;
import java.util.ArrayList;
import org.briarproject.R;
import org.briarproject.api.Author;
import org.briarproject.api.db.MessageHeader;
import android.content.Context;
import android.content.res.Resources;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
class GroupAdapter extends ArrayAdapter<MessageHeader> {
GroupAdapter(Context ctx) {
super(ctx, android.R.layout.simple_expandable_list_item_1,
new ArrayList<MessageHeader>());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MessageHeader item = getItem(position);
Context ctx = getContext();
Resources res = ctx.getResources();
LinearLayout layout = new LinearLayout(ctx);
layout.setOrientation(HORIZONTAL);
if(!item.isRead())
layout.setBackgroundColor(res.getColor(R.color.unread_background));
TextView name = new TextView(ctx);
// Give me all the unused width
name.setLayoutParams(WRAP_WRAP_1);
name.setTextSize(18);
name.setMaxLines(1);
name.setPadding(10, 10, 10, 10);
Author author = item.getAuthor();
if(author == null) {
name.setTextColor(res.getColor(R.color.anonymous_author));
name.setText(R.string.anonymous);
} else {
name.setText(author.getName());
}
layout.addView(name);
TextView date = new TextView(ctx);
date.setTextSize(14);
date.setPadding(10, 10, 10, 10);
long then = item.getTimestamp(), now = System.currentTimeMillis();
date.setText(DateUtils.formatSameDayTime(then, now, SHORT, SHORT));
layout.addView(date);
return layout;
}
}

View File

@@ -0,0 +1,18 @@
package org.briarproject.android.groups;
import org.briarproject.api.messaging.Group;
class GroupItem {
static final GroupItem NEW = new GroupItem(null);
private final Group group;
GroupItem(Group group) {
this.group = group;
}
Group getGroup() {
return group;
}
}

View File

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

View File

@@ -0,0 +1,375 @@
package org.briarproject.android.groups;
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.groups.GroupListItem.MANAGE;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP_1;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.util.HorizontalBorder;
import org.briarproject.android.util.HorizontalSpace;
import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.MessageHeader;
import org.briarproject.api.db.NoSuchSubscriptionException;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.MessageAddedEvent;
import org.briarproject.api.event.MessageExpiredEvent;
import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
import org.briarproject.api.event.SubscriptionAddedEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.GroupStatus;
import roboguice.activity.RoboFragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
public class GroupListActivity extends RoboFragmentActivity
implements EventListener, OnClickListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(GroupListActivity.class.getName());
private final Map<GroupId,GroupId> groups =
new ConcurrentHashMap<GroupId,GroupId>();
private GroupListAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
private ImageButton newGroupButton = null, manageGroupsButton = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_MATCH);
layout.setOrientation(VERTICAL);
layout.setGravity(CENTER_HORIZONTAL);
adapter = new GroupListAdapter(this);
list = new ListView(this);
// Give me all the width and all the unused height
list.setLayoutParams(MATCH_WRAP_1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
layout.addView(list);
// Show a progress bar while the list is loading
list.setVisibility(GONE);
loading = new ListLoadingProgressBar(this);
layout.addView(loading);
layout.addView(new HorizontalBorder(this));
LinearLayout footer = new LinearLayout(this);
footer.setLayoutParams(MATCH_WRAP);
footer.setOrientation(HORIZONTAL);
footer.setGravity(CENTER);
footer.addView(new HorizontalSpace(this));
newGroupButton = new ImageButton(this);
newGroupButton.setBackgroundResource(0);
newGroupButton.setImageResource(R.drawable.social_new_chat);
newGroupButton.setOnClickListener(this);
footer.addView(newGroupButton);
footer.addView(new HorizontalSpace(this));
manageGroupsButton = new ImageButton(this);
manageGroupsButton.setBackgroundResource(0);
manageGroupsButton.setImageResource(R.drawable.action_settings);
manageGroupsButton.setOnClickListener(this);
footer.addView(manageGroupsButton);
footer.addView(new HorizontalSpace(this));
layout.addView(footer);
setContentView(layout);
}
@Override
public void onResume() {
super.onResume();
db.addListener(this);
loadHeaders();
}
private void loadHeaders() {
clearHeaders();
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
int available = 0;
long now = System.currentTimeMillis();
for(GroupStatus s : db.getAvailableGroups()) {
Group g = s.getGroup();
if(s.isSubscribed()) {
try {
Collection<MessageHeader> headers =
db.getMessageHeaders(g.getId());
displayHeaders(g, headers);
} catch(NoSuchSubscriptionException e) {
if(LOG.isLoggable(INFO))
LOG.info("Subscription removed");
}
} else {
available++;
}
}
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Full load took " + duration + " ms");
displayAvailable(available);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void clearHeaders() {
runOnUiThread(new Runnable() {
public void run() {
groups.clear();
list.setVisibility(GONE);
loading.setVisibility(VISIBLE);
adapter.clear();
adapter.notifyDataSetChanged();
}
});
}
private void displayHeaders(final Group g,
final Collection<MessageHeader> headers) {
runOnUiThread(new Runnable() {
public void run() {
GroupId id = g.getId();
groups.put(id, id);
list.setVisibility(VISIBLE);
loading.setVisibility(GONE);
// Remove the old item, if any
GroupListItem item = findGroup(id);
if(item != null) adapter.remove(item);
// Add a new item
adapter.add(new GroupListItem(g, headers));
adapter.sort(ItemComparator.INSTANCE);
adapter.notifyDataSetChanged();
selectFirstUnread();
}
});
}
private void displayAvailable(final int available) {
runOnUiThread(new Runnable() {
public void run() {
list.setVisibility(VISIBLE);
loading.setVisibility(GONE);
adapter.setAvailable(available);
adapter.notifyDataSetChanged();
}
});
}
private GroupListItem findGroup(GroupId g) {
int count = adapter.getCount();
for(int i = 0; i < count; i++) {
GroupListItem item = adapter.getItem(i);
if(item == MANAGE) continue;
if(item.getGroup().getId().equals(g)) return item;
}
return null; // Not found
}
private void selectFirstUnread() {
int firstUnread = -1, count = adapter.getCount();
for(int i = 0; i < count; i++) {
GroupListItem item = adapter.getItem(i);
if(item == MANAGE) continue;
if(item.getUnreadCount() > 0) {
firstUnread = i;
break;
}
}
if(firstUnread == -1) list.setSelection(count - 1);
else list.setSelection(firstUnread);
}
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
}
public void eventOccurred(Event e) {
if(e instanceof MessageAddedEvent) {
Group g = ((MessageAddedEvent) e).getGroup();
if(groups.containsKey(g.getId())) {
if(LOG.isLoggable(INFO)) LOG.info("Message added, reloading");
loadHeaders(g);
}
} else if(e instanceof MessageExpiredEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Message expired, reloading");
loadHeaders();
} else if(e instanceof RemoteSubscriptionsUpdatedEvent) {
if(LOG.isLoggable(INFO))
LOG.info("Remote subscriptions changed, reloading");
loadAvailable();
} else if(e instanceof SubscriptionAddedEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Group added, reloading");
loadHeaders();
} else if(e instanceof SubscriptionRemovedEvent) {
Group g = ((SubscriptionRemovedEvent) e).getGroup();
if(groups.containsKey(g.getId())) {
// Reload the group, expecting NoSuchSubscriptionException
if(LOG.isLoggable(INFO)) LOG.info("Group removed, reloading");
loadHeaders(g);
}
}
}
private void loadHeaders(final Group g) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<MessageHeader> headers =
db.getMessageHeaders(g.getId());
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Partial load took " + duration + " ms");
displayHeaders(g, headers);
} catch(NoSuchSubscriptionException e) {
if(LOG.isLoggable(INFO)) LOG.info("Subscription removed");
removeGroup(g.getId());
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void removeGroup(final GroupId g) {
runOnUiThread(new Runnable() {
public void run() {
GroupListItem item = findGroup(g);
if(item != null) {
groups.remove(g);
adapter.remove(item);
adapter.notifyDataSetChanged();
selectFirstUnread();
}
}
});
}
private void loadAvailable() {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
int available = 0;
long now = System.currentTimeMillis();
for(GroupStatus s : db.getAvailableGroups())
if(!s.isSubscribed()) available++;
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Loading available took " + duration + " ms");
displayAvailable(available);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
public void onClick(View view) {
if(view == newGroupButton) {
startActivity(new Intent(this, CreateGroupActivity.class));
} else if(view == manageGroupsButton) {
startActivity(new Intent(this, ManageGroupsActivity.class));
}
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
GroupListItem item = adapter.getItem(position);
if(item == MANAGE) {
startActivity(new Intent(this, ManageGroupsActivity.class));
} else {
Intent i = new Intent(this, GroupActivity.class);
Group g = item.getGroup();
i.putExtra("org.briarproject.GROUP_ID", g.getId().getBytes());
i.putExtra("org.briarproject.GROUP_NAME", g.getName());
startActivity(i);
}
}
private static class ItemComparator implements Comparator<GroupListItem> {
private static final ItemComparator INSTANCE = new ItemComparator();
public int compare(GroupListItem a, GroupListItem b) {
if(a == b) return 0;
// The manage groups item comes last
if(a == MANAGE) return 1;
if(b == MANAGE) return -1;
// The item with the newest message comes first
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
if(aTime > bTime) return -1;
if(aTime < bTime) return 1;
// Break ties by group name
String aName = a.getGroup().getName();
String bName = b.getGroup().getName();
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
}
}
}

View File

@@ -0,0 +1,121 @@
package org.briarproject.android.groups;
import static android.view.Gravity.CENTER;
import static android.widget.LinearLayout.HORIZONTAL;
import static java.text.DateFormat.SHORT;
import static org.briarproject.android.groups.GroupListItem.MANAGE;
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP_1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.briarproject.R;
import android.content.Context;
import android.content.res.Resources;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
class GroupListAdapter extends BaseAdapter {
private final Context ctx;
private final List<GroupListItem> list = new ArrayList<GroupListItem>();
private int available = 0;
GroupListAdapter(Context ctx) {
this.ctx = ctx;
}
public void setAvailable(int available) {
this.available = available;
}
public void add(GroupListItem item) {
list.add(item);
}
public void clear() {
list.clear();
}
public int getCount() {
return available == 0 ? list.size() : list.size() + 1;
}
public GroupListItem getItem(int position) {
return position == list.size() ? MANAGE : list.get(position);
}
public long getItemId(int position) {
return android.R.layout.simple_expandable_list_item_1;
}
public View getView(int position, View convertView, ViewGroup parent) {
GroupListItem item = getItem(position);
Resources res = ctx.getResources();
if(item == MANAGE) {
TextView manage = new TextView(ctx);
manage.setGravity(CENTER);
manage.setTextSize(18);
manage.setPadding(10, 10, 10, 10);
String format = res.getQuantityString(R.plurals.forums_available,
available);
manage.setText(String.format(format, available));
return manage;
}
LinearLayout layout = new LinearLayout(ctx);
layout.setOrientation(HORIZONTAL);
if(item.getUnreadCount() > 0)
layout.setBackgroundColor(res.getColor(R.color.unread_background));
TextView name = new TextView(ctx);
// Give me all the unused width
name.setLayoutParams(WRAP_WRAP_1);
name.setTextSize(18);
name.setMaxLines(1);
name.setPadding(10, 10, 10, 10);
int unread = item.getUnreadCount();
String groupName = item.getGroup().getName();
if(unread > 0) name.setText(groupName + " (" + unread + ")");
else name.setText(groupName);
layout.addView(name);
if(item.isEmpty()) {
TextView noPosts = new TextView(ctx);
noPosts.setTextSize(14);
noPosts.setPadding(10, 0, 10, 10);
noPosts.setTextColor(res.getColor(R.color.no_posts));
noPosts.setText(R.string.no_posts);
layout.addView(noPosts);
} else {
TextView date = new TextView(ctx);
date.setTextSize(14);
date.setPadding(10, 0, 10, 10);
long then = item.getTimestamp(), now = System.currentTimeMillis();
date.setText(DateUtils.formatSameDayTime(then, now, SHORT, SHORT));
layout.addView(date);
}
return layout;
}
@Override
public boolean isEmpty() {
return list.isEmpty() && available == 0;
}
public void remove(GroupListItem item) {
list.remove(item);
}
public void sort(Comparator<GroupListItem> comparator) {
Collections.sort(list, comparator);
}
}

View File

@@ -0,0 +1,72 @@
package org.briarproject.android.groups;
import java.util.Collection;
import java.util.Collections;
import org.briarproject.api.Author;
import org.briarproject.api.db.MessageHeader;
import org.briarproject.api.messaging.Group;
class GroupListItem {
static final GroupListItem MANAGE = new GroupListItem(null,
Collections.<MessageHeader>emptyList());
private final Group group;
private final boolean empty;
private final String authorName, contentType;
private final long timestamp;
private final int unread;
GroupListItem(Group group, Collection<MessageHeader> headers) {
this.group = group;
empty = headers.isEmpty();
if(empty) {
authorName = null;
contentType = null;
timestamp = 0;
unread = 0;
} else {
MessageHeader newest = null;
long timestamp = 0;
int unread = 0;
for(MessageHeader h : headers) {
if(h.getTimestamp() > timestamp) {
timestamp = h.getTimestamp();
newest = h;
}
if(!h.isRead()) unread++;
}
Author a = newest.getAuthor();
if(a == null) authorName = null;
else authorName = a.getName();
contentType = newest.getContentType();
this.timestamp = newest.getTimestamp();
this.unread = unread;
}
}
Group getGroup() {
return group;
}
boolean isEmpty() {
return empty;
}
String getAuthorName() {
return authorName;
}
String getContentType() {
return contentType;
}
long getTimestamp() {
return timestamp;
}
int getUnreadCount() {
return unread;
}
}

View File

@@ -0,0 +1,157 @@
package org.briarproject.android.groups;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.groups.ManageGroupsItem.NONE;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_MATCH;
import java.util.Collection;
import java.util.Comparator;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.RemoteSubscriptionsUpdatedEvent;
import org.briarproject.api.event.SubscriptionAddedEvent;
import org.briarproject.api.event.SubscriptionRemovedEvent;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupStatus;
import roboguice.activity.RoboFragmentActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class ManageGroupsActivity extends RoboFragmentActivity
implements EventListener, OnItemClickListener {
private static final Logger LOG =
Logger.getLogger(ManageGroupsActivity.class.getName());
private ManageGroupsAdapter adapter = null;
private ListView list = null;
private ListLoadingProgressBar loading = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
adapter = new ManageGroupsAdapter(this);
list = new ListView(this);
list.setLayoutParams(MATCH_MATCH);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
// Show a progress bar while the list is loading
loading = new ListLoadingProgressBar(this);
setContentView(loading);
}
@Override
public void onResume() {
super.onResume();
db.addListener(this);
loadGroups();
}
private void loadGroups() {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<GroupStatus> available = db.getAvailableGroups();
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayGroups(available);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void displayGroups(final Collection<GroupStatus> available) {
runOnUiThread(new Runnable() {
public void run() {
setContentView(list);
adapter.clear();
for(GroupStatus s : available)
adapter.add(new ManageGroupsItem(s));
adapter.sort(ItemComparator.INSTANCE);
adapter.notifyDataSetChanged();
}
});
}
@Override
public void onPause() {
super.onPause();
db.removeListener(this);
}
public void eventOccurred(Event e) {
if(e instanceof RemoteSubscriptionsUpdatedEvent) {
if(LOG.isLoggable(INFO))
LOG.info("Remote subscriptions changed, reloading");
loadGroups();
} else if(e instanceof SubscriptionAddedEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Group added, reloading");
loadGroups();
} else if(e instanceof SubscriptionRemovedEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Group removed, reloading");
loadGroups();
}
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ManageGroupsItem item = adapter.getItem(position);
if(item == NONE) return;
GroupStatus s = item.getGroupStatus();
Group g = s.getGroup();
Intent i = new Intent(this, ConfigureGroupActivity.class);
i.putExtra("org.briarproject.GROUP_ID", g.getId().getBytes());
i.putExtra("org.briarproject.GROUP_NAME", g.getName());
i.putExtra("org.briarproject.GROUP_SALT", g.getSalt());
i.putExtra("org.briarproject.SUBSCRIBED", s.isSubscribed());
i.putExtra("org.briarproject.VISIBLE_TO_ALL", s.isVisibleToAll());
startActivity(i);
}
private static class ItemComparator
implements Comparator<ManageGroupsItem> {
private static final ItemComparator INSTANCE = new ItemComparator();
public int compare(ManageGroupsItem a, ManageGroupsItem b) {
if(a == b) return 0;
if(a == NONE) return 1;
if(b == NONE) return -1;
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,113 @@
package org.briarproject.android.groups;
import static android.view.Gravity.CENTER;
import static android.view.View.INVISIBLE;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import static org.briarproject.android.groups.ManageGroupsItem.NONE;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.briarproject.R;
import org.briarproject.api.messaging.GroupStatus;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
class ManageGroupsAdapter extends BaseAdapter {
private final Context ctx;
private final List<ManageGroupsItem> list =
new ArrayList<ManageGroupsItem>();
ManageGroupsAdapter(Context ctx) {
this.ctx = ctx;
}
public void add(ManageGroupsItem item) {
list.add(item);
}
public void clear() {
list.clear();
}
public int getCount() {
return list.isEmpty() ? 1 : list.size();
}
public ManageGroupsItem getItem(int position) {
return list.isEmpty() ? NONE : list.get(position);
}
public long getItemId(int position) {
return android.R.layout.simple_expandable_list_item_1;
}
public View getView(int position, View convertView, ViewGroup parent) {
ManageGroupsItem item = getItem(position);
if(item == NONE) {
TextView none = new TextView(ctx);
none.setGravity(CENTER);
none.setTextSize(18);
none.setPadding(10, 10, 10, 10);
none.setText(R.string.no_forums_available);
return none;
}
GroupStatus s = item.getGroupStatus();
LinearLayout layout = new LinearLayout(ctx);
layout.setOrientation(HORIZONTAL);
ImageView subscribed = new ImageView(ctx);
subscribed.setPadding(5, 5, 5, 5);
subscribed.setImageResource(R.drawable.navigation_accept);
if(!s.isSubscribed()) subscribed.setVisibility(INVISIBLE);
layout.addView(subscribed);
LinearLayout innerLayout = new LinearLayout(ctx);
innerLayout.setOrientation(VERTICAL);
TextView name = new TextView(ctx);
name.setTextSize(18);
name.setMaxLines(1);
name.setPadding(0, 10, 10, 10);
name.setText(s.getGroup().getName());
innerLayout.addView(name);
TextView status = new TextView(ctx);
status.setTextSize(14);
status.setPadding(0, 0, 10, 10);
if(s.isSubscribed()) {
if(s.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;
}
@Override
public boolean isEmpty() {
return false;
}
public void remove(ManageGroupsItem item) {
list.remove(item);
}
public void sort(Comparator<ManageGroupsItem> comparator) {
Collections.sort(list, comparator);
}
}

View File

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

View File

@@ -0,0 +1,43 @@
package org.briarproject.android.groups;
import org.briarproject.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class NoContactsDialog extends DialogFragment {
private Listener listener = null;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
public Dialog onCreateDialog(Bundle state) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.no_contacts);
builder.setPositiveButton(R.string.add_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.contactCreationSelected();
}
});
builder.setNegativeButton(R.string.cancel_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.contactCreationCancelled();
}
});
return builder.create();
}
public interface Listener {
void contactCreationSelected();
void contactCreationCancelled();
}
}

View File

@@ -0,0 +1,276 @@
package org.briarproject.android.groups;
import static android.view.Gravity.CENTER;
import static android.view.Gravity.CENTER_VERTICAL;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import static java.text.DateFormat.SHORT;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP_1;
import static org.briarproject.android.util.CommonLayoutParams.WRAP_WRAP_1;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.util.HorizontalBorder;
import org.briarproject.android.util.HorizontalSpace;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchMessageException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.MessageId;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class ReadGroupPostActivity extends RoboActivity
implements OnClickListener {
static final int RESULT_REPLY = RESULT_FIRST_USER;
static final int RESULT_PREV = RESULT_FIRST_USER + 1;
static final int RESULT_NEXT = RESULT_FIRST_USER + 2;
private static final Logger LOG =
Logger.getLogger(ReadGroupPostActivity.class.getName());
private GroupId groupId = null;
private boolean read;
private ImageButton readButton = null, prevButton = null, nextButton = null;
private ImageButton replyButton = null;
private TextView content = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
private volatile MessageId messageId = null;
private volatile long timestamp = -1;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
byte[] b = i.getByteArrayExtra("org.briarproject.GROUP_ID");
if(b == null) throw new IllegalStateException();
groupId = new GroupId(b);
String groupName = i.getStringExtra("org.briarproject.GROUP_NAME");
if(groupName == null) throw new IllegalStateException();
setTitle(groupName);
b = i.getByteArrayExtra("org.briarproject.MESSAGE_ID");
if(b == null) throw new IllegalStateException();
messageId = new MessageId(b);
String contentType = i.getStringExtra("org.briarproject.CONTENT_TYPE");
if(contentType == null) throw new IllegalStateException();
timestamp = i.getLongExtra("org.briarproject.TIMESTAMP", -1);
if(timestamp == -1) throw new IllegalStateException();
String authorName = i.getStringExtra("org.briarproject.AUTHOR_NAME");
if(state == null) {
read = false;
setReadInDatabase(true);
} else {
read = state.getBoolean("org.briarproject.READ");
}
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_WRAP);
layout.setOrientation(VERTICAL);
ScrollView scrollView = new ScrollView(this);
// Give me all the width and all the unused height
scrollView.setLayoutParams(MATCH_WRAP_1);
LinearLayout message = new LinearLayout(this);
message.setOrientation(VERTICAL);
Resources res = getResources();
message.setBackgroundColor(res.getColor(R.color.content_background));
LinearLayout header = new LinearLayout(this);
header.setLayoutParams(MATCH_WRAP);
header.setOrientation(HORIZONTAL);
header.setGravity(CENTER_VERTICAL);
TextView name = new TextView(this);
// Give me all the unused width
name.setLayoutParams(WRAP_WRAP_1);
name.setTextSize(18);
name.setMaxLines(1);
name.setPadding(10, 10, 10, 10);
if(authorName == null) {
name.setTextColor(res.getColor(R.color.anonymous_author));
name.setText(R.string.anonymous);
} else {
name.setText(authorName);
}
header.addView(name);
TextView date = new TextView(this);
date.setTextSize(14);
date.setPadding(0, 10, 10, 10);
long now = System.currentTimeMillis();
date.setText(DateUtils.formatSameDayTime(timestamp, now, SHORT, SHORT));
header.addView(date);
message.addView(header);
if(contentType.equals("text/plain")) {
// Load and display the message body
content = new TextView(this);
content.setPadding(10, 0, 10, 10);
message.addView(content);
loadMessageBody();
}
scrollView.addView(message);
layout.addView(scrollView);
layout.addView(new HorizontalBorder(this));
LinearLayout footer = new LinearLayout(this);
footer.setLayoutParams(MATCH_WRAP);
footer.setOrientation(HORIZONTAL);
footer.setGravity(CENTER);
readButton = new ImageButton(this);
readButton.setBackgroundResource(0);
if(read) readButton.setImageResource(R.drawable.content_unread);
else readButton.setImageResource(R.drawable.content_read);
readButton.setOnClickListener(this);
footer.addView(readButton);
footer.addView(new HorizontalSpace(this));
prevButton = new ImageButton(this);
prevButton.setBackgroundResource(0);
prevButton.setImageResource(R.drawable.navigation_previous_item);
prevButton.setOnClickListener(this);
footer.addView(prevButton);
footer.addView(new HorizontalSpace(this));
nextButton = new ImageButton(this);
nextButton.setBackgroundResource(0);
nextButton.setImageResource(R.drawable.navigation_next_item);
nextButton.setOnClickListener(this);
footer.addView(nextButton);
footer.addView(new HorizontalSpace(this));
replyButton = new ImageButton(this);
replyButton.setBackgroundResource(0);
replyButton.setImageResource(R.drawable.social_reply_all);
replyButton.setOnClickListener(this);
footer.addView(replyButton);
layout.addView(footer);
setContentView(layout);
}
private void setReadInDatabase(final boolean read) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.setReadFlag(messageId, read);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Setting flag took " + duration + " ms");
setReadInUi(read);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void setReadInUi(final boolean read) {
runOnUiThread(new Runnable() {
public void run() {
ReadGroupPostActivity.this.read = read;
if(read) readButton.setImageResource(R.drawable.content_unread);
else readButton.setImageResource(R.drawable.content_read);
}
});
}
private void loadMessageBody() {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
byte[] body = db.getMessageBody(messageId);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Loading message took " + duration + " ms");
final String text = new String(body, "UTF-8");
runOnUiThread(new Runnable() {
public void run() {
content.setText(text);
}
});
} catch(NoSuchMessageException e) {
if(LOG.isLoggable(INFO)) LOG.info("Message removed");
runOnUiThread(new Runnable() {
public void run() {
finish();
}
});
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
});
}
@Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putBoolean("org.briarproject.READ", read);
}
public void onClick(View view) {
if(view == readButton) {
setReadInDatabase(!read);
} else if(view == prevButton) {
setResult(RESULT_PREV);
finish();
} else if(view == nextButton) {
setResult(RESULT_NEXT);
finish();
} else if(view == replyButton) {
Intent i = new Intent(this, WriteGroupPostActivity.class);
i.putExtra("org.briarproject.GROUP_ID", groupId.getBytes());
i.putExtra("org.briarproject.PARENT_ID", messageId.getBytes());
i.putExtra("org.briarproject.TIMESTAMP", timestamp);
startActivity(i);
setResult(RESULT_REPLY);
finish();
}
}
}

View File

@@ -0,0 +1,299 @@
package org.briarproject.android.groups;
import static android.text.InputType.TYPE_CLASS_TEXT;
import static android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
import static android.view.Gravity.CENTER_VERTICAL;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
import static android.widget.Toast.LENGTH_LONG;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.util.CommonLayoutParams.MATCH_WRAP;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import org.briarproject.R;
import org.briarproject.android.identity.CreateIdentityActivity;
import org.briarproject.android.identity.LocalAuthorItem;
import org.briarproject.android.identity.LocalAuthorItemComparator;
import org.briarproject.android.identity.LocalAuthorSpinnerAdapter;
import org.briarproject.android.util.HorizontalSpace;
import org.briarproject.api.AuthorId;
import org.briarproject.api.LocalAuthor;
import org.briarproject.api.android.DatabaseUiExecutor;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.crypto.KeyParser;
import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.messaging.Group;
import org.briarproject.api.messaging.GroupId;
import org.briarproject.api.messaging.Message;
import org.briarproject.api.messaging.MessageFactory;
import org.briarproject.api.messaging.MessageId;
import roboguice.activity.RoboActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class WriteGroupPostActivity extends RoboActivity
implements OnItemSelectedListener, OnClickListener {
private static final Logger LOG =
Logger.getLogger(WriteGroupPostActivity.class.getName());
private LocalAuthorSpinnerAdapter adapter = null;
private Spinner spinner = null;
private ImageButton sendButton = null;
private TextView to = null;
private EditText content = null;
private AuthorId localAuthorId = null;
private GroupId groupId = null;
// Fields that are accessed from background threads must be volatile
@Inject private volatile DatabaseComponent db;
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
@Inject private volatile LifecycleManager lifecycleManager;
@Inject @CryptoExecutor private volatile Executor cryptoExecutor;
@Inject private volatile CryptoComponent crypto;
@Inject private volatile MessageFactory messageFactory;
private volatile MessageId parentId = null;
private volatile long timestamp = -1;
private volatile LocalAuthor localAuthor = null;
private volatile Group group = null;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
Intent i = getIntent();
byte[] b = i.getByteArrayExtra("org.briarproject.GROUP_ID");
if(b == null) throw new IllegalStateException();
groupId = new GroupId(b);
b = i.getByteArrayExtra("org.briarproject.PARENT_ID");
if(b != null) parentId = new MessageId(b);
timestamp = i.getLongExtra("org.briarproject.TIMESTAMP", -1);
if(state != null) {
b = state.getByteArray("org.briarproject.LOCAL_AUTHOR_ID");
if(b != null) localAuthorId = new AuthorId(b);
}
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_WRAP);
layout.setOrientation(VERTICAL);
LinearLayout header = new LinearLayout(this);
header.setLayoutParams(MATCH_WRAP);
header.setOrientation(HORIZONTAL);
header.setGravity(CENTER_VERTICAL);
TextView from = new TextView(this);
from.setTextSize(18);
from.setPadding(10, 10, 0, 10);
from.setText(R.string.from);
header.addView(from);
adapter = new LocalAuthorSpinnerAdapter(this, true);
spinner = new Spinner(this);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
header.addView(spinner);
header.addView(new HorizontalSpace(this));
sendButton = new ImageButton(this);
sendButton.setBackgroundResource(0);
sendButton.setImageResource(R.drawable.social_send_now);
sendButton.setEnabled(false); // Enabled after loading the group
sendButton.setOnClickListener(this);
header.addView(sendButton);
layout.addView(header);
to = new TextView(this);
to.setTextSize(18);
to.setPadding(10, 0, 10, 10);
to.setText(R.string.to);
layout.addView(to);
content = new EditText(this);
content.setId(1);
int inputType = TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE
| TYPE_TEXT_FLAG_CAP_SENTENCES;
content.setInputType(inputType);
layout.addView(content);
setContentView(layout);
}
@Override
public void onResume() {
super.onResume();
loadAuthorsAndGroup();
}
private void loadAuthorsAndGroup() {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
Collection<LocalAuthor> localAuthors = db.getLocalAuthors();
group = db.getGroup(groupId);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Load took " + duration + " ms");
displayAuthorsAndGroup(localAuthors);
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
private void displayAuthorsAndGroup(
final Collection<LocalAuthor> localAuthors) {
runOnUiThread(new Runnable() {
public void run() {
if(localAuthors.isEmpty()) throw new IllegalStateException();
adapter.clear();
for(LocalAuthor a : localAuthors)
adapter.add(new LocalAuthorItem(a));
adapter.sort(LocalAuthorItemComparator.INSTANCE);
adapter.notifyDataSetChanged();
int count = adapter.getCount();
for(int i = 0; i < count; i++) {
LocalAuthorItem item = adapter.getItem(i);
if(item == LocalAuthorItem.ANONYMOUS) continue;
if(item == LocalAuthorItem.NEW) continue;
if(item.getLocalAuthor().getId().equals(localAuthorId)) {
localAuthor = item.getLocalAuthor();
spinner.setSelection(i);
break;
}
}
Resources res = getResources();
String format = res.getString(R.string.format_to);
to.setText(String.format(format, group.getName()));
sendButton.setEnabled(true);
}
});
}
@Override
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
if(localAuthorId != null) {
byte[] b = localAuthorId.getBytes();
state.putByteArray("org.briarproject.LOCAL_AUTHOR_ID", b);
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
LocalAuthorItem item = adapter.getItem(position);
if(item == LocalAuthorItem.ANONYMOUS) {
localAuthor = null;
localAuthorId = null;
} else if(item == LocalAuthorItem.NEW) {
localAuthor = null;
localAuthorId = null;
startActivity(new Intent(this, CreateIdentityActivity.class));
} else {
localAuthor = item.getLocalAuthor();
localAuthorId = localAuthor.getId();
}
}
public void onNothingSelected(AdapterView<?> parent) {
localAuthor = null;
localAuthorId = null;
}
public void onClick(View view) {
if(group == null) throw new IllegalStateException();
try {
createMessage(content.getText().toString().getBytes("UTF-8"));
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
Toast.makeText(this, R.string.post_sent_toast, LENGTH_LONG).show();
finish();
}
private void createMessage(final byte[] body) {
cryptoExecutor.execute(new Runnable() {
public void run() {
// Don't use an earlier timestamp than the parent
long time = System.currentTimeMillis();
time = Math.max(time, timestamp + 1);
Message m;
try {
if(localAuthor == null) {
m = messageFactory.createAnonymousMessage(parentId,
group, "text/plain", time, body);
} else {
KeyParser keyParser = crypto.getSignatureKeyParser();
byte[] b = localAuthor.getPrivateKey();
PrivateKey authorKey = keyParser.parsePrivateKey(b);
m = messageFactory.createPseudonymousMessage(parentId,
group, localAuthor, authorKey, "text/plain",
time, body);
}
} catch(GeneralSecurityException e) {
throw new RuntimeException(e);
} catch(IOException e) {
throw new RuntimeException(e);
}
storeMessage(m);
}
});
}
private void storeMessage(final Message m) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
lifecycleManager.waitForDatabase();
long now = System.currentTimeMillis();
db.addLocalMessage(m);
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Storing message took " + duration + " ms");
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
} catch(InterruptedException e) {
if(LOG.isLoggable(INFO))
LOG.info("Interrupted while waiting for database");
Thread.currentThread().interrupt();
}
}
});
}
}