ff883c56304d8f6e21d3c4bdd91b8a2b79e4fff1
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.adapter
;
21 import java
.util
.Arrays
;
22 import java
.util
.Comparator
;
24 import com
.owncloud
.android
.DisplayUtils
;
25 import com
.owncloud
.android
.R
;
28 import android
.content
.Context
;
29 import android
.view
.LayoutInflater
;
30 import android
.view
.View
;
31 import android
.view
.ViewGroup
;
32 import android
.widget
.BaseAdapter
;
33 import android
.widget
.ImageView
;
34 import android
.widget
.ListAdapter
;
35 import android
.widget
.ListView
;
36 import android
.widget
.TextView
;
39 * This Adapter populates a ListView with all files and directories contained
40 * in a local directory
42 * @author David A. Velasco
45 public class LocalFileListAdapter
extends BaseAdapter
implements ListAdapter
{
47 private Context mContext
;
48 private File mDirectory
;
49 private File
[] mFiles
= null
;
51 public LocalFileListAdapter(File directory
, Context context
) {
53 swapDirectory(directory
);
57 public boolean areAllItemsEnabled() {
62 public boolean isEnabled(int position
) {
67 public int getCount() {
68 return mFiles
!= null ? mFiles
.length
: 0;
72 public Object
getItem(int position
) {
73 if (mFiles
== null
|| mFiles
.length
<= position
)
75 return mFiles
[position
];
79 public long getItemId(int position
) {
80 return mFiles
!= null
&& mFiles
.length
<= position ? position
: -1;
84 public int getItemViewType(int position
) {
89 public View
getView(int position
, View convertView
, ViewGroup parent
) {
90 View view
= convertView
;
92 LayoutInflater inflator
= (LayoutInflater
) mContext
93 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
94 view
= inflator
.inflate(R
.layout
.list_item
, null
);
96 if (mFiles
!= null
&& mFiles
.length
> position
) {
97 File file
= mFiles
[position
];
99 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
100 String name
= file
.getName();
101 fileName
.setText(name
);
103 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
104 if (!file
.isDirectory()) {
105 fileIcon
.setImageResource(R
.drawable
.file
);
107 fileIcon
.setImageResource(R
.drawable
.ic_menu_archive
);
110 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
111 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
112 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
113 if (!file
.isDirectory()) {
114 fileSizeV
.setVisibility(View
.VISIBLE
);
115 fileSizeV
.setText(DisplayUtils
.bytesToHumanReadable(file
.length()));
116 lastModV
.setVisibility(View
.VISIBLE
);
117 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.lastModified()));
118 ListView parentList
= (ListView
)parent
;
119 if (parentList
.getChoiceMode() == ListView
.CHOICE_MODE_NONE
) {
120 checkBoxV
.setVisibility(View
.GONE
);
122 if (parentList
.isItemChecked(position
)) {
123 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_on_background
);
125 checkBoxV
.setImageResource(android
.R
.drawable
.checkbox_off_background
);
127 checkBoxV
.setVisibility(View
.VISIBLE
);
131 fileSizeV
.setVisibility(View
.GONE
);
132 lastModV
.setVisibility(View
.GONE
);
133 checkBoxV
.setVisibility(View
.GONE
);
136 view
.findViewById(R
.id
.imageView2
).setVisibility(View
.INVISIBLE
); // not GONE; the alignment changes; ugly way to keep it
137 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
144 public int getViewTypeCount() {
149 public boolean hasStableIds() {
154 public boolean isEmpty() {
155 return (mFiles
== null
|| mFiles
.length
== 0);
159 * Change the adapted directory for a new one
160 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
162 public void swapDirectory(File directory
) {
163 mDirectory
= directory
;
164 mFiles
= (mDirectory
!= null ? mDirectory
.listFiles() : null
);
165 if (mFiles
!= null
) {
166 Arrays
.sort(mFiles
, new Comparator
<File
>() {
168 public int compare(File lhs
, File rhs
) {
169 if (lhs
.isDirectory() && !rhs
.isDirectory()) {
171 } else if (!lhs
.isDirectory() && rhs
.isDirectory()) {
174 return compareNames(lhs
, rhs
);
177 private int compareNames(File lhs
, File rhs
) {
178 return lhs
.getName().toLowerCase().compareTo(rhs
.getName().toLowerCase());
183 notifyDataSetChanged();