PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/automated/taxinow/fragments/UberSignInFragment.java

https://gitlab.com/provenlogic/wishrider_android_taxi
Java | 424 lines | 311 code | 66 blank | 47 comment | 28 complexity | 032c97db4a4875f98c5ad912a1418e81 MD5 | raw file
  1. package com.automated.taxinow.fragments;
  2. import java.util.HashMap;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.content.IntentSender.SendIntentException;
  6. import android.os.Bundle;
  7. import android.text.TextUtils;
  8. import android.util.Log;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.Button;
  13. import android.widget.ImageButton;
  14. import android.widget.Toast;
  15. import com.automated.taxinow.MainActivity;
  16. import com.automated.taxinow.MainDrawerActivity;
  17. import com.automated.taxinow.R;
  18. import com.automated.taxinow.component.MyFontButton;
  19. import com.automated.taxinow.component.MyFontEdittextView;
  20. import com.automated.taxinow.parse.HttpRequester;
  21. import com.automated.taxinow.parse.ParseContent;
  22. import com.automated.taxinow.utils.AndyUtils;
  23. import com.automated.taxinow.utils.AppLog;
  24. import com.automated.taxinow.utils.Const;
  25. import com.automated.taxinow.utils.PreferenceHelper;
  26. import com.google.android.gms.common.ConnectionResult;
  27. import com.google.android.gms.common.api.GoogleApiClient;
  28. import com.google.android.gms.common.api.Scope;
  29. import com.google.android.gms.plus.Plus;
  30. import com.google.android.gms.plus.model.people.Person;
  31. import com.sromku.simple.fb.Permission.Type;
  32. import com.sromku.simple.fb.SimpleFacebook;
  33. import com.sromku.simple.fb.entities.Profile;
  34. import com.sromku.simple.fb.listeners.OnLoginListener;
  35. import com.sromku.simple.fb.listeners.OnProfileListener;
  36. /**
  37. * @author Hardik A Bhalodi
  38. */
  39. public class UberSignInFragment extends UberBaseFragmentRegister
  40. implements
  41. com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks,
  42. com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener {
  43. private MyFontEdittextView etEmail, etPassword;
  44. private MyFontButton btnSignIn;
  45. private ImageButton btnGPlus;
  46. private ImageButton btnFb;
  47. // Gplus
  48. private ConnectionResult mConnectionResult;
  49. private GoogleApiClient mGoogleApiClient;
  50. private boolean mIntentInProgress;
  51. private static final int RC_SIGN_IN = 0;
  52. private boolean mSignInClicked;
  53. // FB
  54. private SimpleFacebook mSimpleFacebook;
  55. private Button btnForgetPassowrd;
  56. @Override
  57. public void onCreate(Bundle savedInstanceState) {
  58. // TODO Auto-generated method stub
  59. super.onCreate(savedInstanceState);
  60. Scope scope = new Scope("https://www.googleapis.com/auth/plus.login");
  61. // Scope scopePro = new
  62. // Scope("https://www.googleapis.com/auth/plus.me");
  63. mGoogleApiClient = new GoogleApiClient.Builder(activity)
  64. .addConnectionCallbacks(this)
  65. .addOnConnectionFailedListener(this).addApi(Plus.API, null)
  66. .addScope(scope).build();
  67. }
  68. @Override
  69. public void onStart() {
  70. // TODO Auto-generated method stub
  71. super.onStart();
  72. }
  73. @Override
  74. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  75. Bundle savedInstanceState) {
  76. // TODO Auto-generated method stub
  77. activity.setTitle(getResources().getString(R.string.text_signin_small));
  78. activity.setIconMenu(R.drawable.taxi);
  79. View view = inflater.inflate(R.layout.login, container, false);
  80. etEmail = (MyFontEdittextView) view.findViewById(R.id.etEmail);
  81. etEmail.setText("moorthi@provenlogic.net");
  82. etPassword = (MyFontEdittextView) view.findViewById(R.id.etPassword);
  83. etPassword.setText("1358fc2d");
  84. btnSignIn = (MyFontButton) view.findViewById(R.id.btnSignIn);
  85. btnGPlus = (ImageButton) view.findViewById(R.id.btnGplus);
  86. btnFb = (ImageButton) view.findViewById(R.id.btnFb);
  87. btnForgetPassowrd = (Button) view.findViewById(R.id.btnForgetPassword);
  88. btnForgetPassowrd.setOnClickListener(this);
  89. btnGPlus.setOnClickListener(this);
  90. btnSignIn.setOnClickListener(this);
  91. btnFb.setOnClickListener(this);
  92. btnSignIn.setOnClickListener(this);
  93. return view;
  94. }
  95. @Override
  96. public void onResume() {
  97. // TODO Auto-generated method stub
  98. super.onResume();
  99. activity.currentFragment = Const.FRAGMENT_SIGNIN;
  100. activity.actionBar.setTitle(getString(R.string.text_signin_small));
  101. mSimpleFacebook = SimpleFacebook.getInstance(activity);
  102. }
  103. @Override
  104. public void onClick(View v) {
  105. // TODO Auto-generated method stub
  106. switch (v.getId()) {
  107. case R.id.btnFb:
  108. if (!mSimpleFacebook.isLogin()) {
  109. activity.setFbTag(Const.FRAGMENT_SIGNIN);
  110. mSimpleFacebook.login(new OnLoginListener() {
  111. @Override
  112. public void onFail(String arg0) {
  113. // TODO Auto-generated method stub
  114. Toast.makeText(activity, "fb login failed",
  115. Toast.LENGTH_SHORT).show();
  116. }
  117. @Override
  118. public void onException(Throwable arg0) {
  119. // TODO Auto-generated method stub
  120. }
  121. @Override
  122. public void onThinking() {
  123. // TODO Auto-generated method stub
  124. }
  125. @Override
  126. public void onNotAcceptingPermissions(Type arg0) {
  127. // TODO Auto-generated method stub
  128. // Log.w("UBER",
  129. // String.format(
  130. // "You didn't accept %s permissions",
  131. // arg0.name()));
  132. }
  133. @Override
  134. public void onLogin() {
  135. // TODO Auto-generated method stub
  136. Toast.makeText(activity, "success", Toast.LENGTH_SHORT)
  137. .show();
  138. }
  139. });
  140. } else {
  141. getProfile();
  142. }
  143. break;
  144. case R.id.btnGplus:
  145. mSignInClicked = true;
  146. if (!mGoogleApiClient.isConnecting()) {
  147. AndyUtils.showCustomProgressDialog(activity,
  148. getString(R.string.text_getting_info), true, null);
  149. mGoogleApiClient.connect();
  150. }
  151. break;
  152. case R.id.btnSignIn:
  153. if (isValidate()) {
  154. login();
  155. }
  156. break;
  157. case R.id.btnForgetPassword:
  158. activity.addFragment(new ForgetPasswordFragment(), true,
  159. Const.FOREGETPASS_FRAGMENT_TAG);
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. private void getProfile() {
  166. AndyUtils.showCustomProgressDialog(activity,
  167. getString(R.string.text_getting_info), true, null);
  168. mSimpleFacebook.getProfile(new OnProfileListener() {
  169. @Override
  170. public void onComplete(Profile profile) {
  171. AndyUtils.removeCustomProgressDialog();
  172. Log.i("Uber", "My profile id = " + profile.getId());
  173. btnGPlus.setEnabled(false);
  174. btnFb.setEnabled(false);
  175. loginSocial(profile.getId(), Const.SOCIAL_FACEBOOK);
  176. }
  177. });
  178. }
  179. @Override
  180. public void onConnectionFailed(ConnectionResult result) {
  181. // TODO Auto-generated method stub
  182. if (!mIntentInProgress) {
  183. // Store the ConnectionResult so that we can use it later when the
  184. // user clicks
  185. // 'sign-in'.
  186. mConnectionResult = result;
  187. if (mSignInClicked) {
  188. // The user has already clicked 'sign-in' so we attempt to
  189. // resolve all
  190. // errors until the user is signed in, or they cancel.
  191. resolveSignInError();
  192. }
  193. }
  194. }
  195. @Override
  196. public void onConnected(Bundle arg0) {
  197. // TODO Auto-generated method stub
  198. AndyUtils.removeCustomProgressDialog();
  199. mSignInClicked = false;
  200. btnGPlus.setEnabled(false);
  201. String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
  202. Person currentPerson = Plus.PeopleApi
  203. .getCurrentPerson(mGoogleApiClient);
  204. String personName = currentPerson.getDisplayName();
  205. String personPhoto = currentPerson.getImage().toString();
  206. String personGooglePlusProfile = currentPerson.getUrl();
  207. loginSocial(currentPerson.getId(), Const.SOCIAL_GOOGLE);
  208. // signIn();
  209. }
  210. @Override
  211. public void onStop() {
  212. // TODO Auto-generated method stub
  213. super.onStop();
  214. if (mGoogleApiClient.isConnected()) {
  215. mGoogleApiClient.disconnect();
  216. }
  217. }
  218. private void resolveSignInError() {
  219. if (mConnectionResult.hasResolution()) {
  220. try {
  221. mIntentInProgress = true;
  222. activity.startIntentSenderForResult(mConnectionResult
  223. .getResolution().getIntentSender(), RC_SIGN_IN, null,
  224. 0, 0, 0, Const.FRAGMENT_SIGNIN);
  225. } catch (SendIntentException e) {
  226. // The intent was canceled before it was sent. Return to the
  227. // default
  228. // state and attempt to connect to get an updated
  229. // ConnectionResult.
  230. mIntentInProgress = false;
  231. mGoogleApiClient.connect();
  232. }
  233. }
  234. }
  235. @Override
  236. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  237. // TODO Auto-generated method stub
  238. if (requestCode == RC_SIGN_IN) {
  239. if (resultCode != Activity.RESULT_OK) {
  240. mSignInClicked = false;
  241. AndyUtils.removeCustomProgressDialog();
  242. }
  243. mIntentInProgress = false;
  244. if (!mGoogleApiClient.isConnecting()) {
  245. mGoogleApiClient.connect();
  246. }
  247. } else {
  248. mSimpleFacebook.onActivityResult(activity, requestCode, resultCode,
  249. data);
  250. if (mSimpleFacebook.isLogin()) {
  251. getProfile();
  252. } else {
  253. Toast.makeText(activity, "facebook login failed",
  254. Toast.LENGTH_SHORT).show();
  255. }
  256. super.onActivityResult(requestCode, resultCode, data);
  257. }
  258. }
  259. @Override
  260. public void onConnectionSuspended(int arg0) {
  261. // TODO Auto-generated method stub
  262. }
  263. @Override
  264. protected boolean isValidate() {
  265. // TODO Auto-generated method stub
  266. String msg = null;
  267. if (TextUtils.isEmpty(etEmail.getText().toString())) {
  268. msg = getResources().getString(R.string.text_enter_email);
  269. } else if (!AndyUtils.eMailValidation(etEmail.getText().toString())) {
  270. msg = getResources().getString(R.string.text_enter_valid_email);
  271. }
  272. if (TextUtils.isEmpty(etPassword.getText().toString())) {
  273. msg = getResources().getString(R.string.text_enter_password);
  274. }
  275. if (msg == null)
  276. return true;
  277. Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
  278. return false;
  279. }
  280. private void signIn() {
  281. Intent intent = new Intent(activity, MainDrawerActivity.class);
  282. startActivity(intent);
  283. activity.finish();
  284. }
  285. private void login() {
  286. if (!AndyUtils.isNetworkAvailable(activity)) {
  287. AndyUtils.showToast(getResources().getString(R.string.no_internet),
  288. activity);
  289. return;
  290. }
  291. AndyUtils.showCustomProgressDialog(activity,
  292. getResources().getString(R.string.text_signing), false, null);
  293. HashMap<String, String> map = new HashMap<String, String>();
  294. map.put(Const.URL, Const.ServiceType.LOGIN);
  295. map.put(Const.Params.EMAIL, etEmail.getText().toString());
  296. map.put(Const.Params.PASSWORD, etPassword.getText().toString());
  297. map.put(Const.Params.DEVICE_TYPE, Const.DEVICE_TYPE_ANDROID);
  298. map.put(Const.Params.DEVICE_TOKEN,
  299. new PreferenceHelper(activity).getDeviceToken());
  300. map.put(Const.Params.LOGIN_BY, Const.MANUAL);
  301. new HttpRequester(activity, map, Const.ServiceCode.LOGIN, this);
  302. }
  303. private void loginSocial(String id, String loginType) {
  304. if (!AndyUtils.isNetworkAvailable(activity)) {
  305. AndyUtils.showToast(getResources().getString(R.string.no_internet),
  306. activity);
  307. return;
  308. }
  309. AndyUtils.showCustomProgressDialog(activity,
  310. getResources().getString(R.string.text_signin), false, null);
  311. HashMap<String, String> map = new HashMap<String, String>();
  312. map.put(Const.URL, Const.ServiceType.LOGIN);
  313. map.put(Const.Params.SOCIAL_UNIQUE_ID, id);
  314. map.put(Const.Params.DEVICE_TYPE, Const.DEVICE_TYPE_ANDROID);
  315. map.put(Const.Params.DEVICE_TOKEN,
  316. new PreferenceHelper(activity).getDeviceToken());
  317. map.put(Const.Params.LOGIN_BY, loginType);
  318. new HttpRequester(activity, map, Const.ServiceCode.LOGIN, this);
  319. }
  320. @Override
  321. public void onTaskCompleted(String response, int serviceCode) {
  322. // TODO Auto-generated method stub
  323. ParseContent parseContent = new ParseContent(activity);
  324. AndyUtils.removeCustomProgressDialog();
  325. super.onTaskCompleted(response, serviceCode);
  326. switch (serviceCode) {
  327. case Const.ServiceCode.LOGIN:
  328. if (parseContent.isSuccessWithStoreId(response)) {
  329. parseContent.parseUserAndStoreToDb(response);
  330. new PreferenceHelper(activity).putPassword(etPassword.getText()
  331. .toString());
  332. signIn();
  333. } else {
  334. AndyUtils.showToast(
  335. getResources().getString(R.string.signin_failed),
  336. activity);
  337. btnFb.setEnabled(true);
  338. btnGPlus.setEnabled(true);
  339. }
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. @Override
  346. public boolean OnBackPressed() {
  347. // TODO Auto-generated method stub
  348. // activity.removeAllFragment(new UberMainFragment(), false,
  349. // Const.FRAGMENT_MAIN);
  350. activity.goToMainActivity();
  351. return false;
  352. }
  353. /*
  354. * (non-Javadoc)
  355. *
  356. * @see com.uberorg.fragments.BaseFragmentRegister#OnBackPressed()
  357. */
  358. }