summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/launcher/home/CMHomeAdapter.java
blob: 76656d2a1324026a6ce4f62cae85bc3f7739d100 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package org.cyanogenmod.launcher.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.launcher3.R;
import org.w3c.dom.Text;

import java.io.InputStream;
import java.util.List;

/**
 * Created by Schoen on 7/25/15.
 */
public class CMHomeAdapter extends RecyclerView.Adapter<CMHomeAdapter.ViewHolder>{

    Context mContext;
    List<CMHomeCard> mCards;

    public static class ViewHolder extends RecyclerView.ViewHolder{
        //contact
        LinearLayout contactCard;
        ImageView contactImage1;
        ImageView contactImage2;
        ImageView contactImage3;
        ImageView contactImage4;

        //calendar card
        LinearLayout calendarCard;
        LinearLayout eventContainer;

        //news card
        LinearLayout newsCard;
        ImageView newsImage;
        TextView newsTitle;
        TextView sourceAndTime;


        ViewHolder(View cardView, int cardType){
            super(cardView);

            switch(cardType){
                case 0:
                    contactCard = (LinearLayout)itemView.findViewById(R.id.contact_card);
                    contactImage1 = (ImageView)itemView.findViewById(R.id.contact_image_one);
                    contactImage2 = (ImageView)itemView.findViewById(R.id.contact_image_two);
                    contactImage3 = (ImageView)itemView.findViewById(R.id.contact_image_three);
                    contactImage4 = (ImageView)itemView.findViewById(R.id.contact_image_four);
                    break;
                case 1:
                    calendarCard = (LinearLayout)itemView.findViewById(R.id.calendar_card);
                    eventContainer = (LinearLayout)itemView.findViewById(R.id.event_container);
                    break;
                case 2:
                    newsCard = (LinearLayout)itemView.findViewById(R.id.news_card);
                    newsImage = (ImageView)itemView.findViewById(R.id.news_image);
                    newsTitle = (TextView)itemView.findViewById(R.id.news_title);
                    sourceAndTime = (TextView)itemView.findViewById(R.id.news_source_time);
                    break;
                case 3:
                    newsCard = (LinearLayout)itemView.findViewById(R.id.news_card);
                    newsImage = (ImageView)itemView.findViewById(R.id.news_image);
                    newsTitle = (TextView)itemView.findViewById(R.id.news_title);
                    sourceAndTime = (TextView)itemView.findViewById(R.id.news_source_time);
                    break;
            }
        }

    }

    CMHomeAdapter(Context context, List<CMHomeCard> cards){
        mContext = context;
        mCards = cards;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView){
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public int getItemViewType(int position) {

        Log.w("HAX", "get item view type");
        //default type
        int viewType = 3;

        if(position == 0){
            //contact card
            viewType = 0;
        }
        if(position == 1){
            //calendar card
            viewType = 1;
        }
        if(position == 2){
            viewType = 2;
        }

        return viewType;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int cardType){
        View v = null;

        switch(cardType){
            case 0:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.contact_card, viewGroup, false);
                Log.w("HAX","feature first");
                break;
            case 1:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.calendar_card, viewGroup, false);
                Log.w("HAX","feature");
                break;
            case 2:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.news_card_first, viewGroup, false);
                Log.w("HAX","item first");
                break;
            case 3:
                v = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.news_card, viewGroup, false);
                Log.w("HAX","item");
                break;
        }

        ViewHolder vh = new ViewHolder(v, cardType);

        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder vh,int i){

        if(i == 0){
            setupContact(vh.contactImage1);
            setupContact(vh.contactImage2);
            setupContact(vh.contactImage3);
            setupContact(vh.contactImage4);
        }

        if(i == 1){
            int numEvents = getUpcomingEventCount();
            getEventData();

            for(int e = 0; e < numEvents; e++){
                createEventEntry(vh.eventContainer, e);
            }

        }

        if(i > 1){
            createNewsCard(vh, i);
        }


        Log.w("HAX","we binded");
    }

    @Override
    public int getItemCount(){
        return mCards.size();
    }

    private void setupContact(View view){
        ImageView iv = (ImageView)view;
        iv.setImageResource(R.drawable.persona2);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //launch the appropriate contact card
            }
        });
    }

    private int getUpcomingEventCount(){
        //get the upcoming calendar events here
        //right now I am just returning an int representing the count available that maxes at 3

        return 2;
    }

    private void getEventData(){
        //doesn't do anything yet
    }

    private void createEventEntry(View view, int eventNum){
        LinearLayout ll = (LinearLayout)view;
        View v = LayoutInflater.from(mContext).inflate(R.layout.calendar_event_item,ll,false);
        TextView startTime = (TextView)v.findViewById(R.id.start_time);
        TextView endTime = (TextView)v.findViewById(R.id.end_time);
        TextView title = (TextView)v.findViewById(R.id.event_title);
        TextView location = (TextView)v.findViewById(R.id.event_location);
        startTime.setText("12:00");
        endTime.setText("to 1:00");
        title.setText("Stand Up");
        location.setText("Conference Room");

        ll.addView(v);
    }

    private void createNewsCard(ViewHolder vh, int i){
        new DownloadImageTask(vh.newsImage)
                .execute("http://slidell-independent.com/wp-content/uploads/2013/01/wsne.jpg");//need to get the url out of the spoof data

        vh.newsTitle.setText("This is a temp title");
        vh.sourceAndTime.setText("This is a temp source and time");

    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage){
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls){
            String urldisplay = urls[0];
            Bitmap image = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                image = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return image;
        }
        protected  void onPostExecute(Bitmap result){
            bmImage.setImageBitmap(result);
        }
    }

}