Advertisement
Guest User

RegisterActivity.java

a guest
Aug 4th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.49 KB | None | 0 0
  1. package info.happyretired.activity;
  2.  
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5.  
  6. import info.happyretired.activity.userprofile.ProfileActivity;
  7. import info.happyretired.db.MySQLiteHelper;
  8. import info.happyretired.ult.UserFunctionsUtil;
  9.  
  10.  
  11. import info.happyretired.R;
  12.  
  13. import android.app.ActionBar;
  14. import android.app.Activity;
  15. import android.content.Context;
  16. import android.content.Intent;
  17. import android.os.Bundle;
  18. import android.text.Editable;
  19. import android.text.TextUtils;
  20. import android.text.TextWatcher;
  21. import android.text.method.LinkMovementMethod;
  22. import android.util.Log;
  23. import android.view.KeyEvent;
  24. import android.view.MenuItem;
  25. import android.view.View;
  26. import android.widget.Button;
  27. import android.widget.CheckBox;
  28. import android.widget.EditText;
  29. import android.widget.LinearLayout;
  30. import android.widget.Spinner;
  31. import android.widget.TextView;
  32. import android.widget.TextView.OnEditorActionListener;
  33. import android.widget.Toast;
  34. import android.provider.Settings.Secure;
  35.  
  36. public class RegisterActivity extends Activity {
  37. Button btnRegister;
  38. Button btn_show_hobby, btn_show_health;
  39. Button btnLinkToLogin;
  40. LinearLayout show_hobby, show_health;
  41. CheckBox hobby1, hobby2, hobby3, hobby4, hobby5, hobby6, hobby7, hobby8, hobby9, hobby10, hobby11, hobby12, hobby13, hobby14, hobby15, hobby16, hobby17, hobby18, hobby19, hobby20, hobby21, hobby22, hobby23, hobby24, hobby25, hobby26, hobby27, hobby28, hobby29, hobby30, hobby31, hobby32, hobby33, hobby34, hobby35, hobby36, hobby37, hobby38, hobby39, hobby40;
  42. CheckBox health1, health2, health3, health4, health5, health6, health7, health8, health9, health10, health11, health12, health13, health14, health15, health16, health17, health18;
  43. EditText inputFullName;
  44. EditText inputDisplayName;
  45. EditText inputEmail;
  46. EditText inputPassword;
  47. EditText inputPassword2;
  48. EditText mobile;
  49. EditText recommandation_email;
  50.  
  51. //EditText recommandation_mobile;
  52. TextView registerErrorMsg;
  53. TextView link;
  54. ActionBar actionBar = null;
  55. CheckBox chkIos;
  56. private Spinner district;
  57. private Spinner age;
  58. Context c;
  59.  
  60. // JSON Response node names
  61. private static String KEY_SUCCESS = "success";
  62. private static String KEY_ERROR = "error";
  63. private static String KEY_ERROR_MSG = "error_msg";
  64. private static String KEY_UID = "uid";
  65. private static String KEY_USERNAME = "username";
  66. private static String KEY_NAME = "name";
  67. private static String KEY_EMAIL = "email";
  68. private static String KEY_CREATED_AT = "created_at";
  69.  
  70. @Override
  71. public void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. setContentView(R.layout.register);
  74.  
  75. // Importing all assets like buttons, text fields
  76. inputFullName = (EditText) findViewById(R.id.registerName);
  77. inputDisplayName = (EditText) findViewById(R.id.displayName);
  78. inputEmail = (EditText) findViewById(R.id.registerEmail);
  79. inputPassword = (EditText) findViewById(R.id.registerPassword);
  80. inputPassword2 = (EditText) findViewById(R.id.registerPassword2);
  81. mobile = (EditText) findViewById(R.id.mobile);
  82. chkIos = (CheckBox) findViewById(R.id.chkIos);
  83. recommandation_email = (EditText) findViewById(R.id.recommandation_email);
  84. //recommandation_mobile = (EditText) findViewById(R.id.recommandation_mobile);
  85. btnRegister = (Button) findViewById(R.id.btnRegister);
  86. btn_show_hobby = (Button) findViewById(R.id.btn_show_hobby);
  87. show_hobby = (LinearLayout) findViewById(R.id.show_hobby);
  88. btn_show_health = (Button) findViewById(R.id.btn_show_health);
  89. show_health = (LinearLayout) findViewById(R.id.show_health);
  90. hobby1 = (CheckBox) findViewById(R.id.hobby1);
  91. hobby2 = (CheckBox) findViewById(R.id.hobby2);
  92. hobby3 = (CheckBox) findViewById(R.id.hobby3);
  93. hobby4 = (CheckBox) findViewById(R.id.hobby4);
  94. hobby5 = (CheckBox) findViewById(R.id.hobby5);
  95. hobby6 = (CheckBox) findViewById(R.id.hobby6);
  96. hobby7 = (CheckBox) findViewById(R.id.hobby7);
  97. hobby8 = (CheckBox) findViewById(R.id.hobby8);
  98. hobby9 = (CheckBox) findViewById(R.id.hobby9);
  99. hobby10 = (CheckBox) findViewById(R.id.hobby10);
  100. hobby11 = (CheckBox) findViewById(R.id.hobby11);
  101. hobby12 = (CheckBox) findViewById(R.id.hobby12);
  102. hobby13 = (CheckBox) findViewById(R.id.hobby13);
  103. hobby14 = (CheckBox) findViewById(R.id.hobby14);
  104. hobby15 = (CheckBox) findViewById(R.id.hobby15);
  105. hobby16 = (CheckBox) findViewById(R.id.hobby16);
  106. hobby17 = (CheckBox) findViewById(R.id.hobby17);
  107. hobby18 = (CheckBox) findViewById(R.id.hobby18);
  108. hobby19 = (CheckBox) findViewById(R.id.hobby19);
  109. hobby20 = (CheckBox) findViewById(R.id.hobby20);
  110. hobby21 = (CheckBox) findViewById(R.id.hobby21);
  111. hobby22 = (CheckBox) findViewById(R.id.hobby22);
  112. hobby23 = (CheckBox) findViewById(R.id.hobby23);
  113. hobby24 = (CheckBox) findViewById(R.id.hobby24);
  114. hobby25 = (CheckBox) findViewById(R.id.hobby25);
  115. hobby26 = (CheckBox) findViewById(R.id.hobby26);
  116. hobby27 = (CheckBox) findViewById(R.id.hobby27);
  117. hobby28 = (CheckBox) findViewById(R.id.hobby28);
  118. hobby29 = (CheckBox) findViewById(R.id.hobby29);
  119. hobby30 = (CheckBox) findViewById(R.id.hobby30);
  120. hobby31 = (CheckBox) findViewById(R.id.hobby31);
  121. hobby32 = (CheckBox) findViewById(R.id.hobby32);
  122. hobby33 = (CheckBox) findViewById(R.id.hobby33);
  123. hobby34 = (CheckBox) findViewById(R.id.hobby34);
  124. hobby35 = (CheckBox) findViewById(R.id.hobby35);
  125. hobby36 = (CheckBox) findViewById(R.id.hobby36);
  126. hobby37 = (CheckBox) findViewById(R.id.hobby37);
  127. hobby38 = (CheckBox) findViewById(R.id.hobby38);
  128. hobby39 = (CheckBox) findViewById(R.id.hobby39);
  129. hobby40 = (CheckBox) findViewById(R.id.hobby40);
  130. health1 = (CheckBox) findViewById(R.id.health1);
  131. health2 = (CheckBox) findViewById(R.id.health2);
  132. health3 = (CheckBox) findViewById(R.id.health3);
  133. health4 = (CheckBox) findViewById(R.id.health4);
  134. health5 = (CheckBox) findViewById(R.id.health5);
  135. health6 = (CheckBox) findViewById(R.id.health6);
  136. health7 = (CheckBox) findViewById(R.id.health7);
  137. health8 = (CheckBox) findViewById(R.id.health8);
  138. health9 = (CheckBox) findViewById(R.id.health9);
  139. health10 = (CheckBox) findViewById(R.id.health10);
  140. health11 = (CheckBox) findViewById(R.id.health11);
  141. health12 = (CheckBox) findViewById(R.id.health12);
  142. health13 = (CheckBox) findViewById(R.id.health13);
  143. health14 = (CheckBox) findViewById(R.id.health14);
  144. health15 = (CheckBox) findViewById(R.id.health15);
  145. health16 = (CheckBox) findViewById(R.id.health16);
  146. health17 = (CheckBox) findViewById(R.id.health17);
  147. health18 = (CheckBox) findViewById(R.id.health18);
  148. //btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
  149. registerErrorMsg = (TextView) findViewById(R.id.register_error);
  150. district = (Spinner) findViewById(R.id.district);
  151. age = (Spinner) findViewById(R.id.age);
  152. link = (TextView) findViewById(R.id.register_confirm_link);
  153. c = getApplicationContext();
  154. if (link != null) {
  155. link.setMovementMethod(LinkMovementMethod.getInstance());
  156. }
  157.  
  158. actionBar = getActionBar();
  159. actionBar.setDisplayHomeAsUpEnabled(true);
  160.  
  161. /*
  162. inputFullName.addTextChangedListener(new TextWatcher(){
  163. @Override
  164. public void afterTextChanged(Editable s) {}
  165.  
  166. @Override
  167. public void beforeTextChanged(CharSequence s, int start,
  168. int count, int after) {
  169. }
  170.  
  171. @Override
  172. public void onTextChanged(CharSequence s, int start,
  173. int before, int count) {
  174. if(s.length() != 0){
  175. String text = inputFullName.getText().toString().trim();
  176. //inputFullName.setText(text);
  177.  
  178. if(TextUtils.isDigitsOnly(s)){
  179. inputEmail.setText(text+"@happy-retired.com");
  180. mobile.setText(text);
  181. }
  182. else if(inputEmail.getText().toString().endsWith("@happy-retired.com")){
  183. inputEmail.setText("");
  184. mobile.setText("");
  185. }
  186. }
  187. else{
  188. inputEmail.setText("");
  189. mobile.setText("");
  190. }
  191. }
  192. });
  193. */
  194.  
  195. // Register Button Click event
  196.  
  197. btn_show_hobby.setOnClickListener(new View.OnClickListener() {
  198. public void onClick(View view) {
  199. if (show_hobby.getVisibility()==show_hobby.VISIBLE)
  200. show_hobby.setVisibility(show_hobby.GONE);
  201. else
  202. show_hobby.setVisibility(show_hobby.VISIBLE);
  203. }
  204. });
  205.  
  206. btn_show_health.setOnClickListener(new View.OnClickListener() {
  207. public void onClick(View view) {
  208. if (show_health.getVisibility()==show_health.VISIBLE)
  209. show_health.setVisibility(show_health.GONE);
  210. else
  211. show_health.setVisibility(show_health.VISIBLE);
  212. }
  213. });
  214.  
  215. btnRegister.setOnClickListener(new View.OnClickListener() {
  216. public void onClick(View view) {
  217. String name = inputFullName.getText().toString();
  218. String displayname = inputDisplayName.getText().toString();
  219. String email = inputEmail.getText().toString();
  220. String password = inputPassword.getText().toString();
  221. String password2 = inputPassword2.getText().toString();
  222. String mobileValue = mobile.getText().toString();
  223. //String rec_mobileValue = recommandation_mobile.getText().toString();
  224. String rec_mobileValue = "";
  225. String rec_emailValue = recommandation_email.getText().toString();
  226. String districtField = String.valueOf(district.getSelectedItem());
  227. String ageField = String.valueOf(age.getSelectedItem());
  228. if (district.getSelectedItemId() == 0)
  229. districtField = "";
  230.  
  231. if (name.isEmpty()) {
  232. registerErrorMsg.setText("請輸入登入名稱/聯絡電話 ");
  233. inputFullName.requestFocus();
  234. } else if (displayname.isEmpty()) {
  235. registerErrorMsg.setText("請輸入個人姓名 ");
  236. inputDisplayName.requestFocus();
  237. } else if (name.length() < 3) {
  238. registerErrorMsg.setText("最少 3 個字 ");
  239. inputFullName.requestFocus();
  240. } else if (email.isEmpty() && mobileValue.isEmpty()) {
  241. registerErrorMsg.setText("請輸入電郵或聯絡電話其中一項");
  242. inputEmail.requestFocus();
  243. } else if (password.isEmpty()) {
  244. registerErrorMsg.setText("請輸入密碼");
  245. inputPassword.requestFocus();
  246. } else if (password2.isEmpty()) {
  247. registerErrorMsg.setText("請輸入確認密碼");
  248. inputPassword2.requestFocus();
  249. } else if (password.length() < 6) {
  250. registerErrorMsg.setText("最少 6 個字");
  251. inputPassword.requestFocus();
  252. } else if (!password.equals(password2)) {
  253. registerErrorMsg.setText("密碼與確認密碼不相同");
  254. inputPassword2.requestFocus();
  255. } else if (!chkIos.isChecked()) {
  256. registerErrorMsg.setText("請同意以上兩點");
  257. } else {
  258. UserFunctionsUtil userFunction = new UserFunctionsUtil();
  259. String device_id = Secure.getString(c.getContentResolver(),
  260. Secure.ANDROID_ID);
  261. String hobby = "" , health = "";
  262.  
  263. CheckBox[] hobbyCheckbox = new CheckBox[]{hobby1, hobby2, hobby3, hobby4, hobby5, hobby6, hobby7, hobby8, hobby9, hobby10, hobby11, hobby12, hobby13, hobby14, hobby15, hobby16, hobby17, hobby18, hobby19, hobby20, hobby21, hobby22, hobby23, hobby24, hobby25, hobby26, hobby27, hobby28, hobby29, hobby30, hobby31, hobby32, hobby33, hobby34, hobby35, hobby36, hobby37, hobby38, hobby39, hobby40};
  264. int hobbyCount = 0;
  265. for (int i = 0;i<hobbyCheckbox.length;i++) {
  266. if (hobbyCheckbox[i].isChecked()) {
  267. if (hobbyCount == 0)
  268. hobby = hobbyCheckbox[i].getText().toString();
  269. else
  270. hobby = hobby + "|*|" + hobbyCheckbox[i].getText().toString();
  271. hobbyCount++;
  272. }
  273. }
  274. int healthCount = 0;
  275. CheckBox[] healthCheckbox = new CheckBox[]{health1, health2, health3, health4, health5, health6, health7, health8, health9, health10, health11, health12, health13, health14, health15, health16, health17, health18};
  276. for (int i = 0;i<healthCheckbox.length;i++) {
  277. if (healthCheckbox[i].isChecked()) {
  278. if (healthCount == 0)
  279. health = healthCheckbox[i].getText().toString();
  280. else
  281. health = health + "|*|" + healthCheckbox[i].getText().toString();
  282. healthCount++;
  283. }
  284. }
  285. if (email.isEmpty()) {
  286. email = mobileValue + "@happy-retired.com";
  287. //inputEmail.setText(email);
  288. }
  289.  
  290. JSONObject json = userFunction.registerUser(name, displayname, email, password, districtField, ageField, mobileValue, rec_mobileValue, rec_emailValue, device_id, hobby, health);
  291.  
  292. // check for login response
  293. try {
  294. if (json.getString(KEY_SUCCESS) != null) {
  295. registerErrorMsg.setText("");
  296. String res = json.getString(KEY_SUCCESS);
  297. if (Integer.parseInt(res) == 1) {
  298. // user successfully registred
  299. // Store user details in SQLite Database
  300. MySQLiteHelper db = new MySQLiteHelper(getApplicationContext());
  301. JSONObject json_user = json.getJSONObject("user");
  302.  
  303. // Clear all previous data in database
  304. userFunction.logoutUser(getApplicationContext());
  305. db.addUser(json_user.getString(KEY_USERNAME), json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
  306. // Launch Dashboard Screen
  307. Intent dashboard = new Intent(getApplicationContext(), HomeActivity.class);
  308. // Close all views before launching Dashboard
  309. dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  310. startActivity(dashboard);
  311. Toast.makeText(getApplicationContext(), "歡迎註冊成為樂活會員!", Toast.LENGTH_LONG).show();
  312. // Close Registration Screen
  313. finish();
  314. } else {
  315. // Error in registration
  316. if (json.getString(KEY_ERROR) != null) {
  317. registerErrorMsg.setText("");
  318. res = json.getString(KEY_ERROR);
  319. if (Integer.parseInt(res) == 1) {
  320. registerErrorMsg.setText("電郵已被使用");
  321. } else if (Integer.parseInt(res) == 2) {
  322. registerErrorMsg.setText("用戶名稱已被使用 ");
  323. } else if (Integer.parseInt(res) == 3) {
  324. registerErrorMsg.setText("聯絡電話已被使用");
  325. } else if (Integer.parseInt(res) == 4) {
  326. registerErrorMsg.setText("系統找不到介紹人電郵的登記");
  327. } else if (Integer.parseInt(res) == 5) {
  328. registerErrorMsg.setText("系統找不到介紹人電話的登記");
  329. } else if (Integer.parseInt(res) == 6) {
  330. registerErrorMsg.setText("每部裝置只能做一次會員推薦");
  331. } else {
  332. registerErrorMsg.setText("請聯絡系統管理員");
  333. }
  334. }
  335.  
  336.  
  337. }
  338. }
  339. } catch (JSONException e) {
  340. e.printStackTrace();
  341. }
  342. }
  343. }
  344. });
  345.  
  346. // Link to Login Screen
  347. /*
  348. btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
  349.  
  350. public void onClick(View view) {
  351. Intent i = new Intent(getApplicationContext(),
  352. LoginActivity.class);
  353. startActivity(i);
  354. // Close Registration View
  355. finish();
  356. }
  357. });
  358. */
  359. }
  360.  
  361. @Override
  362. public boolean onOptionsItemSelected(MenuItem menuItem) {
  363. onBackPressed();
  364. return true;
  365. }
  366.  
  367. protected void onStop() {
  368. setResult(2);
  369. super.onStop();
  370. }
  371.  
  372. @Override
  373. protected void onDestroy() {
  374. setResult(2);
  375. super.onDestroy();
  376. }
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement