View Javadoc
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.R;
22  import org.exoplatform.accountswitcher.AccountSwitcherProxy.AccountSwitcherListener;
23  import org.exoplatform.model.ExoAccount;
24  
25  import android.os.Bundle;
26  import android.support.v4.app.Fragment;
27  import android.view.LayoutInflater;
28  import android.view.View;
29  import android.view.View.OnClickListener;
30  import android.view.ViewGroup;
31  import android.widget.Button;
32  import android.widget.TextView;
33  import android.widget.Toast;
34  
35  /**
36   * Created by The eXo Platform SAS
37   * 
38   * @author Philippe Aristote paristote@exoplatform.com
39   * @since Sep 10, 2014
40   */
41  public class SignInFragment extends Fragment implements AccountSwitcherListener {
42  
43    private ExoAccount         mCurrentAccount;
44  
45    private TextView           mPassword;
46  
47    private final String       EXO_ACCOUNT     = "exo_account";
48  
49    private OnClickListener    mSignInListener = new OnClickListener() {
50                                                 @Override
51                                                 public void onClick(View v) {
52                                                   String password = mPassword.getText().toString();
53                                                   if (!password.isEmpty()) {
54                                                     mCurrentAccount.password = password;
55                                                     switchToAccount();
56                                                   } else {
57                                                     Toast.makeText(getActivity(), R.string.NoPasswordEnter, Toast.LENGTH_LONG)
58                                                          .show();
59                                                   }
60                                                 }
61                                               };
62  
63    public static final String FRAGMENT_TAG    = "account_switcher_signin_fragment";
64  
65    public static final String TAG             = "eXo____AccountSwitcherSignInFragment____";
66  
67    public SignInFragment() {
68      super();
69    }
70  
71    public SignInFragment(ExoAccount account) {
72      mCurrentAccount = account;
73    }
74  
75    @Override
76    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
77  
78      if (savedInstanceState != null && savedInstanceState.containsKey(EXO_ACCOUNT)) {
79        mCurrentAccount = (ExoAccount) savedInstanceState.getParcelable(EXO_ACCOUNT);
80      }
81  
82      View layout = inflater.inflate(R.layout.account_switcher_signin_fragment, container, false);
83  
84      // TODO replace by an action bar menu button
85      Button signInBtn = (Button) layout.findViewById(R.id.account_switcher_signin_btn);
86      signInBtn.setOnClickListener(mSignInListener);
87  
88      TextView username = (TextView) layout.findViewById(R.id.account_switcher_signin_user_edit_txt);
89      username.setText(mCurrentAccount.username);
90  
91      mPassword = (TextView) layout.findViewById(R.id.account_switcher_signin_pass_edit_txt);
92  
93      return layout;
94    }
95  
96    @Override
97    public void onSaveInstanceState(Bundle outState) {
98      if (mCurrentAccount != null)
99        outState.putParcelable(EXO_ACCOUNT, mCurrentAccount);
100     super.onSaveInstanceState(outState);
101   }
102 
103   /**
104    * Launches the switching procedure.
105    */
106   private void switchToAccount() {
107     AccountSwitcherProxy controller = new AccountSwitcherProxy(getActivity(), this, true);
108     controller.switchToAccount(mCurrentAccount);
109   }
110 
111   @Override
112   public void onSwitchAccountFinished(boolean result) {
113     AccountSwitcherActivity parentActivity = (AccountSwitcherActivity) getActivity();
114     if (result)
115       // Login successful
116       parentActivity.redirectToHomeScreenAndFinish();
117     else
118       // Login failed
119       parentActivity.redirectToLoginScreenAndFinish();
120   }
121 
122   @Override
123   public void onMissingPassword(ExoAccount account) {
124     // nothing to do, the screen should remain the same
125   }
126 
127   @Override
128   public void onAccountInvalid(ExoAccount account) {
129     //
130   }
131 }