1 /*
2 * Copyright (C) 2003-2014 eXo Platform SAS.
3 *
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free
16 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18 */
19 package org.exoplatform.accountswitcher;
20
21 import org.exoplatform.model.ExoAccount;
22 import org.exoplatform.ui.login.LoginProxy;
23 import org.exoplatform.utils.SettingUtils;
24
25 import android.content.Context;
26 import android.os.Bundle;
27 import android.util.Log;
28
29 /**
30 * Created by The eXo Platform SAS Author : Philippe Aristote
31 * paristote@exoplatform.com Sep 10, 2014
32 */
33 public class AccountSwitcherProxy implements LoginProxy.ProxyListener {
34
35 private Context mContext;
36
37 private AccountSwitcherListener mListener;
38
39 private boolean mIgnoreRememberMe = false;
40
41 public static final String TAG = "eXo____AccountSwitcherController____";
42
43 public AccountSwitcherProxy(Context c, AccountSwitcherListener l, boolean ignore) {
44 mContext = c;
45 mListener = l;
46 mIgnoreRememberMe = ignore;
47 }
48
49 /**
50 * Sign out the current account and sign in with the given account.<br/>
51 * If the password is unknown, a dialog will open to let the user type it.
52 *
53 * @param account the ExoAccount to switch to
54 * @return true if the procedure has started, false if password is missing
55 */
56 public void switchToAccount(ExoAccount account) {
57 if (account == null || account.serverUrl == null || account.serverUrl.isEmpty()) {
58 // Invalid account object
59 if (mListener != null)
60 mListener.onAccountInvalid(account);
61 } else if (account.username == null || account.username.isEmpty()
62 || account.password == null || account.password.isEmpty()
63 || (!mIgnoreRememberMe && !account.isRememberEnabled)) {
64 // Credentials are missing or remember me is off, we inform the
65 // fragment that is listening
66 if (mListener != null)
67 mListener.onMissingPassword(account);
68 } else {
69 Log.i(TAG, "Switching to account " + account.accountName);
70
71 // We have all information to sign-in to the selected account
72 Bundle params = new Bundle();
73 params.putString(LoginProxy.USERNAME, account.username);
74 params.putString(LoginProxy.PASSWORD, account.password);
75 params.putString(LoginProxy.DOMAIN, account.serverUrl);
76 params.putString(LoginProxy.ACCOUNT_NAME, account.accountName);
77 // Logout is done automatically in LoginTask.preExecute started by
78 // LoginProxy. Therefore we don't need to logout here, but we must
79 // disable Auto Login.
80 // Jul 21, 2015 should use application context here?
81 SettingUtils.disableAutoLogin(mContext);
82 LoginProxy login = new LoginProxy(mContext, LoginProxy.SWITCH_ACCOUNT, params);
83 login.setListener(this);
84 login.performLogin();
85 }
86 }
87
88 /**
89 * @inheritDoc
90 */
91 @Override
92 public void onLoginFinished(boolean result) {
93 if (mListener != null)
94 mListener.onSwitchAccountFinished(result);
95 }
96
97 /**
98 * An object that is listening to events sent by the Account Switcher Proxy.
99 *
100 * @author paristote
101 */
102 public interface AccountSwitcherListener {
103 /**
104 * Called when the switch operation is finished, successful or not.
105 *
106 * @param result true if switching is successful, false otherwise
107 */
108 public void onSwitchAccountFinished(boolean result);
109
110 /**
111 * Called when the switcher proxy detected that the password was missing
112 * for the account
113 *
114 * @param account the account whose password is missing
115 */
116 public void onMissingPassword(ExoAccount account);
117
118 /**
119 * Called when the account doesn't contain a URL.
120 *
121 * @param account the invalid ExoAccount
122 */
123 public void onAccountInvalid(ExoAccount account);
124 }
125
126 }