diff options
author | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-01-28 19:21:54 +0100 |
---|---|---|
committer | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-01-28 19:21:54 +0100 |
commit | 52da0fe7c9c68d4545f068fb84efbf0854c37392 (patch) | |
tree | 4c6bfae86fc90274b26e0c18ebd7793040663759 /src/de/gultsch/chat/ui | |
parent | 4f902d8210d50a586c7ece1bb7da2ab26fd18da5 (diff) |
account management
Diffstat (limited to 'src/de/gultsch/chat/ui')
-rw-r--r-- | src/de/gultsch/chat/ui/ConversationFragment.java | 22 | ||||
-rw-r--r-- | src/de/gultsch/chat/ui/EditAccount.java | 196 | ||||
-rw-r--r-- | src/de/gultsch/chat/ui/ManageAccountActivity.java | 139 | ||||
-rw-r--r-- | src/de/gultsch/chat/ui/NewConversationActivity.java | 16 |
4 files changed, 351 insertions, 22 deletions
diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java index 5ba58bbf..1ae04ce9 100644 --- a/src/de/gultsch/chat/ui/ConversationFragment.java +++ b/src/de/gultsch/chat/ui/ConversationFragment.java @@ -154,17 +154,15 @@ public class ConversationFragment extends Fragment { this.conversation = activity.getConversationList().get(activity.getSelectedConversation()); this.messageList.clear(); this.messageList.addAll(this.conversation.getMessages()); + // rendering complete. now go tell activity to close pane + if (!activity.shouldPaneBeOpen()) { + activity.getSlidingPaneLayout().closePane(); + } + + int size = this.messageList.size(); + if (size >= 1) + messagesView.setSelection(size - 1); } - - - // rendering complete. now go tell activity to close pane - if (!activity.shouldPaneBeOpen()) { - activity.getSlidingPaneLayout().closePane(); - } - - int size = this.messageList.size(); - if (size >= 1) - messagesView.setSelection(size - 1); } public void onBackendConnected() { @@ -172,5 +170,9 @@ public class ConversationFragment extends Fragment { this.conversation = activity.getConversationList().get(activity.getSelectedConversation()); this.messageList.clear(); this.messageList.addAll(this.conversation.getMessages()); + // rendering complete. now go tell activity to close pane + if (!activity.shouldPaneBeOpen()) { + activity.getSlidingPaneLayout().closePane(); + } } } diff --git a/src/de/gultsch/chat/ui/EditAccount.java b/src/de/gultsch/chat/ui/EditAccount.java new file mode 100644 index 00000000..7cb48472 --- /dev/null +++ b/src/de/gultsch/chat/ui/EditAccount.java @@ -0,0 +1,196 @@ +package de.gultsch.chat.ui; + +import de.gultsch.chat.R; +import de.gultsch.chat.entities.Account; +import de.gultsch.chat.utils.Validator; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.CompoundButton.OnCheckedChangeListener; +import android.widget.EditText; +import android.widget.RelativeLayout; +import android.widget.TextView; + +public class EditAccount extends DialogFragment { + + protected Account account; + + public void setAccount(Account account) { + this.account = account; + } + + public interface EditAccountListener { + public void onAccountEdited(Account account); + + public void onAccountDelete(Account account); + } + + protected EditAccountListener listener = null; + + public void setEditAccountListener(EditAccountListener listener) { + this.listener = listener; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + View view = inflater.inflate(R.layout.edit_account_dialog, null); + final EditText jidText = (EditText) view.findViewById(R.id.account_jid); + final EditText usernameText = (EditText) view + .findViewById(R.id.account_username); + final EditText serverText = (EditText) view + .findViewById(R.id.account_server); + final TextView usernameDesc = (TextView) view + .findViewById(R.id.textView2); + final TextView confirmPwDesc = (TextView) view.findViewById(R.id.account_confirm_password_desc); + CheckBox showAdvanced = (CheckBox) view + .findViewById(R.id.account_show_advanced); + final RelativeLayout advancedOptions = (RelativeLayout) view + .findViewById(R.id.advanced_options); + showAdvanced.setOnCheckedChangeListener(new OnCheckedChangeListener() { + + @Override + public void onCheckedChanged(CompoundButton buttonView, + boolean isChecked) { + if (isChecked) { + advancedOptions.setVisibility(View.VISIBLE); + usernameDesc.setText("Username"); + usernameText.setVisibility(View.VISIBLE); + jidText.setVisibility(View.GONE); + } else { + advancedOptions.setVisibility(View.GONE); + usernameDesc.setText("Jabber ID"); + usernameText.setVisibility(View.GONE); + jidText.setVisibility(View.VISIBLE); + } + } + }); + + final EditText password = (EditText) view + .findViewById(R.id.account_password); + final EditText passwordConfirm = (EditText) view + .findViewById(R.id.account_password_confirm2); + final CheckBox registerAccount = (CheckBox) view + .findViewById(R.id.edit_account_register_new); + + final String okButtonDesc; + + if (account != null) { + builder.setTitle("Edit account"); + registerAccount.setVisibility(View.GONE); + jidText.setText(account.getJid()); + password.setText(account.getPassword()); + usernameText.setText(account.getUsername()); + serverText.setText(account.getServer()); + okButtonDesc = "Edit"; + builder.setNegativeButton("Delete Account", new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + 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) { + if (listener!=null) { + listener.onAccountDelete(account); + } + } + }); + builder.setNegativeButton("Cancel",null); + builder.create().show(); + } + }); + } else { + builder.setTitle("Add account"); + okButtonDesc = "Add"; + } + + registerAccount + .setOnCheckedChangeListener(new OnCheckedChangeListener() { + + @Override + public void onCheckedChanged(CompoundButton buttonView, + boolean isChecked) { + AlertDialog d = (AlertDialog) getDialog(); + Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE); + if (isChecked) { + positiveButton.setText("Register"); + passwordConfirm.setVisibility(View.VISIBLE); + confirmPwDesc.setVisibility(View.VISIBLE); + } else { + passwordConfirm.setVisibility(View.GONE); + positiveButton.setText("Add"); + confirmPwDesc.setVisibility(View.GONE); + } + } + }); + + builder.setView(view); + builder.setNeutralButton("Cancel", null); + builder.setPositiveButton(okButtonDesc, null); + return builder.create(); + } + + @Override + public void onStart() { + super.onStart(); + final AlertDialog d = (AlertDialog) getDialog(); + Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE); + positiveButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean showAdvanced = ((CheckBox) d.findViewById(R.id.account_show_advanced)).isChecked(); + EditText jidEdit = (EditText) d.findViewById(R.id.account_jid); + String jid = jidEdit.getText().toString(); + EditText usernameEdit = (EditText) d.findViewById(R.id.account_username); + String username = usernameEdit.getText().toString(); + EditText serverEdit = (EditText) d.findViewById(R.id.account_server); + String server = serverEdit.getText().toString(); + EditText passwordEdit = (EditText) d.findViewById(R.id.account_password); + String password = passwordEdit.getText().toString(); + if (!showAdvanced) { + if (Validator.isValidJid(jid)) { + String[] parts = jid.split("@"); + username = parts[0]; + server = parts[1]; + } else { + jidEdit.setError("Invalid Jabber ID"); + return; + } + } else { + if (username.length()==0) { + usernameEdit.setError("username is too short"); + return; + } else if (server.length()==0) { + serverEdit.setError("server is too short"); + return; + } + } + if (account!=null) { + account.setPassword(password); + account.setUsername(username); + account.setServer(server); + } else { + account = new Account(username, server, password); + } + if (listener!=null) { + listener.onAccountEdited(account); + d.dismiss(); + } + } + }); + } +} diff --git a/src/de/gultsch/chat/ui/ManageAccountActivity.java b/src/de/gultsch/chat/ui/ManageAccountActivity.java index 741f60c3..8ba092db 100644 --- a/src/de/gultsch/chat/ui/ManageAccountActivity.java +++ b/src/de/gultsch/chat/ui/ManageAccountActivity.java @@ -1,7 +1,142 @@ package de.gultsch.chat.ui; -import android.app.Activity; +import java.util.ArrayList; +import java.util.List; -public class ManageAccountActivity extends Activity { +import de.gultsch.chat.R; +import de.gultsch.chat.entities.Account; +import de.gultsch.chat.ui.EditAccount.EditAccountListener; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.TextView; +public class ManageAccountActivity extends XmppActivity { + + + protected List<Account> accountList = new ArrayList<Account>(); + protected ListView accountListView; + protected ArrayAdapter<Account> accountListViewAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + + setContentView(R.layout.manage_accounts); + + accountListView = (ListView) findViewById(R.id.account_list); + accountListViewAdapter = new ArrayAdapter<Account>(getApplicationContext(), R.layout.account_row, this.accountList) { + @Override + public View getView(int position, View view, ViewGroup parent) { + 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(getItem(position).getJid()); + + return view; + } + }; + accountListView.setAdapter(this.accountListViewAdapter); + accountListView.setOnItemClickListener(new OnItemClickListener() { + + @Override + public void onItemClick(AdapterView<?> arg0, View view, int position, + long arg3) { + EditAccount dialog = new EditAccount(); + dialog.setAccount(accountList.get(position)); + dialog.setEditAccountListener(new EditAccountListener() { + + @Override + public void onAccountEdited(Account account) { + xmppConnectionService.updateAccount(account); + } + + @Override + public void onAccountDelete(Account account) { + + Log.d("gultsch","deleting account:"+account.getJid()); + + xmppConnectionService.deleteAccount(account); + + //dont bother finding the right account in the frontend list. just reload + accountList.clear(); + accountList.addAll(xmppConnectionService.getAccounts()); + + accountListViewAdapter.notifyDataSetChanged(); + + } + }); + dialog.show(getFragmentManager(),"edit_account"); + } + }); + } + + @Override + public void onStart() { + super.onStart(); + if (xmppConnectionServiceBound) { + Log.d("gultsch","already bound"); + this.accountList.clear(); + this.accountList.addAll(xmppConnectionService + .getAccounts()); + accountListViewAdapter.notifyDataSetChanged(); + } + } + + @Override + void onBackendConnected() { + Log.d("gultsch","called on backend connected"); + this.accountList.clear(); + this.accountList.addAll(xmppConnectionService.getAccounts()); + accountListViewAdapter.notifyDataSetChanged(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.manageaccounts, menu); + return true; + } + + @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: + EditAccount dialog = new EditAccount(); + dialog.setEditAccountListener(new EditAccountListener() { + + @Override + public void onAccountEdited(Account account) { + xmppConnectionService.createAccount(account); + accountList.add(account); + accountListViewAdapter.notifyDataSetChanged(); + } + + @Override + public void onAccountDelete(Account account) { + //this will never be called + } + }); + dialog.show(getFragmentManager(),"add_account"); + break; + default: + break; + } + return super.onOptionsItemSelected(item); + } } diff --git a/src/de/gultsch/chat/ui/NewConversationActivity.java b/src/de/gultsch/chat/ui/NewConversationActivity.java index d3a3aa00..e30fcbb8 100644 --- a/src/de/gultsch/chat/ui/NewConversationActivity.java +++ b/src/de/gultsch/chat/ui/NewConversationActivity.java @@ -2,14 +2,13 @@ package de.gultsch.chat.ui; import java.util.LinkedHashMap; import java.util.Map.Entry; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import de.gultsch.chat.R; import de.gultsch.chat.entities.Account; import de.gultsch.chat.entities.Contact; import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.persistance.DatabaseBackend; +import de.gultsch.chat.utils.Validator; import android.os.Bundle; import android.provider.ContactsContract; import android.text.Editable; @@ -36,9 +35,6 @@ public class NewConversationActivity extends XmppActivity { final protected LinkedHashMap<Contact, View> availableJabberContacts = new LinkedHashMap<Contact, View>(); protected View newContactView; protected Contact newContact; - - public static final Pattern VALID_JID = - Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE); static final String[] PROJECTION = new String[] { ContactsContract.Data.CONTACT_ID, @@ -181,8 +177,7 @@ public class NewConversationActivity extends XmppActivity { } LinearLayout createNewContact = (LinearLayout) findViewById(R.id.create_new_contact); - Matcher matcher = VALID_JID.matcher(search); - if (matcher.find()) { + if (Validator.isValidJid(search)) { createNewContact.removeAllViews(); String name = search.split("@")[0]; newContact = new Contact(name,search,DEFAULT_PROFILE_PHOTO); @@ -217,9 +212,10 @@ public class NewConversationActivity extends XmppActivity { @Override void onBackendConnected() { - - getActionBar().setDisplayHomeAsUpEnabled(false); - getActionBar().setHomeButtonEnabled(false); + if (xmppConnectionService.getConversationCount()==0) { + getActionBar().setDisplayHomeAsUpEnabled(false); + getActionBar().setHomeButtonEnabled(false); + } } @Override |