2016-01-19 20:50:58 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Christian Basler
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
package ch.dissem.apps.abit;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2016-02-12 00:19:15 +01:00
|
|
|
import android.graphics.Point;
|
2017-02-23 17:38:00 +01:00
|
|
|
import android.os.AsyncTask;
|
2015-08-05 19:48:41 +02:00
|
|
|
import android.os.Bundle;
|
2015-09-11 07:59:39 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2017-07-10 06:21:29 +02:00
|
|
|
import android.support.v4.app.FragmentTransaction;
|
2015-08-05 19:48:41 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.view.View;
|
2016-02-12 00:19:15 +01:00
|
|
|
import android.view.ViewGroup;
|
2016-11-03 23:04:41 +01:00
|
|
|
import android.widget.CompoundButton;
|
2016-02-12 00:19:15 +01:00
|
|
|
import android.widget.RelativeLayout;
|
2015-10-06 12:51:09 +02:00
|
|
|
|
2016-02-12 00:19:15 +01:00
|
|
|
import com.github.amlcurran.showcaseview.ShowcaseView;
|
2016-11-03 23:04:41 +01:00
|
|
|
import com.github.amlcurran.showcaseview.targets.Target;
|
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;
|
2016-09-16 17:35:24 +02:00
|
|
|
import com.mikepenz.materialdrawer.AccountHeader;
|
|
|
|
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
|
2015-08-05 19:48:41 +02:00
|
|
|
import com.mikepenz.materialdrawer.Drawer;
|
|
|
|
import com.mikepenz.materialdrawer.DrawerBuilder;
|
2016-11-03 23:04:41 +01:00
|
|
|
import com.mikepenz.materialdrawer.interfaces.OnCheckedChangeListener;
|
2016-02-12 00:19:15 +01:00
|
|
|
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
|
2015-08-05 19:48:41 +02:00
|
|
|
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem;
|
2015-10-12 14:44:01 +02:00
|
|
|
import com.mikepenz.materialdrawer.model.SwitchDrawerItem;
|
2015-08-05 19:48:41 +02:00
|
|
|
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
|
|
|
|
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
|
|
|
|
import com.mikepenz.materialdrawer.model.interfaces.Nameable;
|
2015-10-06 12:51:09 +02:00
|
|
|
|
2015-09-11 07:59:39 +02:00
|
|
|
import java.io.Serializable;
|
2016-02-23 07:06:34 +01:00
|
|
|
import java.lang.ref.WeakReference;
|
2015-08-05 19:48:41 +02:00
|
|
|
import java.util.ArrayList;
|
2015-11-22 12:28:31 +01:00
|
|
|
import java.util.List;
|
2015-08-05 19:48:41 +02:00
|
|
|
|
2016-10-20 12:53:35 +02:00
|
|
|
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity;
|
2017-04-25 23:00:31 +02:00
|
|
|
import ch.dissem.apps.abit.drawer.ProfileImageListener;
|
|
|
|
import ch.dissem.apps.abit.drawer.ProfileSelectionListener;
|
2015-10-18 21:47:07 +02:00
|
|
|
import ch.dissem.apps.abit.listener.ActionBarListener;
|
|
|
|
import ch.dissem.apps.abit.listener.ListSelectionListener;
|
2015-10-31 07:49:03 +01:00
|
|
|
import ch.dissem.apps.abit.service.BitmessageService;
|
2015-10-12 14:44:01 +02:00
|
|
|
import ch.dissem.apps.abit.service.Singleton;
|
2015-12-21 15:31:48 +01:00
|
|
|
import ch.dissem.apps.abit.synchronization.SyncAdapter;
|
2017-03-23 16:59:36 +01:00
|
|
|
import ch.dissem.apps.abit.util.Labels;
|
2015-12-21 15:31:48 +01:00
|
|
|
import ch.dissem.apps.abit.util.Preferences;
|
2016-01-19 20:50:58 +01:00
|
|
|
import ch.dissem.bitmessage.BitmessageContext;
|
2015-10-12 14:44:01 +02:00
|
|
|
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
|
|
|
import ch.dissem.bitmessage.entity.Plaintext;
|
|
|
|
import ch.dissem.bitmessage.entity.valueobject.Label;
|
|
|
|
|
2016-10-20 12:53:35 +02:00
|
|
|
import static ch.dissem.apps.abit.ComposeMessageActivity.launchReplyTo;
|
2017-04-21 07:23:39 +02:00
|
|
|
import static ch.dissem.apps.abit.repository.AndroidMessageRepository.LABEL_ARCHIVE;
|
2016-01-19 20:50:58 +01:00
|
|
|
import static ch.dissem.apps.abit.service.BitmessageService.isRunning;
|
2015-10-12 14:44:01 +02:00
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2015-11-22 12:28:31 +01:00
|
|
|
public class MainActivity extends AppCompatActivity
|
2016-09-16 17:35:24 +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";
|
2016-11-02 20:55:56 +01:00
|
|
|
public static final String EXTRA_SHOW_LABEL = "ch.dissem.abit.ShowLabel";
|
2016-10-20 12:53:35 +02:00
|
|
|
public static final String EXTRA_REPLY_TO_MESSAGE = "ch.dissem.abit.ReplyToMessage";
|
2015-09-04 08:19:07 +02:00
|
|
|
public static final String ACTION_SHOW_INBOX = "ch.dissem.abit.ShowInbox";
|
2015-08-28 13:49:53 +02:00
|
|
|
|
2017-04-25 23:00:31 +02:00
|
|
|
public static final int ADD_IDENTITY = 1;
|
|
|
|
public static final int MANAGE_IDENTITY = 2;
|
2016-10-16 23:16:38 +02:00
|
|
|
|
|
|
|
private static final long ID_NODE_SWITCH = 1;
|
2015-08-05 19:48:41 +02:00
|
|
|
|
2016-09-16 17:35:24 +02:00
|
|
|
private static WeakReference<MainActivity> instance;
|
2016-02-23 07:06:34 +01:00
|
|
|
|
2017-07-22 08:16:06 +02:00
|
|
|
private boolean active;
|
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
/**
|
|
|
|
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
private boolean twoPane;
|
|
|
|
|
|
|
|
private Label selectedLabel;
|
|
|
|
|
2016-01-19 20:50:58 +01:00
|
|
|
private BitmessageContext bmc;
|
|
|
|
private AccountHeader accountHeader;
|
2015-10-23 22:40:09 +02:00
|
|
|
|
2016-02-12 00:19:15 +01:00
|
|
|
private Drawer drawer;
|
2016-10-16 23:16:38 +02:00
|
|
|
private SwitchDrawerItem nodeSwitch;
|
2016-02-12 00:19:15 +01:00
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2016-10-01 10:33:11 +02:00
|
|
|
instance = new WeakReference<>(this);
|
2016-01-19 20:50:58 +01:00
|
|
|
bmc = Singleton.getBitmessageContext(this);
|
2015-08-05 19:48:41 +02:00
|
|
|
|
|
|
|
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();
|
2016-10-16 23:16:38 +02:00
|
|
|
getSupportFragmentManager()
|
|
|
|
.beginTransaction()
|
|
|
|
.replace(R.id.item_list, listFragment)
|
2016-09-16 17:35:24 +02:00
|
|
|
.commit();
|
2015-09-11 07:59:39 +02:00
|
|
|
|
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;
|
2015-10-08 14:11:45 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-02-23 17:38:00 +01:00
|
|
|
createDrawer(toolbar);
|
2015-08-05 19:48:41 +02:00
|
|
|
|
2015-09-04 08:19:07 +02:00
|
|
|
// handle intents
|
2016-10-20 12:53:35 +02:00
|
|
|
Intent intent = getIntent();
|
|
|
|
if (intent.hasExtra(EXTRA_SHOW_MESSAGE)) {
|
|
|
|
onItemSelected(intent.getSerializableExtra(EXTRA_SHOW_MESSAGE));
|
|
|
|
}
|
|
|
|
if (intent.hasExtra(EXTRA_REPLY_TO_MESSAGE)) {
|
|
|
|
Plaintext item = (Plaintext) intent.getSerializableExtra(EXTRA_REPLY_TO_MESSAGE);
|
|
|
|
launchReplyTo(this, item);
|
2015-10-06 12:51:09 +02:00
|
|
|
}
|
2015-10-08 14:11:45 +02:00
|
|
|
|
2015-12-21 15:31:48 +01:00
|
|
|
if (Preferences.useTrustedNode(this)) {
|
|
|
|
SyncAdapter.startSync(this);
|
|
|
|
} else {
|
|
|
|
SyncAdapter.stopSync(this);
|
2015-10-08 14:11:45 +02:00
|
|
|
}
|
2016-02-12 00:19:15 +01:00
|
|
|
if (drawer.isDrawerOpen()) {
|
|
|
|
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(ViewGroup
|
2016-09-16 17:35:24 +02:00
|
|
|
.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
2016-02-12 00:19:15 +01:00
|
|
|
lps.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
|
|
|
|
lps.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
|
|
|
|
int margin = ((Number) (getResources().getDisplayMetrics().density * 12)).intValue();
|
|
|
|
lps.setMargins(margin, margin, margin, margin);
|
|
|
|
|
2016-10-16 23:16:38 +02:00
|
|
|
new ShowcaseView.Builder(this)
|
2016-09-16 17:35:24 +02:00
|
|
|
.withMaterialShowcase()
|
|
|
|
.setStyle(R.style.CustomShowcaseTheme)
|
|
|
|
.setContentTitle(R.string.full_node)
|
|
|
|
.setContentText(R.string.full_node_description)
|
2016-11-03 23:04:41 +01:00
|
|
|
.setTarget(new Target() {
|
|
|
|
@Override
|
|
|
|
public Point getPoint() {
|
|
|
|
View view = drawer.getStickyFooter();
|
|
|
|
int[] location = new int[2];
|
|
|
|
view.getLocationInWindow(location);
|
|
|
|
int x = location[0] + 7 * view.getWidth() / 8;
|
|
|
|
int y = location[1] + view.getHeight() / 2;
|
|
|
|
return new Point(x, y);
|
|
|
|
}
|
2016-10-16 23:16:38 +02:00
|
|
|
})
|
2016-09-16 17:35:24 +02:00
|
|
|
.replaceEndButton(R.layout.showcase_button)
|
|
|
|
.hideOnTouchOutside()
|
2016-10-16 23:16:38 +02:00
|
|
|
.build()
|
|
|
|
.setButtonPosition(lps);
|
2016-02-12 00:19:15 +01:00
|
|
|
}
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 23:46:57 +02:00
|
|
|
private <F extends Fragment & ListHolder> void changeList(F listFragment) {
|
2017-07-22 08:16:06 +02:00
|
|
|
if (active) {
|
|
|
|
FragmentTransaction transaction = getSupportFragmentManager()
|
|
|
|
.beginTransaction();
|
|
|
|
transaction.replace(R.id.item_list, listFragment);
|
|
|
|
Fragment detailFragment = getSupportFragmentManager().findFragmentById(R.id.message_detail_container);
|
|
|
|
if (detailFragment != null) {
|
|
|
|
transaction.remove(detailFragment);
|
|
|
|
}
|
|
|
|
transaction.addToBackStack(null).commit();
|
2017-07-10 06:21:29 +02:00
|
|
|
|
2017-07-22 08:16:06 +02:00
|
|
|
if (twoPane) {
|
|
|
|
// In two-pane mode, list items should be given the
|
|
|
|
// 'activated' state when touched.
|
|
|
|
listFragment.setActivateOnItemClick(true);
|
|
|
|
}
|
2015-09-11 07:59:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:38:00 +01:00
|
|
|
private void createDrawer(Toolbar toolbar) {
|
2015-08-05 19:48:41 +02:00
|
|
|
final ArrayList<IProfile> profiles = new ArrayList<>();
|
|
|
|
profiles.add(new ProfileSettingDrawerItem()
|
2016-09-16 17:35:24 +02:00
|
|
|
.withName(getString(R.string.add_identity))
|
|
|
|
.withDescription(getString(R.string.add_identity_summary))
|
|
|
|
.withIcon(new IconicsDrawable(this, GoogleMaterial.Icon.gmd_add)
|
|
|
|
.actionBar()
|
|
|
|
.paddingDp(5)
|
|
|
|
.colorRes(R.color.icons))
|
|
|
|
.withIdentifier(ADD_IDENTITY)
|
2015-08-05 19:48:41 +02:00
|
|
|
);
|
2016-09-12 09:55:48 +02:00
|
|
|
profiles.add(new ProfileSettingDrawerItem()
|
2016-09-16 17:35:24 +02:00
|
|
|
.withName(getString(R.string.manage_identity))
|
|
|
|
.withIcon(GoogleMaterial.Icon.gmd_settings)
|
|
|
|
.withIdentifier(MANAGE_IDENTITY)
|
2015-08-05 19:48:41 +02:00
|
|
|
);
|
|
|
|
// Create the AccountHeader
|
2016-01-19 20:50:58 +01:00
|
|
|
accountHeader = new AccountHeaderBuilder()
|
2016-09-16 17:35:24 +02:00
|
|
|
.withActivity(this)
|
|
|
|
.withHeaderBackground(R.drawable.header)
|
|
|
|
.withProfiles(profiles)
|
2017-04-25 23:00:31 +02:00
|
|
|
.withOnAccountHeaderProfileImageListener(new ProfileImageListener(this))
|
|
|
|
.withOnAccountHeaderListener(new ProfileSelectionListener(MainActivity.this, getSupportFragmentManager()))
|
2016-09-16 17:35:24 +02:00
|
|
|
.build();
|
2015-12-21 15:31:48 +01:00
|
|
|
if (profiles.size() > 2) { // There's always the add and manage identity items
|
2015-11-11 21:03:03 +01:00
|
|
|
accountHeader.setActiveProfile(profiles.get(0), true);
|
|
|
|
}
|
2015-08-05 19:48:41 +02:00
|
|
|
|
2017-02-23 17:38:00 +01:00
|
|
|
final ArrayList<IDrawerItem> drawerItems = new ArrayList<>();
|
2015-10-12 16:00:43 +02:00
|
|
|
drawerItems.add(new PrimaryDrawerItem()
|
2016-09-16 17:35:24 +02:00
|
|
|
.withName(R.string.archive)
|
2017-04-21 07:23:39 +02:00
|
|
|
.withTag(LABEL_ARCHIVE)
|
2016-09-16 17:35:24 +02:00
|
|
|
.withIcon(CommunityMaterial.Icon.cmd_archive)
|
2015-10-12 16:00:43 +02:00
|
|
|
);
|
2016-02-12 00:19:15 +01:00
|
|
|
drawerItems.add(new DividerDrawerItem());
|
|
|
|
drawerItems.add(new PrimaryDrawerItem()
|
2016-09-16 17:35:24 +02:00
|
|
|
.withName(R.string.contacts_and_subscriptions)
|
|
|
|
.withIcon(GoogleMaterial.Icon.gmd_contacts));
|
2016-02-12 00:19:15 +01:00
|
|
|
drawerItems.add(new PrimaryDrawerItem()
|
2016-09-16 17:35:24 +02:00
|
|
|
.withName(R.string.settings)
|
|
|
|
.withIcon(GoogleMaterial.Icon.gmd_settings));
|
2015-10-12 16:00:43 +02:00
|
|
|
|
2016-10-16 23:16:38 +02:00
|
|
|
nodeSwitch = new SwitchDrawerItem()
|
|
|
|
.withIdentifier(ID_NODE_SWITCH)
|
|
|
|
.withName(R.string.full_node)
|
|
|
|
.withIcon(CommunityMaterial.Icon.cmd_cloud_outline)
|
|
|
|
.withChecked(isRunning())
|
2016-11-03 23:04:41 +01:00
|
|
|
.withOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView,
|
|
|
|
boolean isChecked) {
|
2017-05-07 13:39:30 +02:00
|
|
|
Preferences.setFullNodeActive(MainActivity.this, isChecked);
|
2016-11-03 23:04:41 +01:00
|
|
|
if (isChecked) {
|
|
|
|
checkAndStartNode();
|
|
|
|
} else {
|
|
|
|
stopService(new Intent(MainActivity.this, BitmessageService.class));
|
|
|
|
}
|
2016-10-16 23:16:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-12 00:19:15 +01:00
|
|
|
drawer = new DrawerBuilder()
|
2016-09-16 17:35:24 +02:00
|
|
|
.withActivity(this)
|
|
|
|
.withToolbar(toolbar)
|
|
|
|
.withAccountHeader(accountHeader)
|
|
|
|
.withDrawerItems(drawerItems)
|
2016-10-16 23:16:38 +02:00
|
|
|
.addStickyDrawerItems(nodeSwitch)
|
2017-04-25 23:00:31 +02:00
|
|
|
.withOnDrawerItemClickListener(new DrawerItemClickListener())
|
2016-09-16 17:35:24 +02:00
|
|
|
.withShowDrawerOnFirstLaunch(true)
|
|
|
|
.build();
|
2017-02-23 17:38:00 +01:00
|
|
|
|
2017-05-05 07:27:24 +02:00
|
|
|
loadDrawerItemsAsynchronously();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadDrawerItemsAsynchronously() {
|
2017-02-23 17:38:00 +01:00
|
|
|
new AsyncTask<Void, Void, List<BitmessageAddress>>() {
|
|
|
|
@Override
|
|
|
|
protected List<BitmessageAddress> doInBackground(Void... params) {
|
|
|
|
List<BitmessageAddress> identities = bmc.addresses().getIdentities();
|
|
|
|
if (identities.isEmpty()) {
|
|
|
|
// Create an initial identity
|
|
|
|
Singleton.getIdentity(MainActivity.this);
|
|
|
|
}
|
|
|
|
return identities;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<BitmessageAddress> identities) {
|
|
|
|
for (BitmessageAddress identity : identities) {
|
|
|
|
addIdentityEntry(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
|
|
|
|
new AsyncTask<Void, Void, List<Label>>() {
|
|
|
|
@Override
|
|
|
|
protected List<Label> doInBackground(Void... params) {
|
|
|
|
return bmc.messages().getLabels();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<Label> labels) {
|
|
|
|
if (getIntent().hasExtra(EXTRA_SHOW_LABEL)) {
|
|
|
|
selectedLabel = (Label) getIntent().getSerializableExtra(EXTRA_SHOW_LABEL);
|
|
|
|
} else if (selectedLabel == null) {
|
|
|
|
selectedLabel = labels.get(0);
|
|
|
|
}
|
|
|
|
for (Label label : labels) {
|
|
|
|
addLabelEntry(label);
|
|
|
|
}
|
2017-04-07 14:36:16 +02:00
|
|
|
IDrawerItem selectedDrawerItem = drawer.getDrawerItem(selectedLabel);
|
|
|
|
if (selectedDrawerItem != null) {
|
|
|
|
drawer.setSelection(selectedDrawerItem);
|
|
|
|
}
|
2017-02-23 17:38:00 +01:00
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
2017-07-10 06:21:29 +02:00
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
Fragment listFragment = getSupportFragmentManager().findFragmentById(R.id.item_list);
|
|
|
|
if (listFragment instanceof ListHolder) {
|
|
|
|
ListHolder listHolder = (ListHolder) listFragment;
|
|
|
|
if (listHolder.showPreviousList()) {
|
|
|
|
IDrawerItem drawerItem = drawer.getDrawerItem(listHolder.getCurrentLabel());
|
2017-07-22 08:16:06 +02:00
|
|
|
if (drawerItem != null) {
|
2017-07-10 06:21:29 +02:00
|
|
|
drawer.setSelection(drawerItem);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
|
2017-04-25 23:00:31 +02:00
|
|
|
private class DrawerItemClickListener implements Drawer.OnDrawerItemClickListener {
|
|
|
|
@Override
|
|
|
|
public boolean onItemClick(View view, int position, IDrawerItem item) {
|
2017-07-10 06:21:29 +02:00
|
|
|
Fragment itemList = getSupportFragmentManager().findFragmentById(R.id.item_list);
|
2017-04-25 23:00:31 +02:00
|
|
|
if (item.getTag() instanceof Label) {
|
|
|
|
selectedLabel = (Label) item.getTag();
|
2017-07-10 06:21:29 +02:00
|
|
|
if (itemList instanceof
|
2017-04-25 23:00:31 +02:00
|
|
|
MessageListFragment) {
|
2017-07-10 06:21:29 +02:00
|
|
|
((MessageListFragment) itemList).updateList(selectedLabel);
|
2017-04-25 23:00:31 +02:00
|
|
|
} else {
|
|
|
|
MessageListFragment listFragment = new MessageListFragment();
|
|
|
|
changeList(listFragment);
|
|
|
|
listFragment.updateList(selectedLabel);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if (item instanceof Nameable<?>) {
|
|
|
|
Nameable<?> ni = (Nameable<?>) item;
|
|
|
|
switch (ni.getName().getTextRes()) {
|
|
|
|
case R.string.contacts_and_subscriptions:
|
2017-07-10 06:21:29 +02:00
|
|
|
if (!(itemList instanceof AddressListFragment)) {
|
2017-04-25 23:00:31 +02:00
|
|
|
changeList(new AddressListFragment());
|
|
|
|
} else {
|
2017-07-10 06:21:29 +02:00
|
|
|
((AddressListFragment) itemList).updateList();
|
2017-04-25 23:00:31 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case R.string.settings:
|
2017-07-10 06:21:29 +02:00
|
|
|
getSupportFragmentManager()
|
|
|
|
.beginTransaction()
|
|
|
|
.replace(R.id.item_list, new SettingsFragment())
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
2017-04-25 23:00:31 +02:00
|
|
|
return false;
|
|
|
|
case R.string.full_node:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:38:00 +01:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle savedInstanceState) {
|
|
|
|
super.onSaveInstanceState(savedInstanceState);
|
|
|
|
savedInstanceState.putSerializable("selectedLabel", selectedLabel);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
|
selectedLabel = (Label) savedInstanceState.getSerializable("selectedLabel");
|
2017-03-18 07:09:03 +01:00
|
|
|
|
2017-04-07 14:36:16 +02:00
|
|
|
IDrawerItem selectedItem = drawer.getDrawerItem(selectedLabel);
|
|
|
|
if (selectedItem != null) {
|
|
|
|
drawer.setSelection(selectedItem);
|
|
|
|
}
|
2017-02-23 17:38:00 +01:00
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|
|
|
|
|
2016-02-23 07:06:34 +01:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
updateUnread();
|
2017-05-08 20:28:43 +02:00
|
|
|
if (Preferences.isFullNodeActive(this) && Preferences.isConnectionAllowed(MainActivity.this)) {
|
|
|
|
startService(new Intent(this, BitmessageService.class));
|
2017-05-07 13:39:30 +02:00
|
|
|
}
|
2016-10-16 23:16:38 +02:00
|
|
|
updateNodeSwitch();
|
2016-10-27 17:37:34 +02:00
|
|
|
Singleton.getMessageListener(this).resetNotification();
|
2017-07-22 08:16:06 +02:00
|
|
|
active = true;
|
2016-02-23 07:06:34 +01:00
|
|
|
super.onResume();
|
|
|
|
}
|
|
|
|
|
2017-07-22 08:16:06 +02:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
2016-10-01 10:33:11 +02:00
|
|
|
public void addIdentityEntry(BitmessageAddress identity) {
|
|
|
|
IProfile newProfile = new ProfileDrawerItem()
|
|
|
|
.withIcon(new Identicon(identity))
|
2016-09-16 17:35:24 +02:00
|
|
|
.withName(identity.toString())
|
2016-10-01 10:33:11 +02:00
|
|
|
.withNameShown(true)
|
2016-09-16 17:35:24 +02:00
|
|
|
.withEmail(identity.getAddress())
|
|
|
|
.withTag(identity);
|
2016-09-12 09:55:48 +02:00
|
|
|
if (accountHeader.getProfiles() != null) {
|
2016-10-27 17:37:34 +02:00
|
|
|
// we know that there are 2 setting elements.
|
2016-09-12 09:55:48 +02:00
|
|
|
// Set the new profile above them ;)
|
|
|
|
accountHeader.addProfile(
|
2016-10-27 17:37:34 +02:00
|
|
|
newProfile, accountHeader.getProfiles().size() - 2);
|
2016-09-12 09:55:48 +02:00
|
|
|
} else {
|
|
|
|
accountHeader.addProfiles(newProfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:38:00 +01:00
|
|
|
public void addLabelEntry(Label label) {
|
|
|
|
PrimaryDrawerItem item = new PrimaryDrawerItem()
|
|
|
|
.withName(label.toString())
|
2017-03-23 16:59:36 +01:00
|
|
|
.withTag(label)
|
|
|
|
.withIcon(Labels.getIcon(label))
|
|
|
|
.withIconColor(Labels.getColor(label));
|
2017-02-23 17:38:00 +01:00
|
|
|
drawer.addItemAtPosition(item, drawer.getDrawerItems().size() - 3);
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:01:33 +02:00
|
|
|
public void updateIdentityEntry(BitmessageAddress identity) {
|
|
|
|
for (IProfile profile : accountHeader.getProfiles()) {
|
|
|
|
if (profile instanceof ProfileDrawerItem) {
|
2017-04-25 23:00:31 +02:00
|
|
|
ProfileDrawerItem profileDrawerItem = (ProfileDrawerItem) profile;
|
|
|
|
if (identity.equals(profileDrawerItem.getTag())) {
|
|
|
|
profileDrawerItem
|
2016-10-06 22:01:33 +02:00
|
|
|
.withName(identity.toString())
|
|
|
|
.withTag(identity);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 10:33:11 +02:00
|
|
|
public void removeIdentityEntry(BitmessageAddress identity) {
|
|
|
|
for (IProfile profile : accountHeader.getProfiles()) {
|
|
|
|
if (profile instanceof ProfileDrawerItem) {
|
2017-04-25 23:00:31 +02:00
|
|
|
ProfileDrawerItem profileDrawerItem = (ProfileDrawerItem) profile;
|
|
|
|
if (identity.equals(profileDrawerItem.getTag())) {
|
2016-10-01 10:33:11 +02:00
|
|
|
accountHeader.removeProfile(profile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 07:06:34 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 12:53:35 +02:00
|
|
|
private void checkAndStartNode() {
|
2015-12-27 20:04:17 +01:00
|
|
|
if (Preferences.isConnectionAllowed(MainActivity.this)) {
|
2016-10-20 12:53:35 +02:00
|
|
|
startService(new Intent(this, BitmessageService.class));
|
2015-12-27 20:04:17 +01:00
|
|
|
} else {
|
2016-10-20 12:53:35 +02:00
|
|
|
startActivity(new Intent(this, FullNodeDialogActivity.class));
|
2015-12-27 20:04:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 07:06:34 +01:00
|
|
|
@Override
|
|
|
|
public void updateUnread() {
|
|
|
|
for (IDrawerItem item : drawer.getDrawerItems()) {
|
|
|
|
if (item.getTag() instanceof Label) {
|
|
|
|
Label label = (Label) item.getTag();
|
2017-04-21 07:23:39 +02:00
|
|
|
if (label != LABEL_ARCHIVE) {
|
|
|
|
int unread = bmc.messages().countUnread(label);
|
|
|
|
if (unread > 0) {
|
|
|
|
((PrimaryDrawerItem) item).withBadge(String.valueOf(unread));
|
|
|
|
} else {
|
|
|
|
((PrimaryDrawerItem) item).withBadge((String) null);
|
|
|
|
}
|
|
|
|
drawer.updateItem(item);
|
2016-02-23 07:06:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 12:53:35 +02:00
|
|
|
public static void updateNodeSwitch() {
|
2016-11-03 23:04:41 +01:00
|
|
|
final MainActivity i = getInstance();
|
2016-10-20 12:53:35 +02:00
|
|
|
if (i != null) {
|
2016-11-03 23:04:41 +01:00
|
|
|
i.runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-05-18 07:29:16 +02:00
|
|
|
i.nodeSwitch.withChecked(Preferences.isFullNodeActive(i));
|
2016-11-03 23:04:41 +01:00
|
|
|
i.drawer.updateStickyFooterItem(i.nodeSwitch);
|
|
|
|
}
|
2016-10-20 12:53:35 +02:00
|
|
|
});
|
|
|
|
}
|
2016-10-16 23:16:38 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2017-06-29 23:29:56 +02:00
|
|
|
if (item instanceof Plaintext) {
|
2015-09-11 07:59:39 +02:00
|
|
|
fragment = new MessageDetailFragment();
|
2017-07-10 06:21:29 +02:00
|
|
|
} else if (item instanceof BitmessageAddress) {
|
2016-01-29 18:05:43 +01:00
|
|
|
fragment = new AddressDetailFragment();
|
2017-06-29 23:29:56 +02:00
|
|
|
} 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()
|
2016-09-16 17:35:24 +02:00
|
|
|
.replace(R.id.message_detail_container, fragment)
|
|
|
|
.commit();
|
2015-08-05 19:48:41 +02:00
|
|
|
} 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;
|
2016-11-02 20:55:56 +01:00
|
|
|
if (item instanceof Plaintext) {
|
2015-09-11 07:59:39 +02:00
|
|
|
detailIntent = new Intent(this, MessageDetailActivity.class);
|
2016-11-02 20:55:56 +01:00
|
|
|
detailIntent.putExtra(EXTRA_SHOW_LABEL, selectedLabel);
|
2017-07-10 06:21:29 +02:00
|
|
|
} else if (item instanceof BitmessageAddress) {
|
2016-01-29 18:05:43 +01:00
|
|
|
detailIntent = new Intent(this, AddressDetailActivity.class);
|
2016-11-02 20:55:56 +01:00
|
|
|
} else {
|
2015-12-21 15:31:48 +01:00
|
|
|
throw new IllegalArgumentException("Plaintext or BitmessageAddress expected, but " +
|
2016-09-16 17:35:24 +02:00
|
|
|
"was "
|
|
|
|
+ item.getClass().getSimpleName());
|
2016-11-02 20:55:56 +01:00
|
|
|
}
|
2015-09-11 07:59:39 +02:00
|
|
|
detailIntent.putExtra(MessageDetailFragment.ARG_ITEM, item);
|
2015-08-05 19:48:41 +02:00
|
|
|
startActivity(detailIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 06:21:29 +02:00
|
|
|
public boolean hasDetailPane() {
|
|
|
|
return twoPane;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDetailView(Fragment fragment) {
|
|
|
|
if (twoPane) {
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.replace(R.id.message_detail_container, fragment)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:45:21 +02:00
|
|
|
@Override
|
|
|
|
public void updateTitle(CharSequence title) {
|
2016-01-13 17:28:18 +01:00
|
|
|
if (getSupportActionBar() != null) {
|
|
|
|
getSupportActionBar().setTitle(title);
|
|
|
|
}
|
2015-09-06 17:45:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-05 19:48:41 +02:00
|
|
|
public Label getSelectedLabel() {
|
|
|
|
return selectedLabel;
|
|
|
|
}
|
2015-08-28 13:49:53 +02:00
|
|
|
|
2016-02-23 07:06:34 +01:00
|
|
|
public static MainActivity getInstance() {
|
|
|
|
if (instance == null) return null;
|
|
|
|
return instance.get();
|
|
|
|
}
|
2015-08-05 19:48:41 +02:00
|
|
|
}
|