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.singleton;
20  
21  import java.util.ArrayList;
22  
23  import org.exoplatform.model.ExoAccount;
24  
25  import android.os.Parcel;
26  import android.os.Parcelable;
27  
28  /**
29   * Represents as temporary instance of SharedPref, which is used to manage all
30   * the account information Changes in account setting will be populated to
31   * SharedPref only when user logs in successfully or launching app using custom
32   * URL scheme
33   */
34  public class AccountSetting implements Parcelable {
35  
36      private static AccountSetting accountSetting = new AccountSetting();
37  
38      /**
39       * The index of checked server, in case no server selected, index = -1
40       */
41      private String                domainIndex;
42  
43      /*
44       * SETTING_SOCIAL_FILTER
45       */
46      public String                 socialKey;
47  
48      /*
49       * SETTING_SOCIAL_FILTER_INDEX
50       */
51      public String                 socialKeyIndex;
52  
53      /*
54       * SETTING_DOCUMENT_SHOW_HIDDEN_FILE
55       */
56      public String                 documentKey;
57  
58      public ArrayList<String>      cookiesList;
59  
60      /** current server */
61      private ExoAccount            mCurrentAccount;
62  
63      private AccountSetting() {
64      }
65  
66      public static AccountSetting getInstance() {
67          return accountSetting;
68      }
69  
70      public void setInstance(AccountSetting instance) {
71          accountSetting = instance;
72      }
73  
74      public ExoAccount getCurrentAccount() {
75          return mCurrentAccount;
76      }
77  
78      public void setCurrentAccount(ExoAccount acc) {
79          mCurrentAccount = acc;
80      }
81  
82      public String getUsername() {
83          return (mCurrentAccount != null) ? mCurrentAccount.username : "";
84      }
85  
86      public String getPassword() {
87          return (mCurrentAccount != null) ? mCurrentAccount.password : "";
88      }
89  
90      public String getDomainName() {
91          return (mCurrentAccount != null) ? mCurrentAccount.serverUrl : "";
92      }
93  
94      public String getServerName() {
95          return (mCurrentAccount != null) ? mCurrentAccount.accountName : "";
96      }
97  
98      public String getUserFullName() {
99          return (mCurrentAccount != null) ? mCurrentAccount.userFullName : "";
100     }
101 
102     public String getUserAvatarUrl() {
103         return (mCurrentAccount != null) ? mCurrentAccount.avatarUrl : "";
104     }
105 
106     public boolean shouldSaveProfileInfo(String newUserFullName, String newUserAvatarUrl) {
107         boolean shouldSave = false;
108         if (!getUserFullName().equalsIgnoreCase(newUserFullName)) {
109             mCurrentAccount.userFullName = newUserFullName;
110             shouldSave = true;
111         }
112         if (!getUserAvatarUrl().equalsIgnoreCase(newUserAvatarUrl)) {
113             mCurrentAccount.avatarUrl = newUserAvatarUrl;
114             shouldSave = true;
115         }
116         return shouldSave;
117     }
118 
119     /**
120      * Whether auto-login is enabled or not if no server defined then disable
121      * auto-login
122      * 
123      * @return
124      */
125     public boolean isAutoLoginEnabled() {
126         return (mCurrentAccount != null) ? mCurrentAccount.isAutoLoginEnabled : false;
127     }
128 
129     /**
130      * Whether remember-me is enabled or not if no server defined then disable
131      * remember-me
132      * 
133      * @return
134      */
135     public boolean isRememberMeEnabled() {
136         return (mCurrentAccount != null) ? mCurrentAccount.isRememberEnabled : false;
137     }
138 
139     public String getDomainIndex() {
140         return (domainIndex == null) ? "-1" : domainIndex;
141     }
142 
143     public void setDomainIndex(String index) {
144         domainIndex = index;
145     }
146 
147     /**
148      * Set all internal properties to null except domainIndex and currentAccount
149      */
150     public void clear() {
151         socialKey = null;
152         socialKeyIndex = null;
153         documentKey = null;
154         cookiesList = null;
155     }
156 
157     private AccountSetting(Parcel in) {
158         readFromParcel(in);
159     }
160 
161     public void readFromParcel(Parcel in) {
162         domainIndex = in.readString();
163         mCurrentAccount = in.readParcelable(ExoAccount.class.getClassLoader());
164         cookiesList = new ArrayList<String>();
165         in.readStringList(cookiesList);
166 
167     }
168 
169     public static final Parcelable.Creator<AccountSetting> CREATOR = new Parcelable.Creator<AccountSetting>() {
170                                                                        public AccountSetting createFromParcel(Parcel in) {
171                                                                            return new AccountSetting(in);
172                                                                        }
173 
174                                                                        public AccountSetting[] newArray(int size) {
175                                                                            return new AccountSetting[size];
176                                                                        }
177                                                                    };
178 
179     /*
180      * (non-Javadoc)
181      * @see android.os.Parcelable#describeContents()
182      */
183     @Override
184     public int describeContents() {
185         return 0;
186     }
187 
188     /*
189      * (non-Javadoc)
190      * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
191      */
192     @Override
193     public void writeToParcel(Parcel par, int flags) {
194         par.writeString(domainIndex);
195         par.writeParcelable(mCurrentAccount, flags);
196         par.writeStringList(cookiesList);
197     }
198 
199 }