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