1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.ui.login;
20
21 import java.util.ArrayList;
22
23 import org.exoplatform.R;
24 import org.exoplatform.base.BaseActivity;
25 import org.exoplatform.base.BaseActivity.BasicActivityLifecycleCallbacks;
26 import org.exoplatform.model.ExoAccount;
27 import org.exoplatform.singleton.AccountSetting;
28 import org.exoplatform.singleton.ServerSettingHelper;
29 import org.exoplatform.ui.login.tasks.CheckAccountExistsTask;
30 import org.exoplatform.ui.login.tasks.CheckingTenantStatusTask;
31 import org.exoplatform.ui.login.tasks.LoginTask;
32 import org.exoplatform.ui.login.tasks.RequestTenantTask;
33 import org.exoplatform.utils.CrashUtils;
34 import org.exoplatform.utils.ExoConnectionUtils;
35 import org.exoplatform.utils.ExoConstants;
36 import org.exoplatform.utils.ExoUtils;
37 import org.exoplatform.utils.Log;
38 import org.exoplatform.utils.SettingUtils;
39 import org.exoplatform.widget.WaitingDialog;
40
41 import android.app.Activity;
42 import android.content.Context;
43 import android.content.res.Resources;
44 import android.os.AsyncTask.Status;
45 import android.os.Bundle;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class LoginProxy implements CheckingTenantStatusTask.AsyncTaskListener, RequestTenantTask.AsyncTaskListener,
63 LoginTask.AsyncTaskListener, CheckAccountExistsTask.AsyncTaskListener {
64
65
66 public static boolean userIsLoggedIn;
67
68
69 private String mNewUserName;
70
71 private String mNewPassword;
72
73 private String mTenant;
74
75 private String mAccountName;
76
77 private String mDomain;
78
79 private String mUpdatedDomain;
80
81 private String mEmail;
82
83 private Context mContext;
84
85 private AccountSetting mSetting;
86
87 private Resources mResource;
88
89
90 private LoginTask mLoginTask;
91
92 private RequestTenantTask mRequestTenantTask;
93
94
95 private LoginWarningDialog mWarningDialog;
96
97 private LoginWaitingDialog mProgressDialog;
98
99 private BasicActivityLifecycleCallbacks mLifecycleCallback = new BasicActivityLifecycleCallbacks() {
100
101 public void onPause(org.exoplatform.base.BaseActivity act) {
102 if (act == mContext) {
103
104
105 if (Log.LOGD)
106 Log.d(TAG, "onPause cancel task");
107 setListener(null);
108 if (mLoginTask != null && mLoginTask.getStatus() == Status.RUNNING) {
109 mLoginTask.cancel(true);
110 }
111 dismissDialog();
112 }
113 };
114 };
115
116
117 private int mLaunchMode;
118
119 private int mState = WORKING;
120
121 public static final int WITH_EXISTING_ACCOUNT = 0;
122
123 public static final int WITH_USERNAME = 10;
124
125 public static final int WITH_EMAIL = 11;
126
127 public static final int SWITCH_ACCOUNT = 12;
128
129 public static final String USERNAME = "USERNAME";
130
131 public static final String PASSWORD = "PASSWORD";
132
133 public static final String EMAIL = "EMAIL";
134
135 public static final String DOMAIN = "DOMAIN";
136
137 public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
138
139 public static final String SHOW_PROGRESS = "SHOW_PROGRESS";
140
141 private static final int WORKING = 100;
142
143 private static final int FINISHED = 101;
144
145 private static final String TAG = "eXo____LoginProxy____";
146
147
148 public LoginProxy(Context context, int state, Bundle loginData) {
149 SettingUtils.setDefaultLanguage(context);
150 mContext = context;
151 mResource = mContext.getResources();
152 mSetting = AccountSetting.getInstance();
153 mLaunchMode = state;
154 mUpdatedDomain = null;
155 if (mContext instanceof BaseActivity) {
156 ((BaseActivity) mContext).addLifeCycleObserverRef(mLifecycleCallback);
157 }
158 initStates(loginData);
159 }
160
161
162
163
164
165
166
167 private void initStates(Bundle loginData) {
168
169 mWarningDialog = new LoginWarningDialog(mContext);
170 mProgressDialog = new LoginWaitingDialog(mContext, null, mResource.getString(R.string.SigningIn));
171
172 switch (mLaunchMode) {
173
174
175
176
177
178 case WITH_EXISTING_ACCOUNT:
179 mNewUserName = loginData.getString(USERNAME);
180 mNewPassword = loginData.getString(PASSWORD);
181 mDomain = loginData.getString(DOMAIN);
182 mTenant = getTenant(mDomain);
183
184
185 mEmail = mNewUserName + "@" + mTenant + ".com";
186
187 mProgressDialog = loginData.getBoolean(SHOW_PROGRESS, true)
188 ? new LoginWaitingDialog(mContext,null,mResource.getString(R.string.SigningIn))
189 : null;
190 break;
191
192
193
194
195 case WITH_EMAIL:
196 mEmail = loginData.getString(EMAIL);
197 mNewPassword = loginData.getString(PASSWORD);
198
199 if (!checkNetworkConnection())
200 return;
201 mProgressDialog.show();
202
203
204 mRequestTenantTask = new RequestTenantTask();
205 mRequestTenantTask.setListener(this);
206 mRequestTenantTask.execute(mEmail);
207 break;
208
209
210
211
212 case WITH_USERNAME:
213 mNewUserName = loginData.getString(USERNAME);
214 mNewPassword = loginData.getString(PASSWORD);
215 mDomain = loginData.getString(DOMAIN);
216 mTenant = getTenant(mDomain);
217 mEmail = mNewUserName + "@" + mTenant + ".com";
218 break;
219
220
221
222 case SWITCH_ACCOUNT:
223 mNewUserName = loginData.getString(USERNAME);
224 mNewPassword = loginData.getString(PASSWORD);
225 mDomain = loginData.getString(DOMAIN);
226 mAccountName = loginData.getString(ACCOUNT_NAME);
227 break;
228 }
229
230 }
231
232 public LoginWarningDialog getWarningDialog() {
233 return mWarningDialog;
234 }
235
236 @Override
237 public void onRequestingTenantFinished(int result, String[] userAndTenant) {
238 Log.i(TAG, "onRequestingTenantFinished: " + result);
239 if (result != ExoConnectionUtils.TENANT_OK) {
240 finish(result);
241 return;
242 }
243
244 mNewUserName = userAndTenant[0];
245 mTenant = userAndTenant[1];
246 mDomain = ExoConnectionUtils.HTTPS + mTenant + "." + ExoConnectionUtils.EXO_CLOUD_WS_DOMAIN;
247
248 performLogin();
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 private String getTenant(String domain) {
266
267 String cloudDomain = domain.startsWith(ExoConnectionUtils.HTTPS) ? domain.substring(ExoConnectionUtils.HTTPS.length())
268 : domain.startsWith(ExoConnectionUtils.HTTP) ? domain.substring(ExoConnectionUtils.HTTP.length())
269 : domain;
270 int idx = cloudDomain.indexOf(ExoConnectionUtils.EXO_CLOUD_WS_DOMAIN);
271 if (idx <= 1)
272 return null;
273 String tenant = cloudDomain.substring(0, idx);
274 if (!tenant.endsWith("."))
275 return null;
276
277 tenant = tenant.substring(0, tenant.length() - 1);
278 return (tenant.contains(".")) ? null : tenant;
279 }
280
281
282
283
284 public void performLogin() {
285 if (mState == FINISHED)
286 return;
287
288 if (!checkNetworkConnection())
289 return;
290
291 if (mProgressDialog != null && !mProgressDialog.isShowing())
292 mProgressDialog.show();
293
294
295 if (mTenant != null) {
296 CheckingTenantStatusTask checkingTenantStatus = new CheckingTenantStatusTask();
297 checkingTenantStatus.setListener(this);
298 checkingTenantStatus.execute(mTenant, mEmail);
299 } else
300 launchLoginTask();
301 }
302
303 @Override
304 public void onCheckingTenantStatusFinished(int result) {
305 if (result != ExoConnectionUtils.TENANT_OK) {
306 finish(result);
307 return;
308 }
309
310
311 if (mTenant != null && mLaunchMode == WITH_EMAIL) {
312
313 CheckAccountExistsTask accountExists = new CheckAccountExistsTask();
314 accountExists.setListener(this);
315 accountExists.execute(mNewUserName, mTenant);
316
317 return;
318
319 }
320
321 launchLoginTask();
322
323 }
324
325 @Override
326 public void onCheckAccountExistsFinished(boolean accountExists) {
327
328 if (accountExists)
329 launchLoginTask();
330 else
331 finish(ExoConnectionUtils.SIGNIN_NO_ACCOUNT);
332
333 }
334
335 private void launchLoginTask() {
336 mDomain = !(mDomain.startsWith(ExoConnectionUtils.HTTP) || mDomain.startsWith(ExoConnectionUtils.HTTPS))
337 ? ExoConnectionUtils.HTTP + mDomain : mDomain;
338 mLoginTask = new LoginTask();
339 mLoginTask.setListener(this);
340 mLoginTask.execute(mNewUserName, mNewPassword, mDomain);
341 }
342
343 private boolean checkNetworkConnection() {
344 if (!ExoConnectionUtils.isNetworkAvailableExt(mContext)) {
345 finish(ExoConnectionUtils.SIGNIN_CONNECTION_ERR);
346 return false;
347 }
348 return true;
349 }
350
351 @Override
352 public void onLoggingInFinished(int result) {
353 try {
354 if (!((Activity) mContext).isFinishing()) {
355 finish(result);
356 } else {
357 String resultStr = result == ExoConnectionUtils.LOGIN_SUCCESS ? "success" : "failed";
358 Log.i(TAG, String.format("Login %s but activity is finishing...", resultStr));
359 }
360 } catch (ClassCastException e) {
361 Log.i(TAG, "Login proxy was not executed in the context of an activity...");
362 }
363
364 }
365
366 @Override
367 public void onUpdateDomain(String newDomain) {
368 if (newDomain != null && !newDomain.equalsIgnoreCase(mDomain)) {
369 mUpdatedDomain = newDomain;
370 }
371 }
372
373 @Override
374 public void onCanceled() {
375 dismissDialog();
376 }
377
378 private void dismissDialog() {
379 if (mProgressDialog != null && mProgressDialog.isShowing())
380 mProgressDialog.dismiss();
381 }
382
383
384
385
386
387
388 private void finish(int result) {
389 Log.i(TAG, "PROXY FINISHED - result: " + result);
390
391 userIsLoggedIn = false;
392
393 if (mState == FINISHED)
394 return;
395 mState = FINISHED;
396 dismissDialog();
397
398 switch (result) {
399 case ExoConnectionUtils.LOGIN_INCOMPATIBLE:
400 mWarningDialog.setMessage(mResource.getString(R.string.CompliantMessage)).show();
401 break;
402
403 case ExoConnectionUtils.SIGNIN_SERVER_NAV:
404 mWarningDialog.setMessage(mResource.getString(R.string.ServerNotAvailable)).show();
405 break;
406
407 case ExoConnectionUtils.SIGNIN_NO_TENANT_FOR_EMAIL:
408 mWarningDialog.setMessage(mResource.getString(R.string.NoAccountExists)).show();
409 break;
410
411 case ExoConnectionUtils.SIGNIN_NO_ACCOUNT:
412 mWarningDialog.setMessage(mResource.getString(R.string.NoAccountExists)).show();
413 break;
414
415 case ExoConnectionUtils.LOGIN_SERVER_RESUMING:
416 mWarningDialog.setMessage(mResource.getString(R.string.ServerResuming)).show();
417 break;
418
419 case ExoConnectionUtils.LOGIN_UNAUTHORIZED:
420 mWarningDialog.setMessage(mResource.getString(R.string.InvalidCredentials)).show();
421 break;
422
423 case ExoConnectionUtils.SIGNIN_CONNECTION_ERR:
424 mWarningDialog.setMessage(mResource.getString(R.string.ServerNotAvailable)).show();
425 break;
426
427 default:
428 mWarningDialog.setMessage(mResource.getString(R.string.ServerNotAvailable)).show();
429 break;
430
431
432 case ExoConnectionUtils.LOGIN_SUCCESS:
433
434
435 StringBuilder builder = new StringBuilder(mDomain).append("_").append(mNewUserName).append("_");
436
437 mSetting.socialKey = builder.toString() + ExoConstants.SETTING_SOCIAL_FILTER;
438 mSetting.socialKeyIndex = builder.toString() + ExoConstants.SETTING_SOCIAL_FILTER_INDEX;
439 mSetting.documentKey = builder.toString() + ExoConstants.SETTING_DOCUMENT_SHOW_HIDDEN_FILE;
440
441 ExoAccount newAccountObj;
442 int serverIdx;
443 if (mLaunchMode == SWITCH_ACCOUNT) {
444 newAccountObj = new ExoAccount();
445 newAccountObj.username = mNewUserName;
446 newAccountObj.password = mNewPassword;
447 newAccountObj.serverUrl = mDomain;
448 newAccountObj.accountName = mAccountName;
449 } else if (mLaunchMode == WITH_EXISTING_ACCOUNT) {
450 newAccountObj = mSetting.getCurrentAccount().clone();
451 newAccountObj.username = mNewUserName;
452 newAccountObj.password = mNewPassword;
453 } else {
454 newAccountObj = new ExoAccount();
455 String name = mTenant;
456 if (name == null)
457 name = getTenant(mDomain);
458 if (name == null)
459 name = ExoUtils.getAccountNameFromURL(mDomain, mResource.getString(R.string.DefaultServer));
460 newAccountObj.accountName = ExoUtils.capitalize(name);
461 newAccountObj.serverUrl = mDomain;
462 newAccountObj.username = mNewUserName;
463 newAccountObj.password = mNewPassword;
464 }
465 newAccountObj.lastLoginDate = System.currentTimeMillis();
466
467 ArrayList<ExoAccount> serverList = ServerSettingHelper.getInstance().getServerInfoList(mContext);
468 int duplicatedIdx = serverList.indexOf(newAccountObj);
469
470
471 if (duplicatedIdx == -1) {
472 serverList.add(newAccountObj);
473 serverIdx = serverList.size() - 1;
474 } else {
475
476
477 ExoAccount duplicatedServer = serverList.get(duplicatedIdx);
478 serverIdx = duplicatedIdx;
479
480 if (!duplicatedServer.password.equals(newAccountObj.password)) {
481 duplicatedServer.password = newAccountObj.password;
482 }
483
484 if (mUpdatedDomain != null) {
485 duplicatedServer.serverUrl = mUpdatedDomain;
486 }
487 duplicatedServer.lastLoginDate = newAccountObj.lastLoginDate;
488 }
489
490 mSetting.setCurrentAccount(serverList.get(serverIdx));
491 mSetting.setDomainIndex(String.valueOf(serverIdx));
492 userIsLoggedIn = true;
493
494
495 SettingUtils.persistServerSetting(mContext);
496
497
498 CrashUtils.setUsername(mNewUserName);
499 CrashUtils.setServerInfo(mDomain, ServerSettingHelper.getInstance().getServerVersion());
500
501 break;
502 }
503
504
505 if (mWarningDialog != null && mLaunchMode == SWITCH_ACCOUNT && result != ExoConnectionUtils.LOGIN_SUCCESS) {
506
507
508 mWarningDialog.setViewListener(new LoginWarningDialog.ViewListener() {
509 @Override
510 public void onClickOk(LoginWarningDialog dialog) {
511 if (mListener != null)
512 mListener.onLoginFinished(false);
513 }
514 });
515 } else if (mListener != null)
516
517 mListener.onLoginFinished(result == ExoConnectionUtils.LOGIN_SUCCESS);
518 }
519
520 public void onCancelLoad() {
521 if (mLoginTask != null && mLoginTask.getStatus() == LoginTask.Status.RUNNING) {
522 mLoginTask.cancel(true);
523 mLoginTask = null;
524 }
525 }
526
527
528
529
530 private class LoginWaitingDialog extends WaitingDialog {
531
532 public boolean mIsShowing = false;
533
534 public LoginWaitingDialog(Context context, String titleString, String contentString) {
535 super(context, titleString, contentString);
536 }
537
538 @Override
539 public void onBackPressed() {
540 super.onBackPressed();
541 onCancelLoad();
542 }
543
544 @Override
545 public void show() {
546 if (mIsShowing)
547 return;
548 mIsShowing = true;
549 super.show();
550 }
551 }
552
553 private ProxyListener mListener;
554
555 public void setListener(ProxyListener listener) {
556 mListener = listener;
557 }
558
559 public interface ProxyListener {
560
561
562
563
564
565 void onLoginFinished(boolean result);
566 }
567
568 }