mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Added activities for managing blog and group subscriptions.
This commit is contained in:
@@ -74,7 +74,7 @@ public class HomeScreenActivity extends BriarActivity {
|
||||
@Inject @DatabaseUiExecutor private Executor dbUiExecutor = null;
|
||||
@Inject @CryptoExecutor private Executor cryptoExecutor = null;
|
||||
private boolean bound = false;
|
||||
private TextView tryAgain = null;
|
||||
private TextView enterPassword = null;
|
||||
private Button continueButton = null;
|
||||
private ProgressBar progress = null;
|
||||
|
||||
@@ -194,7 +194,7 @@ public class HomeScreenActivity extends BriarActivity {
|
||||
layout.setOrientation(VERTICAL);
|
||||
layout.setGravity(CENTER_HORIZONTAL);
|
||||
|
||||
TextView enterPassword = new TextView(this);
|
||||
enterPassword = new TextView(this);
|
||||
enterPassword.setGravity(CENTER);
|
||||
enterPassword.setTextSize(18);
|
||||
enterPassword.setPadding(10, 10, 10, 10);
|
||||
@@ -214,14 +214,6 @@ public class HomeScreenActivity extends BriarActivity {
|
||||
});
|
||||
layout.addView(passwordEntry);
|
||||
|
||||
tryAgain = new TextView(this);
|
||||
tryAgain.setGravity(CENTER);
|
||||
tryAgain.setTextSize(14);
|
||||
tryAgain.setPadding(10, 10, 10, 10);
|
||||
tryAgain.setText(R.string.try_again);
|
||||
tryAgain.setVisibility(GONE);
|
||||
layout.addView(tryAgain);
|
||||
|
||||
continueButton = new Button(this);
|
||||
continueButton.setLayoutParams(WRAP_WRAP);
|
||||
continueButton.setText(R.string.continue_button);
|
||||
@@ -241,7 +233,7 @@ public class HomeScreenActivity extends BriarActivity {
|
||||
}
|
||||
|
||||
private void validatePassword(final byte[] encrypted, Editable e) {
|
||||
if(tryAgain == null || continueButton == null || progress == null)
|
||||
if(enterPassword == null || continueButton == null || progress == null)
|
||||
return;
|
||||
// Hide the soft keyboard
|
||||
Object o = getSystemService(INPUT_METHOD_SERVICE);
|
||||
@@ -270,7 +262,7 @@ public class HomeScreenActivity extends BriarActivity {
|
||||
private void tryAgain() {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
tryAgain.setVisibility(VISIBLE);
|
||||
enterPassword.setText(R.string.try_again);
|
||||
continueButton.setVisibility(VISIBLE);
|
||||
progress.setVisibility(GONE);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package net.sf.briar.android;
|
||||
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.widget.LinearLayout.HORIZONTAL;
|
||||
import static android.widget.LinearLayout.VERTICAL;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.sf.briar.R;
|
||||
import net.sf.briar.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.ListAdapter;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class ManageGroupsAdapter extends ArrayAdapter<GroupStatus>
|
||||
implements ListAdapter {
|
||||
|
||||
public ManageGroupsAdapter(Context ctx) {
|
||||
super(ctx, android.R.layout.simple_expandable_list_item_1,
|
||||
new ArrayList<GroupStatus>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
final GroupStatus item = getItem(position);
|
||||
Context ctx = getContext();
|
||||
|
||||
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(!item.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(item.getGroup().getName());
|
||||
innerLayout.addView(name);
|
||||
|
||||
TextView status = new TextView(ctx);
|
||||
status.setTextSize(14);
|
||||
status.setPadding(0, 0, 10, 10);
|
||||
if(item.isSubscribed()) {
|
||||
if(item.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;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ 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 net.sf.briar.android.blogs.BlogListItem.MANAGE;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_MATCH;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_WRAP;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_WRAP_1;
|
||||
@@ -32,13 +33,18 @@ import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.db.event.GroupMessageAddedEvent;
|
||||
import net.sf.briar.api.db.event.MessageExpiredEvent;
|
||||
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
|
||||
import net.sf.briar.api.messaging.Group;
|
||||
import net.sf.briar.api.messaging.GroupId;
|
||||
import net.sf.briar.api.messaging.GroupStatus;
|
||||
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;
|
||||
@@ -46,7 +52,8 @@ import android.widget.ListView;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class BlogListActivity extends BriarFragmentActivity
|
||||
implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
implements DatabaseListener, OnClickListener, NoBlogsDialog.Listener,
|
||||
OnItemClickListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(BlogListActivity.class.getName());
|
||||
@@ -57,6 +64,7 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
private BlogListAdapter adapter = null;
|
||||
private ListView list = null;
|
||||
private ImageButton newBlogButton = null, composeButton = null;
|
||||
private ImageButton manageBlogsButton = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
@@ -75,7 +83,7 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
// Give me all the width and all the unused height
|
||||
list.setLayoutParams(MATCH_WRAP_1);
|
||||
list.setAdapter(adapter);
|
||||
list.setOnItemClickListener(adapter);
|
||||
list.setOnItemClickListener(this);
|
||||
layout.addView(list);
|
||||
|
||||
layout.addView(new HorizontalBorder(this));
|
||||
@@ -99,6 +107,13 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
composeButton.setOnClickListener(this);
|
||||
footer.addView(composeButton);
|
||||
footer.addView(new HorizontalSpace(this));
|
||||
|
||||
manageBlogsButton = new ImageButton(this);
|
||||
manageBlogsButton.setBackgroundResource(0);
|
||||
manageBlogsButton.setImageResource(R.drawable.action_settings);
|
||||
manageBlogsButton.setOnClickListener(this);
|
||||
footer.addView(manageBlogsButton);
|
||||
footer.addView(new HorizontalSpace(this));
|
||||
layout.addView(footer);
|
||||
|
||||
setContentView(layout);
|
||||
@@ -124,21 +139,28 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
long now = System.currentTimeMillis();
|
||||
Set<GroupId> local = new HashSet<GroupId>();
|
||||
for(Group g : db.getLocalGroups()) local.add(g.getId());
|
||||
for(Group g : db.getSubscriptions()) {
|
||||
int available = 0;
|
||||
for(GroupStatus s : db.getAvailableGroups()) {
|
||||
Group g = s.getGroup();
|
||||
if(!g.isRestricted()) continue;
|
||||
boolean postable = local.contains(g.getId());
|
||||
try {
|
||||
Collection<GroupMessageHeader> headers =
|
||||
db.getGroupMessageHeaders(g.getId());
|
||||
displayHeaders(g, postable, headers);
|
||||
} catch(NoSuchSubscriptionException e) {
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Subscription removed");
|
||||
if(s.isSubscribed()) {
|
||||
boolean postable = local.contains(g.getId());
|
||||
try {
|
||||
Collection<GroupMessageHeader> headers =
|
||||
db.getGroupMessageHeaders(g.getId());
|
||||
displayHeaders(g, postable, 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);
|
||||
@@ -155,6 +177,7 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
adapter.clear();
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -168,18 +191,28 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
if(item != null) adapter.remove(item);
|
||||
// Add a new item
|
||||
adapter.add(new BlogListItem(g, postable, headers));
|
||||
adapter.sort(GroupComparator.INSTANCE);
|
||||
adapter.sort(ItemComparator.INSTANCE);
|
||||
adapter.notifyDataSetChanged();
|
||||
selectFirstUnread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayAvailable(final int available) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
adapter.setAvailable(available);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private BlogListItem findGroup(GroupId g) {
|
||||
int count = adapter.getCount();
|
||||
for(int i = 0; i < count; i++) {
|
||||
BlogListItem item = adapter.getItem(i);
|
||||
if(item.getGroupId().equals(g)) return item;
|
||||
if(item == MANAGE) continue;
|
||||
if(item.getGroup().getId().equals(g)) return item;
|
||||
}
|
||||
return null; // Not found
|
||||
}
|
||||
@@ -187,7 +220,9 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
private void selectFirstUnread() {
|
||||
int firstUnread = -1, count = adapter.getCount();
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(adapter.getItem(i).getUnreadCount() > 0) {
|
||||
BlogListItem item = adapter.getItem(i);
|
||||
if(item == MANAGE) continue;
|
||||
if(item.getUnreadCount() > 0) {
|
||||
firstUnread = i;
|
||||
break;
|
||||
}
|
||||
@@ -208,27 +243,6 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
unbindService(serviceConnection);
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
if(view == newBlogButton) {
|
||||
startActivity(new Intent(this, CreateBlogActivity.class));
|
||||
} else if(view == composeButton) {
|
||||
if(countPostableGroups() == 0) {
|
||||
NoBlogsDialog dialog = new NoBlogsDialog();
|
||||
dialog.setListener(this);
|
||||
dialog.show(getSupportFragmentManager(), "NoBlogsDialog");
|
||||
} else {
|
||||
startActivity(new Intent(this, WriteBlogPostActivity.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int countPostableGroups() {
|
||||
int postable = 0, count = adapter.getCount();
|
||||
for(int i = 0; i < count; i++)
|
||||
if(adapter.getItem(i).isPostable()) postable++;
|
||||
return postable;
|
||||
}
|
||||
|
||||
public void eventOccurred(DatabaseEvent e) {
|
||||
if(e instanceof GroupMessageAddedEvent) {
|
||||
Group g = ((GroupMessageAddedEvent) e).getGroup();
|
||||
@@ -239,6 +253,16 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
} 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) {
|
||||
Group g = ((SubscriptionAddedEvent) e).getGroup();
|
||||
if(g.isRestricted()) {
|
||||
if(LOG.isLoggable(INFO)) LOG.info("Group added, reloading");
|
||||
loadHeaders();
|
||||
}
|
||||
} else if(e instanceof SubscriptionRemovedEvent) {
|
||||
Group g = ((SubscriptionRemovedEvent) e).getGroup();
|
||||
if(g.isRestricted()) {
|
||||
@@ -290,23 +314,96 @@ implements OnClickListener, DatabaseListener, NoBlogsDialog.Listener {
|
||||
});
|
||||
}
|
||||
|
||||
private void loadAvailable() {
|
||||
dbUiExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
serviceConnection.waitForStartup();
|
||||
int available = 0;
|
||||
long now = System.currentTimeMillis();
|
||||
for(GroupStatus s : db.getAvailableGroups()) {
|
||||
if(s.getGroup().isRestricted() && !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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
if(view == newBlogButton) {
|
||||
startActivity(new Intent(this, CreateBlogActivity.class));
|
||||
} else if(view == composeButton) {
|
||||
if(countPostableGroups() == 0) {
|
||||
NoBlogsDialog dialog = new NoBlogsDialog();
|
||||
dialog.setListener(this);
|
||||
dialog.show(getSupportFragmentManager(), "NoBlogsDialog");
|
||||
} else {
|
||||
startActivity(new Intent(this, WriteBlogPostActivity.class));
|
||||
}
|
||||
} else if(view == manageBlogsButton) {
|
||||
startActivity(new Intent(this, ManageBlogsActivity.class));
|
||||
}
|
||||
}
|
||||
|
||||
private int countPostableGroups() {
|
||||
int postable = 0, count = adapter.getCount();
|
||||
for(int i = 0; i < count; i++) {
|
||||
BlogListItem item = adapter.getItem(i);
|
||||
if(item == MANAGE) continue;
|
||||
if(item.isPostable()) postable++;
|
||||
}
|
||||
return postable;
|
||||
}
|
||||
|
||||
public void blogCreationSelected() {
|
||||
startActivity(new Intent(this, CreateBlogActivity.class));
|
||||
}
|
||||
|
||||
public void blogCreationCancelled() {}
|
||||
|
||||
private static class GroupComparator implements Comparator<BlogListItem> {
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
BlogListItem item = adapter.getItem(position);
|
||||
if(item == MANAGE) {
|
||||
startActivity(new Intent(this, ManageBlogsActivity.class));
|
||||
} else {
|
||||
Intent i = new Intent(this, BlogActivity.class);
|
||||
i.putExtra("net.sf.briar.GROUP_ID",
|
||||
item.getGroup().getId().getBytes());
|
||||
i.putExtra("net.sf.briar.GROUP_NAME", item.getGroup().getName());
|
||||
i.putExtra("net.sf.briar.POSTABLE", item.isPostable());
|
||||
startActivity(i);
|
||||
}
|
||||
}
|
||||
|
||||
private static final GroupComparator INSTANCE = new GroupComparator();
|
||||
private static class ItemComparator implements Comparator<BlogListItem> {
|
||||
|
||||
private static final ItemComparator INSTANCE = new ItemComparator();
|
||||
|
||||
public int compare(BlogListItem a, BlogListItem b) {
|
||||
if(a == b) return 0;
|
||||
// The manage blogs 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.getGroupName(), bName = b.getGroupName();
|
||||
String aName = a.getGroup().getName();
|
||||
String bName = b.getGroup().getName();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,79 @@
|
||||
package net.sf.briar.android.blogs;
|
||||
|
||||
import static android.graphics.Typeface.BOLD;
|
||||
import static android.view.Gravity.CENTER;
|
||||
import static android.widget.LinearLayout.HORIZONTAL;
|
||||
import static android.widget.LinearLayout.VERTICAL;
|
||||
import static java.text.DateFormat.SHORT;
|
||||
import static net.sf.briar.android.blogs.BlogListItem.MANAGE;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.WRAP_WRAP_1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.briar.R;
|
||||
import net.sf.briar.android.widgets.HorizontalSpace;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.text.format.DateUtils;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
class BlogListAdapter extends ArrayAdapter<BlogListItem>
|
||||
implements OnItemClickListener {
|
||||
class BlogListAdapter extends BaseAdapter {
|
||||
|
||||
private final Context ctx;
|
||||
private final List<BlogListItem> list = new ArrayList<BlogListItem>();
|
||||
private int available = 0;
|
||||
|
||||
BlogListAdapter(Context ctx) {
|
||||
super(ctx, android.R.layout.simple_expandable_list_item_1,
|
||||
new ArrayList<BlogListItem>());
|
||||
this.ctx = ctx;
|
||||
}
|
||||
|
||||
public void setAvailable(int available) {
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
public void add(BlogListItem item) {
|
||||
list.add(item);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
list.clear();
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return available == 0 ? list.size() : list.size() + 1;
|
||||
}
|
||||
|
||||
public BlogListItem 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
BlogListItem item = getItem(position);
|
||||
Context ctx = getContext();
|
||||
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.blogs_available,
|
||||
available);
|
||||
manage.setText(String.format(format, available));
|
||||
return manage;
|
||||
}
|
||||
|
||||
LinearLayout layout = new LinearLayout(ctx);
|
||||
layout.setOrientation(HORIZONTAL);
|
||||
if(item.getUnreadCount() > 0)
|
||||
@@ -52,8 +89,9 @@ implements OnItemClickListener {
|
||||
name.setMaxLines(1);
|
||||
name.setPadding(10, 10, 10, 10);
|
||||
int unread = item.getUnreadCount();
|
||||
if(unread > 0) name.setText(item.getGroupName() + " (" + unread + ")");
|
||||
else name.setText(item.getGroupName());
|
||||
String groupName = item.getGroup().getName();
|
||||
if(unread > 0) name.setText(groupName + " (" + unread + ")");
|
||||
else name.setText(groupName);
|
||||
innerLayout.addView(name);
|
||||
|
||||
if(item.isEmpty()) {
|
||||
@@ -97,13 +135,16 @@ implements OnItemClickListener {
|
||||
return layout;
|
||||
}
|
||||
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
BlogListItem item = getItem(position);
|
||||
Intent i = new Intent(getContext(), BlogActivity.class);
|
||||
i.putExtra("net.sf.briar.GROUP_ID", item.getGroupId().getBytes());
|
||||
i.putExtra("net.sf.briar.GROUP_NAME", item.getGroupName());
|
||||
i.putExtra("net.sf.briar.POSTABLE", item.isPostable());
|
||||
getContext().startActivity(i);
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void remove(BlogListItem item) {
|
||||
list.remove(item);
|
||||
}
|
||||
|
||||
public void sort(Comparator<BlogListItem> comparator) {
|
||||
Collections.sort(list, comparator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ import net.sf.briar.android.DescendingHeaderComparator;
|
||||
import net.sf.briar.api.Author;
|
||||
import net.sf.briar.api.db.GroupMessageHeader;
|
||||
import net.sf.briar.api.messaging.Group;
|
||||
import net.sf.briar.api.messaging.GroupId;
|
||||
|
||||
class BlogListItem {
|
||||
|
||||
static final BlogListItem MANAGE = new BlogListItem(null, false,
|
||||
Collections.<GroupMessageHeader>emptyList());
|
||||
|
||||
private final Group group;
|
||||
private final boolean postable, empty;
|
||||
private final String authorName, contentType, subject;
|
||||
@@ -47,12 +49,8 @@ class BlogListItem {
|
||||
}
|
||||
}
|
||||
|
||||
GroupId getGroupId() {
|
||||
return group.getId();
|
||||
}
|
||||
|
||||
String getGroupName() {
|
||||
return group.getName();
|
||||
Group getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
boolean isPostable() {
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
package net.sf.briar.android.blogs;
|
||||
|
||||
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 net.sf.briar.android.widgets.CommonLayoutParams.MATCH_MATCH;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.WRAP_WRAP;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.R;
|
||||
import net.sf.briar.android.BriarFragmentActivity;
|
||||
import net.sf.briar.android.BriarService;
|
||||
import net.sf.briar.android.BriarService.BriarServiceConnection;
|
||||
import net.sf.briar.android.contact.SelectContactsDialog;
|
||||
import net.sf.briar.android.invitation.AddContactActivity;
|
||||
import net.sf.briar.android.messages.NoContactsDialog;
|
||||
import net.sf.briar.api.Contact;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.android.DatabaseUiExecutor;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.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.CheckBox;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class ConfigureBlogActivity extends BriarFragmentActivity
|
||||
implements OnClickListener, NoContactsDialog.Listener,
|
||||
SelectContactsDialog.Listener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConfigureBlogActivity.class.getName());
|
||||
|
||||
private final BriarServiceConnection serviceConnection =
|
||||
new BriarServiceConnection();
|
||||
|
||||
private boolean wasSubscribed = false;
|
||||
private CheckBox subscribeCheckBox = null;
|
||||
private RadioGroup radioGroup = null;
|
||||
private RadioButton visibleToAll = null, visibleToSome = null;
|
||||
private Button doneButton = null;
|
||||
private ProgressBar progress = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
|
||||
private volatile GroupId groupId = null;
|
||||
private volatile Collection<ContactId> selected = Collections.emptyList();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(null);
|
||||
|
||||
Intent i = getIntent();
|
||||
byte[] b = i.getByteArrayExtra("net.sf.briar.GROUP_ID");
|
||||
if(b == null) throw new IllegalStateException();
|
||||
groupId = new GroupId(b);
|
||||
String groupName = i.getStringExtra("net.sf.briar.GROUP_NAME");
|
||||
if(groupName == null) throw new IllegalArgumentException();
|
||||
setTitle(groupName);
|
||||
wasSubscribed = i.getBooleanExtra("net.sf.briar.SUBSCRIBED", false);
|
||||
boolean all = i.getBooleanExtra("net.sf.briar.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.setText(R.string.subscribe_to_this_blog);
|
||||
subscribeCheckBox.setChecked(wasSubscribed);
|
||||
subscribeCheckBox.setOnClickListener(this);
|
||||
layout.addView(subscribeCheckBox);
|
||||
|
||||
radioGroup = new RadioGroup(this);
|
||||
radioGroup.setOrientation(VERTICAL);
|
||||
radioGroup.setEnabled(wasSubscribed);
|
||||
|
||||
visibleToAll = new RadioButton(this);
|
||||
visibleToAll.setId(1);
|
||||
visibleToAll.setText(R.string.blog_visible_to_all);
|
||||
visibleToAll.setOnClickListener(this);
|
||||
radioGroup.addView(visibleToAll);
|
||||
|
||||
visibleToSome = new RadioButton(this);
|
||||
visibleToSome.setId(2);
|
||||
visibleToSome.setText(R.string.blog_visible_to_some);
|
||||
visibleToSome.setOnClickListener(this);
|
||||
radioGroup.addView(visibleToSome);
|
||||
|
||||
if(all) radioGroup.check(1);
|
||||
else radioGroup.check(2);
|
||||
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);
|
||||
|
||||
// Bind to the service so we can wait for it to start
|
||||
bindService(new Intent(BriarService.class.getName()),
|
||||
serviceConnection, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unbindService(serviceConnection);
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
if(view == subscribeCheckBox) {
|
||||
radioGroup.setEnabled(subscribeCheckBox.isChecked());
|
||||
} 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 || wasSubscribed)
|
||||
updateGroup(subscribe, wasSubscribed, all, visible);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadContacts() {
|
||||
dbUiExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
serviceConnection.waitForStartup();
|
||||
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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayContacts(final Collection<Contact> contacts) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if(contacts.isEmpty()) {
|
||||
NoContactsDialog dialog = new NoContactsDialog();
|
||||
dialog.setListener(ConfigureBlogActivity.this);
|
||||
dialog.show(getSupportFragmentManager(),
|
||||
"NoContactsDialog");
|
||||
} else {
|
||||
SelectContactsDialog dialog = new SelectContactsDialog();
|
||||
dialog.setListener(ConfigureBlogActivity.this);
|
||||
dialog.setContacts(contacts);
|
||||
dialog.show(getSupportFragmentManager(),
|
||||
"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 {
|
||||
serviceConnection.waitForStartup();
|
||||
long now = System.currentTimeMillis();
|
||||
if(subscribe) {
|
||||
if(!wasSubscribed) db.subscribe(db.getGroup(groupId));
|
||||
db.setVisibleToAll(groupId, all);
|
||||
if(!all) db.setVisibility(groupId, visible);
|
||||
} else if(wasSubscribed) {
|
||||
db.unsubscribe(db.getGroup(groupId));
|
||||
}
|
||||
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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
finishOnUiThread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void contactCreationSelected() {
|
||||
startActivity(new Intent(this, AddContactActivity.class));
|
||||
}
|
||||
|
||||
public void contactCreationCancelled() {}
|
||||
|
||||
public void contactsSelected(Collection<ContactId> selected) {
|
||||
this.selected = selected;
|
||||
}
|
||||
|
||||
public void contactSelectionCancelled() {}
|
||||
}
|
||||
@@ -255,11 +255,7 @@ SelectContactsDialog.Listener {
|
||||
LOG.info("Interrupted while waiting for service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
finishOnUiThread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package net.sf.briar.android.blogs;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_MATCH;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.android.BriarFragmentActivity;
|
||||
import net.sf.briar.android.BriarService;
|
||||
import net.sf.briar.android.ManageGroupsAdapter;
|
||||
import net.sf.briar.android.BriarService.BriarServiceConnection;
|
||||
import net.sf.briar.api.android.DatabaseUiExecutor;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
|
||||
import net.sf.briar.api.messaging.Group;
|
||||
import net.sf.briar.api.messaging.GroupStatus;
|
||||
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;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class ManageBlogsActivity extends BriarFragmentActivity
|
||||
implements DatabaseListener, OnItemClickListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ManageBlogsActivity.class.getName());
|
||||
|
||||
private final BriarServiceConnection serviceConnection =
|
||||
new BriarServiceConnection();
|
||||
|
||||
private ManageGroupsAdapter adapter = null;
|
||||
private ListView list = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(null);
|
||||
|
||||
adapter = new ManageGroupsAdapter(this);
|
||||
list = new ListView(this);
|
||||
list.setLayoutParams(MATCH_MATCH);
|
||||
list.setAdapter(adapter);
|
||||
list.setOnItemClickListener(this);
|
||||
setContentView(list);
|
||||
|
||||
// Bind to the service so we can wait for it to start
|
||||
bindService(new Intent(BriarService.class.getName()),
|
||||
serviceConnection, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
db.addListener(this);
|
||||
loadAvailableGroups();
|
||||
}
|
||||
|
||||
private void loadAvailableGroups() {
|
||||
dbUiExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
serviceConnection.waitForStartup();
|
||||
long now = System.currentTimeMillis();
|
||||
List<GroupStatus> available = new ArrayList<GroupStatus>();
|
||||
for(GroupStatus s : db.getAvailableGroups())
|
||||
if(s.getGroup().isRestricted()) available.add(s);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Load took " + duration + " ms");
|
||||
available = Collections.unmodifiableList(available);
|
||||
displayAvailableGroups(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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayAvailableGroups(
|
||||
final Collection<GroupStatus> available) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
adapter.clear();
|
||||
for(GroupStatus g : available) adapter.add(g);
|
||||
adapter.sort(ItemComparator.INSTANCE);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
db.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unbindService(serviceConnection);
|
||||
}
|
||||
|
||||
public void eventOccurred(DatabaseEvent e) {
|
||||
if(e instanceof RemoteSubscriptionsUpdatedEvent) {
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Remote subscriptions changed, reloading");
|
||||
loadAvailableGroups();
|
||||
} else if(e instanceof SubscriptionAddedEvent) {
|
||||
Group g = ((SubscriptionAddedEvent) e).getGroup();
|
||||
if(g.isRestricted()) {
|
||||
if(LOG.isLoggable(INFO)) LOG.info("Group added, reloading");
|
||||
loadAvailableGroups();
|
||||
}
|
||||
} else if(e instanceof SubscriptionRemovedEvent) {
|
||||
Group g = ((SubscriptionRemovedEvent) e).getGroup();
|
||||
if(g.isRestricted()) {
|
||||
if(LOG.isLoggable(INFO)) LOG.info("Group removed, reloading");
|
||||
loadAvailableGroups();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
GroupStatus item = adapter.getItem(position);
|
||||
Intent i = new Intent(this, ConfigureBlogActivity.class);
|
||||
i.putExtra("net.sf.briar.GROUP_ID", item.getGroup().getId().getBytes());
|
||||
i.putExtra("net.sf.briar.GROUP_NAME", item.getGroup().getName());
|
||||
i.putExtra("net.sf.briar.SUBSCRIBED", item.isSubscribed());
|
||||
i.putExtra("net.sf.briar.VISIBLE_TO_ALL", item.isVisibleToAll());
|
||||
startActivity(i);
|
||||
}
|
||||
|
||||
private static class ItemComparator implements Comparator<GroupStatus> {
|
||||
|
||||
private static final ItemComparator INSTANCE = new ItemComparator();
|
||||
|
||||
public int compare(GroupStatus a, GroupStatus b) {
|
||||
String aName = a.getGroup().getName();
|
||||
String bName = b.getGroup().getName();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,7 @@ implements OnClickListener, DatabaseListener, ConnectionListener {
|
||||
if(last != null)
|
||||
adapter.add(new ContactListItem(c, now, last));
|
||||
}
|
||||
adapter.sort(ContactComparator.INSTANCE);
|
||||
adapter.sort(ItemComparator.INSTANCE);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
@@ -198,10 +198,9 @@ implements OnClickListener, DatabaseListener, ConnectionListener {
|
||||
});
|
||||
}
|
||||
|
||||
private static class ContactComparator
|
||||
implements Comparator<ContactListItem> {
|
||||
private static class ItemComparator implements Comparator<ContactListItem> {
|
||||
|
||||
static final ContactComparator INSTANCE = new ContactComparator();
|
||||
private static final ItemComparator INSTANCE = new ItemComparator();
|
||||
|
||||
public int compare(ContactListItem a, ContactListItem b) {
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(a.getContactName(),
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
package net.sf.briar.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 net.sf.briar.android.widgets.CommonLayoutParams.MATCH_MATCH;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.WRAP_WRAP;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.R;
|
||||
import net.sf.briar.android.BriarFragmentActivity;
|
||||
import net.sf.briar.android.BriarService;
|
||||
import net.sf.briar.android.BriarService.BriarServiceConnection;
|
||||
import net.sf.briar.android.contact.SelectContactsDialog;
|
||||
import net.sf.briar.android.invitation.AddContactActivity;
|
||||
import net.sf.briar.android.messages.NoContactsDialog;
|
||||
import net.sf.briar.api.Contact;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.android.DatabaseUiExecutor;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.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.CheckBox;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class ConfigureGroupActivity extends BriarFragmentActivity
|
||||
implements OnClickListener, NoContactsDialog.Listener,
|
||||
SelectContactsDialog.Listener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConfigureGroupActivity.class.getName());
|
||||
|
||||
private final BriarServiceConnection serviceConnection =
|
||||
new BriarServiceConnection();
|
||||
|
||||
private boolean wasSubscribed = false;
|
||||
private CheckBox subscribeCheckBox = null;
|
||||
private RadioGroup radioGroup = null;
|
||||
private RadioButton visibleToAll = null, visibleToSome = null;
|
||||
private Button doneButton = null;
|
||||
private ProgressBar progress = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
|
||||
private volatile GroupId groupId = null;
|
||||
private volatile Collection<ContactId> selected = Collections.emptyList();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(null);
|
||||
|
||||
Intent i = getIntent();
|
||||
byte[] b = i.getByteArrayExtra("net.sf.briar.GROUP_ID");
|
||||
if(b == null) throw new IllegalStateException();
|
||||
groupId = new GroupId(b);
|
||||
String groupName = i.getStringExtra("net.sf.briar.GROUP_NAME");
|
||||
if(groupName == null) throw new IllegalArgumentException();
|
||||
setTitle(groupName);
|
||||
wasSubscribed = i.getBooleanExtra("net.sf.briar.SUBSCRIBED", false);
|
||||
boolean all = i.getBooleanExtra("net.sf.briar.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.setText(R.string.subscribe_to_this_group);
|
||||
subscribeCheckBox.setChecked(wasSubscribed);
|
||||
subscribeCheckBox.setOnClickListener(this);
|
||||
layout.addView(subscribeCheckBox);
|
||||
|
||||
radioGroup = new RadioGroup(this);
|
||||
radioGroup.setOrientation(VERTICAL);
|
||||
radioGroup.setEnabled(wasSubscribed);
|
||||
|
||||
visibleToAll = new RadioButton(this);
|
||||
visibleToAll.setId(1);
|
||||
visibleToAll.setText(R.string.group_visible_to_all);
|
||||
visibleToAll.setOnClickListener(this);
|
||||
radioGroup.addView(visibleToAll);
|
||||
|
||||
visibleToSome = new RadioButton(this);
|
||||
visibleToSome.setId(2);
|
||||
visibleToSome.setText(R.string.group_visible_to_some);
|
||||
visibleToSome.setOnClickListener(this);
|
||||
radioGroup.addView(visibleToSome);
|
||||
|
||||
if(all) radioGroup.check(1);
|
||||
else radioGroup.check(2);
|
||||
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);
|
||||
|
||||
// Bind to the service so we can wait for it to start
|
||||
bindService(new Intent(BriarService.class.getName()),
|
||||
serviceConnection, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unbindService(serviceConnection);
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
if(view == subscribeCheckBox) {
|
||||
radioGroup.setEnabled(subscribeCheckBox.isChecked());
|
||||
} 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 || wasSubscribed)
|
||||
updateGroup(subscribe, wasSubscribed, all, visible);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadContacts() {
|
||||
dbUiExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
serviceConnection.waitForStartup();
|
||||
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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayContacts(final Collection<Contact> contacts) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if(contacts.isEmpty()) {
|
||||
NoContactsDialog dialog = new NoContactsDialog();
|
||||
dialog.setListener(ConfigureGroupActivity.this);
|
||||
dialog.show(getSupportFragmentManager(),
|
||||
"NoContactsDialog");
|
||||
} else {
|
||||
SelectContactsDialog dialog = new SelectContactsDialog();
|
||||
dialog.setListener(ConfigureGroupActivity.this);
|
||||
dialog.setContacts(contacts);
|
||||
dialog.show(getSupportFragmentManager(),
|
||||
"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 {
|
||||
serviceConnection.waitForStartup();
|
||||
long now = System.currentTimeMillis();
|
||||
if(subscribe) {
|
||||
if(!wasSubscribed) db.subscribe(db.getGroup(groupId));
|
||||
db.setVisibleToAll(groupId, all);
|
||||
if(!all) db.setVisibility(groupId, visible);
|
||||
} else if(wasSubscribed) {
|
||||
db.unsubscribe(db.getGroup(groupId));
|
||||
}
|
||||
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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
finishOnUiThread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void contactCreationSelected() {
|
||||
startActivity(new Intent(this, AddContactActivity.class));
|
||||
}
|
||||
|
||||
public void contactCreationCancelled() {}
|
||||
|
||||
public void contactsSelected(Collection<ContactId> selected) {
|
||||
this.selected = selected;
|
||||
}
|
||||
|
||||
public void contactSelectionCancelled() {}
|
||||
}
|
||||
@@ -189,11 +189,7 @@ SelectContactsDialog.Listener {
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
finishOnUiThread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ 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 net.sf.briar.android.groups.GroupListItem.MANAGE;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_MATCH;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_WRAP;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_WRAP_1;
|
||||
@@ -30,13 +31,18 @@ import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.db.event.GroupMessageAddedEvent;
|
||||
import net.sf.briar.api.db.event.MessageExpiredEvent;
|
||||
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
|
||||
import net.sf.briar.api.messaging.Group;
|
||||
import net.sf.briar.api.messaging.GroupId;
|
||||
import net.sf.briar.api.messaging.GroupStatus;
|
||||
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;
|
||||
@@ -44,7 +50,8 @@ import android.widget.ListView;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class GroupListActivity extends BriarFragmentActivity
|
||||
implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
implements DatabaseListener, OnClickListener, NoGroupsDialog.Listener,
|
||||
OnItemClickListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(GroupListActivity.class.getName());
|
||||
@@ -55,6 +62,7 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
private GroupListAdapter adapter = null;
|
||||
private ListView list = null;
|
||||
private ImageButton newGroupButton = null, composeButton = null;
|
||||
private ImageButton manageGroupsButton = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
@@ -73,7 +81,7 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
// Give me all the width and all the unused height
|
||||
list.setLayoutParams(MATCH_WRAP_1);
|
||||
list.setAdapter(adapter);
|
||||
list.setOnItemClickListener(adapter);
|
||||
list.setOnItemClickListener(this);
|
||||
layout.addView(list);
|
||||
|
||||
layout.addView(new HorizontalBorder(this));
|
||||
@@ -97,6 +105,13 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
composeButton.setOnClickListener(this);
|
||||
footer.addView(composeButton);
|
||||
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);
|
||||
@@ -119,21 +134,28 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
public void run() {
|
||||
try {
|
||||
serviceConnection.waitForStartup();
|
||||
int available = 0;
|
||||
long now = System.currentTimeMillis();
|
||||
for(Group g : db.getSubscriptions()) {
|
||||
for(GroupStatus s : db.getAvailableGroups()) {
|
||||
Group g = s.getGroup();
|
||||
if(g.isRestricted()) continue;
|
||||
try {
|
||||
Collection<GroupMessageHeader> headers =
|
||||
db.getGroupMessageHeaders(g.getId());
|
||||
displayHeaders(g, headers);
|
||||
} catch(NoSuchSubscriptionException e) {
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Subscription removed");
|
||||
if(s.isSubscribed()) {
|
||||
try {
|
||||
Collection<GroupMessageHeader> headers =
|
||||
db.getGroupMessageHeaders(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);
|
||||
@@ -150,6 +172,7 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
adapter.clear();
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -163,18 +186,28 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
if(item != null) adapter.remove(item);
|
||||
// Add a new item
|
||||
adapter.add(new GroupListItem(g, headers));
|
||||
adapter.sort(GroupComparator.INSTANCE);
|
||||
adapter.sort(ItemComparator.INSTANCE);
|
||||
adapter.notifyDataSetChanged();
|
||||
selectFirstUnread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayAvailable(final int available) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
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.getGroupId().equals(g)) return item;
|
||||
if(item == MANAGE) continue;
|
||||
if(item.getGroup().getId().equals(g)) return item;
|
||||
}
|
||||
return null; // Not found
|
||||
}
|
||||
@@ -182,7 +215,9 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
private void selectFirstUnread() {
|
||||
int firstUnread = -1, count = adapter.getCount();
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(adapter.getItem(i).getUnreadCount() > 0) {
|
||||
GroupListItem item = adapter.getItem(i);
|
||||
if(item == MANAGE) continue;
|
||||
if(item.getUnreadCount() > 0) {
|
||||
firstUnread = i;
|
||||
break;
|
||||
}
|
||||
@@ -203,20 +238,6 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
unbindService(serviceConnection);
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
if(view == newGroupButton) {
|
||||
startActivity(new Intent(this, CreateGroupActivity.class));
|
||||
} else if(view == composeButton) {
|
||||
if(adapter.isEmpty()) {
|
||||
NoGroupsDialog dialog = new NoGroupsDialog();
|
||||
dialog.setListener(this);
|
||||
dialog.show(getSupportFragmentManager(), "NoGroupsDialog");
|
||||
} else {
|
||||
startActivity(new Intent(this, WriteGroupPostActivity.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void eventOccurred(DatabaseEvent e) {
|
||||
if(e instanceof GroupMessageAddedEvent) {
|
||||
Group g = ((GroupMessageAddedEvent) e).getGroup();
|
||||
@@ -227,6 +248,16 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
} 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) {
|
||||
Group g = ((SubscriptionAddedEvent) e).getGroup();
|
||||
if(!g.isRestricted()) {
|
||||
if(LOG.isLoggable(INFO)) LOG.info("Group added, reloading");
|
||||
loadHeaders();
|
||||
}
|
||||
} else if(e instanceof SubscriptionRemovedEvent) {
|
||||
Group g = ((SubscriptionRemovedEvent) e).getGroup();
|
||||
if(!g.isRestricted()) {
|
||||
@@ -277,23 +308,85 @@ implements OnClickListener, DatabaseListener, NoGroupsDialog.Listener {
|
||||
});
|
||||
}
|
||||
|
||||
private void loadAvailable() {
|
||||
dbUiExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
serviceConnection.waitForStartup();
|
||||
int available = 0;
|
||||
long now = System.currentTimeMillis();
|
||||
for(GroupStatus s : db.getAvailableGroups()) {
|
||||
if(!s.getGroup().isRestricted() && !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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
if(view == newGroupButton) {
|
||||
startActivity(new Intent(this, CreateGroupActivity.class));
|
||||
} else if(view == composeButton) {
|
||||
if(adapter.isEmpty()) {
|
||||
NoGroupsDialog dialog = new NoGroupsDialog();
|
||||
dialog.setListener(this);
|
||||
dialog.show(getSupportFragmentManager(), "NoGroupsDialog");
|
||||
} else {
|
||||
startActivity(new Intent(this, WriteGroupPostActivity.class));
|
||||
}
|
||||
} else if(view == manageGroupsButton) {
|
||||
startActivity(new Intent(this, ManageGroupsActivity.class));
|
||||
}
|
||||
}
|
||||
|
||||
public void groupCreationSelected() {
|
||||
startActivity(new Intent(this, CreateGroupActivity.class));
|
||||
}
|
||||
|
||||
public void groupCreationCancelled() {}
|
||||
|
||||
private static class GroupComparator implements Comparator<GroupListItem> {
|
||||
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);
|
||||
i.putExtra("net.sf.briar.GROUP_ID",
|
||||
item.getGroup().getId().getBytes());
|
||||
i.putExtra("net.sf.briar.GROUP_NAME", item.getGroup().getName());
|
||||
startActivity(i);
|
||||
}
|
||||
}
|
||||
|
||||
private static final GroupComparator INSTANCE = new GroupComparator();
|
||||
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.getGroupName(), bName = b.getGroupName();
|
||||
String aName = a.getGroup().getName();
|
||||
String bName = b.getGroup().getName();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,79 @@
|
||||
package net.sf.briar.android.groups;
|
||||
|
||||
import static android.graphics.Typeface.BOLD;
|
||||
import static android.view.Gravity.CENTER;
|
||||
import static android.widget.LinearLayout.HORIZONTAL;
|
||||
import static android.widget.LinearLayout.VERTICAL;
|
||||
import static java.text.DateFormat.SHORT;
|
||||
import static net.sf.briar.android.groups.GroupListItem.MANAGE;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.WRAP_WRAP_1;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import net.sf.briar.R;
|
||||
import net.sf.briar.android.widgets.HorizontalSpace;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.text.format.DateUtils;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
class GroupListAdapter extends ArrayAdapter<GroupListItem>
|
||||
implements OnItemClickListener {
|
||||
class GroupListAdapter extends BaseAdapter {
|
||||
|
||||
private final Context ctx;
|
||||
private final List<GroupListItem> list = new ArrayList<GroupListItem>();
|
||||
private int available = 0;
|
||||
|
||||
GroupListAdapter(Context ctx) {
|
||||
super(ctx, android.R.layout.simple_expandable_list_item_1,
|
||||
new ArrayList<GroupListItem>());
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
GroupListItem item = getItem(position);
|
||||
Context ctx = getContext();
|
||||
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.groups_available,
|
||||
available);
|
||||
manage.setText(String.format(format, available));
|
||||
return manage;
|
||||
}
|
||||
|
||||
LinearLayout layout = new LinearLayout(ctx);
|
||||
layout.setOrientation(HORIZONTAL);
|
||||
if(item.getUnreadCount() > 0)
|
||||
@@ -52,8 +89,9 @@ implements OnItemClickListener {
|
||||
name.setMaxLines(1);
|
||||
name.setPadding(10, 10, 10, 10);
|
||||
int unread = item.getUnreadCount();
|
||||
if(unread > 0) name.setText(item.getGroupName() + " (" + unread + ")");
|
||||
else name.setText(item.getGroupName());
|
||||
String groupName = item.getGroup().getName();
|
||||
if(unread > 0) name.setText(groupName + " (" + unread + ")");
|
||||
else name.setText(groupName);
|
||||
innerLayout.addView(name);
|
||||
|
||||
if(item.isEmpty()) {
|
||||
@@ -97,12 +135,16 @@ implements OnItemClickListener {
|
||||
return layout;
|
||||
}
|
||||
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
GroupListItem item = getItem(position);
|
||||
Intent i = new Intent(getContext(), GroupActivity.class);
|
||||
i.putExtra("net.sf.briar.GROUP_ID", item.getGroupId().getBytes());
|
||||
i.putExtra("net.sf.briar.GROUP_NAME", item.getGroupName());
|
||||
getContext().startActivity(i);
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void remove(GroupListItem item) {
|
||||
list.remove(item);
|
||||
}
|
||||
|
||||
public void sort(Comparator<GroupListItem> comparator) {
|
||||
Collections.sort(list, comparator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ import net.sf.briar.android.DescendingHeaderComparator;
|
||||
import net.sf.briar.api.Author;
|
||||
import net.sf.briar.api.db.GroupMessageHeader;
|
||||
import net.sf.briar.api.messaging.Group;
|
||||
import net.sf.briar.api.messaging.GroupId;
|
||||
|
||||
class GroupListItem {
|
||||
|
||||
static final GroupListItem MANAGE = new GroupListItem(null,
|
||||
Collections.<GroupMessageHeader>emptyList());
|
||||
|
||||
private final Group group;
|
||||
private final boolean empty;
|
||||
private final String authorName, contentType, subject;
|
||||
@@ -45,12 +47,8 @@ class GroupListItem {
|
||||
}
|
||||
}
|
||||
|
||||
GroupId getGroupId() {
|
||||
return group.getId();
|
||||
}
|
||||
|
||||
String getGroupName() {
|
||||
return group.getName();
|
||||
Group getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package net.sf.briar.android.groups;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static net.sf.briar.android.widgets.CommonLayoutParams.MATCH_MATCH;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.android.BriarFragmentActivity;
|
||||
import net.sf.briar.android.BriarService;
|
||||
import net.sf.briar.android.BriarService.BriarServiceConnection;
|
||||
import net.sf.briar.android.ManageGroupsAdapter;
|
||||
import net.sf.briar.api.android.DatabaseUiExecutor;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.db.event.DatabaseEvent;
|
||||
import net.sf.briar.api.db.event.DatabaseListener;
|
||||
import net.sf.briar.api.db.event.RemoteSubscriptionsUpdatedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionAddedEvent;
|
||||
import net.sf.briar.api.db.event.SubscriptionRemovedEvent;
|
||||
import net.sf.briar.api.messaging.Group;
|
||||
import net.sf.briar.api.messaging.GroupStatus;
|
||||
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;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
public class ManageGroupsActivity extends BriarFragmentActivity
|
||||
implements DatabaseListener, OnItemClickListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ManageGroupsActivity.class.getName());
|
||||
|
||||
private final BriarServiceConnection serviceConnection =
|
||||
new BriarServiceConnection();
|
||||
|
||||
private ManageGroupsAdapter adapter = null;
|
||||
private ListView list = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
@Inject @DatabaseUiExecutor private volatile Executor dbUiExecutor;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(null);
|
||||
|
||||
adapter = new ManageGroupsAdapter(this);
|
||||
list = new ListView(this);
|
||||
list.setLayoutParams(MATCH_MATCH);
|
||||
list.setAdapter(adapter);
|
||||
list.setOnItemClickListener(this);
|
||||
setContentView(list);
|
||||
|
||||
// Bind to the service so we can wait for it to start
|
||||
bindService(new Intent(BriarService.class.getName()),
|
||||
serviceConnection, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
db.addListener(this);
|
||||
loadAvailableGroups();
|
||||
}
|
||||
|
||||
private void loadAvailableGroups() {
|
||||
dbUiExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
serviceConnection.waitForStartup();
|
||||
long now = System.currentTimeMillis();
|
||||
List<GroupStatus> available = new ArrayList<GroupStatus>();
|
||||
for(GroupStatus s : db.getAvailableGroups())
|
||||
if(!s.getGroup().isRestricted()) available.add(s);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Load took " + duration + " ms");
|
||||
available = Collections.unmodifiableList(available);
|
||||
displayAvailableGroups(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 service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayAvailableGroups(
|
||||
final Collection<GroupStatus> available) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
adapter.clear();
|
||||
for(GroupStatus g : available) adapter.add(g);
|
||||
adapter.sort(ItemComparator.INSTANCE);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
db.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unbindService(serviceConnection);
|
||||
}
|
||||
|
||||
public void eventOccurred(DatabaseEvent e) {
|
||||
if(e instanceof RemoteSubscriptionsUpdatedEvent) {
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Remote subscriptions changed, reloading");
|
||||
loadAvailableGroups();
|
||||
} else if(e instanceof SubscriptionAddedEvent) {
|
||||
Group g = ((SubscriptionAddedEvent) e).getGroup();
|
||||
if(g.isRestricted()) {
|
||||
if(LOG.isLoggable(INFO)) LOG.info("Group added, reloading");
|
||||
loadAvailableGroups();
|
||||
}
|
||||
} else if(e instanceof SubscriptionRemovedEvent) {
|
||||
Group g = ((SubscriptionRemovedEvent) e).getGroup();
|
||||
if(g.isRestricted()) {
|
||||
if(LOG.isLoggable(INFO)) LOG.info("Group removed, reloading");
|
||||
loadAvailableGroups();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
GroupStatus item = adapter.getItem(position);
|
||||
Intent i = new Intent(this, ConfigureGroupActivity.class);
|
||||
i.putExtra("net.sf.briar.GROUP_ID", item.getGroup().getId().getBytes());
|
||||
i.putExtra("net.sf.briar.GROUP_NAME", item.getGroup().getName());
|
||||
i.putExtra("net.sf.briar.SUBSCRIBED", item.isSubscribed());
|
||||
i.putExtra("net.sf.briar.VISIBLE_TO_ALL", item.isVisibleToAll());
|
||||
startActivity(i);
|
||||
}
|
||||
|
||||
private static class ItemComparator implements Comparator<GroupStatus> {
|
||||
|
||||
private static final ItemComparator INSTANCE = new ItemComparator();
|
||||
|
||||
public int compare(GroupStatus a, GroupStatus b) {
|
||||
String aName = a.getGroup().getName();
|
||||
String bName = b.getGroup().getName();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,11 +169,7 @@ implements OnEditorActionListener, OnClickListener {
|
||||
LOG.info("Interrupted while waiting for service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
finishOnUiThread();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
|
||||
if(item != null) adapter.remove(item);
|
||||
// Add a new item
|
||||
adapter.add(new ConversationListItem(c, headers));
|
||||
adapter.sort(ConversationComparator.INSTANCE);
|
||||
adapter.sort(ItemComparator.INSTANCE);
|
||||
adapter.notifyDataSetChanged();
|
||||
selectFirstUnread();
|
||||
}
|
||||
@@ -244,11 +244,10 @@ implements OnClickListener, DatabaseListener, NoContactsDialog.Listener {
|
||||
|
||||
public void contactCreationCancelled() {}
|
||||
|
||||
private static class ConversationComparator
|
||||
private static class ItemComparator
|
||||
implements Comparator<ConversationListItem> {
|
||||
|
||||
static final ConversationComparator INSTANCE =
|
||||
new ConversationComparator();
|
||||
private static final ItemComparator INSTANCE = new ItemComparator();
|
||||
|
||||
public int compare(ConversationListItem a, ConversationListItem b) {
|
||||
// The item with the newest message comes first
|
||||
|
||||
Reference in New Issue
Block a user