diff --git a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.java b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.java index 142e8f8..13833c8 100644 --- a/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/ComposeMessageFragment.java @@ -67,26 +67,17 @@ public class ComposeMessageFragment extends Fragment { private Plaintext.Encoding encoding; private Plaintext parent; - /** - * Mandatory empty constructor for the fragment manager to instantiate the - * fragment (e.g. upon screen orientation changes). - */ - public ComposeMessageFragment() { - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { if (getArguments().containsKey(EXTRA_IDENTITY)) { identity = (BitmessageAddress) getArguments().getSerializable(EXTRA_IDENTITY); - if (getActivity() != null) { - if (identity == null || identity.getPrivateKey() == null) { - identity = Singleton.getIdentity(getActivity()); - } + if (getActivity() != null && (identity == null || identity.getPrivateKey() == null)) { + identity = Singleton.getIdentity(getActivity()); } } else { - throw new RuntimeException("No identity set for ComposeMessageFragment"); + throw new IllegalStateException("No identity set for ComposeMessageFragment"); } broadcast = getArguments().getBoolean(EXTRA_BROADCAST, false); if (getArguments().containsKey(EXTRA_RECIPIENT)) { @@ -107,7 +98,7 @@ public class ComposeMessageFragment extends Fragment { parent = (Plaintext) getArguments().getSerializable(EXTRA_PARENT); } } else { - throw new RuntimeException("No identity set for ComposeMessageFragment"); + throw new IllegalStateException("No identity set for ComposeMessageFragment"); } setHasOptionsMenu(true); } @@ -139,6 +130,7 @@ public class ComposeMessageFragment extends Fragment { @Override public void onNothingSelected(AdapterView parent) { + // leave current selection } }); if (recipient != null) { diff --git a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java index f7d2c07..20e238a 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java @@ -16,25 +16,17 @@ package ch.dissem.apps.abit; -import android.app.Dialog; -import android.content.DialogInterface; import android.content.Intent; import android.graphics.Point; -import android.graphics.drawable.ColorDrawable; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.view.Display; import android.view.View; import android.view.ViewGroup; -import android.view.Window; -import android.view.WindowManager; import android.widget.CompoundButton; -import android.widget.ImageView; import android.widget.RelativeLayout; -import android.widget.Toast; import com.github.amlcurran.showcaseview.ShowcaseView; import com.github.amlcurran.showcaseview.targets.Target; @@ -60,15 +52,14 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; -import ch.dissem.apps.abit.dialog.AddIdentityDialogFragment; import ch.dissem.apps.abit.dialog.FullNodeDialogActivity; +import ch.dissem.apps.abit.drawer.ProfileImageListener; +import ch.dissem.apps.abit.drawer.ProfileSelectionListener; import ch.dissem.apps.abit.listener.ActionBarListener; import ch.dissem.apps.abit.listener.ListSelectionListener; -import ch.dissem.apps.abit.repository.AndroidMessageRepository; import ch.dissem.apps.abit.service.BitmessageService; import ch.dissem.apps.abit.service.Singleton; import ch.dissem.apps.abit.synchronization.SyncAdapter; -import ch.dissem.apps.abit.util.Drawables; import ch.dissem.apps.abit.util.Labels; import ch.dissem.apps.abit.util.Preferences; import ch.dissem.bitmessage.BitmessageContext; @@ -76,7 +67,6 @@ import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.entity.valueobject.Label; -import static android.widget.Toast.LENGTH_LONG; import static ch.dissem.apps.abit.ComposeMessageActivity.launchReplyTo; import static ch.dissem.apps.abit.repository.AndroidMessageRepository.LABEL_ARCHIVE; import static ch.dissem.apps.abit.service.BitmessageService.isRunning; @@ -106,8 +96,8 @@ public class MainActivity extends AppCompatActivity public static final String EXTRA_REPLY_TO_MESSAGE = "ch.dissem.abit.ReplyToMessage"; public static final String ACTION_SHOW_INBOX = "ch.dissem.abit.ShowInbox"; - private static final int ADD_IDENTITY = 1; - private static final int MANAGE_IDENTITY = 2; + public static final int ADD_IDENTITY = 1; + public static final int MANAGE_IDENTITY = 2; private static final long ID_NODE_SWITCH = 1; @@ -239,81 +229,8 @@ public class MainActivity extends AppCompatActivity .withActivity(this) .withHeaderBackground(R.drawable.header) .withProfiles(profiles) - .withOnAccountHeaderProfileImageListener(new AccountHeader.OnAccountHeaderProfileImageListener() { - @Override - public boolean onProfileImageClick(View view, IProfile profile, boolean current) { - if (current) { - // Show QR code in modal dialog - final Dialog dialog = new Dialog(MainActivity.this); - dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); - - ImageView imageView = new ImageView(MainActivity.this); - imageView.setImageBitmap(Drawables.qrCode(Singleton.getIdentity(MainActivity.this))); - imageView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dialog.dismiss(); - } - }); - dialog.addContentView(imageView, new RelativeLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.MATCH_PARENT)); - Window window = dialog.getWindow(); - if (window != null) { - Display display = window.getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - int dim = size.x < size.y ? size.x : size.y; - - WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); - lp.copyFrom(window.getAttributes()); - lp.width = dim; - lp.height = dim; - - window.setAttributes(lp); - } - dialog.show(); - return true; - } - return false; - } - - @Override - public boolean onProfileImageLongClick(View view, IProfile iProfile, boolean b) { - return false; - } - }) - .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() { - @Override - public boolean onProfileChanged(View view, IProfile profile, boolean current) { - switch ((int) profile.getIdentifier()) { - case ADD_IDENTITY: - addIdentityDialog(); - break; - case MANAGE_IDENTITY: - BitmessageAddress identity = Singleton.getIdentity(MainActivity.this); - if (identity == null) { - Toast.makeText(MainActivity.this, - R.string.no_identity_warning, LENGTH_LONG).show(); - } else { - Intent show = new Intent(MainActivity.this, - AddressDetailActivity.class); - show.putExtra(AddressDetailFragment.ARG_ITEM, identity); - startActivity(show); - } - break; - default: - if (profile instanceof ProfileDrawerItem) { - Object tag = ((ProfileDrawerItem) profile).getTag(); - if (tag instanceof BitmessageAddress) { - Singleton.setIdentity((BitmessageAddress) tag); - } - } - } - // false if it should close the drawer - return false; - } - }) + .withOnAccountHeaderProfileImageListener(new ProfileImageListener(this)) + .withOnAccountHeaderListener(new ProfileSelectionListener(MainActivity.this, getSupportFragmentManager())) .build(); if (profiles.size() > 2) { // There's always the add and manage identity items accountHeader.setActiveProfile(profiles.get(0), true); @@ -356,44 +273,7 @@ public class MainActivity extends AppCompatActivity .withAccountHeader(accountHeader) .withDrawerItems(drawerItems) .addStickyDrawerItems(nodeSwitch) - .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { - @Override - public boolean onItemClick(View view, int position, IDrawerItem item) { - if (item.getTag() instanceof Label) { - selectedLabel = (Label) item.getTag(); - if (getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof - MessageListFragment) { - ((MessageListFragment) getSupportFragmentManager() - .findFragmentById(R.id.item_list)).updateList(selectedLabel); - } 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: - if (!(getSupportFragmentManager().findFragmentById(R.id - .item_list) instanceof AddressListFragment)) { - changeList(new AddressListFragment()); - } else { - ((AddressListFragment) getSupportFragmentManager() - .findFragmentById(R.id.item_list)).updateList(); - } - break; - case R.string.settings: - startActivity(new Intent(MainActivity.this, SettingsActivity - .class)); - break; - case R.string.full_node: - return true; - } - } - return false; - } - }) + .withOnDrawerItemClickListener(new DrawerItemClickListener()) .withShowDrawerOnFirstLaunch(true) .build(); @@ -440,6 +320,47 @@ public class MainActivity extends AppCompatActivity }.execute(); } + private class DrawerItemClickListener implements Drawer.OnDrawerItemClickListener { + @Override + public boolean onItemClick(View view, int position, IDrawerItem item) { + if (item.getTag() instanceof Label) { + selectedLabel = (Label) item.getTag(); + if (getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof + MessageListFragment) { + ((MessageListFragment) getSupportFragmentManager() + .findFragmentById(R.id.item_list)).updateList(selectedLabel); + } 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: + if (!(getSupportFragmentManager().findFragmentById(R.id + .item_list) instanceof AddressListFragment)) { + changeList(new AddressListFragment()); + } else { + ((AddressListFragment) getSupportFragmentManager() + .findFragmentById(R.id.item_list)).updateList(); + } + return false; + case R.string.settings: + startActivity(new Intent(MainActivity.this, SettingsActivity + .class)); + return false; + case R.string.full_node: + return true; + default: + return false; + } + } + return false; + } + } + @Override protected void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); @@ -458,11 +379,6 @@ public class MainActivity extends AppCompatActivity super.onRestoreInstanceState(savedInstanceState); } - private void addIdentityDialog() { - AddIdentityDialogFragment dialog = new AddIdentityDialogFragment(); - dialog.show(getSupportFragmentManager(), "dialog"); - } - @Override protected void onResume() { updateUnread(); @@ -500,8 +416,9 @@ public class MainActivity extends AppCompatActivity public void updateIdentityEntry(BitmessageAddress identity) { for (IProfile profile : accountHeader.getProfiles()) { if (profile instanceof ProfileDrawerItem) { - if (identity.equals(((ProfileDrawerItem) profile).getTag())) { - ((ProfileDrawerItem) profile) + ProfileDrawerItem profileDrawerItem = (ProfileDrawerItem) profile; + if (identity.equals(profileDrawerItem.getTag())) { + profileDrawerItem .withName(identity.toString()) .withTag(identity); return; @@ -513,7 +430,8 @@ public class MainActivity extends AppCompatActivity public void removeIdentityEntry(BitmessageAddress identity) { for (IProfile profile : accountHeader.getProfiles()) { if (profile instanceof ProfileDrawerItem) { - if (identity.equals(((ProfileDrawerItem) profile).getTag())) { + ProfileDrawerItem profileDrawerItem = (ProfileDrawerItem) profile; + if (identity.equals(profileDrawerItem.getTag())) { accountHeader.removeProfile(profile); return; } diff --git a/app/src/main/java/ch/dissem/apps/abit/drawer/ProfileImageListener.java b/app/src/main/java/ch/dissem/apps/abit/drawer/ProfileImageListener.java new file mode 100644 index 0000000..20539b4 --- /dev/null +++ b/app/src/main/java/ch/dissem/apps/abit/drawer/ProfileImageListener.java @@ -0,0 +1,69 @@ +package ch.dissem.apps.abit.drawer; + +import android.app.Dialog; +import android.content.Context; +import android.graphics.Point; +import android.view.Display; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.ImageView; +import android.widget.RelativeLayout; + +import com.mikepenz.materialdrawer.AccountHeader; +import com.mikepenz.materialdrawer.model.interfaces.IProfile; + +import ch.dissem.apps.abit.service.Singleton; +import ch.dissem.apps.abit.util.Drawables; + +public class ProfileImageListener implements AccountHeader.OnAccountHeaderProfileImageListener { + private final Context ctx; + + public ProfileImageListener(Context ctx) { + this.ctx = ctx; + } + + @Override + public boolean onProfileImageClick(View view, IProfile profile, boolean current) { + if (current) { + // Show QR code in modal dialog + final Dialog dialog = new Dialog(ctx); + dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); + + ImageView imageView = new ImageView(ctx); + imageView.setImageBitmap(Drawables.qrCode(Singleton.getIdentity(ctx))); + imageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dialog.dismiss(); + } + }); + dialog.addContentView(imageView, new RelativeLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT)); + Window window = dialog.getWindow(); + if (window != null) { + Display display = window.getWindowManager().getDefaultDisplay(); + Point size = new Point(); + display.getSize(size); + int dim = size.x < size.y ? size.x : size.y; + + WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); + lp.copyFrom(window.getAttributes()); + lp.width = dim; + lp.height = dim; + + window.setAttributes(lp); + } + dialog.show(); + return true; + } + return false; + } + + @Override + public boolean onProfileImageLongClick(View view, IProfile iProfile, boolean b) { + return false; + } +} diff --git a/app/src/main/java/ch/dissem/apps/abit/drawer/ProfileSelectionListener.java b/app/src/main/java/ch/dissem/apps/abit/drawer/ProfileSelectionListener.java new file mode 100644 index 0000000..1b4d66d --- /dev/null +++ b/app/src/main/java/ch/dissem/apps/abit/drawer/ProfileSelectionListener.java @@ -0,0 +1,65 @@ +package ch.dissem.apps.abit.drawer; + +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.FragmentManager; +import android.view.View; +import android.widget.Toast; + +import com.mikepenz.materialdrawer.AccountHeader; +import com.mikepenz.materialdrawer.model.ProfileDrawerItem; +import com.mikepenz.materialdrawer.model.interfaces.IProfile; + +import ch.dissem.apps.abit.AddressDetailActivity; +import ch.dissem.apps.abit.AddressDetailFragment; +import ch.dissem.apps.abit.MainActivity; +import ch.dissem.apps.abit.R; +import ch.dissem.apps.abit.dialog.AddIdentityDialogFragment; +import ch.dissem.apps.abit.service.Singleton; +import ch.dissem.bitmessage.entity.BitmessageAddress; + +import static android.widget.Toast.LENGTH_LONG; + +public class ProfileSelectionListener implements AccountHeader.OnAccountHeaderListener { + private final Context ctx; + private final FragmentManager fragmentManager; + + public ProfileSelectionListener(Context ctx, FragmentManager fragmentManager) { + this.ctx = ctx; + this.fragmentManager = fragmentManager; + } + + @Override + public boolean onProfileChanged(View view, IProfile profile, boolean current) { + switch ((int) profile.getIdentifier()) { + case MainActivity.ADD_IDENTITY: + addIdentityDialog(); + break; + case MainActivity.MANAGE_IDENTITY: + BitmessageAddress identity = Singleton.getIdentity(ctx); + if (identity == null) { + Toast.makeText(ctx, R.string.no_identity_warning, LENGTH_LONG).show(); + } else { + Intent show = new Intent(ctx, AddressDetailActivity.class); + show.putExtra(AddressDetailFragment.ARG_ITEM, identity); + ctx.startActivity(show); + } + break; + default: + if (profile instanceof ProfileDrawerItem) { + Object tag = ((ProfileDrawerItem) profile).getTag(); + if (tag instanceof BitmessageAddress) { + Singleton.setIdentity((BitmessageAddress) tag); + } + } + break; + } + // false if it should close the drawer + return false; + } + + private void addIdentityDialog() { + AddIdentityDialogFragment dialog = new AddIdentityDialogFragment(); + dialog.show(fragmentManager, "dialog"); + } +}