|
@@ -22,6 +22,7 @@ import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.Vector;
|
|
|
|
|
|
+import third_parties.daveKoeller.AlphanumComparator;
|
|
|
import android.accounts.Account;
|
|
|
import android.content.Context;
|
|
|
import android.content.SharedPreferences;
|
|
@@ -37,7 +38,6 @@ import android.widget.TextView;
|
|
|
|
|
|
import com.owncloud.android.R;
|
|
|
import com.owncloud.android.authentication.AccountUtils;
|
|
|
-import com.owncloud.android.datamodel.AlphanumComparator;
|
|
|
import com.owncloud.android.datamodel.FileDataStorageManager;
|
|
|
import com.owncloud.android.datamodel.OCFile;
|
|
|
import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
|
|
@@ -45,8 +45,6 @@ import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
|
|
|
import com.owncloud.android.ui.activity.ComponentsGetter;
|
|
|
import com.owncloud.android.utils.DisplayUtils;
|
|
|
import com.owncloud.android.utils.FileStorageUtils;
|
|
|
-import com.owncloud.android.utils.Log_OC;
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* This Adapter populates a ListView with all files and folders in an ownCloud
|
|
@@ -56,409 +54,422 @@ import com.owncloud.android.utils.Log_OC;
|
|
|
*
|
|
|
*/
|
|
|
public class FileListListAdapter extends BaseAdapter implements ListAdapter {
|
|
|
- private final static String PERMISSION_SHARED_WITH_ME = "S";
|
|
|
-
|
|
|
- private Context mContext;
|
|
|
- private OCFile mFile = null;
|
|
|
- private Vector<OCFile> mFiles = null;
|
|
|
- private boolean mJustFolders;
|
|
|
-
|
|
|
- private FileDataStorageManager mStorageManager;
|
|
|
- private Account mAccount;
|
|
|
- private ComponentsGetter mTransferServiceGetter;
|
|
|
- private Integer sortOrder;
|
|
|
- private Boolean sortAscending;
|
|
|
- private SharedPreferences appPreferences;
|
|
|
-
|
|
|
- public FileListListAdapter(
|
|
|
- boolean justFolders,
|
|
|
- Context context,
|
|
|
- ComponentsGetter transferServiceGetter
|
|
|
- ) {
|
|
|
- mJustFolders = justFolders;
|
|
|
- mContext = context;
|
|
|
- mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
|
|
|
- mTransferServiceGetter = transferServiceGetter;
|
|
|
-
|
|
|
- appPreferences = PreferenceManager
|
|
|
- .getDefaultSharedPreferences(mContext);
|
|
|
-
|
|
|
- // Read sorting order, default to sort by name ascending
|
|
|
- sortOrder = appPreferences
|
|
|
- .getInt("sortOrder", 0);
|
|
|
- sortAscending = appPreferences.getBoolean("sortAscending", true);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean areAllItemsEnabled() {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isEnabled(int position) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int getCount() {
|
|
|
- return mFiles != null ? mFiles.size() : 0;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Object getItem(int position) {
|
|
|
- if (mFiles == null || mFiles.size() <= position)
|
|
|
- return null;
|
|
|
- return mFiles.get(position);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public long getItemId(int position) {
|
|
|
- if (mFiles == null || mFiles.size() <= position)
|
|
|
- return 0;
|
|
|
- return mFiles.get(position).getFileId();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int getItemViewType(int position) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- @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 (mFiles != null && mFiles.size() > position) {
|
|
|
- OCFile file = mFiles.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);
|
|
|
-
|
|
|
- if (!file.isFolder()) {
|
|
|
- fileSizeV.setVisibility(View.VISIBLE);
|
|
|
- fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
|
|
|
- lastModV.setVisibility(View.VISIBLE);
|
|
|
- lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
|
|
|
- // this if-else is needed even thoe fav icon is visible by default
|
|
|
- // because android reuses views in listview
|
|
|
- if (!file.keepInSync()) {
|
|
|
- view.findViewById(R.id.imageView3).setVisibility(View.GONE);
|
|
|
- } else {
|
|
|
- view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
|
|
|
- }
|
|
|
-
|
|
|
- ListView parentList = (ListView)parent;
|
|
|
- if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
|
|
|
- checkBoxV.setVisibility(View.GONE);
|
|
|
- } else {
|
|
|
- if (parentList.isItemChecked(position)) {
|
|
|
- checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
|
|
|
- } else {
|
|
|
- checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
|
|
|
- }
|
|
|
- checkBoxV.setVisibility(View.VISIBLE);
|
|
|
- }
|
|
|
-
|
|
|
- fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
|
|
|
-
|
|
|
- if (checkIfFileIsSharedWithMe(file)) {
|
|
|
- sharedWithMeIconV.setVisibility(View.VISIBLE);
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- if (FileStorageUtils.getDefaultSavePathFor(mAccount.name, file) != null){
|
|
|
- fileSizeV.setVisibility(View.VISIBLE);
|
|
|
- fileSizeV.setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)));
|
|
|
- } else {
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Local Folder size in human readable format
|
|
|
- * @param path String
|
|
|
- * @return Size in human readable format
|
|
|
- */
|
|
|
- private String getFolderSizeHuman(String path) {
|
|
|
-
|
|
|
- File dir = new File(path);
|
|
|
-
|
|
|
- if(dir.exists()) {
|
|
|
- long bytes = getFolderSize(dir);
|
|
|
- if (bytes < 1024) return bytes + " B";
|
|
|
- int exp = (int) (Math.log(bytes) / Math.log(1024));
|
|
|
- String pre = ("KMGTPE").charAt(exp-1) + "";
|
|
|
-
|
|
|
- return String.format("%.1f %sB", bytes / Math.pow(1024, exp), pre);
|
|
|
- }
|
|
|
-
|
|
|
- return "0 B";
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Local Folder size
|
|
|
- * @param dir File
|
|
|
- * @return Size in bytes
|
|
|
- */
|
|
|
- private long getFolderSize(File dir) {
|
|
|
- if (dir.exists()) {
|
|
|
- long result = 0;
|
|
|
- File[] fileList = dir.listFiles();
|
|
|
- for(int i = 0; i < fileList.length; i++) {
|
|
|
- if(fileList[i].isDirectory()) {
|
|
|
- result += getFolderSize(fileList[i]);
|
|
|
- } else {
|
|
|
- result += fileList[i].length();
|
|
|
- }
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int getViewTypeCount() {
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean hasStableIds() {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean isEmpty() {
|
|
|
- return (mFiles == null || mFiles.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) {
|
|
|
- mFiles = mStorageManager.getFolderContent(mFile);
|
|
|
- if (mJustFolders) {
|
|
|
- mFiles = getFolders(mFiles);
|
|
|
- }
|
|
|
- } else {
|
|
|
- mFiles = null;
|
|
|
- }
|
|
|
-
|
|
|
- sortDirectory();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Sorts all filenames, regarding last user decision
|
|
|
- */
|
|
|
- private void sortDirectory(){
|
|
|
- switch (sortOrder){
|
|
|
- case 0:
|
|
|
- sortByName(sortAscending);
|
|
|
- break;
|
|
|
- case 1:
|
|
|
- sortByDate(sortAscending);
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- sortBySize(sortAscending);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- 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));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Sorts list by Date
|
|
|
- * @param sortAscending true: ascending, false: descending
|
|
|
- */
|
|
|
- private void sortByDate(boolean sortAscending){
|
|
|
- final Integer val;
|
|
|
- if (sortAscending){
|
|
|
- val = 1;
|
|
|
- } else {
|
|
|
- val = -1;
|
|
|
- }
|
|
|
-
|
|
|
- Collections.sort(mFiles, new Comparator<OCFile>() {
|
|
|
- public int compare(OCFile o1, OCFile o2) {
|
|
|
- if (o1.isFolder() && o2.isFolder()) {
|
|
|
- return val * Long.compare(o1.getModificationTimestamp(), o2.getModificationTimestamp());
|
|
|
- }
|
|
|
- else if (o1.isFolder()) {
|
|
|
- return -1;
|
|
|
- } else if (o2.isFolder()) {
|
|
|
- return 1;
|
|
|
- } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
|
|
|
- return 0;
|
|
|
- } else {
|
|
|
- return val * Long.compare(o1.getModificationTimestamp(), o2.getModificationTimestamp());
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Sorts list by Size
|
|
|
- * @param sortAscending true: ascending, false: descending
|
|
|
- */
|
|
|
- private void sortBySize(boolean sortAscending){
|
|
|
- final Integer val;
|
|
|
- if (sortAscending){
|
|
|
- val = 1;
|
|
|
- } else {
|
|
|
- val = -1;
|
|
|
- }
|
|
|
-
|
|
|
- Collections.sort(mFiles, new Comparator<OCFile>() {
|
|
|
- public int compare(OCFile o1, OCFile o2) {
|
|
|
- if (o1.isFolder() && o2.isFolder()) {
|
|
|
- return val * Long.compare(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1))),
|
|
|
- getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
|
|
|
- }
|
|
|
- else if (o1.isFolder()) {
|
|
|
- return -1;
|
|
|
- } else if (o2.isFolder()) {
|
|
|
- return 1;
|
|
|
- } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
|
|
|
- return 0;
|
|
|
- } else {
|
|
|
- return val * Long.compare(o1.getFileLength(), o2.getFileLength());
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Sorts list by Name
|
|
|
- * @param sortAscending true: ascending, false: descending
|
|
|
- */
|
|
|
- private void sortByName(boolean sortAscending){
|
|
|
- final Integer val;
|
|
|
- if (sortAscending){
|
|
|
- val = 1;
|
|
|
- } else {
|
|
|
- val = -1;
|
|
|
- }
|
|
|
-
|
|
|
- Collections.sort(mFiles, new Comparator<OCFile>() {
|
|
|
- public int compare(OCFile o1, OCFile o2) {
|
|
|
- if (o1.isFolder() && o2.isFolder()) {
|
|
|
- return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
|
|
|
- } else if (o1.isFolder()) {
|
|
|
- return -1;
|
|
|
- } else if (o2.isFolder()) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- return val * new AlphanumComparator().compare(o1, o2);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- public void setSortOrder(Integer order, boolean ascending) {
|
|
|
- SharedPreferences.Editor editor = appPreferences.edit();
|
|
|
- editor.putInt("sortOrder", order);
|
|
|
- editor.putBoolean("sortAscending", ascending);
|
|
|
- editor.commit();
|
|
|
-
|
|
|
- sortOrder = order;
|
|
|
- sortAscending = ascending;
|
|
|
-
|
|
|
- sortDirectory();
|
|
|
- }
|
|
|
+ private final static String PERMISSION_SHARED_WITH_ME = "S";
|
|
|
+
|
|
|
+ private final Context mContext;
|
|
|
+ private OCFile mFile = null;
|
|
|
+ private Vector<OCFile> mFiles = null;
|
|
|
+ private final boolean mJustFolders;
|
|
|
+
|
|
|
+ private FileDataStorageManager mStorageManager;
|
|
|
+ private Account mAccount;
|
|
|
+ private final ComponentsGetter mTransferServiceGetter;
|
|
|
+ private Integer mSortOrder;
|
|
|
+ public static final Integer mSortName = 0;
|
|
|
+ public static final Integer mSortDate = 1;
|
|
|
+ public static final Integer mSortSize = 2;
|
|
|
+ private Boolean mSortAscending;
|
|
|
+ private final SharedPreferences mAppPreferences;
|
|
|
+
|
|
|
+ public FileListListAdapter(boolean justFolders, Context context, ComponentsGetter transferServiceGetter) {
|
|
|
+ mJustFolders = justFolders;
|
|
|
+ mContext = context;
|
|
|
+ mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
|
|
|
+ mTransferServiceGetter = transferServiceGetter;
|
|
|
+
|
|
|
+ mAppPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
|
|
|
+
|
|
|
+ // Read sorting order, default to sort by name ascending
|
|
|
+ mSortOrder = mAppPreferences.getInt("sortOrder", mSortName);
|
|
|
+ mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean areAllItemsEnabled() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isEnabled(int position) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getCount() {
|
|
|
+ return mFiles != null ? mFiles.size() : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getItem(int position) {
|
|
|
+ if (mFiles == null || mFiles.size() <= position)
|
|
|
+ return null;
|
|
|
+ return mFiles.get(position);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getItemId(int position) {
|
|
|
+ if (mFiles == null || mFiles.size() <= position)
|
|
|
+ return 0;
|
|
|
+ return mFiles.get(position).getFileId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemViewType(int position) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @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 (mFiles != null && mFiles.size() > position) {
|
|
|
+ OCFile file = mFiles.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);
|
|
|
+
|
|
|
+ if (!file.isFolder()) {
|
|
|
+ fileSizeV.setVisibility(View.VISIBLE);
|
|
|
+ fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
|
|
|
+ lastModV.setVisibility(View.VISIBLE);
|
|
|
+ lastModV.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
|
|
|
+ // this if-else is needed even thoe fav icon is visible by
|
|
|
+ // default
|
|
|
+ // because android reuses views in listview
|
|
|
+ if (!file.keepInSync()) {
|
|
|
+ view.findViewById(R.id.imageView3).setVisibility(View.GONE);
|
|
|
+ } else {
|
|
|
+ view.findViewById(R.id.imageView3).setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ ListView parentList = (ListView) parent;
|
|
|
+ if (parentList.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
|
|
|
+ checkBoxV.setVisibility(View.GONE);
|
|
|
+ } else {
|
|
|
+ if (parentList.isItemChecked(position)) {
|
|
|
+ checkBoxV.setImageResource(android.R.drawable.checkbox_on_background);
|
|
|
+ } else {
|
|
|
+ checkBoxV.setImageResource(android.R.drawable.checkbox_off_background);
|
|
|
+ }
|
|
|
+ checkBoxV.setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ fileIcon.setImageResource(DisplayUtils.getResourceId(file.getMimetype(), file.getFileName()));
|
|
|
+
|
|
|
+ if (checkIfFileIsSharedWithMe(file)) {
|
|
|
+ sharedWithMeIconV.setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // TODO Re-enable when server supports folder-size calculation
|
|
|
+ // if (FileStorageUtils.getDefaultSavePathFor(mAccount.name,
|
|
|
+ // file) != null) {
|
|
|
+ // fileSizeV.setVisibility(View.VISIBLE);
|
|
|
+ // fileSizeV
|
|
|
+ // .setText(getFolderSizeHuman(FileStorageUtils.getDefaultSavePathFor(mAccount.name,
|
|
|
+ // file)));
|
|
|
+ // } else {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Local Folder size in human readable format
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * String
|
|
|
+ * @return Size in human readable format
|
|
|
+ */
|
|
|
+ private String getFolderSizeHuman(String path) {
|
|
|
+
|
|
|
+ File dir = new File(path);
|
|
|
+
|
|
|
+ if (dir.exists()) {
|
|
|
+ long bytes = getFolderSize(dir);
|
|
|
+ return DisplayUtils.bytesToHumanReadable(bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ return "0 B";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Local Folder size
|
|
|
+ *
|
|
|
+ * @param dir
|
|
|
+ * File
|
|
|
+ * @return Size in bytes
|
|
|
+ */
|
|
|
+ private long getFolderSize(File dir) {
|
|
|
+ if (dir.exists()) {
|
|
|
+ long result = 0;
|
|
|
+ File[] fileList = dir.listFiles();
|
|
|
+ for (int i = 0; i < fileList.length; i++) {
|
|
|
+ if (fileList[i].isDirectory()) {
|
|
|
+ result += getFolderSize(fileList[i]);
|
|
|
+ } else {
|
|
|
+ result += fileList[i].length();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getViewTypeCount() {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean hasStableIds() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isEmpty() {
|
|
|
+ return (mFiles == null || mFiles.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) {
|
|
|
+ mFiles = mStorageManager.getFolderContent(mFile);
|
|
|
+ if (mJustFolders) {
|
|
|
+ mFiles = getFolders(mFiles);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ mFiles = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ sortDirectory();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sorts all filenames, regarding last user decision
|
|
|
+ */
|
|
|
+ private void sortDirectory() {
|
|
|
+ switch (mSortOrder) {
|
|
|
+ case 0:
|
|
|
+ sortByName(mSortAscending);
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ sortByDate(mSortAscending);
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ sortBySize(mSortAscending);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sorts list by Date
|
|
|
+ *
|
|
|
+ * @param sortAscending
|
|
|
+ * true: ascending, false: descending
|
|
|
+ */
|
|
|
+ private void sortByDate(boolean sortAscending) {
|
|
|
+ final Integer val;
|
|
|
+ if (sortAscending) {
|
|
|
+ val = 1;
|
|
|
+ } else {
|
|
|
+ val = -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ Collections.sort(mFiles, new Comparator<OCFile>() {
|
|
|
+ @Override
|
|
|
+ public int compare(OCFile o1, OCFile o2) {
|
|
|
+ if (o1.isFolder() && o2.isFolder()) {
|
|
|
+ return val * Long.compare(o1.getModificationTimestamp(), o2.getModificationTimestamp());
|
|
|
+ } else if (o1.isFolder()) {
|
|
|
+ return -1;
|
|
|
+ } else if (o2.isFolder()) {
|
|
|
+ return 1;
|
|
|
+ } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0) {
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ return val * Long.compare(o1.getModificationTimestamp(), o2.getModificationTimestamp());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sorts list by Size
|
|
|
+ *
|
|
|
+ * @param sortAscending
|
|
|
+ * true: ascending, false: descending
|
|
|
+ */
|
|
|
+ private void sortBySize(boolean sortAscending) {
|
|
|
+ final Integer val;
|
|
|
+ if (sortAscending) {
|
|
|
+ val = 1;
|
|
|
+ } else {
|
|
|
+ val = -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ Collections.sort(mFiles, new Comparator<OCFile>() {
|
|
|
+ @Override
|
|
|
+ public int compare(OCFile o1, OCFile o2) {
|
|
|
+ if (o1.isFolder() && o2.isFolder()) {
|
|
|
+ return val
|
|
|
+ * Long.compare(
|
|
|
+ getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1))),
|
|
|
+ getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
|
|
|
+ } else if (o1.isFolder()) {
|
|
|
+ return -1;
|
|
|
+ } else if (o2.isFolder()) {
|
|
|
+ return 1;
|
|
|
+ } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0) {
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ return val * Long.compare(o1.getFileLength(), o2.getFileLength());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sorts list by Name
|
|
|
+ *
|
|
|
+ * @param sortAscending
|
|
|
+ * true: ascending, false: descending
|
|
|
+ */
|
|
|
+ private void sortByName(boolean sortAscending) {
|
|
|
+ final Integer val;
|
|
|
+ if (sortAscending) {
|
|
|
+ val = 1;
|
|
|
+ } else {
|
|
|
+ val = -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ Collections.sort(mFiles, new Comparator<OCFile>() {
|
|
|
+ @Override
|
|
|
+ public int compare(OCFile o1, OCFile o2) {
|
|
|
+ if (o1.isFolder() && o2.isFolder()) {
|
|
|
+ return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
|
|
|
+ } else if (o1.isFolder()) {
|
|
|
+ return -1;
|
|
|
+ } else if (o2.isFolder()) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return val * new AlphanumComparator().compare(o1, o2);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSortOrder(Integer order, boolean ascending) {
|
|
|
+ SharedPreferences.Editor editor = mAppPreferences.edit();
|
|
|
+ editor.putInt("sortOrder", order);
|
|
|
+ editor.putBoolean("sortAscending", ascending);
|
|
|
+ editor.commit();
|
|
|
+
|
|
|
+ mSortOrder = order;
|
|
|
+ mSortAscending = ascending;
|
|
|
+
|
|
|
+ sortDirectory();
|
|
|
+ }
|
|
|
}
|