2015-08-05 19:48:41 +02:00
|
|
|
package ch.dissem.apps.abit;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2015-09-11 07:59:39 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2015-08-05 19:48:41 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.Menu;
|
2015-08-14 17:25:05 +02:00
|
|
|
import android.view.MenuItem;
|
2015-08-05 19:48:41 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
2015-09-06 17:45:21 +02:00
|
|
|
import ch.dissem.apps.abit.listeners.ActionBarListener;
|
2015-09-11 07:59:39 +02:00
|
|
|
import ch.dissem.apps.abit.listeners.ListSelectionListener;
|
2015-08-05 19:48:41 +02:00
|
|
|
import ch.dissem.apps.abit.service.Singleton;
|
|
|
|
import ch.dissem.bitmessage.BitmessageContext;
|
|
|
|
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
|
|
|
import ch.dissem.bitmessage.entity.Plaintext;
|
2015-09-11 07:59:39 +02:00
|
|
|
import ch.dissem.bitmessage.entity.Streamable;
|
2015-08-05 19:48:41 +02:00
|
|
|
import ch.dissem.bitmessage.entity.valueobject.Label;
|
2015-08-14 17:25:05 +02:00
|
|
|
import com.mikepenz.community_material_typeface_library.CommunityMaterial;
|
2015-08-05 19:48:41 +02:00
|
|
|
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
|
|
|
|
import com.mikepenz.iconics.IconicsDrawable;
|
|
|
|
import com.mikepenz.materialdrawer.Drawer;
|
|
|
|
import com.mikepenz.materialdrawer.DrawerBuilder;
|
|
|
|
import com.mikepenz.materialdrawer.accountswitcher.AccountHeader;
|
|
|
|
import com.mikepenz.materialdrawer.accountswitcher.AccountHeaderBuilder;
|
|
|
|
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
|
|
|
|
import com.mikepenz.materialdrawer.model.interfaces.Nameable;
|
2015-08-28 13:49:53 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-08-05 19:48:41 +02:00
|
|
|
|
2015-09-11 07:59:39 +02:00
|
|
|
import java.io.Serializable;
|
2015-08-05 19:48:41 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An activity representing a list of Messages. This activity
|
|
|
|
* has different presentations for handset and tablet-size devices. On
|
|
|
|
* handsets, the activity presents a list of items, which when touched,
|
|
|
|
* lead to a {@link MessageDetailActivity} representing
|
|
|
|
* item details. On tablets, the activity presents the list of items and
|
|
|
|
* item details side-by-side using two vertical panes.
|
|
|
|
* <p>
|
|
|
|
* The activity makes heavy use of fragments. The list of items is a
|
|
|
|
* {@link MessageListFragment} and the item details
|
|
|
|
* (if present) is a {@link MessageDetailFragment}.
|
|
|
|
* </p><p>
|
|
|
|
* This activity also implements the required
|
2015-09-11 07:59:39 +02:00
|
|
|
* {@link ListSelectionListener} interface
|
2015-08-05 19:48:41 +02:00
|
|
|
* to listen for item selections.
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
public class MessageListActivity extends AppCompatActivity
|
2015-09-11 07:59:39 +02:00
|
|
|
implements ListSelectionListener<Serializable>, ActionBarListener {
|
2015-09-04 08:19:07 +02:00
|
|
|
public static final String EXTRA_SHOW_MESSAGE = "ch.dissem.abit.ShowMessage";
|
|
|
|
public static final String ACTION_SHOW_INBOX = "ch.dissem.abit.ShowInbox";
|
2015-08-28 13:49:53 +02:00
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MessageListActivity.class);
|
2015-08-05 19:48:41 +02:00
|
|
|
private static final int ADD_IDENTITY = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
private boolean twoPane;
|
|
|
|
|
|
|
|
private AccountHeader accountHeader;
|
|
|
|
private BitmessageContext bmc;
|
|
|
|
private Label selectedLabel;
|
2015-08-14 17:25:05 +02:00
|
|
|
private Menu menu;
|
2015-08-05 19:48:41 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
bmc = Singleton.getBitmessageContext(this);
|
|
|
|
selectedLabel = bmc.messages().getLabels().get(0);
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_message_list);
|
|
|
|
|
2015-08-14 17:25:05 +02:00
|
|
|
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
2015-08-05 19:48:41 +02:00
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
2015-09-11 07:59:39 +02:00
|
|
|
MessageListFragment listFragment = new MessageListFragment();
|
|
|
|
getSupportFragmentManager().beginTransaction().replace(R.id.item_list, listFragment).commit();
|
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
if (findViewById(R.id.message_detail_container) != null) {
|
|
|
|
// The detail container view will be present only in the
|
|
|
|
// large-screen layouts (res/values-large and
|
|
|
|
// res/values-sw600dp). If this view is present, then the
|
|
|
|
// activity should be in two-pane mode.
|
|
|
|
twoPane = true;
|
|
|
|
|
|
|
|
// In two-pane mode, list items should be given the
|
|
|
|
// 'activated' state when touched.
|
2015-09-11 07:59:39 +02:00
|
|
|
listFragment.setActivateOnItemClick(true);
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
createDrawer(toolbar);
|
|
|
|
|
2015-09-04 08:19:07 +02:00
|
|
|
Singleton.getMessageListener(this).resetNotification();
|
|
|
|
|
|
|
|
// handle intents
|
|
|
|
if (getIntent().hasExtra(EXTRA_SHOW_MESSAGE)) {
|
|
|
|
onItemSelected((Plaintext) getIntent().getSerializableExtra(EXTRA_SHOW_MESSAGE));
|
|
|
|
}
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
2015-09-11 07:59:39 +02:00
|
|
|
private void changeList(AbstractItemListFragment<?> listFragment) {
|
|
|
|
getSupportFragmentManager()
|
|
|
|
.beginTransaction()
|
|
|
|
.replace(R.id.item_list, listFragment)
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
|
|
|
|
if (twoPane) {
|
|
|
|
// In two-pane mode, list items should be given the
|
|
|
|
// 'activated' state when touched.
|
|
|
|
listFragment.setActivateOnItemClick(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
private void createDrawer(Toolbar toolbar) {
|
|
|
|
final ArrayList<IProfile> profiles = new ArrayList<>();
|
|
|
|
for (BitmessageAddress identity : bmc.addresses().getIdentities()) {
|
2015-08-28 13:49:53 +02:00
|
|
|
LOG.info("Adding identity " + identity.getAddress());
|
2015-08-05 19:48:41 +02:00
|
|
|
profiles.add(new ProfileDrawerItem()
|
2015-09-04 08:19:07 +02:00
|
|
|
.withIcon(new Identicon(identity))
|
2015-08-05 19:48:41 +02:00
|
|
|
.withName(identity.toString())
|
|
|
|
.withEmail(identity.getAddress())
|
|
|
|
.withTag(identity)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
profiles.add(new ProfileSettingDrawerItem()
|
|
|
|
.withName("Add Identity")
|
|
|
|
.withDescription("Create new identity")
|
|
|
|
.withIcon(new IconicsDrawable(this, GoogleMaterial.Icon.gmd_add)
|
|
|
|
.actionBar()
|
|
|
|
.paddingDp(5)
|
|
|
|
.colorRes(R.color.icons))
|
|
|
|
.withIdentifier(ADD_IDENTITY)
|
|
|
|
);
|
|
|
|
profiles.add(new ProfileSettingDrawerItem()
|
2015-08-14 17:25:05 +02:00
|
|
|
.withName(getString(R.string.manage_identity))
|
2015-08-05 19:48:41 +02:00
|
|
|
.withIcon(GoogleMaterial.Icon.gmd_settings)
|
|
|
|
);
|
|
|
|
// Create the AccountHeader
|
|
|
|
accountHeader = new AccountHeaderBuilder()
|
|
|
|
.withActivity(this)
|
|
|
|
.withHeaderBackground(R.drawable.header)
|
|
|
|
.withProfiles(profiles)
|
|
|
|
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
|
|
|
|
if (profile.getIdentifier() == ADD_IDENTITY) {
|
|
|
|
BitmessageAddress identity = bmc.createIdentity(false);
|
|
|
|
IProfile newProfile = new ProfileDrawerItem()
|
|
|
|
.withName(identity.toString())
|
|
|
|
.withEmail(identity.getAddress())
|
|
|
|
.withTag(identity);
|
|
|
|
if (accountHeader.getProfiles() != null) {
|
|
|
|
//we know that there are 2 setting elements. set the new profile above them ;)
|
|
|
|
accountHeader.addProfile(newProfile, accountHeader.getProfiles().size() - 2);
|
|
|
|
} else {
|
|
|
|
accountHeader.addProfiles(newProfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// false if it should close the drawer
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
|
|
|
|
ArrayList<IDrawerItem> drawerItems = new ArrayList<>();
|
|
|
|
for (Label label : bmc.messages().getLabels()) {
|
2015-09-06 17:45:21 +02:00
|
|
|
PrimaryDrawerItem item = new PrimaryDrawerItem().withName(label.toString()).withTag(label);
|
|
|
|
switch (label.getType()) {
|
|
|
|
case INBOX:
|
|
|
|
item.withIcon(GoogleMaterial.Icon.gmd_inbox);
|
|
|
|
break;
|
|
|
|
case DRAFT:
|
|
|
|
item.withIcon(CommunityMaterial.Icon.cmd_file);
|
|
|
|
break;
|
|
|
|
case SENT:
|
|
|
|
item.withIcon(CommunityMaterial.Icon.cmd_send);
|
|
|
|
break;
|
|
|
|
case BROADCAST:
|
|
|
|
item.withIcon(CommunityMaterial.Icon.cmd_rss);
|
|
|
|
break;
|
|
|
|
case UNREAD:
|
|
|
|
item.withIcon(GoogleMaterial.Icon.gmd_markunread_mailbox);
|
|
|
|
break;
|
|
|
|
case TRASH:
|
|
|
|
item.withIcon(GoogleMaterial.Icon.gmd_delete);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
item.withIcon(CommunityMaterial.Icon.cmd_label);
|
|
|
|
}
|
|
|
|
drawerItems.add(item);
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
new DrawerBuilder()
|
|
|
|
.withActivity(this)
|
|
|
|
.withToolbar(toolbar)
|
|
|
|
.withAccountHeader(accountHeader)
|
|
|
|
.withDrawerItems(drawerItems)
|
|
|
|
.addStickyDrawerItems(
|
2015-08-14 17:25:05 +02:00
|
|
|
new SecondaryDrawerItem()
|
2015-09-11 07:59:39 +02:00
|
|
|
.withName(R.string.subscriptions)
|
2015-09-06 17:45:21 +02:00
|
|
|
.withIcon(CommunityMaterial.Icon.cmd_rss_box),
|
2015-08-05 19:48:41 +02:00
|
|
|
new SecondaryDrawerItem()
|
|
|
|
.withName(R.string.settings)
|
|
|
|
.withIcon(GoogleMaterial.Icon.gmd_settings)
|
|
|
|
)
|
|
|
|
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onItemClick(AdapterView<?> adapterView, View view, int i, long l, IDrawerItem item) {
|
|
|
|
if (item.getTag() instanceof Label) {
|
|
|
|
selectedLabel = (Label) item.getTag();
|
2015-09-11 07:59:39 +02:00
|
|
|
if (!(getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof MessageListFragment)) {
|
|
|
|
MessageListFragment listFragment = new MessageListFragment();
|
|
|
|
changeList(listFragment);
|
|
|
|
listFragment.updateList(selectedLabel);
|
|
|
|
} else {
|
|
|
|
((MessageListFragment) getSupportFragmentManager()
|
|
|
|
.findFragmentById(R.id.item_list)).updateList(selectedLabel);
|
|
|
|
}
|
|
|
|
return false;
|
2015-08-05 19:48:41 +02:00
|
|
|
} else if (item instanceof Nameable<?>) {
|
|
|
|
Nameable<?> ni = (Nameable<?>) item;
|
|
|
|
switch (ni.getNameRes()) {
|
2015-08-14 17:25:05 +02:00
|
|
|
case R.string.subscriptions:
|
2015-09-11 07:59:39 +02:00
|
|
|
if (!(getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof SubscriptionListFragment)) {
|
|
|
|
changeList(new SubscriptionListFragment());
|
|
|
|
} else {
|
|
|
|
((SubscriptionListFragment) getSupportFragmentManager()
|
|
|
|
.findFragmentById(R.id.item_list)).updateList();
|
|
|
|
}
|
2015-08-14 17:25:05 +02:00
|
|
|
break;
|
2015-08-05 19:48:41 +02:00
|
|
|
case R.string.settings:
|
|
|
|
startActivity(new Intent(MessageListActivity.this, SettingsActivity.class));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
2015-09-11 07:59:39 +02:00
|
|
|
.withCloseOnClick(true)
|
2015-08-05 19:48:41 +02:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.main, menu);
|
2015-08-14 17:25:05 +02:00
|
|
|
this.menu = menu;
|
|
|
|
updateMenu();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateMenu() {
|
2015-08-05 19:48:41 +02:00
|
|
|
boolean running = bmc.isRunning();
|
|
|
|
menu.findItem(R.id.sync_enabled).setVisible(running);
|
|
|
|
menu.findItem(R.id.sync_disabled).setVisible(!running);
|
2015-08-14 17:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.sync_disabled:
|
2015-08-28 13:49:53 +02:00
|
|
|
bmc.startup(Singleton.getMessageListener(this));
|
2015-08-14 17:25:05 +02:00
|
|
|
updateMenu();
|
|
|
|
return true;
|
|
|
|
case R.id.sync_enabled:
|
|
|
|
bmc.shutdown();
|
|
|
|
updateMenu();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-11 07:59:39 +02:00
|
|
|
* Callback method from {@link ListSelectionListener}
|
2015-08-05 19:48:41 +02:00
|
|
|
* indicating that the item with the given ID was selected.
|
|
|
|
*/
|
|
|
|
@Override
|
2015-09-11 07:59:39 +02:00
|
|
|
public void onItemSelected(Serializable item) {
|
2015-08-05 19:48:41 +02:00
|
|
|
if (twoPane) {
|
|
|
|
// In two-pane mode, show the detail view in this activity by
|
|
|
|
// adding or replacing the detail fragment using a
|
|
|
|
// fragment transaction.
|
|
|
|
Bundle arguments = new Bundle();
|
2015-09-11 07:59:39 +02:00
|
|
|
arguments.putSerializable(MessageDetailFragment.ARG_ITEM, item);
|
|
|
|
Fragment fragment;
|
|
|
|
if (item instanceof Plaintext)
|
|
|
|
fragment = new MessageDetailFragment();
|
|
|
|
else if (item instanceof BitmessageAddress)
|
|
|
|
fragment = new SubscriptionDetailFragment();
|
|
|
|
else
|
|
|
|
throw new IllegalArgumentException("Plaintext or BitmessageAddress expected, but was "
|
|
|
|
+ item.getClass().getSimpleName());
|
2015-08-05 19:48:41 +02:00
|
|
|
fragment.setArguments(arguments);
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.replace(R.id.message_detail_container, fragment)
|
|
|
|
.commit();
|
|
|
|
} else {
|
|
|
|
// In single-pane mode, simply start the detail activity
|
|
|
|
// for the selected item ID.
|
2015-09-11 07:59:39 +02:00
|
|
|
Intent detailIntent;
|
|
|
|
if (item instanceof Plaintext)
|
|
|
|
detailIntent = new Intent(this, MessageDetailActivity.class);
|
|
|
|
else if (item instanceof BitmessageAddress)
|
|
|
|
detailIntent = new Intent(this, SubscriptionDetailActivity.class);
|
|
|
|
else
|
|
|
|
throw new IllegalArgumentException("Plaintext or BitmessageAddress expected, but was "
|
|
|
|
+ item.getClass().getSimpleName());
|
|
|
|
|
|
|
|
detailIntent.putExtra(MessageDetailFragment.ARG_ITEM, item);
|
2015-08-05 19:48:41 +02:00
|
|
|
startActivity(detailIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:45:21 +02:00
|
|
|
@Override
|
|
|
|
public void updateTitle(CharSequence title) {
|
|
|
|
getSupportActionBar().setTitle(title);
|
|
|
|
}
|
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
public Label getSelectedLabel() {
|
|
|
|
return selectedLabel;
|
|
|
|
}
|
2015-08-28 13:49:53 +02:00
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|