Browse Source

prevent crash when fetching search event while rotating

tobiasKaminsky 8 years ago
parent
commit
9c1339b1e4

+ 2 - 2
src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -189,7 +189,7 @@ public class ThumbnailsCacheManager {
 
 
         public ThumbnailGenerationTask(ImageView imageView, FileDataStorageManager storageManager,
-                                       Account account) {
+                                       Account account) throws IllegalArgumentException {
             // Use a WeakReference to ensure the ImageView can be garbage collected
             mImageViewReference = new WeakReference<ImageView>(imageView);
             if (storageManager == null) {
@@ -577,7 +577,7 @@ public class ThumbnailsCacheManager {
         }
 
         private Bitmap doAvatarInBackground() {
-            String username = (String) mUsername;
+            String username = mUsername;
 
             final String imageKey = "a_" + username;
 

+ 24 - 17
src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java

@@ -49,6 +49,7 @@ import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.db.PreferenceManager;
 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.lib.resources.files.RemoteFile;
 import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
@@ -83,6 +84,7 @@ public class FileListListAdapter extends BaseAdapter {
 
     private FilesFilter mFilesFilter;
     private OCFile currentDirectory;
+    private static final String TAG = FileListListAdapter.class.getSimpleName();
 
     public FileListListAdapter(
             boolean justFolders,
@@ -365,25 +367,30 @@ public class FileListListAdapter extends BaseAdapter {
                     } else {
                         // generate new Thumbnail
                         if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, fileIcon)) {
-                            final ThumbnailsCacheManager.ThumbnailGenerationTask task =
-                                    new ThumbnailsCacheManager.ThumbnailGenerationTask(
-                                            fileIcon, mStorageManager, mAccount
-                                    );
-                            if (thumbnail == null) {
-                                if (MimeTypeUtil.isVideo(file)) {
-                                    thumbnail = ThumbnailsCacheManager.mDefaultVideo;
-                                } else {
-                                    thumbnail = ThumbnailsCacheManager.mDefaultImg;
+                            try {
+                                final ThumbnailsCacheManager.ThumbnailGenerationTask task =
+                                        new ThumbnailsCacheManager.ThumbnailGenerationTask(
+                                                fileIcon, mStorageManager, mAccount
+                                        );
+
+                                if (thumbnail == null) {
+                                    if (MimeTypeUtil.isVideo(file)) {
+                                        thumbnail = ThumbnailsCacheManager.mDefaultVideo;
+                                    } else {
+                                        thumbnail = ThumbnailsCacheManager.mDefaultImg;
+                                    }
                                 }
+                                final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
+                                        new ThumbnailsCacheManager.AsyncThumbnailDrawable(
+                                                mContext.getResources(),
+                                                thumbnail,
+                                                task
+                                        );
+                                fileIcon.setImageDrawable(asyncDrawable);
+                                task.execute(file);
+                            } catch (IllegalArgumentException e) {
+                                Log_OC.d(TAG, "ThumbnailGenerationTask : " + e.getMessage());
                             }
-                            final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
-                                    new ThumbnailsCacheManager.AsyncThumbnailDrawable(
-                                            mContext.getResources(),
-                                            thumbnail,
-                                            task
-                                    );
-                            fileIcon.setImageDrawable(asyncDrawable);
-                            task.execute(file);
                         }
                     }
 

+ 54 - 10
src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -35,6 +35,7 @@ import android.os.Handler;
 import android.os.Looper;
 import android.preference.PreferenceManager;
 import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
 import android.support.design.widget.BottomNavigationView;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v4.widget.SwipeRefreshLayout;
@@ -151,6 +152,8 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
 
     private SearchType currentSearchType;
     private boolean searchFragment = false;
+    private SearchEvent searchEvent;
+    private AsyncTask remoteOperationAsyncTask;
 
     private enum MenuItemAddRemove {
         DO_NOTHING, REMOVE_SORT, REMOVE_GRID_AND_SORT, ADD_SORT, ADD_GRID_AND_SORT, ADD_GRID_AND_SORT_WITH_SEARCH,
@@ -170,6 +173,7 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
         mProgressBarActionModeColor = getResources().getColor(R.color.action_mode_background);
         mProgressBarColor = getResources().getColor(R.color.primary);
         mMultiChoiceModeListener = new MultiChoiceModeListener();
+        searchFragment = false;
     }
 
     /**
@@ -282,12 +286,20 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
     @Override
     public void onResume() {
         super.onResume();
+
+        if (remoteOperationAsyncTask != null) {
+            remoteOperationAsyncTask.cancel(true);
+        }
     }
 
     @Override
     public void onDetach() {
         setOnRefreshListener(null);
         mContainerActivity = null;
+
+        if (remoteOperationAsyncTask != null) {
+            remoteOperationAsyncTask.cancel(true);
+        }
         super.onDetach();
     }
 
@@ -341,8 +353,8 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
             }
         }
 
-        SearchEvent searchEvent = Parcels.unwrap(getArguments().getParcelable(OCFileListFragment.SEARCH_EVENT));
-        if (searchEvent != null){
+        searchEvent = Parcels.unwrap(getArguments().getParcelable(OCFileListFragment.SEARCH_EVENT));
+        if (searchEvent != null) {
             onMessageEvent(searchEvent);
         }
     }
@@ -670,6 +682,26 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
         ((FileActivity) getActivity()).addDrawerListener(mMultiChoiceModeListener);
     }
 
+    @Override
+    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
+        super.onViewStateRestored(savedInstanceState);
+
+        if (savedInstanceState != null) {
+            mAdapter = new FileListListAdapter(
+                    mJustFolders,
+                    getActivity(),
+                    mContainerActivity,
+                    this,
+                    mContainerActivity.getStorageManager());
+
+            searchEvent = Parcels.unwrap(savedInstanceState.getParcelable(SEARCH_EVENT));
+        }
+
+        if (searchEvent != null) {
+            onMessageEvent(searchEvent);
+        }
+    }
+
     /**
      * Saves the current listed folder.
      */
@@ -952,6 +984,11 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
 
     public void refreshDirectory() {
         searchFragment = false;
+
+        if (remoteOperationAsyncTask != null) {
+            remoteOperationAsyncTask.cancel(true);
+        }
+
         listDirectory(getCurrentFile(), MainApp.isOnlyOnDevice(), false);
     }
 
@@ -1312,25 +1349,32 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
 
         final Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
 
-        AsyncTask task = new AsyncTask() {
+        remoteOperationAsyncTask = new AsyncTask() {
             @Override
             protected Object doInBackground(Object[] params) {
-                RemoteOperationResult remoteOperationResult = remoteOperation.execute(currentAccount, getContext());
+                if (getContext() != null && !isCancelled()) {
+                    RemoteOperationResult remoteOperationResult = remoteOperation.execute(currentAccount, getContext());
 
-                if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null) {
-                    mAdapter.setData(remoteOperationResult.getData(), currentSearchType);
-                }
+                    if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null
+                            && !isCancelled() && searchFragment) {
+                        mAdapter.setData(remoteOperationResult.getData(), currentSearchType);
+                    }
 
-                return remoteOperationResult.isSuccess();
+                    return remoteOperationResult.isSuccess();
+                } else {
+                    return false;
+                }
             }
 
             @Override
             protected void onPostExecute(Object o) {
-                mAdapter.notifyDataSetChanged();
+                if (!isCancelled()) {
+                    mAdapter.notifyDataSetChanged();
+                }
             }
         };
 
-        task.execute(true);
+        remoteOperationAsyncTask.execute(true);
 
         if (event.getSearchType().equals(SearchOperation.SearchType.FILE_SEARCH)) {
             setEmptyListMessage(SearchType.FILE_SEARCH);