Guest User

Untitled

a guest
Jan 5th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.06 KB | None | 0 0
  1. public class RegisterActivity extends AppCompatActivity {
  2. private static final String TAG = "RegisterActivity";
  3.  
  4. EditText nameText, emailText, passwordText;
  5. Button registerButton;
  6. TextView loginLink;
  7.  
  8. CheckBox checkBoxShowPassword;
  9.  
  10. private SessionManager session;
  11. private SQLiteHandler db;
  12. private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 0;
  13.  
  14. RegisterActivity registerActivity = RegisterActivity.this;
  15.  
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_register);
  20.  
  21. nameText = findViewById(R.id.input_name);
  22. emailText = findViewById(R.id.input_email);
  23. passwordText = findViewById(R.id.input_password);
  24. registerButton = findViewById(R.id.btn_register);
  25. loginLink = findViewById(R.id.link_login);
  26.  
  27. session = new SessionManager(getApplicationContext());
  28. db = new SQLiteHandler(getApplicationContext());
  29. if (session.isLoggedIn()) {
  30. Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
  31. startActivity(intent);
  32. finish();
  33. }
  34.  
  35. registerButton.setOnClickListener(v -> {
  36. String name = nameText.getText().toString().trim();
  37. String email = emailText.getText().toString().trim();
  38. String password = passwordText.getText().toString().trim();
  39. String phone = "";
  40.  
  41. if (ContextCompat.checkSelfPermission(registerActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
  42.  
  43. if (ActivityCompat.shouldShowRequestPermissionRationale(registerActivity, Manifest.permission.READ_CONTACTS)) {
  44. if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
  45. register(name, email, password, phone, false);
  46. } else {
  47. Toast.makeText(getApplicationContext(), R.string.please_enter_credentials, Toast.LENGTH_LONG).show();
  48. }
  49. } else {
  50. ActivityCompat.requestPermissions(registerActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
  51. }
  52. } else {
  53. if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
  54. register(name, email, password, phone, true);
  55. } else {
  56. Toast.makeText(getApplicationContext(), R.string.please_enter_credentials, Toast.LENGTH_LONG).show();
  57. }
  58. }
  59. });
  60.  
  61. loginLink.setOnClickListener(v -> {
  62. finish();
  63. });
  64.  
  65. checkBoxShowPassword = findViewById(R.id.checkBoxShowPassword);
  66. checkBoxShowPassword.setOnCheckedChangeListener((buttonView, isChecked) -> {
  67. if (!isChecked) { passwordText.setTransformationMethod(PasswordTransformationMethod.getInstance());
  68. } else { passwordText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
  69. }
  70. });
  71. }
  72.  
  73. @Override
  74. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  75.  
  76. String email = emailText.getText().toString().trim();
  77. String password = passwordText.getText().toString().trim();
  78. String name = nameText.getText().toString().trim();
  79. String phone = "";
  80.  
  81. switch (requestCode) {
  82. case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
  83. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  84. if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
  85. register(name, email, password, phone, true);
  86. } else {
  87. Toast.makeText(getApplicationContext(), R.string.please_enter_credentials, Toast.LENGTH_LONG).show();
  88. }
  89. } else {
  90. if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
  91. register(name, email, password, phone, false);
  92. } else {
  93. Toast.makeText(getApplicationContext(), R.string.please_enter_credentials, Toast.LENGTH_LONG).show();
  94. }
  95. }
  96. }
  97. }
  98. }
  99.  
  100. public void register(final String name, final String email, final String password, final String phone, Boolean isGranted) {
  101. Log.d(TAG, "Login");
  102. if (!validate()) {
  103. onRegisterFailed();
  104. return;
  105. }
  106.  
  107. registerButton.setEnabled(true);
  108. String tag_string_req = "req_login";
  109.  
  110. final ProgressDialog progressDialog = new ProgressDialog(RegisterActivity.this,
  111. R.style.AppTheme_Dark_Dialog);
  112. progressDialog.setIndeterminate(true);
  113. progressDialog.setMessage(getString(R.string.authenticating));
  114. progressDialog.show();
  115.  
  116. ApiInterface apiService =
  117. ApiClient.getClient().create(ApiInterface.class);
  118.  
  119. progressDialog.dismiss();
  120.  
  121. Call<ContactResponse> call = apiService.register(name, email, password, phone);
  122. call.enqueue(new Callback<ContactResponse>() {
  123. @Override
  124. public void onResponse(Call<ContactResponse> call, retrofit2.Response<ContactResponse> response) {
  125. if (response.body().getError()) {
  126. String message = response.body().getErrorMessage();
  127. onRegisterFailed(message);
  128. return;
  129. }
  130.  
  131. final Contact contact = response.body().getResults();
  132. if (contact != null) {
  133. session.setLogin(true);
  134.  
  135. db.addUser(contact.getUserName(), contact.getEmail(), contact.getUserId(), contact.getCreatedAt(), contact.getAbout(),
  136. contact.getThumbnailUrl(), contact.getPhone());
  137.  
  138. Intent intent = new Intent(RegisterActivity.this,
  139. MainActivity.class);
  140. startActivity(intent);
  141. finish();
  142. onRegisterSuccess();
  143.  
  144. if (isGranted) {
  145. updateContacts();
  146. }
  147. } else {
  148. onRegisterFailed();
  149. }
  150. }
  151.  
  152. @Override
  153. public void onFailure(Call<ContactResponse> call, Throwable t) {
  154. // Log error here since request failed
  155. Log.e(TAG, t.toString());
  156. onRegisterFailed();
  157. }
  158. });
  159. }
  160.  
  161. public void userExists(final String phone) {
  162. }
  163.  
  164. public void onRegisterSuccess() {
  165. registerButton.setEnabled(true);
  166. setResult(RESULT_OK, null);
  167. finish();
  168. }
  169.  
  170. public void onRegisterFailed() {
  171. Toast.makeText(getBaseContext(), R.string.registration_failed, Toast.LENGTH_LONG).show();
  172. registerButton.setEnabled(true);
  173. }
  174.  
  175. public void onRegisterFailed(String message) {
  176. Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
  177. registerButton.setEnabled(true);
  178. }
  179.  
  180. public boolean validate() {
  181. boolean valid = true;
  182.  
  183. String name = nameText.getText().toString();
  184. String email = emailText.getText().toString();
  185. String password = passwordText.getText().toString();
  186.  
  187. if (name.isEmpty() || name.length() < 3) {
  188. nameText.setError(getString(R.string.enter_3_chars));
  189. valid = false;
  190. } else {
  191. nameText.setError(null);
  192. }
  193. if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
  194. emailText.setError(getString(R.string.enter_valid_email));
  195. valid = false;
  196. } else {
  197. emailText.setError(null);
  198. }
  199. if (password.isEmpty() || password.length() < 4 || password.length() > 20) {
  200. passwordText.setError(getString(R.string.enter_4_20_char));
  201. valid = false;
  202. } else {
  203. passwordText.setError(null);
  204. }
  205.  
  206. return valid;
  207. }
  208.  
  209. private String getMyPhoneNumber() {
  210. TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  211.  
  212. String simCountry = null;
  213. if (tMgr != null) {
  214. simCountry = tMgr.getSimCountryIso().toUpperCase();
  215. }
  216. if (tMgr != null) {
  217. String simOperatorCode = tMgr.getSimOperator();
  218. }
  219. if (tMgr != null) {
  220. String simOperatorName = tMgr.getSimOperatorName();
  221. }
  222. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  223. }
  224. String simSerial = tMgr.getSimSerialNumber();
  225. String MyPhoneNumber = "0000000000";
  226. try {
  227. MyPhoneNumber = tMgr.getLine1Number();
  228. } catch (NullPointerException ex) {
  229. }
  230. if (MyPhoneNumber == null || MyPhoneNumber.equals("")) {
  231. return "";
  232. }
  233. PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
  234. Phonenumber.PhoneNumber countryNumberProto = null;
  235. try {
  236. countryNumberProto = phoneUtil.parse(MyPhoneNumber, simCountry);
  237. } catch (NumberParseException e) {
  238. System.err.println(getString(R.string.numberparseexception_thrown) + e.toString());
  239. }
  240.  
  241. return phoneUtil.format(countryNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);
  242. }
  243.  
  244. public void updateContacts() {
  245.  
  246.  
  247. String[] PROJECTION = new String[]{
  248. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  249. ContactsContract.CommonDataKinds.Phone.NUMBER
  250. };
  251.  
  252. Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);
  253.  
  254. HashMap<String, String> user = db.getUserDetails();
  255. String userId = user.get("uid");
  256.  
  257. if (phones != null) {
  258. while (phones.moveToNext()) {
  259. if (phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) != -1) {
  260. final String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  261.  
  262. ApiInterface apiService =
  263. ApiClient.getClient().create(ApiInterface.class);
  264.  
  265. Call<ContactsResponse> call = apiService.contactExists(phoneNumber, userId);
  266. call.enqueue(new Callback<ContactsResponse>() {
  267. @Override
  268. public void onResponse(Call<ContactsResponse> call, retrofit2.Response<ContactsResponse> response) {
  269. Log.e(TAG, "abc");
  270. }
  271.  
  272. @Override
  273. public void onFailure(Call<ContactsResponse> call, Throwable t) {
  274. Log.e(TAG, t.toString());
  275. }
  276. });
  277. }
  278. }
  279. }
  280. if (phones != null) {
  281. phones.close();
  282. }
  283. }
  284. }
  285.  
  286. registerButton.setOnClickListener(v -> {
  287.  
  288. RequestMultiplePermission();
  289. });
  290.  
  291. loginLink.setOnClickListener(v -> {
  292. finish();
  293. });
  294.  
  295. checkBoxShowPassword = findViewById(R.id.checkBoxShowPassword);
  296. checkBoxShowPassword.setOnCheckedChangeListener((buttonView, isChecked) -> {
  297. if (!isChecked) {
  298. passwordText.setTransformationMethod(PasswordTransformationMethod.getInstance());
  299. } else {
  300. passwordText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
  301. }
  302. });
  303. }
  304. private void RequestMultiplePermission() {
  305.  
  306. ActivityCompat.requestPermissions(registerActivity, new String[]
  307. {
  308. Manifest.permission.READ_PHONE_STATE,
  309. Manifest.permission.READ_CONTACTS
  310. }, RequestPermissionCode);
  311.  
  312. }
  313.  
  314. @Override
  315. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  316. switch (requestCode) {
  317. case RequestPermissionCode:
  318. String name = nameText.getText().toString().trim();
  319. String email = emailText.getText().toString().trim();
  320. String password = passwordText.getText().toString().trim();
  321. String phone = getMyPhoneNumber();
  322.  
  323. if (grantResults.length > 0) {
  324. boolean PhoneStatePermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  325. boolean ContactsPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
  326.  
  327. if (PhoneStatePermission && ContactsPermission) {
  328. register(name, email, password, phone, true);
  329. } else if (!PhoneStatePermission && ContactsPermission) {
  330. register(name, email, password, "", true);
  331. } else if (!ContactsPermission && PhoneStatePermission) {
  332. register(name, email, password, phone, false);
  333. } else {
  334. register(name, email, password, "", false);
  335. }
  336. }
  337. break;
  338. }
  339. }
  340.  
  341. public boolean CheckingPermissionIsEnabledOrNot() {
  342. int FirstPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE);
  343. int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS);
  344. return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
  345. SecondPermissionResult == PackageManager.PERMISSION_GRANTED;
  346. }
  347.  
  348. private String getMyPhoneNumber() {
  349. TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  350.  
  351. String simCountry = null;
  352. if (tMgr != null) {
  353. simCountry = tMgr.getSimCountryIso().toUpperCase();
  354. }
  355. if (tMgr != null) {
  356. String simOperatorCode = tMgr.getSimOperator();
  357. }
  358. if (tMgr != null) {
  359. String simOperatorName = tMgr.getSimOperatorName();
  360. }
  361. RequestMultiplePermission();
  362. ...
Add Comment
Please, Sign In to add comment