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.ui.login;
20  
21  import org.exoplatform.R;
22  import org.exoplatform.singleton.AccountSetting;
23  
24  import android.content.Context;
25  import android.os.Bundle;
26  import android.text.Editable;
27  import android.text.TextWatcher;
28  import android.util.AttributeSet;
29  import android.view.View;
30  import android.widget.Button;
31  import android.widget.EditText;
32  import android.widget.LinearLayout;
33  
34  /**
35   * Represents the account panel in login screen that contains 2 edit input and a
36   * login button
37   */
38  public class AccountPanel extends LinearLayout implements View.OnClickListener {
39  
40      private AccountSetting      mSetting;
41  
42      /** === Components === **/
43      private EditText            mUserEditTxt;
44  
45      private EditText            mPassEditTxt;
46  
47      private Button              mLoginBtn;
48  
49      /** === Constants === **/
50      public static final String  USERNAME = "USERNAME";
51  
52      public static final String  PASSWORD = "PASSWORD";
53  
54      private static final String TAG      = "eXo____AccountPanel____";
55  
56      public AccountPanel(Context context) {
57          super(context);
58      }
59  
60      public AccountPanel(Context context, AttributeSet attrs) {
61          super(context, attrs);
62      }
63  
64      @Override
65      protected void onFinishInflate() {
66          super.onFinishInflate();
67  
68          mSetting = AccountSetting.getInstance();
69  
70          initSubViews();
71      }
72  
73      private void initSubViews() {
74          mUserEditTxt = (EditText) findViewById(R.id.EditText_UserName);
75          mPassEditTxt = (EditText) findViewById(R.id.EditText_Password);
76          mUserEditTxt.addTextChangedListener(mOnEditCredentials);
77          mPassEditTxt.addTextChangedListener(mOnEditCredentials);
78  
79          mLoginBtn = (Button) findViewById(R.id.Button_Login);
80          mLoginBtn.setOnClickListener(this);
81      }
82  
83      /**
84       * Make this panel visible
85       */
86      public void turnOn() {
87          setVisibility(View.VISIBLE);
88  
89          if (mSetting.getCurrentAccount() != null) {
90              // if a server is selected
91              // 1) set username if remember me is enabled otherwise set nothing
92              mUserEditTxt.setText(mSetting.isRememberMeEnabled()
93                      && !mSetting.getUsername().isEmpty() ? mSetting.getUsername() : "");
94              // 2) set password if remember me is enabled otherwise set nothing
95              mPassEditTxt.setText(mSetting.isRememberMeEnabled()
96                      && !mSetting.getPassword().isEmpty() ? mSetting.getPassword() : "");
97              // 3) append the server name to the login button label
98              String to = getResources().getString(R.string.SignInButton_In);
99              String connect = getResources().getString(R.string.SignInButton);
100             String loginButtonLabel = connect + " " + to + " " + mSetting.getServerName();
101             mLoginBtn.setText(loginButtonLabel);
102         }
103 
104         changeStateOfLoginBtn();
105     }
106 
107     /**
108      * Make this panel invisible
109      */
110     public void turnOff() {
111         setVisibility(View.INVISIBLE);
112     }
113 
114     public void onSaveState(Bundle saveState) {
115         saveState.putString(USERNAME, mUserEditTxt.getText().toString());
116         saveState.putString(PASSWORD, mPassEditTxt.getText().toString());
117     }
118 
119     public void onRestoreState(Bundle saveState) {
120         mUserEditTxt.setText(saveState.getString(USERNAME));
121         mPassEditTxt.setText(saveState.getString(PASSWORD));
122     }
123 
124     public void onChangeLanguage() {
125         mUserEditTxt.setHint(getResources().getString(R.string.UserNameCellTitle));
126         mPassEditTxt.setHint(getResources().getString(R.string.PasswordCellTitle));
127         mLoginBtn.setText(getResources().getString(R.string.SignInButton));
128     }
129 
130     /**
131      * Listens for login click Forward click event to controller
132      * 
133      * @param view
134      */
135     @Override
136     public void onClick(View view) {
137 
138         if (view.equals(mLoginBtn)) {
139             mViewListener.onClickLogin(mUserEditTxt.getText().toString(), mPassEditTxt.getText()
140                                                                                       .toString());
141         }
142 
143     }
144 
145     private void changeStateOfLoginBtn() {
146         boolean isCredentialsEntered = !mUserEditTxt.getText().toString().isEmpty()
147                 && !mPassEditTxt.getText().toString().isEmpty();
148         mLoginBtn.setEnabled(isCredentialsEntered);
149     }
150 
151     private TextWatcher  mOnEditCredentials = new TextWatcher() {
152                                                 public void beforeTextChanged(CharSequence s,
153                                                                               int start,
154                                                                               int count,
155                                                                               int after) {
156                                                 }
157 
158                                                 public void afterTextChanged(Editable s) {
159                                                 }
160 
161                                                 @Override
162                                                 public void onTextChanged(CharSequence s,
163                                                                           int start,
164                                                                           int before,
165                                                                           int count) {
166                                                     changeStateOfLoginBtn();
167                                                 }
168 
169                                             };
170 
171     private ViewListener mViewListener;
172 
173     /* interface to listen to view event */
174     public interface ViewListener {
175 
176         void onClickLogin(String username, String password);
177     }
178 
179     public void setViewListener(ViewListener l) {
180         mViewListener = l;
181     }
182 }