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.controller.dashboard;
20  
21  import java.util.ArrayList;
22  
23  import org.exoplatform.R;
24  import org.exoplatform.model.GadgetInfo;
25  import org.exoplatform.ui.WebViewActivity;
26  import org.exoplatform.utils.ExoConstants;
27  
28  import com.squareup.picasso.Picasso;
29  
30  import android.content.Context;
31  import android.content.Intent;
32  import android.graphics.Bitmap;
33  import android.graphics.Canvas;
34  import android.graphics.Color;
35  import android.graphics.Paint;
36  import android.graphics.PorterDuff;
37  import android.graphics.PorterDuffXfermode;
38  import android.graphics.Rect;
39  import android.graphics.RectF;
40  import android.net.Uri;
41  import android.view.LayoutInflater;
42  import android.view.View;
43  import android.view.ViewGroup;
44  import android.widget.BaseAdapter;
45  import android.widget.ImageView;
46  import android.widget.TextView;
47  
48  public class DashboardItemAdapter extends
49                                    BaseAdapter /*
50                                                 * implements ImageProcessor
51                                                 */ {
52  
53    private ArrayList<GadgetInfo> _arrayOfItems;
54  
55    private final Paint           mPaint    = new Paint(Paint.ANTI_ALIAS_FLAG);
56  
57    private final Rect            mRectSrc  = new Rect();
58  
59    private final Rect            mRectDest = new Rect();
60  
61    private Bitmap                mMask;
62  
63    private int                   mThumbnailSize;
64  
65    private int                   mThumbnailRadius;
66  
67    private LayoutInflater        mInflater;
68  
69    public DashboardItemAdapter(Context context, ArrayList<GadgetInfo> items) {
70      // Jul 21, 2015, shouldn't store layout inflater here because it's can be get in getView
71      mInflater = LayoutInflater.from(context);
72  
73      mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
74  
75      mThumbnailSize = context.getResources().getDimensionPixelSize(R.dimen.thumbnail_size);
76      mThumbnailRadius = context.getResources().getDimensionPixelSize(R.dimen.thumbnail_radius);
77  
78      _arrayOfItems = items;
79  
80      prepareMask();
81    }
82  
83    private void prepareMask() {
84      mMask = Bitmap.createBitmap(mThumbnailSize, mThumbnailSize, Bitmap.Config.ARGB_8888);
85  
86      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
87      paint.setColor(Color.RED);
88      paint.setStyle(Paint.Style.FILL_AND_STROKE);
89  
90      Canvas c = new Canvas(mMask);
91      c.drawRoundRect(new RectF(0, 0, mThumbnailSize, mThumbnailSize), mThumbnailRadius, mThumbnailRadius, paint);
92    }
93  
94    public int getCount() {
95  
96      return _arrayOfItems.size();
97    }
98  
99    public Object getItem(int position) {
100     return null;
101   }
102 
103   public long getItemId(int position) {
104     return position;
105   }
106 
107   public View getView(int position, View convertView, ViewGroup parent) {
108 
109     final GadgetInfo inforGadget = _arrayOfItems.get(position);
110 
111     // TODO use ViewHolder pattern
112     if (inforGadget.getTabName() != null) {
113 
114       convertView = mInflater.inflate(R.layout.gadget_tab_layout, parent, false);
115       TextView textViewTabTitle = (TextView) convertView.findViewById(R.id.textView_Tab_Title);
116       textViewTabTitle.setText(inforGadget.getTabName());
117 
118     } else {
119       convertView = mInflater.inflate(R.layout.gadget_item_layout, parent, false);
120       if (position + 1 < _arrayOfItems.size()) {
121         GadgetInfo nextItem = _arrayOfItems.get(position + 1);
122 
123         if (inforGadget.getGadgetIndex() == 0) {
124           if (nextItem.getTabName() != null)
125             convertView.setBackgroundResource(R.drawable.dashboard_single_background_shape);
126           else
127             convertView.setBackgroundResource(R.drawable.dashboard_top_background_shape);
128         } else {
129           if (nextItem.getTabName() != null)
130             convertView.setBackgroundResource(R.drawable.dasboard_bottom_background_shape);
131           else {
132             convertView.setBackgroundResource(R.drawable.dashboard_middle_background_shape);
133 
134           }
135         }
136 
137       } else {
138         GadgetInfo previousItem = _arrayOfItems.get(position - 1);
139         if (previousItem.getTabName() == null)
140           convertView.setBackgroundResource(R.drawable.dasboard_bottom_background_shape);
141         else
142           convertView.setBackgroundResource(R.drawable.dashboard_single_background_shape);
143       }
144 
145       ImageView imageViewAvatar = (ImageView) convertView.findViewById(R.id.gadget_image);
146       Picasso.with(imageViewAvatar.getContext())
147              .load(Uri.parse(inforGadget.getStrGadgetIcon()))
148              .placeholder(R.drawable.gadgetplaceholder)
149              .into(imageViewAvatar);
150       TextView textViewName = (TextView) convertView.findViewById(R.id.gadget_title);
151       textViewName.setText(inforGadget.getGadgetName());
152 
153       TextView textViewMessage = (TextView) convertView.findViewById(R.id.gadget_content);
154       textViewMessage.setText(inforGadget.getGadgetDescription());
155 
156     }
157 
158     convertView.setOnClickListener(new View.OnClickListener() {
159 
160       public void onClick(View v) {
161         if (inforGadget.getTabName() == null)
162           showGadget(v.getContext(), inforGadget);
163       }
164     });
165 
166     return convertView;
167   }
168 
169   public Bitmap processImage(Bitmap bitmap) {
170     Bitmap result = Bitmap.createBitmap(mThumbnailSize, mThumbnailSize, Bitmap.Config.ARGB_8888);
171     Canvas c = new Canvas(result);
172 
173     mRectSrc.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
174     mRectDest.set(0, 0, mThumbnailSize, mThumbnailSize);
175     c.drawBitmap(bitmap, mRectSrc, mRectDest, null);
176     c.drawBitmap(mMask, 0, 0, mPaint);
177 
178     return result;
179   }
180 
181   public void showGadget(Context ctx, GadgetInfo gadget) {
182     String gadgetUrl = gadget.getGadgetUrl();
183     Intent intent = new Intent(ctx, WebViewActivity.class);
184     intent.putExtra(ExoConstants.WEB_VIEW_URL, gadgetUrl);
185     intent.putExtra(ExoConstants.WEB_VIEW_TITLE, gadget.getGadgetName());
186     intent.putExtra(ExoConstants.WEB_VIEW_ALLOW_JS, "true");
187     ctx.startActivity(intent);
188 
189   }
190 
191 }