2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.activity;
|
2009-06-08 23:11:35 -04:00
|
|
|
|
|
|
|
import android.content.Intent;
|
2012-07-12 12:29:41 -04:00
|
|
|
import android.view.ContextMenu;
|
2009-06-08 23:11:35 -04:00
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
2012-07-12 12:29:41 -04:00
|
|
|
import android.view.View;
|
2009-06-08 23:11:35 -04:00
|
|
|
import android.widget.AdapterView;
|
2009-12-09 22:16:42 -05:00
|
|
|
import android.widget.AdapterView.AdapterContextMenuInfo;
|
2009-06-08 23:11:35 -04:00
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.Toast;
|
2012-07-12 12:29:41 -04:00
|
|
|
|
|
|
|
import com.actionbarsherlock.view.Menu;
|
|
|
|
import com.actionbarsherlock.view.MenuItem;
|
2010-03-03 23:00:30 -05:00
|
|
|
import com.fsck.k9.Identity;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.Preferences;
|
|
|
|
import com.fsck.k9.R;
|
2009-06-08 23:11:35 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public class ManageIdentities extends ChooseIdentity {
|
2009-06-08 23:11:35 -04:00
|
|
|
private boolean mIdentitiesChanged = false;
|
2009-12-14 21:50:53 -05:00
|
|
|
public static final String EXTRA_IDENTITIES = "com.fsck.k9.EditIdentity_identities";
|
2009-06-08 23:11:35 -04:00
|
|
|
|
|
|
|
private static final int ACTIVITY_EDIT_IDENTITY = 1;
|
2010-04-16 08:20:10 -04:00
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
protected void setupClickListeners() {
|
|
|
|
this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
2010-04-16 08:20:10 -04:00
|
|
|
editItem(position);
|
2009-06-08 23:11:35 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ListView listView = getListView();
|
|
|
|
registerForContextMenu(listView);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void editItem(int i) {
|
2009-06-08 23:11:35 -04:00
|
|
|
Intent intent = new Intent(ManageIdentities.this, EditIdentity.class);
|
2010-03-03 23:00:30 -05:00
|
|
|
intent.putExtra(EditIdentity.EXTRA_ACCOUNT, mAccount.getUuid());
|
2009-06-08 23:11:35 -04:00
|
|
|
intent.putExtra(EditIdentity.EXTRA_IDENTITY, mAccount.getIdentity(i));
|
|
|
|
intent.putExtra(EditIdentity.EXTRA_IDENTITY_INDEX, i);
|
|
|
|
startActivityForResult(intent, ACTIVITY_EDIT_IDENTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2009-06-08 23:11:35 -04:00
|
|
|
super.onCreateOptionsMenu(menu);
|
2012-07-12 12:29:41 -04:00
|
|
|
getSupportMenuInflater().inflate(R.menu.manage_identities_option, menu);
|
2009-06-08 23:11:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.new_identity:
|
|
|
|
Intent intent = new Intent(ManageIdentities.this, EditIdentity.class);
|
|
|
|
intent.putExtra(EditIdentity.EXTRA_ACCOUNT, mAccount.getUuid());
|
|
|
|
startActivityForResult(intent, ACTIVITY_EDIT_IDENTITY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
2009-06-08 23:11:35 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
|
2009-06-08 23:11:35 -04:00
|
|
|
super.onCreateContextMenu(menu, v, menuInfo);
|
|
|
|
menu.setHeaderTitle(R.string.manage_identities_context_menu_title);
|
|
|
|
getMenuInflater().inflate(R.menu.manage_identities_context, menu);
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2012-07-12 12:29:41 -04:00
|
|
|
public boolean onContextItemSelected(android.view.MenuItem item) {
|
2009-06-08 23:11:35 -04:00
|
|
|
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
|
2011-02-06 17:09:48 -05:00
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.edit:
|
|
|
|
editItem(menuInfo.position);
|
|
|
|
break;
|
|
|
|
case R.id.up:
|
|
|
|
if (menuInfo.position > 0) {
|
2010-03-03 23:00:30 -05:00
|
|
|
Identity identity = identities.remove(menuInfo.position);
|
2011-02-06 17:09:48 -05:00
|
|
|
identities.add(menuInfo.position - 1, identity);
|
2009-06-08 23:11:35 -04:00
|
|
|
mIdentitiesChanged = true;
|
|
|
|
refreshView();
|
2011-02-06 17:09:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case R.id.down:
|
|
|
|
if (menuInfo.position < identities.size() - 1) {
|
|
|
|
Identity identity = identities.remove(menuInfo.position);
|
|
|
|
identities.add(menuInfo.position + 1, identity);
|
|
|
|
mIdentitiesChanged = true;
|
|
|
|
refreshView();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R.id.top:
|
|
|
|
Identity identity = identities.remove(menuInfo.position);
|
|
|
|
identities.add(0, identity);
|
|
|
|
mIdentitiesChanged = true;
|
|
|
|
refreshView();
|
|
|
|
break;
|
|
|
|
case R.id.remove:
|
|
|
|
if (identities.size() > 1) {
|
|
|
|
identities.remove(menuInfo.position);
|
|
|
|
mIdentitiesChanged = true;
|
|
|
|
refreshView();
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, getString(R.string.no_removable_identity),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
break;
|
2009-06-08 23:11:35 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void onResume() {
|
2009-06-08 23:11:35 -04:00
|
|
|
super.onResume();
|
2010-03-03 23:00:30 -05:00
|
|
|
//mAccount.refresh(Preferences.getPreferences(getApplication().getApplicationContext()));
|
2009-06-08 23:11:35 -04:00
|
|
|
refreshView();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2011-11-09 23:34:34 -05:00
|
|
|
public void onBackPressed() {
|
|
|
|
saveIdentities();
|
|
|
|
super.onBackPressed();
|
2009-06-08 23:11:35 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
private void saveIdentities() {
|
|
|
|
if (mIdentitiesChanged) {
|
2009-06-08 23:11:35 -04:00
|
|
|
mAccount.setIdentities(identities);
|
|
|
|
mAccount.save(Preferences.getPreferences(getApplication().getApplicationContext()));
|
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|