Conversations/src/eu/siacs/conversations/ui/ManageAccountActivity.java

381 lines
13 KiB
Java
Raw Normal View History

2014-02-28 18:46:01 +01:00
package eu.siacs.conversations.ui;
2014-01-24 02:04:05 +01:00
2014-01-28 19:21:54 +01:00
import java.util.ArrayList;
import java.util.List;
2014-01-24 02:04:05 +01:00
2014-02-28 18:46:01 +01:00
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.crypto.PgpEngine.UserInputRequiredException;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.ui.EditAccount.EditAccountListener;
2014-03-07 14:24:33 +01:00
import eu.siacs.conversations.xmpp.OnTLSExceptionReceived;
import android.app.Activity;
2014-02-04 21:44:16 +01:00
import android.app.AlertDialog;
2014-01-28 19:21:54 +01:00
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
2014-01-28 19:21:54 +01:00
import android.content.Intent;
2014-02-28 00:22:56 +01:00
import android.content.IntentSender.SendIntentException;
2014-01-28 19:21:54 +01:00
import android.os.Bundle;
import android.util.Log;
2014-02-04 21:44:16 +01:00
import android.view.ActionMode;
2014-01-28 19:21:54 +01:00
import android.view.LayoutInflater;
import android.view.Menu;
2014-02-04 21:44:16 +01:00
import android.view.MenuInflater;
2014-01-28 19:21:54 +01:00
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
2014-02-04 21:44:16 +01:00
import android.widget.AdapterView.OnItemLongClickListener;
2014-01-28 19:21:54 +01:00
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
2014-01-24 02:04:05 +01:00
public class ManageAccountActivity extends XmppActivity {
2014-01-28 19:21:54 +01:00
2014-02-28 00:22:56 +01:00
public static final int REQUEST_ANNOUNCE_PGP = 0x73731;
2014-02-04 21:44:16 +01:00
protected boolean isActionMode = false;
protected ActionMode actionMode;
protected Account selectedAccountForActionMode = null;
2014-03-07 14:24:33 +01:00
protected ManageAccountActivity activity = this;
2014-02-04 21:44:16 +01:00
2014-01-28 19:21:54 +01:00
protected List<Account> accountList = new ArrayList<Account>();
protected ListView accountListView;
protected ArrayAdapter<Account> accountListViewAdapter;
protected OnAccountListChangedListener accountChanged = new OnAccountListChangedListener() {
@Override
public void onAccountListChangedListener() {
Log.d("xmppService", "ui on account list changed listener");
accountList.clear();
accountList.addAll(xmppConnectionService.getAccounts());
runOnUiThread(new Runnable() {
@Override
public void run() {
accountListViewAdapter.notifyDataSetChanged();
}
});
}
};
2014-03-07 14:24:33 +01:00
protected OnTLSExceptionReceived tlsExceptionReceived = new OnTLSExceptionReceived() {
@Override
public void onTLSExceptionReceived(final String fingerprint, final Account account) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Untrusted Certificate");
builder.setIconAttribute(android.R.attr.alertDialogIcon);
View view = (View) getLayoutInflater().inflate(R.layout.cert_warning, null);
TextView sha = (TextView) view.findViewById(R.id.sha);
TextView hint = (TextView) view.findViewById(R.id.hint);
StringBuilder humanReadableSha = new StringBuilder();
humanReadableSha.append(fingerprint);
for(int i = 2; i < 58; i += 3) {
humanReadableSha.insert(i, ":");
}
hint.setText(account.getServer()+" presented you with an unstrusted, possible self signed, certificate.\nThe SHA1 fingerprint is");
sha.setText(humanReadableSha.toString());
builder.setView(view);
//builder.setMessage(server+" presented you with an unstrusted, possible self signed, certificate. The SHA1 fingerprint is "+fingerprint+" Do not connect unless you know exactly what you are doing");
builder.setNegativeButton("Don't connect", null);
builder.setPositiveButton("Trust certificate", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
account.setSSLCertFingerprint(fingerprint);
activity.xmppConnectionService.updateAccount(account);
}
});
builder.create().show();
}
});
}
};
2014-01-28 19:21:54 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_accounts);
2014-01-28 19:21:54 +01:00
accountListView = (ListView) findViewById(R.id.account_list);
accountListViewAdapter = new ArrayAdapter<Account>(
getApplicationContext(), R.layout.account_row, this.accountList) {
2014-01-28 19:21:54 +01:00
@Override
public View getView(int position, View view, ViewGroup parent) {
Account account = getItem(position);
2014-01-28 19:21:54 +01:00
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (View) inflater.inflate(R.layout.account_row, null);
}
((TextView) view.findViewById(R.id.account_jid))
.setText(account.getJid());
TextView statusView = (TextView) view
.findViewById(R.id.account_status);
switch (account.getStatus()) {
2014-02-04 21:44:16 +01:00
case Account.STATUS_DISABLED:
statusView.setText("temporarily disabled");
statusView.setTextColor(0xFF1da9da);
break;
case Account.STATUS_ONLINE:
statusView.setText("online");
statusView.setTextColor(0xFF83b600);
break;
2014-03-06 03:57:29 +01:00
case Account.STATUS_CONNECTING:
statusView.setText("connecting\u2026");
statusView.setTextColor(0xFF1da9da);
break;
case Account.STATUS_OFFLINE:
statusView.setText("offline");
statusView.setTextColor(0xFFe92727);
break;
case Account.STATUS_UNAUTHORIZED:
statusView.setText("unauthorized");
statusView.setTextColor(0xFFe92727);
break;
case Account.STATUS_SERVER_NOT_FOUND:
statusView.setText("server not found");
statusView.setTextColor(0xFFe92727);
break;
2014-03-06 03:57:29 +01:00
case Account.STATUS_NO_INTERNET:
statusView.setText("no internet");
statusView.setTextColor(0xFFe92727);
break;
2014-03-06 15:11:56 +01:00
case Account.STATUS_SERVER_REQUIRES_TLS:
statusView.setText("server requires TLS");
statusView.setTextColor(0xFFe92727);
break;
2014-03-07 14:24:33 +01:00
case Account.STATUS_TLS_ERROR:
statusView.setText("untrusted cerficate");
statusView.setTextColor(0xFFe92727);
break;
default:
break;
}
2014-01-28 19:21:54 +01:00
return view;
}
};
final XmppActivity activity = this;
2014-01-28 19:21:54 +01:00
accountListView.setAdapter(this.accountListViewAdapter);
accountListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
2014-02-04 21:44:16 +01:00
if (!isActionMode) {
2014-03-07 14:24:33 +01:00
Account account = accountList.get(position);
if ((account.getStatus() != Account.STATUS_ONLINE)&&(account.getStatus() != Account.STATUS_CONNECTING)&&(!account.isOptionSet(Account.OPTION_DISABLED))) {
activity.xmppConnectionService.reconnectAccount(accountList.get(position));
} else if (account.getStatus() == Account.STATUS_ONLINE) {
activity.startActivity(new Intent(activity.getApplicationContext(),NewConversationActivity.class));
}
Log.d("gultsch","clicked on account "+accountList.get(position).getJid());
2014-02-04 21:44:16 +01:00
} else {
selectedAccountForActionMode = accountList.get(position);
actionMode.invalidate();
}
}
});
accountListView.setOnItemLongClickListener(new OnItemLongClickListener() {
2014-01-28 19:21:54 +01:00
2014-02-04 21:44:16 +01:00
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int position, long arg3) {
if (!isActionMode) {
accountListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
accountListView.setItemChecked(position,true);
selectedAccountForActionMode = accountList.get(position);
actionMode = activity.startActionMode((new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selectedAccountForActionMode.isOptionSet(Account.OPTION_DISABLED)) {
2014-03-07 14:24:33 +01:00
menu.findItem(R.id.mgmt_account_enable).setVisible(true);
menu.findItem(R.id.mgmt_account_disable).setVisible(false);
} else {
2014-03-07 14:24:33 +01:00
menu.findItem(R.id.mgmt_account_disable).setVisible(true);
menu.findItem(R.id.mgmt_account_enable).setVisible(false);
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.manageaccounts_context, menu);
return true;
}
@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
2014-03-07 14:24:33 +01:00
if (item.getItemId()==R.id.mgmt_account_edit) {
EditAccount dialog = new EditAccount();
dialog.setAccount(selectedAccountForActionMode);
dialog.setEditAccountListener(new EditAccountListener() {
@Override
public void onAccountEdited(Account account) {
xmppConnectionService.updateAccount(account);
actionMode.finish();
}
});
dialog.show(getFragmentManager(), "edit_account");
} else if (item.getItemId()==R.id.mgmt_account_disable) {
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
xmppConnectionService.updateAccount(selectedAccountForActionMode);
mode.finish();
2014-03-07 14:24:33 +01:00
} else if (item.getItemId()==R.id.mgmt_account_enable) {
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
xmppConnectionService.updateAccount(selectedAccountForActionMode);
mode.finish();
2014-03-07 14:24:33 +01:00
} else if (item.getItemId()==R.id.mgmt_account_delete) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Are you sure?");
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setMessage("If you delete your account your entire conversation history will be lost");
builder.setPositiveButton("Delete", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
xmppConnectionService.deleteAccount(selectedAccountForActionMode);
selectedAccountForActionMode = null;
mode.finish();
}
});
builder.setNegativeButton("Cancel",null);
builder.create().show();
2014-03-07 14:24:33 +01:00
} else if (item.getItemId()==R.id.mgmt_account_announce_pgp) {
if (activity.hasPgp()) {
mode.finish();
try {
xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
} catch (PgpEngine.UserInputRequiredException e) {
try {
startIntentSenderForResult(e.getPendingIntent().getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
} catch (SendIntentException e1) {
Log.d("gultsch","sending intent failed");
}
}
}
}
return true;
}
}));
2014-02-04 21:44:16 +01:00
return true;
} else {
return false;
}
2014-01-28 19:21:54 +01:00
}
});
}
@Override
protected void onStop() {
2014-01-28 19:21:54 +01:00
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnAccountListChangedListener();
2014-03-07 14:24:33 +01:00
xmppConnectionService.removeOnTLSExceptionReceivedListener();
2014-01-28 19:21:54 +01:00
}
super.onStop();
2014-01-28 19:21:54 +01:00
}
2014-01-28 19:21:54 +01:00
@Override
void onBackendConnected() {
xmppConnectionService.setOnAccountListChangedListener(accountChanged);
2014-03-07 14:24:33 +01:00
xmppConnectionService.setOnTLSExceptionReceivedListener(tlsExceptionReceived);
2014-01-28 19:21:54 +01:00
this.accountList.clear();
this.accountList.addAll(xmppConnectionService.getAccounts());
accountListViewAdapter.notifyDataSetChanged();
if (this.accountList.size() == 0) {
getActionBar().setDisplayHomeAsUpEnabled(false);
addAccount();
}
2014-01-28 19:21:54 +01:00
}
2014-01-28 19:21:54 +01:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manageaccounts, menu);
return true;
}
2014-01-28 19:21:54 +01:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
startActivity(new Intent(this, SettingsActivity.class));
break;
case R.id.action_add_account:
addAccount();
2014-01-28 19:21:54 +01:00
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
protected void addAccount() {
final Activity activity = this;
EditAccount dialog = new EditAccount();
dialog.setEditAccountListener(new EditAccountListener() {
@Override
public void onAccountEdited(Account account) {
xmppConnectionService.createAccount(account);
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
}
});
dialog.show(getFragmentManager(), "add_account");
}
2014-02-04 21:44:16 +01:00
@Override
public void onActionModeStarted(ActionMode mode) {
super.onActionModeStarted(mode);
this.isActionMode = true;
}
@Override
public void onActionModeFinished(ActionMode mode) {
super.onActionModeFinished(mode);
this.isActionMode = false;
accountListView.clearChoices();
accountListView.requestLayout();
accountListView.post(new Runnable() {
@Override
public void run() {
accountListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
}
});
}
2014-02-28 00:22:56 +01:00
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_ANNOUNCE_PGP) {
try {
xmppConnectionService.generatePgpAnnouncement(selectedAccountForActionMode);
} catch (UserInputRequiredException e) {
Log.d("gultsch","already came back. ignoring");
}
}
}
}
2014-01-24 02:04:05 +01:00
}