1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 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
;
20 import java
.util
.Vector
;
22 import android
.accounts
.Account
;
23 import android
.annotation
.SuppressLint
;
24 import android
.content
.Context
;
25 import android
.view
.LayoutInflater
;
26 import android
.view
.View
;
27 import android
.view
.ViewGroup
;
28 import android
.widget
.BaseAdapter
;
29 import android
.widget
.ImageView
;
30 import android
.widget
.ListAdapter
;
31 import android
.widget
.TextView
;
33 import com
.owncloud
.android
.R
;
34 import com
.owncloud
.android
.authentication
.AccountUtils
;
35 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
36 import com
.owncloud
.android
.datamodel
.OCFile
;
37 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
38 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
39 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
40 import com
.owncloud
.android
.utils
.DisplayUtils
;
44 * This Adapter populates a ListView with all the folders in an ownCloud instance.
46 public class FolderListListAdapter
extends BaseAdapter
implements ListAdapter
{
47 private final static String PERMISSION_SHARED_WITH_ME
= "S";
49 private Context mContext
;
50 private OCFile mFile
= null
;
51 private Vector
<OCFile
> mFolders
= null
;
53 private FileDataStorageManager mStorageManager
;
54 private Account mAccount
;
55 private ComponentsGetter mTransferServiceGetter
;
57 public FolderListListAdapter(Context context
, ComponentsGetter transferServiceGetter
) {
59 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
60 mTransferServiceGetter
= transferServiceGetter
;
64 public boolean areAllItemsEnabled() {
69 public boolean isEnabled(int position
) {
74 public int getCount() {
75 return mFolders
!= null ? mFolders
.size() : 0;
79 public Object
getItem(int position
) {
80 if (mFolders
== null
|| mFolders
.size() <= position
)
82 return mFolders
.get(position
);
86 public long getItemId(int position
) {
87 if (mFolders
== null
|| mFolders
.size() <= position
)
89 return mFolders
.get(position
).getFileId();
93 public int getItemViewType(int position
) {
97 @SuppressLint("InflateParams")
99 public View
getView(int position
, View convertView
, ViewGroup parent
) {
100 View view
= convertView
;
102 LayoutInflater inflator
= (LayoutInflater
) mContext
103 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
104 view
= inflator
.inflate(R
.layout
.list_item
, null
);
107 if (mFolders
!= null
&& mFolders
.size() > position
) {
108 OCFile file
= mFolders
.get(position
);
109 TextView fileName
= (TextView
) view
.findViewById(R
.id
.Filename
);
110 String name
= file
.getFileName();
112 fileName
.setText(name
);
113 ImageView fileIcon
= (ImageView
) view
.findViewById(R
.id
.imageView1
);
114 ImageView sharedIconV
= (ImageView
) view
.findViewById(R
.id
.sharedIcon
);
115 ImageView sharedWithMeIconV
= (ImageView
) view
.findViewById(R
.id
.sharedWithMeIcon
);
116 sharedWithMeIconV
.setVisibility(View
.GONE
);
118 ImageView localStateView
= (ImageView
) view
.findViewById(R
.id
.imageView2
);
119 localStateView
.bringToFront();
120 FileDownloaderBinder downloaderBinder
= mTransferServiceGetter
.getFileDownloaderBinder();
121 FileUploaderBinder uploaderBinder
= mTransferServiceGetter
.getFileUploaderBinder();
123 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
124 localStateView
.setImageResource(R
.drawable
.downloading_file_indicator
);
125 localStateView
.setVisibility(View
.VISIBLE
);
126 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
127 localStateView
.setImageResource(R
.drawable
.uploading_file_indicator
);
128 localStateView
.setVisibility(View
.VISIBLE
);
129 } else if (file
.isDown()) {
130 localStateView
.setImageResource(R
.drawable
.local_file_indicator
);
131 localStateView
.setVisibility(View
.VISIBLE
);
133 localStateView
.setVisibility(View
.INVISIBLE
);
136 TextView fileSizeV
= (TextView
) view
.findViewById(R
.id
.file_size
);
137 TextView lastModV
= (TextView
) view
.findViewById(R
.id
.last_mod
);
138 ImageView checkBoxV
= (ImageView
) view
.findViewById(R
.id
.custom_checkbox
);
141 fileSizeV
.setVisibility(View
.INVISIBLE
);
142 lastModV
.setVisibility(View
.VISIBLE
);
143 lastModV
.setText(DisplayUtils
.unixTimeToHumanReadable(file
.getModificationTimestamp()));
144 checkBoxV
.setVisibility(View
.GONE
);
145 view
.findViewById(R
.id
.imageView3
).setVisibility(View
.GONE
);
147 if (checkIfFileIsSharedWithMe(file
)) {
148 fileIcon
.setImageResource(R
.drawable
.shared_with_me_folder
);
149 sharedWithMeIconV
.setVisibility(View
.VISIBLE
);
151 fileIcon
.setImageResource(DisplayUtils
.getResourceId(file
.getMimetype(), file
.getFileName()));
154 // If folder is sharedByLink, icon folder must be changed to
156 if (file
.isShareByLink()) {
157 fileIcon
.setImageResource(R
.drawable
.folder_public
);
160 if (file
.isShareByLink()) {
161 sharedIconV
.setVisibility(View
.VISIBLE
);
163 sharedIconV
.setVisibility(View
.GONE
);
171 public int getViewTypeCount() {
176 public boolean hasStableIds() {
181 public boolean isEmpty() {
182 return (mFolders
== null
|| mFolders
.isEmpty());
186 * Change the adapted directory for a new one
187 * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
188 * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
190 public void swapDirectory(OCFile directory
, FileDataStorageManager updatedStorageManager
) {
192 if (updatedStorageManager
!= null
&& updatedStorageManager
!= mStorageManager
) {
193 mStorageManager
= updatedStorageManager
;
194 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(mContext
);
196 if (mStorageManager
!= null
) {
197 // Only take into account the folders for after being listed
198 mFolders
= getFolders(mStorageManager
.getFolderContent(mFile
));
202 notifyDataSetChanged();
206 * Filter for getting only the folders
208 * @return Vector<OCFile>
210 public Vector
<OCFile
> getFolders(Vector
<OCFile
> files
) {
211 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
212 OCFile current
= null
;
213 for (int i
=0; i
<files
.size(); i
++) {
214 current
= files
.get(i
);
215 if (current
.isFolder()) {
223 * Check if parent folder does not include 'S' permission and if file/folder
226 * @param file: OCFile
227 * @return boolean: True if it is shared with me and false if it is not
229 private boolean checkIfFileIsSharedWithMe(OCFile file
) {
230 return (mFile
.getPermissions() != null
&& !mFile
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
)
231 && file
.getPermissions() != null
&& file
.getPermissions().contains(PERMISSION_SHARED_WITH_ME
));