mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
174 lines
4.8 KiB
Java
174 lines
4.8 KiB
Java
package org.briarproject.android.sharing;
|
|
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
import android.view.MenuItem;
|
|
|
|
import org.briarproject.R;
|
|
import org.briarproject.android.BriarActivity;
|
|
import org.briarproject.android.contact.ContactListItem;
|
|
import org.briarproject.android.view.BriarRecyclerView;
|
|
import org.briarproject.api.clients.MessageTracker.GroupCount;
|
|
import org.briarproject.api.contact.Contact;
|
|
import org.briarproject.api.db.DbException;
|
|
import org.briarproject.api.identity.IdentityManager;
|
|
import org.briarproject.api.identity.LocalAuthor;
|
|
import org.briarproject.api.sync.GroupId;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.logging.Logger;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import static java.util.logging.Level.WARNING;
|
|
|
|
abstract class SharingStatusActivity extends BriarActivity {
|
|
|
|
private static final Logger LOG =
|
|
Logger.getLogger(SharingStatusActivity.class.getName());
|
|
|
|
private GroupId groupId;
|
|
private BriarRecyclerView sharedByList, sharedWithList;
|
|
private SharingStatusAdapter sharedByAdapter, sharedWithAdapter;
|
|
|
|
// Fields that are accessed from background threads must be volatile
|
|
@Inject
|
|
volatile IdentityManager identityManager;
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_sharing_status);
|
|
|
|
Intent i = getIntent();
|
|
byte[] b = i.getByteArrayExtra(GROUP_ID);
|
|
if (b == null) throw new IllegalStateException("No GroupId");
|
|
groupId = new GroupId(b);
|
|
|
|
sharedByList = (BriarRecyclerView) findViewById(R.id.sharedByView);
|
|
sharedByAdapter = new SharingStatusAdapter(this);
|
|
sharedByList.setLayoutManager(new LinearLayoutManager(this));
|
|
sharedByList.setAdapter(sharedByAdapter);
|
|
sharedByList.setEmptyText(getString(R.string.nobody));
|
|
|
|
sharedWithList = (BriarRecyclerView) findViewById(R.id.sharedWithView);
|
|
sharedWithAdapter = new SharingStatusAdapter(this);
|
|
sharedWithList.setLayoutManager(new LinearLayoutManager(this));
|
|
sharedWithList.setAdapter(sharedWithAdapter);
|
|
sharedWithList.setEmptyText(getString(R.string.nobody));
|
|
}
|
|
|
|
@Override
|
|
public void onStart() {
|
|
super.onStart();
|
|
loadSharedBy();
|
|
loadSharedWith();
|
|
}
|
|
|
|
@Override
|
|
public void onStop() {
|
|
super.onStop();
|
|
sharedByAdapter.clear();
|
|
sharedByList.showProgressBar();
|
|
sharedWithAdapter.clear();
|
|
sharedWithList.showProgressBar();
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(final MenuItem item) {
|
|
// Handle presses on the action bar items
|
|
switch (item.getItemId()) {
|
|
case android.R.id.home:
|
|
onBackPressed();
|
|
return true;
|
|
default:
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This must only be called from the DbThread
|
|
*/
|
|
abstract protected Collection<Contact> getSharedWith() throws DbException;
|
|
|
|
/**
|
|
* This must only be called from the DbThread
|
|
*/
|
|
abstract protected Collection<Contact> getSharedBy() throws DbException;
|
|
|
|
protected GroupId getGroupId() {
|
|
return groupId;
|
|
}
|
|
|
|
private void loadSharedBy() {
|
|
runOnDbThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
List<ContactListItem> contactItems = new ArrayList<>();
|
|
for (Contact c : getSharedBy()) {
|
|
LocalAuthor localAuthor = identityManager
|
|
.getLocalAuthor(c.getLocalAuthorId());
|
|
ContactListItem item =
|
|
new ContactListItem(c, localAuthor, false,
|
|
groupId, new GroupCount(0, 0, 0));
|
|
contactItems.add(item);
|
|
}
|
|
displaySharedBy(contactItems);
|
|
} catch (DbException e) {
|
|
if (LOG.isLoggable(WARNING))
|
|
LOG.log(WARNING, e.toString(), e);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void displaySharedBy(final List<ContactListItem> contacts) {
|
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (contacts.isEmpty()) sharedByList.showData();
|
|
else sharedByAdapter.addAll(contacts);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void loadSharedWith() {
|
|
runOnDbThread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
List<ContactListItem> contactItems = new ArrayList<>();
|
|
for (Contact c : getSharedWith()) {
|
|
LocalAuthor localAuthor = identityManager
|
|
.getLocalAuthor(c.getLocalAuthorId());
|
|
ContactListItem item =
|
|
new ContactListItem(c, localAuthor, false,
|
|
groupId, new GroupCount(0, 0, 0));
|
|
contactItems.add(item);
|
|
}
|
|
displaySharedWith(contactItems);
|
|
} catch (DbException e) {
|
|
if (LOG.isLoggable(WARNING))
|
|
LOG.log(WARNING, e.toString(), e);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void displaySharedWith(final List<ContactListItem> contacts) {
|
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (contacts.isEmpty()) sharedWithList.showData();
|
|
else sharedWithAdapter.addAll(contacts);
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|