make loading of drawer items a bit more dynamic

This commit is contained in:
uberspot 2014-03-17 00:27:52 +02:00
parent cea62b1857
commit e01c99a193
3 changed files with 28 additions and 19 deletions

View File

@ -19,6 +19,11 @@ package org.sufficientlysecure.keychain;
import android.os.Environment; import android.os.Environment;
import org.spongycastle.jce.provider.BouncyCastleProvider; import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.sufficientlysecure.keychain.service.remote.RegisteredAppsListActivity;
import org.sufficientlysecure.keychain.ui.DecryptActivity;
import org.sufficientlysecure.keychain.ui.EncryptActivity;
import org.sufficientlysecure.keychain.ui.ImportKeysActivity;
import org.sufficientlysecure.keychain.ui.KeyListActivity;
public final class Constants { public final class Constants {
@ -63,4 +68,13 @@ public final class Constants {
public static final String KEY_SERVERS = "pool.sks-keyservers.net, subkeys.pgp.net, pgp.mit.edu"; public static final String KEY_SERVERS = "pool.sks-keyservers.net, subkeys.pgp.net, pgp.mit.edu";
} }
public static final class DrawerItems {
public static final Class KEY_LIST = KeyListActivity.class;
public static final Class ENCRYPT = EncryptActivity.class;
public static final Class DECRYPT = DecryptActivity.class;
public static final Class IMPORT_KEYS = ImportKeysActivity.class;
public static final Class REGISTERED_APPS_LIST = RegisteredAppsListActivity.class;
public static final Class[] ARRAY = new Class[]{KEY_LIST, ENCRYPT, DECRYPT,
IMPORT_KEYS, REGISTERED_APPS_LIST};
}
} }

View File

@ -30,9 +30,8 @@ import android.support.v7.app.ActionBarActivity;
import android.view.*; import android.view.*;
import android.widget.*; import android.widget.*;
import com.beardedhen.androidbootstrap.FontAwesomeText; import com.beardedhen.androidbootstrap.FontAwesomeText;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.ActionBarHelper;
import org.sufficientlysecure.keychain.service.remote.RegisteredAppsListActivity;
public class DrawerActivity extends ActionBarActivity { public class DrawerActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout; private DrawerLayout mDrawerLayout;
@ -43,9 +42,6 @@ public class DrawerActivity extends ActionBarActivity {
private CharSequence mTitle; private CharSequence mTitle;
private boolean mIsDrawerLocked = false; private boolean mIsDrawerLocked = false;
private static Class[] mItemsClass = new Class[]{KeyListActivity.class,
EncryptActivity.class, DecryptActivity.class, ImportKeysActivity.class,
RegisteredAppsListActivity.class};
private Class mSelectedItem; private Class mSelectedItem;
private static final int MENU_ID_PREFERENCE = 222; private static final int MENU_ID_PREFERENCE = 222;
@ -102,7 +98,7 @@ public class DrawerActivity extends ActionBarActivity {
public void onDrawerClosed(View view) { public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle); getSupportActionBar().setTitle(mTitle);
callIntentForSelectedItem(); callIntentForDrawerItem(mSelectedItem);
} }
public void onDrawerOpened(View drawerView) { public void onDrawerOpened(View drawerView) {
@ -112,11 +108,10 @@ public class DrawerActivity extends ActionBarActivity {
supportInvalidateOptionsMenu(); supportInvalidateOptionsMenu();
} }
}; };
if ( !mIsDrawerLocked ) { if ( !mIsDrawerLocked ) {
mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.setDrawerListener(mDrawerToggle);
} } else {
if ( mIsDrawerLocked ) {
// If the drawer is locked open make it un-focusable // If the drawer is locked open make it un-focusable
// so that it doesn't consume all the Back button presses // so that it doesn't consume all the Back button presses
mDrawerLayout.setFocusableInTouchMode(false); mDrawerLayout.setFocusableInTouchMode(false);
@ -126,16 +121,20 @@ public class DrawerActivity extends ActionBarActivity {
// } // }
} }
private void callIntentForSelectedItem() { /**
* Uses startActivity to call the Intent of the given class
* @param drawerItem the class of the drawer item you want to load. Based on Constants.DrawerItems.*
*/
public void callIntentForDrawerItem(Class drawerItem) {
// creates call to onPrepareOptionsMenu() // creates call to onPrepareOptionsMenu()
supportInvalidateOptionsMenu(); supportInvalidateOptionsMenu();
// call intent activity if selected // call intent activity if selected
if (mSelectedItem != null) { if (drawerItem != null) {
finish(); finish();
overridePendingTransition(0, 0); overridePendingTransition(0, 0);
Intent intent = new Intent(this, mSelectedItem); Intent intent = new Intent(this, drawerItem);
startActivity(intent); startActivity(intent);
// disable animation of activity start // disable animation of activity start
@ -214,7 +213,7 @@ public class DrawerActivity extends ActionBarActivity {
// update selected item and title, then close the drawer // update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true); mDrawerList.setItemChecked(position, true);
// set selected class // set selected class
mSelectedItem = mItemsClass[position]; mSelectedItem = Constants.DrawerItems.ARRAY[position];
// setTitle(mDrawerTitles[position]); // setTitle(mDrawerTitles[position]);
// If drawer isn't locked just close the drawer and // If drawer isn't locked just close the drawer and
@ -223,7 +222,7 @@ public class DrawerActivity extends ActionBarActivity {
mDrawerLayout.closeDrawer(mDrawerList); mDrawerLayout.closeDrawer(mDrawerList);
// else move to the selected item yourself // else move to the selected item yourself
} else { } else {
callIntentForSelectedItem(); callIntentForDrawerItem(mSelectedItem);
} }
} }

View File

@ -53,11 +53,7 @@ public class KeyListActivity extends DrawerActivity {
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.menu_key_list_import: case R.id.menu_key_list_import:
callIntentForDrawerItem(Constants.DrawerItems.IMPORT_KEYS);
overridePendingTransition(0, 0);
Intent intentImport = new Intent(this, ImportKeysActivity.class);
startActivityForResult(intentImport, 0);
overridePendingTransition(0, 0);
return true; return true;
case R.id.menu_key_list_export: case R.id.menu_key_list_export: