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 greendroid.widget.AsyncImageView;
22  
23  import java.util.ArrayList;
24  import java.util.Locale;
25  
26  import org.exoplatform.R;
27  import org.exoplatform.model.ExoAccount;
28  import org.exoplatform.singleton.AccountSetting;
29  import org.exoplatform.singleton.ServerSettingHelper;
30  import org.exoplatform.utils.SocialActivityUtil;
31  
32  import android.content.Context;
33  import android.view.LayoutInflater;
34  import android.view.View;
35  import android.view.ViewGroup;
36  import android.widget.BaseAdapter;
37  import android.widget.ImageView;
38  import android.widget.TextView;
39  
40  import com.squareup.picasso.Picasso;
41  
42  /**
43   * Created by The eXo Platform SAS
44   * Author : Philippe Aristote
45   *          paristote@exoplatform.com
46   * Sep 3, 2014  
47   */
48  public class AccountListAdapter extends BaseAdapter {
49    
50    private ArrayList<ExoAccount> mAccountList;
51    
52    private final String TAG = "eXo____AccountListAdapter____";
53    
54    public AccountListAdapter(Context ctx) {
55      mAccountList = ServerSettingHelper.getInstance().getServerInfoList(ctx);
56    }
57  
58    @Override
59    public int getCount() {
60      return mAccountList.size();
61    }
62  
63    @Override
64    public Object getItem(int index) {
65      return mAccountList.get(index);
66    }
67  
68    @Override
69    public long getItemId(int index) {
70      return index;
71    }
72  
73    @Override
74    public View getView(int index, View convertView, ViewGroup parent) {
75      ViewHolder holder;
76      Context mContext = parent.getContext();
77      if (convertView == null) {
78        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
79        convertView = inflater.inflate(R.layout.account_switcher_item_layout, parent, false);
80        holder = new ViewHolder();
81        holder.accountName = (TextView)convertView.findViewById(R.id.account_name_textview);
82        holder.accountServerURL = (TextView)convertView.findViewById(R.id.account_server_textview);
83        holder.userFullName = (TextView)convertView.findViewById(R.id.account_user_fullname_textview);
84        holder.accountUsername = (TextView)convertView.findViewById(R.id.account_username_textview);
85        holder.connectionStatus = (TextView)convertView.findViewById(R.id.account_connection_status_textview);
86        holder.userAvatar = (ImageView)convertView.findViewById(R.id.account_avatar_imageview);
87        convertView.setTag(holder);
88      } else {
89        holder = (ViewHolder)convertView.getTag();
90      }
91  
92      ExoAccount account = mAccountList.get(index);
93      holder.accountName.setText(account.accountName.toUpperCase(Locale.getDefault()));
94      holder.accountServerURL.setText(account.serverUrl);
95      holder.accountUsername.setText(account.username);
96      holder.userFullName.setText(account.userFullName);
97      String connStatus;
98      if (String.valueOf(index).equals(AccountSetting.getInstance().getDomainIndex())) {
99        // load 'Connected' label from resources
100       connStatus = mContext.getResources().getString(R.string.Connected);
101       holder.connectionStatus.setVisibility(View.VISIBLE);
102     } else {
103       if (account.lastLoginDate == -1) {
104         // last login date unknown, display nothing
105         connStatus = "";
106         holder.connectionStatus.setVisibility(View.GONE);
107       } else {
108         StringBuilder statusBld = new StringBuilder();
109         // load 'LastLoginDate' label from resources
110         statusBld.append(mContext.getResources().getString(R.string.LastLoginDate));
111         // append the date written in words
112         statusBld.append(": ").append(SocialActivityUtil.getPostedTimeString(mContext, account.lastLoginDate));
113         connStatus = statusBld.toString();
114         holder.connectionStatus.setVisibility(View.VISIBLE);
115       }
116     }
117     holder.connectionStatus.setText(connStatus);
118     
119     if ("".equalsIgnoreCase(account.avatarUrl)) {
120       // no avatar URL, load a standard image
121       Picasso.with(mContext).load(R.drawable.default_avatar)
122              .resizeDimen(R.dimen.account_list_avatar_size, R.dimen.account_list_avatar_size).centerCrop()
123              .into(holder.userAvatar);
124     } else {
125       // load the avatar from its URL
126       Picasso.with(mContext).load(account.avatarUrl)
127              .resizeDimen(R.dimen.account_list_avatar_size, R.dimen.account_list_avatar_size).centerCrop()
128              .into(holder.userAvatar);
129     }
130     
131     return convertView;
132   }
133   
134   static class ViewHolder {
135     TextView accountName;
136     TextView accountServerURL;
137     TextView userFullName;
138     TextView accountUsername;
139     TextView connectionStatus; // contains either the last login date or the label 'connected'
140     ImageView userAvatar;
141   }
142   
143 }