1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 package com.example.android.directoryselection;
18 
19 import android.support.v7.widget.RecyclerView;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 import java.util.List;
27 
28 /**
29  * Provide views to RecyclerView with the directory entries.
30  */
31 public class DirectoryEntryAdapter extends RecyclerView.Adapter<DirectoryEntryAdapter.ViewHolder> {
32 
33     static final String DIRECTORY_MIME_TYPE = "vnd.android.document/directory";
34     private List<DirectoryEntry> mDirectoryEntries;
35 
36     /**
37      * Provide a reference to the type of views that you are using (custom ViewHolder)
38      */
39     public static class ViewHolder extends RecyclerView.ViewHolder {
40         private final TextView mFileName;
41         private final TextView mMimeType;
42         private final ImageView mImageView;
43 
ViewHolder(View v)44         public ViewHolder(View v) {
45             super(v);
46             mFileName = (TextView) v.findViewById(R.id.textview_filename);
47             mMimeType = (TextView) v.findViewById(R.id.textview_mimetype);
48             mImageView = (ImageView) v.findViewById(R.id.entry_image);
49         }
50 
getFileName()51         public TextView getFileName() {
52             return mFileName;
53         }
54 
getMimeType()55         public TextView getMimeType() {
56             return mMimeType;
57         }
58 
getImageView()59         public ImageView getImageView() {
60             return mImageView;
61         }
62     }
63 
64     /**
65      * Initialize the directory entries of the Adapter.
66      *
67      * @param directoryEntries an array of {@link DirectoryEntry}.
68      */
DirectoryEntryAdapter(List<DirectoryEntry> directoryEntries)69     public DirectoryEntryAdapter(List<DirectoryEntry> directoryEntries) {
70         mDirectoryEntries = directoryEntries;
71     }
72 
73     @Override
onCreateViewHolder(ViewGroup viewGroup, int viewType)74     public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
75         View v = LayoutInflater.from(viewGroup.getContext())
76                 .inflate(R.layout.directory_item, viewGroup, false);
77         return new ViewHolder(v);
78     }
79 
80     @Override
onBindViewHolder(ViewHolder viewHolder, final int position)81     public void onBindViewHolder(ViewHolder viewHolder, final int position) {
82         viewHolder.getFileName().setText(mDirectoryEntries.get(position).fileName);
83         viewHolder.getMimeType().setText(mDirectoryEntries.get(position).mimeType);
84 
85         if (DIRECTORY_MIME_TYPE.equals(mDirectoryEntries.get(position).mimeType)) {
86             viewHolder.getImageView().setImageResource(R.drawable.ic_folder_grey600_36dp);
87         } else {
88             viewHolder.getImageView().setImageResource(R.drawable.ic_description_grey600_36dp);
89         }
90     }
91 
92     @Override
getItemCount()93     public int getItemCount() {
94         return mDirectoryEntries.size();
95     }
96 
setDirectoryEntries(List<DirectoryEntry> directoryEntries)97     public void setDirectoryEntries(List<DirectoryEntry> directoryEntries) {
98         mDirectoryEntries = directoryEntries;
99     }
100 }
101