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.model;
20  
21  import android.os.Parcel;
22  import android.os.Parcelable;
23  
24  /**
25   * Describe an Account configuration, containing the URL to connect to the
26   * server and credentials. A server object information is identified by the
27   * combination: server name - server url - username
28   */
29  public class ExoAccount implements Parcelable {
30  
31      /** Describe the server; if nothing specified, use authority of url */
32      public String  accountName;
33  
34      /** url of server, contains the protocol (http:// ) */
35      public String  serverUrl;
36  
37      /** username */
38      public String  username;
39  
40      /** unencrypted password */
41      public String  password;
42  
43      public String  avatarUrl;
44  
45      public String  userFullName;
46  
47      public long    lastLoginDate;
48  
49      /**
50       * Whether remember me is enabled on this credential by default remember me
51       * is set to true
52       */
53      public boolean isRememberEnabled;
54  
55      /** Whether autologin is enabled */
56      public boolean isAutoLoginEnabled;
57  
58      public ExoAccount() {
59          accountName = "";
60          serverUrl = "";
61          username = "";
62          password = "";
63          isRememberEnabled = false;
64          isAutoLoginEnabled = false;
65          userFullName = "";
66          avatarUrl = "";
67          lastLoginDate = -1;
68      }
69  
70      private ExoAccount(Parcel in) {
71          readFromParcel(in);
72      }
73  
74      public static final Parcelable.Creator<ExoAccount> CREATOR = new Parcelable.Creator<ExoAccount>() {
75                                                                     public ExoAccount createFromParcel(Parcel in) {
76                                                                         return new ExoAccount(in);
77                                                                     }
78  
79                                                                     public ExoAccount[] newArray(int size) {
80                                                                         return new ExoAccount[size];
81                                                                     }
82                                                                 };
83  
84      private void readFromParcel(Parcel in) {
85          accountName = in.readString();
86          serverUrl = in.readString();
87          username = in.readString();
88          password = in.readString();
89          isRememberEnabled = in.readByte() == 1;
90          isAutoLoginEnabled = in.readByte() == 1;
91          userFullName = in.readString();
92          avatarUrl = in.readString();
93          lastLoginDate = in.readLong();
94      }
95  
96      @Override
97      public int describeContents() {
98          return 0;
99      }
100 
101     @Override
102     public void writeToParcel(Parcel dest, int flags) {
103         dest.writeString(accountName);
104         dest.writeString(serverUrl);
105         dest.writeString(username);
106         dest.writeString(password);
107         dest.writeByte((byte) (isRememberEnabled ? 1 : 0));
108         dest.writeByte((byte) (isAutoLoginEnabled ? 1 : 0));
109         dest.writeString(userFullName);
110         dest.writeString(avatarUrl);
111         dest.writeLong(lastLoginDate);
112     }
113 
114     /**
115      * Compares this ServerObjInfo with the one given.<br/>
116      * Returns true if server name and server URL and username are identical.
117      */
118     @Override
119     public boolean equals(Object obj) {
120         if (!(obj instanceof ExoAccount))
121             return false;
122         ExoAccount _server = (ExoAccount) obj;
123         if (_server.accountName.equals(accountName) && _server.serverUrl.equals(serverUrl)
124                 && _server.username.equals(username))
125             return true;
126         return false;
127     }
128 
129     /** clones this instance */
130     public ExoAccount clone() {
131         ExoAccount newAccount = new ExoAccount();
132         newAccount.serverUrl = serverUrl;
133         newAccount.accountName = accountName;
134         newAccount.username = username;
135         newAccount.password = password;
136         newAccount.isAutoLoginEnabled = isAutoLoginEnabled;
137         newAccount.isRememberEnabled = isRememberEnabled;
138         newAccount.userFullName = userFullName;
139         newAccount.avatarUrl = avatarUrl;
140         newAccount.lastLoginDate = lastLoginDate;
141         return newAccount;
142     }
143 
144     @Override
145     public int hashCode() {
146         return (serverUrl + username).hashCode();
147     }
148 
149     @Override
150     public String toString() {
151         StringBuilder b = new StringBuilder("Account Details:\n");
152         b.append("* Name: ").append(accountName).append("\n");
153         b.append("* URL: ").append(serverUrl).append("\n");
154         b.append("* User: ").append(username).append("\n");
155         b.append("* RM: [")
156          .append(isRememberEnabled)
157          .append("] / AL: [")
158          .append(isAutoLoginEnabled)
159          .append("]\n");
160         b.append("* Full name: ").append(userFullName).append("\n");
161         b.append("* Last login: ").append(lastLoginDate).append("\n");
162         b.append("* Avatar: ").append(avatarUrl).append("\n");
163         return b.toString();
164     }
165 }