mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
This makes the contact list behave how alpha testers expected: touching a contact shows private messages exchanged with the contact.
239 lines
7.7 KiB
Java
239 lines
7.7 KiB
Java
package net.sf.briar.android.contact;
|
|
|
|
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.util.CommonLayoutParams.MATCH_MATCH;
|
|
import static net.sf.briar.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 net.sf.briar.R;
|
|
import net.sf.briar.android.AscendingHeaderComparator;
|
|
import net.sf.briar.android.util.HorizontalBorder;
|
|
import net.sf.briar.android.util.ListLoadingProgressBar;
|
|
import net.sf.briar.api.AuthorId;
|
|
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.db.NoSuchContactException;
|
|
import net.sf.briar.api.db.PrivateMessageHeader;
|
|
import net.sf.briar.api.db.event.ContactRemovedEvent;
|
|
import net.sf.briar.api.db.event.DatabaseEvent;
|
|
import net.sf.briar.api.db.event.DatabaseListener;
|
|
import net.sf.briar.api.db.event.MessageExpiredEvent;
|
|
import net.sf.briar.api.db.event.PrivateMessageAddedEvent;
|
|
import net.sf.briar.api.lifecycle.LifecycleManager;
|
|
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 ConversationActivity extends RoboActivity
|
|
implements DatabaseListener, OnClickListener, OnItemClickListener {
|
|
|
|
private static final Logger LOG =
|
|
Logger.getLogger(ConversationActivity.class.getName());
|
|
|
|
private String contactName = null;
|
|
private ConversationAdapter 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 ContactId contactId = null;
|
|
private volatile AuthorId localAuthorId = null;
|
|
|
|
@Override
|
|
public void onCreate(Bundle state) {
|
|
super.onCreate(state);
|
|
|
|
Intent i = getIntent();
|
|
int id = i.getIntExtra("net.sf.briar.CONTACT_ID", -1);
|
|
if(id == -1) throw new IllegalStateException();
|
|
contactId = new ContactId(id);
|
|
contactName = i.getStringExtra("net.sf.briar.CONTACT_NAME");
|
|
if(contactName == null) throw new IllegalStateException();
|
|
setTitle(contactName);
|
|
byte[] b = i.getByteArrayExtra("net.sf.briar.LOCAL_AUTHOR_ID");
|
|
if(b == null) throw new IllegalStateException();
|
|
localAuthorId = new AuthorId(b);
|
|
|
|
LinearLayout layout = new LinearLayout(this);
|
|
layout.setLayoutParams(MATCH_MATCH);
|
|
layout.setOrientation(VERTICAL);
|
|
layout.setGravity(CENTER_HORIZONTAL);
|
|
|
|
adapter = new ConversationAdapter(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<PrivateMessageHeader> headers =
|
|
db.getPrivateMessageHeaders(contactId);
|
|
long duration = System.currentTimeMillis() - now;
|
|
if(LOG.isLoggable(INFO))
|
|
LOG.info("Load took " + duration + " ms");
|
|
displayHeaders(headers);
|
|
} catch(NoSuchContactException e) {
|
|
if(LOG.isLoggable(INFO)) LOG.info("Contact 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<PrivateMessageHeader> headers) {
|
|
runOnUiThread(new Runnable() {
|
|
public void run() {
|
|
list.setVisibility(VISIBLE);
|
|
loading.setVisibility(GONE);
|
|
adapter.clear();
|
|
for(PrivateMessageHeader 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 == ReadPrivateMessageActivity.RESULT_PREV) {
|
|
int position = request - 1;
|
|
if(position >= 0 && position < adapter.getCount())
|
|
displayMessage(position);
|
|
} else if(result == ReadPrivateMessageActivity.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(DatabaseEvent e) {
|
|
if(e instanceof ContactRemovedEvent) {
|
|
ContactRemovedEvent c = (ContactRemovedEvent) e;
|
|
if(c.getContactId().equals(contactId)) {
|
|
if(LOG.isLoggable(INFO)) LOG.info("Contact removed");
|
|
runOnUiThread(new Runnable() {
|
|
public void run() {
|
|
finish();
|
|
}
|
|
});
|
|
}
|
|
} else if(e instanceof MessageExpiredEvent) {
|
|
if(LOG.isLoggable(INFO)) LOG.info("Message expired, reloading");
|
|
loadHeaders();
|
|
} else if(e instanceof PrivateMessageAddedEvent) {
|
|
PrivateMessageAddedEvent p = (PrivateMessageAddedEvent) e;
|
|
if(p.getContactId().equals(contactId)) {
|
|
if(LOG.isLoggable(INFO)) LOG.info("Message added, reloading");
|
|
loadHeaders();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void onClick(View view) {
|
|
Intent i = new Intent(this, WritePrivateMessageActivity.class);
|
|
i.putExtra("net.sf.briar.CONTACT_ID", contactId.getInt());
|
|
i.putExtra("net.sf.briar.LOCAL_AUTHOR_ID", localAuthorId.getBytes());
|
|
startActivity(i);
|
|
}
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position,
|
|
long id) {
|
|
displayMessage(position);
|
|
}
|
|
|
|
private void displayMessage(int position) {
|
|
PrivateMessageHeader item = adapter.getItem(position);
|
|
Intent i = new Intent(this, ReadPrivateMessageActivity.class);
|
|
i.putExtra("net.sf.briar.CONTACT_ID", contactId.getInt());
|
|
i.putExtra("net.sf.briar.CONTACT_NAME", contactName);
|
|
i.putExtra("net.sf.briar.AUTHOR_NAME", item.getAuthor().getName());
|
|
i.putExtra("net.sf.briar.MESSAGE_ID", item.getId().getBytes());
|
|
i.putExtra("net.sf.briar.CONTENT_TYPE", item.getContentType());
|
|
i.putExtra("net.sf.briar.TIMESTAMP", item.getTimestamp());
|
|
startActivityForResult(i, position);
|
|
}
|
|
}
|