2014-01-24 01:04:05 +00:00
|
|
|
package de.gultsch.chat.ui;
|
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
import de.gultsch.chat.R;
|
|
|
|
import de.gultsch.chat.entities.Account;
|
|
|
|
import de.gultsch.chat.ui.EditAccount.EditAccountListener;
|
2014-01-28 22:15:30 +00:00
|
|
|
import android.app.Activity;
|
2014-02-04 20:44:16 +00:00
|
|
|
import android.app.AlertDialog;
|
2014-01-28 18:21:54 +00:00
|
|
|
import android.content.Context;
|
2014-02-04 21:26:46 +00:00
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.DialogInterface.OnClickListener;
|
2014-01-28 18:21:54 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
2014-02-04 20:44:16 +00:00
|
|
|
import android.view.ActionMode;
|
|
|
|
import android.view.ActionMode.Callback;
|
2014-01-28 18:21:54 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.Menu;
|
2014-02-04 20:44:16 +00:00
|
|
|
import android.view.MenuInflater;
|
2014-01-28 18:21:54 +00: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 20:44:16 +00:00
|
|
|
import android.widget.AdapterView.OnItemLongClickListener;
|
2014-01-28 18:21:54 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
2014-01-24 01:04:05 +00:00
|
|
|
|
2014-02-04 20:44:16 +00:00
|
|
|
public class ManageAccountActivity extends XmppActivity implements ActionMode.Callback {
|
2014-01-28 18:21:54 +00:00
|
|
|
|
2014-02-04 20:44:16 +00:00
|
|
|
protected boolean isActionMode = false;
|
|
|
|
protected ActionMode actionMode;
|
|
|
|
protected Account selectedAccountForActionMode = null;
|
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
protected List<Account> accountList = new ArrayList<Account>();
|
|
|
|
protected ListView accountListView;
|
|
|
|
protected ArrayAdapter<Account> accountListViewAdapter;
|
2014-02-04 14:09:50 +00:00
|
|
|
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() {
|
|
|
|
if (accountList.size() == 1) {
|
|
|
|
startActivity(new Intent(getApplicationContext(),
|
|
|
|
NewConversationActivity.class));
|
|
|
|
}
|
|
|
|
accountListViewAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
setContentView(R.layout.manage_accounts);
|
2014-02-04 14:09:50 +00:00
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
accountListView = (ListView) findViewById(R.id.account_list);
|
2014-02-04 14:09:50 +00:00
|
|
|
accountListViewAdapter = new ArrayAdapter<Account>(
|
|
|
|
getApplicationContext(), R.layout.account_row, this.accountList) {
|
2014-01-28 18:21:54 +00:00
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
2014-02-04 14:09:50 +00:00
|
|
|
Account account = getItem(position);
|
2014-01-28 18:21:54 +00:00
|
|
|
if (view == null) {
|
|
|
|
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
view = (View) inflater.inflate(R.layout.account_row, null);
|
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
((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 20:44:16 +00:00
|
|
|
case Account.STATUS_DISABLED:
|
|
|
|
statusView.setText("temporarily disabled");
|
|
|
|
statusView.setTextColor(0xFF1da9da);
|
|
|
|
break;
|
2014-02-04 14:09:50 +00:00
|
|
|
case Account.STATUS_ONLINE:
|
|
|
|
statusView.setText("online");
|
|
|
|
statusView.setTextColor(0xFF83b600);
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
};
|
2014-02-04 20:44:16 +00:00
|
|
|
final Activity activity = this;
|
2014-01-28 18:21:54 +00:00
|
|
|
accountListView.setAdapter(this.accountListViewAdapter);
|
|
|
|
accountListView.setOnItemClickListener(new OnItemClickListener() {
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 14:09:50 +00:00
|
|
|
public void onItemClick(AdapterView<?> arg0, View view,
|
|
|
|
int position, long arg3) {
|
2014-02-04 20:44:16 +00:00
|
|
|
if (!isActionMode) {
|
|
|
|
EditAccount dialog = new EditAccount();
|
|
|
|
dialog.setAccount(accountList.get(position));
|
|
|
|
dialog.setEditAccountListener(new EditAccountListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAccountEdited(Account account) {
|
|
|
|
xmppConnectionService.updateAccount(account);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dialog.show(getFragmentManager(), "edit_account");
|
|
|
|
} else {
|
|
|
|
selectedAccountForActionMode = accountList.get(position);
|
|
|
|
actionMode.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
accountListView.setOnItemLongClickListener(new OnItemLongClickListener() {
|
2014-01-28 18:21:54 +00:00
|
|
|
|
2014-02-04 20:44:16 +00: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((Callback) activity);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 14:09:50 +00:00
|
|
|
protected void onStop() {
|
2014-01-28 18:21:54 +00:00
|
|
|
if (xmppConnectionServiceBound) {
|
2014-02-04 14:09:50 +00:00
|
|
|
xmppConnectionService.removeOnAccountListChangedListener();
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-02-05 21:33:39 +00:00
|
|
|
super.onStop();
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
2014-02-04 14:09:50 +00:00
|
|
|
xmppConnectionService.setOnAccountListChangedListener(accountChanged);
|
2014-01-28 18:21:54 +00:00
|
|
|
this.accountList.clear();
|
|
|
|
this.accountList.addAll(xmppConnectionService.getAccounts());
|
|
|
|
accountListViewAdapter.notifyDataSetChanged();
|
2014-01-28 22:15:30 +00:00
|
|
|
if (this.accountList.size() == 0) {
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(false);
|
|
|
|
addAccount();
|
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.manageaccounts, menu);
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-04 14:09:50 +00:00
|
|
|
|
2014-01-28 18:21:54 +00: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:
|
2014-01-28 22:15:30 +00:00
|
|
|
addAccount();
|
2014-01-28 18:21:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2014-01-28 22:15:30 +00:00
|
|
|
|
|
|
|
protected void addAccount() {
|
|
|
|
final Activity activity = this;
|
|
|
|
EditAccount dialog = new EditAccount();
|
|
|
|
dialog.setEditAccountListener(new EditAccountListener() {
|
2014-02-04 14:09:50 +00:00
|
|
|
|
2014-01-28 22:15:30 +00:00
|
|
|
@Override
|
|
|
|
public void onAccountEdited(Account account) {
|
|
|
|
xmppConnectionService.createAccount(account);
|
|
|
|
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
}
|
|
|
|
});
|
2014-02-04 14:09:50 +00:00
|
|
|
dialog.show(getFragmentManager(), "add_account");
|
2014-01-28 22:15:30 +00:00
|
|
|
}
|
2014-02-04 20:44:16 +00:00
|
|
|
|
|
|
|
@Override
|
2014-02-04 21:26:46 +00:00
|
|
|
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
|
2014-02-04 20:44:16 +00:00
|
|
|
if (item.getItemId()==R.id.account_disable) {
|
|
|
|
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, true);
|
|
|
|
xmppConnectionService.updateAccount(selectedAccountForActionMode);
|
|
|
|
mode.finish();
|
|
|
|
} else if (item.getItemId()==R.id.account_enable) {
|
|
|
|
selectedAccountForActionMode.setOption(Account.OPTION_DISABLED, false);
|
|
|
|
xmppConnectionService.updateAccount(selectedAccountForActionMode);
|
|
|
|
mode.finish();
|
2014-02-04 21:26:46 +00:00
|
|
|
} else if (item.getItemId()==R.id.account_delete) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
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-02-04 20:44:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
MenuInflater inflater = mode.getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.manageaccounts_context, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
|
|
|
if (selectedAccountForActionMode.isOptionSet(Account.OPTION_DISABLED)) {
|
|
|
|
menu.findItem(R.id.account_enable).setVisible(true);
|
|
|
|
menu.findItem(R.id.account_disable).setVisible(false);
|
|
|
|
} else {
|
|
|
|
menu.findItem(R.id.account_disable).setVisible(true);
|
|
|
|
menu.findItem(R.id.account_enable).setVisible(false);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-01-24 01:04:05 +00:00
|
|
|
}
|