minafaw3

loginActivity dogville

Oct 15th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. package com.tech4life.dogville.View;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. import com.facebook.AccessToken;
  12. import com.facebook.AccessTokenTracker;
  13. import com.facebook.CallbackManager;
  14. import com.facebook.FacebookCallback;
  15. import com.facebook.FacebookException;
  16. import com.facebook.FacebookSdk;
  17. import com.facebook.Profile;
  18. import com.facebook.ProfileTracker;
  19. import com.facebook.appevents.AppEventsLogger;
  20. import com.facebook.login.LoginResult;
  21. import com.facebook.login.widget.LoginButton;
  22. import com.tech4life.dogville.Controller.MainControl;
  23. import com.tech4life.dogville.Controller.Utils;
  24. import com.tech4life.dogville.Interfaces.VolleyResponeCallBack;
  25. import com.tech4life.dogville.R;
  26. import com.tech4life.dogville.webservice.VolleyRequestHandler;
  27. import com.tech4life.dogville.webservice.data.LoginResponse;
  28. import com.tech4life.dogville.webservice.data.RegistrationRequest;
  29. import com.tech4life.dogville.Controller.Constants.AppConstants;
  30.  
  31. import java.util.HashMap;
  32. import java.util.Map;
  33.  
  34. /**
  35. * Created by karma on 18/05/2015.
  36. */
  37.  
  38. public class LoginActivity extends MainControl implements VolleyResponeCallBack {
  39.  
  40. // login views
  41. private EditText usernameET;
  42. private EditText passwordET;
  43. private LoginButton loginButton;
  44.  
  45. public static Handler operationHandler;
  46. private LoginResponse loginResponse;
  47. private RegistrationRequest registrationRequest = new RegistrationRequest();
  48.  
  49. // facebook login variables
  50. private AccessTokenTracker accessTokenTracker;
  51. private ProfileTracker profileTracker;
  52. private CallbackManager callbackManager;
  53.  
  54. private VolleyRequestHandler _VolleyRequestHandler;
  55.  
  56. @Override
  57. protected void onCreate(Bundle savedInstanceState) {
  58. super.onCreate(savedInstanceState);
  59.  
  60. // facebook methods
  61. FacebookSdk.sdkInitialize(getApplicationContext());
  62. callbackManager = CallbackManager.Factory.create();
  63. accessTokenTracker = new AccessTokenTracker() {
  64. @Override
  65. protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
  66. updateWithToken(currentAccessToken);
  67. }
  68. };
  69.  
  70.  
  71. setContentView(R.layout.activity_sign_in);
  72. _VolleyRequestHandler = new VolleyRequestHandler(this);
  73. initialize();
  74. setHandlerAction();
  75.  
  76. loginButton = (LoginButton) findViewById(R.id.facebook_btn);
  77. loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
  78. @Override
  79. public void onSuccess(LoginResult loginResult) {
  80. Profile profile = Profile.getCurrentProfile();
  81. if (profile != null) {
  82. // registrationRequest.setPassword(profile.getId());
  83. // registrationRequest.setUserName(profile.getName());
  84. // registrationRequest.setEmail(profile.getFirstName());
  85.  
  86. Intent intent = new Intent(LoginActivity.this, MultiFragActivity.class);
  87. startActivity(intent);
  88.  
  89. }
  90.  
  91. }
  92.  
  93. @Override
  94. public void onCancel() {
  95.  
  96. }
  97.  
  98. @Override
  99. public void onError(FacebookException e) {
  100.  
  101. }
  102. });
  103. loginButton.setText("");
  104. }
  105.  
  106. @Override
  107. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  108. super.onActivityResult(requestCode, resultCode, data);
  109. callbackManager.onActivityResult(requestCode, resultCode, data);
  110.  
  111. }
  112.  
  113. private void updateWithToken(final AccessToken currentAccessToken) {
  114. //start next activity if user is already logged in
  115. if (currentAccessToken != null) {
  116. Intent i = new Intent(LoginActivity.this, MultiFragActivity.class);
  117. Utils.hideLoader();
  118. startActivity(i);
  119. finish();
  120. }
  121. }
  122.  
  123. private void initialize() {
  124.  
  125. usernameET = (EditText) findViewById(R.id.username_et);
  126. passwordET = (EditText) findViewById(R.id.password_et);
  127.  
  128. }
  129.  
  130. private void setHandlerAction() {
  131. operationHandler = new Handler() {
  132. public void handleMessage(Message msg) {
  133. Utils.hideLoader();
  134. loginResponse = (LoginResponse) msg.obj;
  135. if (loginResponse.getIsSuccessful()) {
  136. Intent intent = new Intent(LoginActivity.this, MultiFragActivity.class);
  137. startActivity(intent);
  138. } else {
  139. Toast.makeText(getBaseContext(), loginResponse.getResultString(), Toast.LENGTH_SHORT).show();
  140. }
  141. }
  142. };
  143. }
  144.  
  145. public void go_ActionHandler(View v) {
  146.  
  147. String sUserName = usernameET.getText().toString();
  148. String sPassword = passwordET.getText().toString();
  149.  
  150. if (sUserName.isEmpty() || sPassword.isEmpty()) {
  151. Utils.showAlertDialog(this, AppConstants.ALERT, AppConstants.EMPTY_MESSAGE);
  152. } else {
  153. Utils.showLoader(LoginActivity.this);
  154. get_user_data(sUserName , sPassword);
  155. // Intent intent = new Intent(LoginActivity.this, LoginService.class);
  156. // intent.putExtra(AppConstants.USERNAME, sUserName);
  157. // intent.putExtra(AppConstants.PASSWORD, sPassword);
  158. //
  159. // startService(intent);
  160.  
  161. }
  162. }
  163.  
  164. public void forget_ActionHandler(View v) {
  165. Intent intent = new Intent(LoginActivity.this, ForgetPassword.class);
  166. startActivity(intent);
  167. }
  168.  
  169. public void signUp_ActionHandler(View v) {
  170. Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
  171. startActivity(intent);
  172.  
  173. }
  174.  
  175. public void facebook_ActionHandler(View v) {
  176.  
  177. }
  178.  
  179. @Override
  180. protected void onResume() {
  181. super.onResume();
  182. // Logs 'install' and 'app activate' App Events.
  183. AppEventsLogger.activateApp(this);
  184. }
  185.  
  186. @Override
  187. protected void onPause() {
  188. super.onPause();
  189. // Logs 'app deactivate' App Event.
  190. AppEventsLogger.deactivateApp(this);
  191. }
  192.  
  193. private void get_user_data(String user_name , String pass){
  194. Map<String , String> params = new HashMap<>();
  195. params.put(AppConstants.USERNAME ,user_name );
  196. params.put(AppConstants.PASSWORD , pass);
  197.  
  198. _VolleyRequestHandler.HandleRequests(VolleyRequestHandler.LOGIN_INT, params, this);
  199. }
  200.  
  201.  
  202. @Override
  203. public void RequestSuccess(Object response) {
  204. Utils.hideLoader();
  205. Toast.makeText(this, "response is " + response ,Toast.LENGTH_LONG).show();
  206.  
  207. }
  208.  
  209. @Override
  210. public void RequestFailed(String error) {
  211. Utils.hideLoader();
  212. Toast.makeText(this, error ,Toast.LENGTH_LONG).show();
  213.  
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment