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;
|
2009-12-09 22:16:42 -05:00
|
|
|
import android.view.*;
|
2009-06-08 23:11:35 -04:00
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
|
|
|
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;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.Account;
|
|
|
|
import com.fsck.k9.Preferences;
|
|
|
|
import com.fsck.k9.R;
|
2009-06-08 23:11:35 -04:00
|
|
|
|
2009-11-24 19:40:29 -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;
|
2009-11-24 19:40:29 -05:00
|
|
|
protected void setupClickListeners()
|
|
|
|
{
|
|
|
|
this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
|
|
|
|
{
|
|
|
|
public void onItemClick(AdapterView adapterview, View view, int i, long l)
|
|
|
|
{
|
2009-06-08 23:11:35 -04:00
|
|
|
editItem(i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ListView listView = getListView();
|
|
|
|
registerForContextMenu(listView);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
private void editItem(int i)
|
|
|
|
{
|
2009-06-08 23:11:35 -04:00
|
|
|
Intent intent = new Intent(ManageIdentities.this, EditIdentity.class);
|
|
|
|
|
|
|
|
intent.putExtra(EditIdentity.EXTRA_ACCOUNT, mAccount);
|
|
|
|
intent.putExtra(EditIdentity.EXTRA_IDENTITY, mAccount.getIdentity(i));
|
|
|
|
intent.putExtra(EditIdentity.EXTRA_IDENTITY_INDEX, i);
|
|
|
|
startActivityForResult(intent, ACTIVITY_EDIT_IDENTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
|
|
{
|
2009-06-08 23:11:35 -04:00
|
|
|
super.onCreateOptionsMenu(menu);
|
|
|
|
getMenuInflater().inflate(R.menu.manage_identities_option, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -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);
|
|
|
|
startActivityForResult(intent, ACTIVITY_EDIT_IDENTITY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
2009-06-08 23:11:35 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -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);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean onContextItemSelected(MenuItem item)
|
|
|
|
{
|
2009-06-08 23:11:35 -04:00
|
|
|
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
|
2009-11-24 19:40:29 -05:00
|
|
|
switch (item.getItemId())
|
|
|
|
{
|
|
|
|
case R.id.edit:
|
|
|
|
editItem(menuInfo.position);
|
|
|
|
break;
|
|
|
|
case R.id.up:
|
|
|
|
if (menuInfo.position > 0)
|
|
|
|
{
|
|
|
|
Account.Identity identity = identities.remove(menuInfo.position);
|
|
|
|
identities.add(menuInfo.position - 1, identity);
|
|
|
|
mIdentitiesChanged = true;
|
|
|
|
refreshView();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case R.id.down:
|
|
|
|
if (menuInfo.position < identities.size() - 1)
|
|
|
|
{
|
|
|
|
Account.Identity identity = identities.remove(menuInfo.position);
|
|
|
|
identities.add(menuInfo.position + 1, identity);
|
|
|
|
mIdentitiesChanged = true;
|
|
|
|
refreshView();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case R.id.top:
|
2009-06-08 23:11:35 -04:00
|
|
|
Account.Identity identity = identities.remove(menuInfo.position);
|
2009-11-24 19:40:29 -05:00
|
|
|
identities.add(0, identity);
|
2009-06-08 23:11:35 -04:00
|
|
|
mIdentitiesChanged = true;
|
|
|
|
refreshView();
|
2009-11-24 19:40:29 -05:00
|
|
|
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
|
2009-11-24 19:40:29 -05:00
|
|
|
public void onResume()
|
|
|
|
{
|
2009-06-08 23:11:35 -04:00
|
|
|
super.onResume();
|
|
|
|
mAccount.refresh(Preferences.getPreferences(getApplication().getApplicationContext()));
|
|
|
|
refreshView();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event)
|
|
|
|
{
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK)
|
|
|
|
{
|
2009-06-08 23:11:35 -04:00
|
|
|
saveIdentities();
|
|
|
|
}
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -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();
|
|
|
|
}
|
|
|
|
}
|