Android UI for blogs (restricted groups).

This commit is contained in:
akwizgran
2013-03-23 14:30:59 +00:00
parent d5879df6eb
commit 4e5366509d
24 changed files with 168 additions and 65 deletions

View File

@@ -89,8 +89,12 @@ public class HomeScreenActivity extends BriarActivity {
groupsButton.setText(R.string.groups_button);
groupsButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(HomeScreenActivity.this,
GroupListActivity.class));
Intent i = new Intent(HomeScreenActivity.this,
GroupListActivity.class);
i.putExtra("net.sf.briar.RESTRICTED", false);
i.putExtra("net.sf.briar.TITLE",
getResources().getString(R.string.groups_title));
startActivity(i);
}
});
buttons.add(groupsButton);
@@ -99,11 +103,16 @@ public class HomeScreenActivity extends BriarActivity {
blogsButton.setLayoutParams(matchParent);
blogsButton.setBackgroundResource(0);
blogsButton.setCompoundDrawablesWithIntrinsicBounds(0,
R.drawable.social_share, 0, 0);
R.drawable.social_blog, 0, 0);
blogsButton.setText(R.string.blogs_button);
blogsButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// FIXME: Hook this button up to an activity
Intent i = new Intent(HomeScreenActivity.this,
GroupListActivity.class);
i.putExtra("net.sf.briar.RESTRICTED", true);
i.putExtra("net.sf.briar.TITLE",
getResources().getString(R.string.blogs_title));
startActivity(i);
}
});
buttons.add(blogsButton);
@@ -138,6 +147,8 @@ public class HomeScreenActivity extends BriarActivity {
grid.setLayoutParams(matchParent);
grid.setGravity(CENTER);
grid.setPadding(5, 5, 5, 5);
grid.setBackgroundColor(getResources().getColor(
R.color.home_screen_background));
grid.setNumColumns(2);
grid.setAdapter(new BaseAdapter() {

View File

@@ -51,6 +51,7 @@ OnClickListener, OnItemClickListener {
private final BriarServiceConnection serviceConnection =
new BriarServiceConnection();
private boolean restricted = false;
private String groupName = null;
private GroupAdapter adapter = null;
private ListView list = null;
@@ -65,6 +66,7 @@ OnClickListener, OnItemClickListener {
super.onCreate(null);
Intent i = getIntent();
restricted = i.getBooleanExtra("net.sf.briar.RESTRICTED", false);
byte[] id = i.getByteArrayExtra("net.sf.briar.GROUP_ID");
if(id == null) throw new IllegalStateException();
groupId = new GroupId(id);
@@ -194,7 +196,8 @@ OnClickListener, OnItemClickListener {
public void eventOccurred(DatabaseEvent e) {
if(e instanceof GroupMessageAddedEvent) {
if(((GroupMessageAddedEvent) e).getGroupId().equals(groupId)) {
GroupMessageAddedEvent g = (GroupMessageAddedEvent) e;
if(g.getGroup().getId().equals(groupId)) {
if(LOG.isLoggable(INFO)) LOG.info("Message added, reloading");
loadHeaders();
}
@@ -205,7 +208,8 @@ OnClickListener, OnItemClickListener {
if(LOG.isLoggable(INFO)) LOG.info("Rating changed, reloading");
loadHeaders();
} else if(e instanceof SubscriptionRemovedEvent) {
if(((SubscriptionRemovedEvent) e).getGroupId().equals(groupId)) {
SubscriptionRemovedEvent s = (SubscriptionRemovedEvent) e;
if(s.getGroup().getId().equals(groupId)) {
if(LOG.isLoggable(INFO)) LOG.info("Subscription removed");
finishOnUiThread();
}
@@ -214,6 +218,7 @@ OnClickListener, OnItemClickListener {
public void onClick(View view) {
Intent i = new Intent(this, WriteGroupMessageActivity.class);
i.putExtra("net.sf.briar.RESTRICTED", restricted);
i.putExtra("net.sf.briar.GROUP_ID", groupId.getBytes());
startActivity(i);
}

View File

@@ -1,6 +1,8 @@
package net.sf.briar.android.groups;
import static android.view.Gravity.CENTER;
import static android.view.Gravity.CENTER_HORIZONTAL;
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;
@@ -26,6 +28,7 @@ import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection;
import net.sf.briar.android.widgets.CommonLayoutParams;
import net.sf.briar.android.widgets.HorizontalBorder;
import net.sf.briar.android.widgets.HorizontalSpace;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.android.DatabaseUiExecutor;
import net.sf.briar.api.crypto.CryptoComponent;
@@ -67,6 +70,7 @@ implements OnClickListener, DatabaseListener {
private GroupListAdapter adapter = null;
private ListView list = null;
private ImageButton newGroupButton = null, composeButton = null;
// Fields that are accessed from DB threads must be volatile
@Inject private volatile CryptoComponent crypto;
@@ -76,6 +80,7 @@ implements OnClickListener, DatabaseListener {
@Inject private volatile AuthorFactory authorFactory;
@Inject private volatile GroupFactory groupFactory;
@Inject private volatile MessageFactory messageFactory;
private volatile boolean restricted = false;
@Override
public void onCreate(Bundle state) {
@@ -85,6 +90,12 @@ implements OnClickListener, DatabaseListener {
layout.setOrientation(VERTICAL);
layout.setGravity(CENTER_HORIZONTAL);
Intent i = getIntent();
restricted = i.getBooleanExtra("net.sf.briar.RESTRICTED", false);
String title = i.getStringExtra("net.sf.briar.TITLE");
if(title == null) throw new IllegalStateException();
setTitle(title);
adapter = new GroupListAdapter(this);
list = new ListView(this);
// Give me all the width and all the unused height
@@ -95,11 +106,28 @@ implements OnClickListener, DatabaseListener {
layout.addView(new HorizontalBorder(this));
ImageButton newGroupButton = new ImageButton(this);
LinearLayout footer = new LinearLayout(this);
footer.setLayoutParams(CommonLayoutParams.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);
if(restricted)
newGroupButton.setImageResource(R.drawable.social_new_blog);
else newGroupButton.setImageResource(R.drawable.social_new_chat);
newGroupButton.setOnClickListener(this);
layout.addView(newGroupButton);
footer.addView(newGroupButton);
footer.addView(new HorizontalSpace(this));
composeButton = new ImageButton(this);
composeButton.setBackgroundResource(0);
composeButton.setImageResource(R.drawable.content_new_email);
composeButton.setOnClickListener(this);
footer.addView(composeButton);
footer.addView(new HorizontalSpace(this));
layout.addView(footer);
setContentView(layout);
@@ -146,7 +174,11 @@ implements OnClickListener, DatabaseListener {
Group group1 = groupFactory.createGroup("Godwin's Lore");
db.subscribe(group1);
db.setVisibility(group1.getId(), Arrays.asList(contactId));
// Insert some text messages to the groups
Group group2 = groupFactory.createGroup(
"All Kids Love Blog", publicKey);
db.subscribe(group2);
db.setVisibility(group2.getId(), Arrays.asList(contactId));
// Insert some text messages to the unrestricted groups
for(int i = 0; i < 20; i++) {
String body;
if(i % 3 == 0) {
@@ -197,6 +229,43 @@ implements OnClickListener, DatabaseListener {
m = messageFactory.createAnonymousMessage(m.getId(),
group1, "text/plain", body.getBytes("UTF-8"));
db.addLocalGroupMessage(m);
// Insert some text messages to the restricted group
for(int i = 0; i < 20; i++) {
if(i % 3 == 0) {
body = "Message " + i + " is short.";
} else {
body = "Message " + i + " is long enough to wrap"
+ " onto a second line on some screens.";
}
now = System.currentTimeMillis();
if(i % 5 == 0) {
m = messageFactory.createAnonymousMessage(null,
group2, privateKey, "text/plain",
body.getBytes("UTF-8"));
} else if(i % 5 == 2) {
m = messageFactory.createPseudonymousMessage(null,
group2, privateKey, author, privateKey,
"text/plain", body.getBytes("UTF-8"));
} else {
m = messageFactory.createPseudonymousMessage(null,
group2, privateKey, author1, privateKey,
"text/plain", body.getBytes("UTF-8"));
}
duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) {
LOG.info("Message creation took " +
duration + " ms");
}
now = System.currentTimeMillis();
if(Math.random() < 0.5) db.addLocalGroupMessage(m);
else db.receiveMessage(contactId, m);
db.setReadFlag(m.getId(), i % 4 == 0);
duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO)) {
LOG.info("Message storage took " +
duration + " ms");
}
}
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
@@ -233,8 +302,8 @@ implements OnClickListener, DatabaseListener {
new ArrayList<CountDownLatch>();
long now = System.currentTimeMillis();
for(Group g : db.getSubscriptions()) {
// Filter out restricted groups
if(g.getPublicKey() != null) continue;
// Filter out restricted/unrestricted groups
if(g.isRestricted() != restricted) continue;
try {
// Load the headers from the database
Collection<GroupMessageHeader> headers =
@@ -322,41 +391,52 @@ implements OnClickListener, DatabaseListener {
}
public void onClick(View view) {
startActivity(new Intent(this, WriteGroupMessageActivity.class));
if(view == newGroupButton) {
// FIXME: Hook this button up to an activity
} else if(view == composeButton) {
Intent i = new Intent(this, WriteGroupMessageActivity.class);
i.putExtra("net.sf.briar.RESTRICTED", restricted);
startActivity(i);
}
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof GroupMessageAddedEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Message added, reloading");
loadHeaders(((GroupMessageAddedEvent) e).getGroupId());
Group g = ((GroupMessageAddedEvent) e).getGroup();
if(g.isRestricted() == restricted) {
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(); // FIXME: Don't reload everything
} else if(e instanceof SubscriptionRemovedEvent) {
// Reload the group, expecting NoSuchSubscriptionException
if(LOG.isLoggable(INFO)) LOG.info("Group removed, reloading");
loadHeaders(((SubscriptionRemovedEvent) e).getGroupId());
Group g = ((SubscriptionRemovedEvent) e).getGroup();
if(g.isRestricted() == restricted) {
if(LOG.isLoggable(INFO)) LOG.info("Group removed, reloading");
loadHeaders(g);
}
}
}
private void loadHeaders(final GroupId g) {
private void loadHeaders(final Group g) {
dbUiExecutor.execute(new Runnable() {
public void run() {
try {
serviceConnection.waitForStartup();
long now = System.currentTimeMillis();
Group group = db.getGroup(g);
Collection<GroupMessageHeader> headers =
db.getMessageHeaders(g);
db.getMessageHeaders(g.getId());
long duration = System.currentTimeMillis() - now;
if(LOG.isLoggable(INFO))
LOG.info("Partial load took " + duration + " ms");
CountDownLatch latch = new CountDownLatch(1);
displayHeaders(latch, group, headers);
displayHeaders(latch, g, headers);
latch.await();
} catch(NoSuchSubscriptionException e) {
if(LOG.isLoggable(INFO)) LOG.info("Subscription removed");
removeGroup(g);
removeGroup(g.getId());
} catch(DbException e) {
if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);

View File

@@ -53,6 +53,7 @@ implements OnClickListener, OnItemSelectedListener {
new BriarServiceConnection();
@Inject private BundleEncrypter bundleEncrypter;
private boolean restricted = false;
private GroupNameSpinnerAdapter adapter = null;
private Spinner spinner = null;
private ImageButton sendButton = null;
@@ -71,6 +72,7 @@ implements OnClickListener, OnItemSelectedListener {
super.onCreate(null);
Intent i = getIntent();
restricted = i.getBooleanExtra("net.sf.briar.RESTRICTED", false);
byte[] id = i.getByteArrayExtra("net.sf.briar.GROUP_ID");
if(id != null) groupId = new GroupId(id);
id = i.getByteArrayExtra("net.sf.briar.PARENT_ID");
@@ -123,6 +125,7 @@ implements OnClickListener, OnItemSelectedListener {
serviceConnection, 0);
}
// FIXME: If restricted, only load groups the user can post to
private void loadGroupList() {
dbExecutor.execute(new Runnable() {
public void run() {
@@ -144,6 +147,7 @@ implements OnClickListener, OnItemSelectedListener {
runOnUiThread(new Runnable() {
public void run() {
for(Group g : groups) {
if(g.isRestricted() != restricted) continue;
if(g.getId().equals(groupId)) {
group = g;
spinner.setSelection(adapter.getCount());