Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.49 KB | None | 0 0
  1. private FirebaseAuth mAuth;
  2. private FirebaseAuth.AuthStateListener mAuthListener;
  3. public String email;
  4. public String password;
  5. private EditText emailedittext;
  6. private EditText passwordedittext;
  7. private TextView mStatusTextView;
  8. private TextView mDetailTextView;
  9. private static final String TAG = "EmailPassword";
  10. SharedPreferences sharedpref;
  11. private SharedPreferences.Editor loginStateEditor;
  12. ProgressDialog progressdialog;
  13. Handler handler = new Handler();
  14. int status = 0;
  15.  
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
  21. Fabric.with(this, new Twitter(authConfig));
  22. setContentView(R.layout.activity_main);
  23.  
  24. EditText emailedittext = (EditText)findViewById(R.id.emailedittext);
  25. EditText passwordedittext = (EditText)findViewById(R.id.passwordedittext);
  26. final TextView mStatusTextView = (TextView) findViewById(R.id.mStatus);
  27. TextView mDetailTextView = (TextView) findViewById(R.id.mDetail);
  28.  
  29. Button loginbutton = (Button)findViewById(R.id.login);
  30. Button signupbutton = (Button)findViewById(R.id.signup);
  31. Button signoutbutton = (Button)findViewById(R.id.signout);
  32.  
  33. findViewById(R.id.login).setOnClickListener(this);
  34. findViewById(R.id.signup).setOnClickListener(this);
  35. findViewById(R.id.signout).setOnClickListener(this);
  36.  
  37. CreateProgressDialog();
  38.  
  39.  
  40.  
  41. sharedpref = getSharedPreferences("myPref", Context.MODE_PRIVATE);
  42. loginStateEditor = sharedpref.edit();
  43.  
  44. if (sharedpref.getBoolean("success_login", false)) {
  45. // Start Your Menu Activity
  46. Intent i = new Intent(MainActivity.this, Menu.class);
  47. i.putExtra(EXTRA_MESSAGE, email);
  48. i.putExtra(EXTRA_MESSAGE, password);
  49. startActivity(i);
  50.  
  51. mAuth = FirebaseAuth.getInstance();
  52. mAuthListener = new FirebaseAuth.AuthStateListener() {
  53. @Override
  54. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  55. FirebaseUser user = firebaseAuth.getCurrentUser();
  56. if (user != null) {
  57. // User is signed in
  58. Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
  59. Intent i = new Intent(MainActivity.this, Menu.class);
  60. i.putExtra(EXTRA_MESSAGE, email);
  61. i.putExtra(EXTRA_MESSAGE, password);
  62. startActivity(i);
  63. } else {
  64. // User is signed out
  65. Log.d(TAG, "onAuthStateChanged:signed_out");
  66. }
  67. updateUI(user);
  68. }
  69.  
  70.  
  71. };}}
  72.  
  73. @SuppressLint("StringFormatInvalid")
  74. private void updateUI(FirebaseUser user) {
  75.  
  76. final TextView mStatusTextView = (TextView) findViewById(R.id.mStatus);
  77. TextView mDetailTextView = (TextView) findViewById(R.id.mDetail);
  78.  
  79. Button loginbutton = (Button)findViewById(R.id.login);
  80. Button signupbutton = (Button)findViewById(R.id.signup);
  81. Button signoutbutton = (Button)findViewById(R.id.signout);
  82.  
  83. findViewById(R.id.login).setOnClickListener(this);
  84. findViewById(R.id.signup).setOnClickListener(this);
  85. findViewById(R.id.signout).setOnClickListener(this);
  86.  
  87. hideProgressDialog();{
  88. if (user != null) {
  89. mStatusTextView.setText(getString(R.string.emailpassword_status_fmt, user.getEmail()));
  90. mDetailTextView.setText(getString(R.string.firebase_status_fmt, user.getUid()));
  91.  
  92.  
  93. this.findViewById(R.id.login).setVisibility(View.GONE);
  94. this.findViewById(R.id.signup).setVisibility(View.GONE);
  95. this.findViewById(R.id.mStatus).setVisibility(View.GONE);
  96. this.findViewById(R.id.mDetail).setVisibility(View.GONE);
  97. this.findViewById(R.id.signout).setVisibility(View.VISIBLE);
  98. } else {
  99. mStatusTextView.setText(R.string.signed_out);
  100. mDetailTextView.setText(null);
  101.  
  102.  
  103. this.findViewById(R.id.login).setVisibility(View.VISIBLE);
  104. this.findViewById(R.id.signup).setVisibility(View.VISIBLE);
  105. this.findViewById(R.id.mStatus).setVisibility(View.GONE);
  106. this.findViewById(R.id.mDetail).setVisibility(View.GONE);
  107. this.findViewById(R.id.signout).setVisibility(View.GONE);
  108. }
  109.  
  110. }
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. private void createAccount(final String email, final String password){
  119. Log.d(TAG, "createAccount:" + email);
  120. if (!validateForm()) {
  121. return;
  122. }
  123.  
  124. showProgressDialog();
  125.  
  126. mAuth.createUserWithEmailAndPassword(email, password)
  127. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  128. @Override
  129. public void onComplete(@NonNull Task<AuthResult> task) {
  130. Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
  131.  
  132. // If sign in fails, display a message to the user. If sign in succeeds
  133. // the auth state listener will be notified and logic to handle the
  134. // signed in user can be handled in the listener.
  135. if (!task.isSuccessful()) {
  136. Toast.makeText(MainActivity.this, "Authentication failed.",
  137. Toast.LENGTH_SHORT).show();
  138. loginStateEditor.putBoolean("success_login", true);
  139. loginStateEditor.commit();
  140.  
  141. Intent i = new Intent(MainActivity.this, Menu.class);
  142. i.putExtra(EXTRA_MESSAGE, email);
  143. i.putExtra(EXTRA_MESSAGE, password);
  144. startActivity(i);
  145. }
  146. hideProgressDialog();
  147.  
  148.  
  149. loginStateEditor.putBoolean("success_login", true);
  150. loginStateEditor.commit();
  151.  
  152. Intent i = new Intent(MainActivity.this, Menu.class);
  153. startActivity(i);
  154. }
  155.  
  156.  
  157. });
  158. }
  159.  
  160. private void hideProgressDialog() {
  161.  
  162. progressdialog.dismiss();
  163.  
  164. }
  165.  
  166.  
  167. private boolean validateForm() {
  168. boolean valid = true;
  169.  
  170.  
  171. String email = emailedittext.getText().toString();
  172. if (TextUtils.isEmpty(email)) {
  173. emailedittext.setError("Required.");
  174. valid = false;
  175. } else {
  176. emailedittext.setError(null);
  177. }
  178.  
  179.  
  180. String password = passwordedittext.getText().toString();
  181. if (TextUtils.isEmpty(password)) {
  182. passwordedittext.setError("Required.");
  183. valid = false;
  184. } else {
  185. passwordedittext.setError(null);
  186. }
  187. return valid;
  188. }
  189.  
  190. public void showProgressDialog() {
  191. status = 0;
  192.  
  193. new Thread(new Runnable() {
  194. @Override
  195. public void run() {
  196. while(status < 100){
  197.  
  198. status +=1;
  199.  
  200. try{
  201. Thread.sleep(200);
  202. }catch(InterruptedException e){
  203. e.printStackTrace();
  204. }
  205.  
  206. handler.post(new Runnable() {
  207. @Override
  208. public void run() {
  209.  
  210. progressdialog.setProgress(status);
  211.  
  212. if(status == 100){
  213.  
  214. progressdialog.dismiss();
  215. }
  216. }
  217. });
  218. }
  219. }
  220. }).start();
  221.  
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. @Override
  231. public void onClick(View v) {
  232.  
  233.  
  234.  
  235. EditText emailedittext = (EditText)MainActivity.this.findViewById(R.id.emailedittext);
  236. EditText passwordedittext = (EditText)MainActivity.this.findViewById(R.id.passwordedittext);
  237.  
  238. int i = v.getId();
  239. try {
  240. if (i == R.id.signup && emailedittext.getText() != null && passwordedittext.getText() != null) {
  241.  
  242. createAccount(emailedittext.getText().toString(), passwordedittext.getText().toString());
  243. String email = emailedittext.getText().toString();
  244. String password = passwordedittext.getText().toString();
  245.  
  246.  
  247. } else if (i == R.id.login && emailedittext.getText() != null && passwordedittext.getText() != null) {
  248.  
  249. signIn(emailedittext.getText().toString(), passwordedittext.getText().toString());
  250. String email = emailedittext.getText().toString();
  251. String password = passwordedittext.getText().toString();
  252.  
  253. } else if (i == R.id.signout) {
  254. signOut();
  255. }
  256. }catch(Exception e){
  257. e.printStackTrace();
  258. }
  259. }
  260.  
  261.  
  262. public void CreateProgressDialog()
  263. {
  264.  
  265. progressdialog = new ProgressDialog(MainActivity.this);
  266.  
  267. progressdialog.setIndeterminate(false);
  268.  
  269. progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  270.  
  271. progressdialog.setCancelable(true);
  272.  
  273. progressdialog.setMax(100);
  274.  
  275. progressdialog.show();
  276.  
  277. }
  278.  
  279.  
  280.  
  281.  
  282. private void signIn(final String email, final String password){
  283. Log.d(TAG, "signIn:" + email);
  284. if (!validateForm()) {
  285. return;
  286. }
  287.  
  288. showProgressDialog();
  289.  
  290. mAuth.signInWithEmailAndPassword(email, password)
  291. .addOnCompleteListener(MainActivity.this, new OnCompleteListener<com.google.firebase.auth.AuthResult>() {
  292. @Override
  293. public void onComplete(@NonNull Task<AuthResult> task) {
  294. Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
  295.  
  296. // If sign in fails, display a message to the user. If sign in succeeds
  297. // the auth state listener will be notified and logic to handle the
  298. // signed in user can be handled in the listener.
  299. if (!task.isSuccessful()) {
  300. onSuccess();
  301. {
  302. Log.w(TAG, "signInWithEmail", task.getException());
  303. Toast.makeText(MainActivity.this, "Authentication failed.",
  304. Toast.LENGTH_SHORT).show();
  305.  
  306. loginStateEditor.putBoolean("success_login", true);
  307. loginStateEditor.commit();
  308. Intent i = new Intent(MainActivity.this, Menu.class);
  309. i.putExtra(EXTRA_MESSAGE, email);
  310. i.putExtra(EXTRA_MESSAGE, password);
  311. startActivity(i);
  312. }
  313. if (!task.isSuccessful()) {
  314. mStatusTextView.setText(R.string.auth_failed);
  315. }
  316. hideProgressDialog();
  317.  
  318. }
  319.  
  320.  
  321. }
  322. });
  323.  
  324. }
  325.  
  326.  
  327. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  328.  
  329. {
  330. if (user != null) {
  331. // Name, email address, and profile photo Url
  332. String name = user.getDisplayName();
  333. String email = user.getEmail();
  334. Uri photoUrl = user.getPhotoUrl();
  335.  
  336.  
  337. // The user's ID, unique to the Firebase project. Do NOT use this value to
  338. // authenticate with your backend server, if you have one. Use
  339. // FirebaseUser.getToken() instead.
  340. String uid = user.getUid();
  341. }
  342.  
  343. }
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350. private void signOut() {
  351. mAuth.signOut();
  352. updateUI(null);
  353. }
  354.  
  355. @Override
  356. protected void onStart() {
  357. super.onStart();
  358. }
  359.  
  360. @Override
  361. protected void onStop() {
  362. super.onStop();
  363. }
  364.  
  365. public void onSuccess(){
  366. sharedpref.getBoolean("success_login", true);
  367. loginStateEditor.commit();
  368. Intent i = new Intent(MainActivity.this, Menu.class);
  369. i.putExtra(EXTRA_MESSAGE, email);
  370. i.putExtra(EXTRA_MESSAGE, password);
  371. startActivity(i);
  372. }
  373.  
  374. @Override
  375. public void onClick(View v) {
  376.  
  377.  
  378.  
  379. EditText emailedittext = (EditText)MainActivity.this.findViewById(R.id.emailedittext);
  380. EditText passwordedittext = (EditText)MainActivity.this.findViewById(R.id.passwordedittext);
  381.  
  382. int i = v.getId();
  383. try {
  384. if (i == R.id.signup && emailedittext.getText() != null && passwordedittext.getText() != null) {
  385.  
  386. createAccount(emailedittext.getText().toString(), passwordedittext.getText().toString());
  387. String email = emailedittext.getText().toString();
  388. String password = passwordedittext.getText().toString();
  389.  
  390.  
  391. } else if (i == R.id.login && emailedittext.getText() != null && passwordedittext.getText() != null) {
  392.  
  393. signIn(emailedittext.getText().toString(), passwordedittext.getText().toString());
  394. String email = emailedittext.getText().toString();
  395. String password = passwordedittext.getText().toString();
  396.  
  397. } else if (i == R.id.signout) {
  398. signOut();
  399. }
  400. }catch(Exception e){
  401. e.printStackTrace();
  402. }
  403. }
  404.  
  405. ittouch W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement