From: jabarros Date: Thu, 23 Oct 2014 08:18:20 +0000 (+0200) Subject: Merge branch 'develop' into sorting_files_2 X-Git-Tag: oc-android-1.7.0_signed~134^2~4 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/2fad1e1a996c1f6058445ca398a74743b0e93023 Merge branch 'develop' into sorting_files_2 Conflicts: src/com/owncloud/android/ui/adapter/FileListListAdapter.java --- 2fad1e1a996c1f6058445ca398a74743b0e93023 diff --cc src/com/owncloud/android/ui/adapter/FileListListAdapter.java index ffc2d69f,0d10e7fa..451def0e --- a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java @@@ -17,25 -17,11 +17,17 @@@ */ package com.owncloud.android.ui.adapter; ++ +import java.io.File; - import java.lang.ref.WeakReference; +import java.util.Collections; +import java.util.Comparator; import java.util.Vector; import android.accounts.Account; import android.content.Context; +import android.content.SharedPreferences; - import android.content.res.Resources; import android.graphics.Bitmap; - import android.graphics.Bitmap.CompressFormat; - import android.graphics.BitmapFactory; - import android.graphics.drawable.BitmapDrawable; - import android.graphics.drawable.Drawable; - import android.media.ThumbnailUtils; - import android.os.AsyncTask; +import android.preference.PreferenceManager; - import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@@ -47,17 -33,14 +39,17 @@@ 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.datamodel.ThumbnailsCacheManager; + import com.owncloud.android.datamodel.ThumbnailsCacheManager.AsyncDrawable; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; - import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.ui.activity.ComponentsGetter; - import com.owncloud.android.utils.BitmapUtils; import com.owncloud.android.utils.DisplayUtils; +import com.owncloud.android.utils.FileStorageUtils; + /** @@@ -81,18 -62,7 +71,10 @@@ public class FileListListAdapter extend private FileDataStorageManager mStorageManager; private Account mAccount; private ComponentsGetter mTransferServiceGetter; + private Integer sortOrder; + private Boolean sortAscending; + private SharedPreferences appPreferences; - private final Object thumbnailDiskCacheLock = new Object(); - private DiskLruImageCache mThumbnailCache; - private boolean mThumbnailCacheStarting = true; - private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB - private static final CompressFormat mCompressFormat = CompressFormat.JPEG; - private static final int mCompressQuality = 70; - private Bitmap defaultImg; - public FileListListAdapter( boolean justFolders, Context context, @@@ -102,153 -72,12 +84,20 @@@ mJustFolders = justFolders; mContext = context; mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext); -- mTransferServiceGetter = transferServiceGetter; ++ 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); - - defaultImg = BitmapFactory.decodeResource(mContext.getResources(), - DisplayUtils.getResourceId("image/png", "default.png")); - - // Initialise disk cache on background thread - new InitDiskCacheTask().execute(); - } - - class InitDiskCacheTask extends AsyncTask { - @Override - protected Void doInBackground(File... params) { - synchronized (thumbnailDiskCacheLock) { - try { - mThumbnailCache = new DiskLruImageCache(mContext, "thumbnailCache", - DISK_CACHE_SIZE, mCompressFormat, mCompressQuality); - } catch (Exception e) { - Log_OC.d(TAG, "Thumbnail cache could not be opened ", e); - mThumbnailCache = null; - } - mThumbnailCacheStarting = false; // Finished initialization - thumbnailDiskCacheLock.notifyAll(); // Wake any waiting threads - } - return null; - } - } - - static class AsyncDrawable extends BitmapDrawable { - private final WeakReference bitmapWorkerTaskReference; - - public AsyncDrawable(Resources res, Bitmap bitmap, - ThumbnailGenerationTask bitmapWorkerTask) { - super(res, bitmap); - bitmapWorkerTaskReference = - new WeakReference(bitmapWorkerTask); - } - - public ThumbnailGenerationTask getBitmapWorkerTask() { - return bitmapWorkerTaskReference.get(); - } - } - - class ThumbnailGenerationTask extends AsyncTask { - private final WeakReference imageViewReference; - private OCFile file; - - - public ThumbnailGenerationTask(ImageView imageView) { - // Use a WeakReference to ensure the ImageView can be garbage collected - imageViewReference = new WeakReference(imageView); - } - - // Decode image in background. - @Override - protected Bitmap doInBackground(OCFile... params) { - Bitmap thumbnail = null; - - try { - file = params[0]; - final String imageKey = String.valueOf(file.getRemoteId()); - - // Check disk cache in background thread - thumbnail = getBitmapFromDiskCache(imageKey); - - // Not found in disk cache - if (thumbnail == null || file.needsUpdateThumbnail()) { - // Converts dp to pixel - Resources r = mContext.getResources(); - int px = (int) Math.round(TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, 150, r.getDisplayMetrics() - )); - - if (file.isDown()){ - Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile( - file.getStoragePath(), px, px); - - if (bitmap != null) { - thumbnail = ThumbnailUtils.extractThumbnail(bitmap, px, px); - - // Add thumbnail to cache - addBitmapToCache(imageKey, thumbnail); - - file.setNeedsUpdateThumbnail(false); - mStorageManager.saveFile(file); - } - - } - } - - } catch (Throwable t) { - // the app should never break due to a problem with thumbnails - Log_OC.e(TAG, "Generation of thumbnail for " + file + " failed", t); - if (t instanceof OutOfMemoryError) { - System.gc(); - } - } - - return thumbnail; - } ++ sortAscending = appPreferences.getBoolean("sortAscending", true); - protected void onPostExecute(Bitmap bitmap){ - if (isCancelled()) { - bitmap = null; - } - - if (imageViewReference != null && bitmap != null) { - final ImageView imageView = imageViewReference.get(); - final ThumbnailGenerationTask bitmapWorkerTask = - getBitmapWorkerTask(imageView); - if (this == bitmapWorkerTask && imageView != null) { - imageView.setImageBitmap(bitmap); - } - } - } - } - - public void addBitmapToCache(String key, Bitmap bitmap) { - synchronized (thumbnailDiskCacheLock) { - if (mThumbnailCache != null) { - mThumbnailCache.put(key, bitmap); - } - } + // initialise thumbnails cache on background thread + new ThumbnailsCacheManager.InitDiskCacheTask().execute(); } - - public Bitmap getBitmapFromDiskCache(String key) { - synchronized (thumbnailDiskCacheLock) { - // Wait while disk cache is started from background thread - while (mThumbnailCacheStarting) { - try { - thumbnailDiskCacheLock.wait(); - } catch (InterruptedException e) {} - } - if (mThumbnailCache != null) { - return (Bitmap) mThumbnailCache.getBitmap(key); - } - } - return null; - } - - ++ @Override public boolean areAllItemsEnabled() { return true; @@@ -419,78 -254,7 +279,49 @@@ return view; } - + - public static boolean cancelPotentialWork(OCFile file, ImageView imageView) { - final ThumbnailGenerationTask bitmapWorkerTask = getBitmapWorkerTask(imageView); - - if (bitmapWorkerTask != null) { - final OCFile bitmapData = bitmapWorkerTask.file; - // If bitmapData is not yet set or it differs from the new data - if (bitmapData == null || bitmapData != file) { - // Cancel previous task - bitmapWorkerTask.cancel(true); - } else { - // The same work is already in progress - return false; - } - } - // No task associated with the ImageView, or an existing task was cancelled - return true; - } - - private static ThumbnailGenerationTask getBitmapWorkerTask(ImageView imageView) { - if (imageView != null) { - final Drawable drawable = imageView.getDrawable(); - if (drawable instanceof AsyncDrawable) { - final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; - return asyncDrawable.getBitmapWorkerTask(); - } - } - return null; - } - + /** + * 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;