Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.fragments; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 2 | |
| 3 | import java.util.HashMap; |
| 4 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 5 | import org.sflphone.R; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 6 | import org.sflphone.account.AccountDetailBasic; |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 7 | import org.sflphone.client.HomeActivity; |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 8 | import org.sflphone.service.ISipService; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 9 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 10 | import android.app.Activity; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 11 | import android.app.Fragment; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 12 | import android.content.Intent; |
| 13 | import android.os.Bundle; |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 14 | import android.os.RemoteException; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 15 | import android.text.TextUtils; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 16 | import android.view.KeyEvent; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 17 | import android.view.LayoutInflater; |
| 18 | import android.view.View; |
| 19 | import android.view.ViewGroup; |
| 20 | import android.widget.EditText; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 21 | import android.widget.TextView; |
| 22 | import android.widget.TextView.OnEditorActionListener; |
Alexandre Lision | 681d029 | 2013-11-22 12:47:34 -0500 | [diff] [blame] | 23 | import android.widget.Toast; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 24 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 25 | public class AccountCreationFragment extends Fragment { |
| 26 | |
| 27 | // Values for email and password at the time of the login attempt. |
| 28 | private String mAlias; |
| 29 | private String mHostname; |
| 30 | private String mUsername; |
| 31 | private String mPassword; |
| 32 | |
| 33 | // UI references. |
| 34 | private EditText mAliasView; |
| 35 | private EditText mHostnameView; |
| 36 | private EditText mUsernameView; |
| 37 | private EditText mPasswordView; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 38 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 39 | private Callbacks mCallbacks = sDummyCallbacks; |
| 40 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 41 | |
| 42 | @Override |
| 43 | public ISipService getService() { |
| 44 | return null; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | public interface Callbacks { |
| 49 | |
| 50 | public ISipService getService(); |
| 51 | |
| 52 | } |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 53 | |
| 54 | @Override |
| 55 | public void onCreate(Bundle savedInstanceState) { |
| 56 | super.onCreate(savedInstanceState); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 61 | View inflatedView = inflater.inflate(R.layout.frag_account_creation, parent, false); |
| 62 | |
| 63 | mAliasView = (EditText) inflatedView.findViewById(R.id.alias); |
| 64 | mHostnameView = (EditText) inflatedView.findViewById(R.id.hostname); |
| 65 | mUsernameView = (EditText) inflatedView.findViewById(R.id.username); |
| 66 | mPasswordView = (EditText) inflatedView.findViewById(R.id.password); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 67 | |
| 68 | mPasswordView.setOnEditorActionListener(new OnEditorActionListener() { |
| 69 | |
| 70 | @Override |
| 71 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 72 | // if(actionId == EditorInfo.IME_ACTION_GO || event.getAction() == KeyEvent.KEYCODE_ENTER){ |
| 73 | mAlias = mAliasView.getText().toString(); |
| 74 | mHostname = mHostnameView.getText().toString(); |
| 75 | mUsername = mUsernameView.getText().toString(); |
| 76 | mPassword = mPasswordView.getText().toString(); |
| 77 | attemptCreation(); |
| 78 | // } |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | }); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 83 | inflatedView.findViewById(R.id.create_button).setOnClickListener(new View.OnClickListener() { |
| 84 | @Override |
| 85 | public void onClick(View view) { |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 86 | mAlias = mAliasView.getText().toString(); |
| 87 | mHostname = mHostnameView.getText().toString(); |
| 88 | mUsername = mUsernameView.getText().toString(); |
| 89 | mPassword = mPasswordView.getText().toString(); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 90 | attemptCreation(); |
| 91 | } |
| 92 | }); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 93 | |
Alexandre Lision | 681d029 | 2013-11-22 12:47:34 -0500 | [diff] [blame] | 94 | inflatedView.findViewById(R.id.dev_account).setVisibility(View.GONE); // Hide this button in release apk |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 95 | inflatedView.findViewById(R.id.dev_account).setOnClickListener(new View.OnClickListener() { |
| 96 | @Override |
| 97 | public void onClick(View view) { |
| 98 | createDevAccount(); |
| 99 | } |
| 100 | |
| 101 | private void createDevAccount() { |
| 102 | mUsername = mUsernameView.getText().toString(); |
| 103 | if (TextUtils.isEmpty(mUsername)) { |
| 104 | mUsernameView.setError(getString(R.string.error_field_required)); |
| 105 | mUsernameView.requestFocus(); |
| 106 | return; |
| 107 | } else { |
| 108 | mAlias = mUsername; |
| 109 | mHostname = "192.95.9.63"; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 110 | mPassword = "sfl_u" + mUsername; |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 111 | attemptCreation(); |
| 112 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 113 | |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 114 | } |
| 115 | }); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 116 | |
| 117 | return inflatedView; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onResume() { |
| 122 | super.onResume(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onStart() { |
| 127 | super.onStart(); |
| 128 | |
| 129 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 130 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 131 | @Override |
| 132 | public void onAttach(Activity activity) { |
| 133 | super.onAttach(activity); |
| 134 | if (!(activity instanceof Callbacks)) { |
| 135 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 136 | } |
| 137 | |
| 138 | mCallbacks = (Callbacks) activity; |
| 139 | } |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Attempts to sign in or register the account specified by the login form. If there are form errors (invalid email, missing fields, etc.), the |
| 143 | * errors are presented and no actual login attempt is made. |
| 144 | */ |
| 145 | public void attemptCreation() { |
| 146 | |
| 147 | // Reset errors. |
| 148 | mAliasView.setError(null); |
| 149 | mPasswordView.setError(null); |
| 150 | |
| 151 | // Store values at the time of the login attempt. |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 152 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 153 | boolean cancel = false; |
| 154 | View focusView = null; |
| 155 | |
| 156 | // Check for a valid password. |
| 157 | if (TextUtils.isEmpty(mPassword)) { |
| 158 | mPasswordView.setError(getString(R.string.error_field_required)); |
| 159 | focusView = mPasswordView; |
| 160 | cancel = true; |
| 161 | } |
| 162 | |
| 163 | if (TextUtils.isEmpty(mUsername)) { |
| 164 | mUsernameView.setError(getString(R.string.error_field_required)); |
| 165 | focusView = mUsernameView; |
| 166 | cancel = true; |
| 167 | } |
| 168 | |
| 169 | if (TextUtils.isEmpty(mHostname)) { |
| 170 | mHostnameView.setError(getString(R.string.error_field_required)); |
| 171 | focusView = mHostnameView; |
| 172 | cancel = true; |
| 173 | } |
| 174 | |
| 175 | // Check for a valid email address. |
| 176 | if (TextUtils.isEmpty(mAlias)) { |
| 177 | mAliasView.setError(getString(R.string.error_field_required)); |
| 178 | focusView = mAliasView; |
| 179 | cancel = true; |
| 180 | } |
| 181 | |
| 182 | if (cancel) { |
| 183 | // There was an error; don't attempt login and focus the first |
| 184 | // form field with an error. |
| 185 | focusView.requestFocus(); |
| 186 | } else { |
| 187 | // Show a progress spinner, and kick off a background task to |
| 188 | // perform the user login attempt. |
| 189 | initCreation(); |
| 190 | |
| 191 | } |
| 192 | } |
| 193 | |
Alexandre Lision | f30ff85 | 2013-12-09 17:08:40 -0500 | [diff] [blame] | 194 | @SuppressWarnings("unchecked") |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 195 | private void initCreation() { |
Alexandre Lision | 681d029 | 2013-11-22 12:47:34 -0500 | [diff] [blame] | 196 | |
Alexandre Lision | 451f2a8 | 2013-11-12 12:55:55 -0500 | [diff] [blame] | 197 | try { |
| 198 | HashMap<String, String> accountDetails = (HashMap<String, String>) mCallbacks.getService().getAccountTemplate(); |
| 199 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS, mAlias); |
| 200 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME, mHostname); |
| 201 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME, mUsername); |
| 202 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, mPassword); |
Alexandre Lision | 681d029 | 2013-11-22 12:47:34 -0500 | [diff] [blame] | 203 | |
Alexandre Lision | 451f2a8 | 2013-11-12 12:55:55 -0500 | [diff] [blame] | 204 | createNewAccount(accountDetails); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 205 | |
Alexandre Lision | 451f2a8 | 2013-11-12 12:55:55 -0500 | [diff] [blame] | 206 | } catch (RemoteException e) { |
| 207 | Toast.makeText(getActivity(), "Error creating account", Toast.LENGTH_SHORT).show(); |
| 208 | e.printStackTrace(); |
| 209 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 210 | |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 211 | Intent resultIntent = new Intent(getActivity(), HomeActivity.class); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 212 | getActivity().setResult(Activity.RESULT_OK, resultIntent); |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 213 | resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); |
| 214 | startActivity(resultIntent); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 215 | getActivity().finish(); |
| 216 | |
| 217 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 218 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 219 | private void createNewAccount(HashMap<String, String> accountDetails) { |
| 220 | try { |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 221 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 222 | mCallbacks.getService().addAccount(accountDetails); |
| 223 | } catch (RemoteException e) { |
| 224 | e.printStackTrace(); |
| 225 | } |
| 226 | } |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 227 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 228 | } |