-/* ownCloud Android client application
- * Copyright (C) 2011 Bartek Przybylski
- * Copyright (C) 2012-2014 ownCloud Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-package com.owncloud.android.ui.adapter;
-
-import java.util.Vector;
-
-import android.accounts.Account;
-import android.annotation.SuppressLint;
-import android.content.Context;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.BaseAdapter;
-import android.widget.ImageView;
-import android.widget.ListAdapter;
-import android.widget.TextView;
-
-import com.owncloud.android.R;
-import com.owncloud.android.authentication.AccountUtils;
-import com.owncloud.android.datamodel.FileDataStorageManager;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
-import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
-import com.owncloud.android.ui.activity.ComponentsGetter;
-import com.owncloud.android.utils.DisplayUtils;
-
-
-/**
- * This Adapter populates a ListView with all the folders in an ownCloud instance.
- */
-public class FolderListListAdapter extends BaseAdapter implements ListAdapter {
- private final static String PERMISSION_SHARED_WITH_ME = "S";
-
- private Context mContext;
- private OCFile mFile = null;
- private Vector<OCFile> mFolders = null;
-
- private FileDataStorageManager mStorageManager;
- private Account mAccount;
- private ComponentsGetter mTransferServiceGetter;
-
- public FolderListListAdapter(Context context, ComponentsGetter transferServiceGetter) {
- mContext = context;
- mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
- mTransferServiceGetter = transferServiceGetter;
- }
-
- @Override
- public boolean areAllItemsEnabled() {
- return true;
- }
-
- @Override
- public boolean isEnabled(int position) {
- return true;
- }
-
- @Override
- public int getCount() {
- return mFolders != null ? mFolders.size() : 0;
- }
-
- @Override
- public Object getItem(int position) {
- if (mFolders == null || mFolders.size() <= position)
- return null;
- return mFolders.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- if (mFolders == null || mFolders.size() <= position)
- return 0;
- return mFolders.get(position).getFileId();
- }
-
- @Override
- public int getItemViewType(int position) {
- return 0;
- }
-
- @SuppressLint("InflateParams")
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null) {
- LayoutInflater inflator = (LayoutInflater) mContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view = inflator.inflate(R.layout.list_item, null);
- }
-
- if (mFolders != null && mFolders.size() > position) {
- OCFile file = mFolders.get(position);
- TextView fileName = (TextView) view.findViewById(R.id.Filename);
- String name = file.getFileName();
-
- fileName.setText(name);
- ImageView fileIcon = (ImageView) view.findViewById(R.id.imageView1);
- ImageView sharedIconV = (ImageView) view.findViewById(R.id.sharedIcon);
- ImageView sharedWithMeIconV = (ImageView) view.findViewById(R.id.sharedWithMeIcon);
- sharedWithMeIconV.setVisibility(View.GONE);
-
- ImageView localStateView = (ImageView) view.findViewById(R.id.imageView2);
- localStateView.bringToFront();
- FileDownloaderBinder downloaderBinder = mTransferServiceGetter.getFileDownloaderBinder();
- FileUploaderBinder uploaderBinder = mTransferServiceGetter.getFileUploaderBinder();
-
- if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) {
- localStateView.setImageResource(R.drawable.downloading_file_indicator);
- localStateView.setVisibility(View.VISIBLE);
- } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file)) {
- localStateView.setImageResource(R.drawable.uploading_file_indicator);
- localStateView.setVisibility(View.VISIBLE);
- } else if (file.isDown()) {
- localStateView.setImageResource(R.drawable.local_file_indicator);
- localStateView.setVisibility(View.VISIBLE);
- } else {
- localStateView.setVisibility(View.INVISIBLE);
- }
-
- TextView fileSizeV = (TextView) view.findViewById(R.id.file_size);
- TextView lastModV = (TextView) view.findViewById(R.id.last_mod);
- ImageView checkBoxV = (ImageView) view.findViewById(R.id.custom_checkbox);
-
-
- fileSizeV.setVisibility(View.INVISIBLE);
- lastModV.setVisibility(View.VISIBLE);
- lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
- checkBoxV.setVisibility(View.GONE);
- view.findViewById(R.id.imageView3).setVisibility(View.GONE);
-
- if (checkIfFileIsSharedWithMe(file)) {
- fileIcon.setImageResource(R.drawable.shared_with_me_folder);
- sharedWithMeIconV.setVisibility(View.VISIBLE);
- } else {
- fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
- }
-
- // If folder is sharedByLink, icon folder must be changed to
- // folder-public one
- if (file.isShareByLink()) {
- fileIcon.setImageResource(R.drawable.folder_public);
- }
-
- if (file.isShareByLink()) {
- sharedIconV.setVisibility(View.VISIBLE);
- } else {
- sharedIconV.setVisibility(View.GONE);
- }
- }
-
- return view;
- }
-
- @Override
- public int getViewTypeCount() {
- return 1;
- }
-
- @Override
- public boolean hasStableIds() {
- return true;
- }
-
- @Override
- public boolean isEmpty() {
- return (mFolders == null || mFolders.isEmpty());
- }
-
- /**
- * Change the adapted directory for a new one
- * @param directory New file to adapt. Can be NULL, meaning "no content to adapt".
- * @param updatedStorageManager Optional updated storage manager; used to replace mStorageManager if is different (and not NULL)
- */
- public void swapDirectory(OCFile directory, FileDataStorageManager updatedStorageManager) {
- mFile = directory;
- if (updatedStorageManager != null && updatedStorageManager != mStorageManager) {
- mStorageManager = updatedStorageManager;
- mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
- }
- if (mStorageManager != null) {
- // Only take into account the folders for after being listed
- mFolders = getFolders(mStorageManager.getFolderContent(mFile));
- } else {
- mFolders = null;
- }
- notifyDataSetChanged();
- }
-
- /**
- * Filter for getting only the folders
- * @param files
- * @return Vector<OCFile>
- */
- public Vector<OCFile> getFolders(Vector<OCFile> files) {
- Vector<OCFile> ret = new Vector<OCFile>();
- OCFile current = null;
- for (int i=0; i<files.size(); i++) {
- current = files.get(i);
- if (current.isFolder()) {
- ret.add(current);
- }
- }
- return ret;
- }
-
- /**
- * Check if parent folder does not include 'S' permission and if file/folder
- * is shared with me
- *
- * @param file: OCFile
- * @return boolean: True if it is shared with me and false if it is not
- */
- private boolean checkIfFileIsSharedWithMe(OCFile file) {
- return (mFile.getPermissions() != null && !mFile.getPermissions().contains(PERMISSION_SHARED_WITH_ME)
- && file.getPermissions() != null && file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
- }
-}