Эх сурвалжийг харах

bugfix for #798
checking cast if remote request is long running
preventing MissingActivityException if fragment is detached from Activity

tobiasKaminsky 8 жил өмнө
parent
commit
1a6b14c0ff

+ 18 - 4
src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -60,11 +60,13 @@ import com.owncloud.android.ui.events.ChangeMenuEvent;
 import com.owncloud.android.ui.events.DummyDrawerEvent;
 import com.owncloud.android.ui.events.MenuItemClickEvent;
 import com.owncloud.android.ui.events.SearchEvent;
+import com.owncloud.android.ui.fragment.OCFileListFragment;
 import com.owncloud.android.utils.DisplayUtils;
 
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
+import org.parceler.Parcels;
 
 /**
  * Base class to handle setup of the drawer implementation including user switching and avatar fetching and fallback
@@ -382,12 +384,24 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU
             case R.id.nav_favorites:
                 menuItem.setChecked(true);
                 mCheckedMenuItem = menuItem.getItemId();
-                EventBus.getDefault().post(new SearchEvent("", SearchOperation.SearchType.FAVORITE_SEARCH,
-                        SearchEvent.UnsetType.NO_UNSET));
+
+                Intent favoriteIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
+                favoriteIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(new SearchEvent("",
+                        SearchOperation.SearchType.FAVORITE_SEARCH, SearchEvent.UnsetType.NO_UNSET)));
+                favoriteIntent.putExtra(FileDisplayActivity.DRAWER_MENU_ID, menuItem.getItemId());
+                favoriteIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                startActivity(favoriteIntent);
                 break;
             case R.id.nav_photos:
-                EventBus.getDefault().post(new SearchEvent("image/%",
-                        SearchOperation.SearchType.CONTENT_TYPE_SEARCH, SearchEvent.UnsetType.NO_UNSET));
+                menuItem.setChecked(true);
+                mCheckedMenuItem = menuItem.getItemId();
+
+                Intent photoIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
+                photoIntent.putExtra(OCFileListFragment.SEARCH_EVENT, Parcels.wrap(new SearchEvent("image/%",
+                        SearchOperation.SearchType.CONTENT_TYPE_SEARCH, SearchEvent.UnsetType.NO_UNSET)));
+                photoIntent.putExtra(FileDisplayActivity.DRAWER_MENU_ID, menuItem.getItemId());
+                photoIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                startActivity(photoIntent);
                 break;
             case R.id.nav_on_device:
                 menuItem.setChecked(true);

+ 26 - 5
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -131,6 +131,8 @@ public class FileDisplayActivity extends HookActivity
 
     public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS";
 
+    public static final String DRAWER_MENU_ID = "DRAWER_MENU_ID";
+
     public static final int REQUEST_CODE__SELECT_CONTENT_FROM_APPS = REQUEST_CODE__LAST_SHARED + 1;
     public static final int REQUEST_CODE__SELECT_FILES_FROM_FILE_SYSTEM = REQUEST_CODE__LAST_SHARED + 2;
     public static final int REQUEST_CODE__MOVE_FILES = REQUEST_CODE__LAST_SHARED + 3;
@@ -170,10 +172,10 @@ public class FileDisplayActivity extends HookActivity
 
         /// Load of saved instance state
         if (savedInstanceState != null) {
-            mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(
+            mWaitingToPreview = savedInstanceState.getParcelable(
                     FileDisplayActivity.KEY_WAITING_TO_PREVIEW);
             mSyncInProgress = savedInstanceState.getBoolean(KEY_SYNC_IN_PROGRESS);
-            mWaitingToSend = (OCFile) savedInstanceState.getParcelable(
+            mWaitingToSend = savedInstanceState.getParcelable(
                     FileDisplayActivity.KEY_WAITING_TO_SEND);
             searchQuery = savedInstanceState.getString(KEY_SEARCH_QUERY);
         } else {
@@ -245,12 +247,18 @@ public class FileDisplayActivity extends HookActivity
             }
         }
 
-        if (savedInstanceState == null) {
+        if (getIntent().getParcelableExtra(OCFileListFragment.SEARCH_EVENT) != null){
+            switchToSearchFragment();
+
+            int menuId = getIntent().getIntExtra(DRAWER_MENU_ID, -1);
+            if (menuId != -1){
+                setupDrawer(menuId);
+            }
+        } else if (savedInstanceState == null) {
             createMinFragments();
+            refreshList(true);
         }
 
-        refreshList(true);
-
         setIndeterminate(mSyncInProgress);
         // always AFTER setContentView(...) in onCreate(); to work around bug in its implementation
 
@@ -377,6 +385,19 @@ public class FileDisplayActivity extends HookActivity
         }
     }
 
+    private void switchToSearchFragment() {
+        OCFileListFragment listOfFiles = new OCFileListFragment();
+        Bundle args = new Bundle();
+
+        args.putParcelable(OCFileListFragment.SEARCH_EVENT,
+                getIntent().getParcelableExtra(OCFileListFragment.SEARCH_EVENT));
+
+        listOfFiles.setArguments(args);
+        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
+        transaction.add(R.id.left_fragment_container, listOfFiles, TAG_LIST_OF_FILES);
+        transaction.commit();
+    }
+
     private void createMinFragments() {
         OCFileListFragment listOfFiles = new OCFileListFragment();
         Bundle args = new Bundle();

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

@@ -109,6 +109,17 @@ public class FileListListAdapter extends BaseAdapter {
         new ThumbnailsCacheManager.InitDiskCacheTask().execute();
     }
 
+    public FileListListAdapter(
+            boolean justFolders,
+            Context context,
+            ComponentsGetter transferServiceGetter,
+            OCFileListFragmentInterface OCFileListFragmentInterface,
+            FileDataStorageManager fileDataStorageManager
+    ) {
+    this(justFolders, context, transferServiceGetter, OCFileListFragmentInterface);
+        mStorageManager = fileDataStorageManager;
+    }
+
     @Override
     public boolean areAllItemsEnabled() {
         return true;
@@ -467,21 +478,24 @@ public class FileListListAdapter extends BaseAdapter {
         if (searchType.equals(ExtendedListFragment.SearchType.SHARED_FILTER)) {
             ArrayList<OCShare> shares = new ArrayList<>();
             for (int i = 0; i < objects.size(); i++) {
-                shares.add((OCShare) objects.get(i));
+                // check type before cast as of long running data fetch it is possible that old result is filled
+                if (objects.get(i) instanceof OCShare) {
+                    OCShare ocShare = (OCShare) objects.get(i);
+
+                    shares.add(ocShare);
+
+                    OCFile ocFile = mStorageManager.getFileByPath(ocShare.getPath());
+                    if (!mFiles.contains(ocFile)) {
+                        mFiles.add(ocFile);
+                    }
+                }
             }
             mStorageManager.saveShares(shares);
-        }
-        for (int i = 0; i < objects.size(); i++) {
-            if (!searchType.equals(ExtendedListFragment.SearchType.SHARED_FILTER)) {
+        } else {
+            for (int i = 0; i < objects.size(); i++) {
                 OCFile ocFile = FileStorageUtils.fillOCFile((RemoteFile) objects.get(i));
                 searchForLocalFileInDefaultPath(ocFile);
                 mFiles.add(ocFile);
-            } else {
-                OCShare ocShare = (OCShare) objects.get(i);
-                OCFile ocFile = mStorageManager.getFileByPath(ocShare.getPath());
-                if (!mFiles.contains(ocFile)) {
-                    mFiles.add(ocFile);
-                }
             }
         }
 

+ 11 - 4
src/main/java/com/owncloud/android/ui/events/SearchEvent.java

@@ -21,20 +21,27 @@ package com.owncloud.android.ui.events;
 
 import com.owncloud.android.lib.resources.files.SearchOperation;
 
+import org.parceler.Parcel;
+
 /**
  * Search event
  */
+@Parcel
 public class SearchEvent {
-    public final String searchQuery;
+    public String searchQuery;
 
-    public final SearchOperation.SearchType searchType;
+    public SearchOperation.SearchType searchType;
 
-    public final UnsetType unsetType;
+    public UnsetType unsetType;
 
     public enum UnsetType {
         NO_UNSET,
         UNSET_DRAWER,
-        UNSET_BOTTOM_NAV_BAR;
+        UNSET_BOTTOM_NAV_BAR
+    }
+
+    public SearchEvent() {
+
     }
 
     public SearchEvent(String searchQuery, SearchOperation.SearchType searchType, UnsetType unsetType) {

+ 105 - 93
src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -28,6 +28,7 @@ import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.os.AsyncTask;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
@@ -61,6 +62,7 @@ import com.owncloud.android.files.FileMenuFilter;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
+import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.resources.files.SearchOperation;
@@ -116,6 +118,8 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
     public final static String ARG_ALLOW_CONTEXTUAL_ACTIONS = MY_PACKAGE + ".ALLOW_CONTEXTUAL";
     public final static String ARG_HIDE_FAB = MY_PACKAGE + ".HIDE_FAB";
 
+    public static final String SEARCH_EVENT = "SEARCH_EVENT";
+
     private static final String KEY_FILE = MY_PACKAGE + ".extra.FILE";
     private static final String KEY_FAB_EVER_CLICKED = "FAB_EVER_CLICKED";
 
@@ -146,6 +150,7 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
     private BottomNavigationView bottomNavigationView;
 
     private SearchType currentSearchType;
+    private boolean searchFragment = false;
 
     private enum MenuItemAddRemove {
         DO_NOTHING, REMOVE_SORT, REMOVE_GRID_AND_SORT, ADD_SORT, ADD_GRID_AND_SORT, ADD_GRID_AND_SORT_WITH_SEARCH,
@@ -310,7 +315,8 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
                 mJustFolders,
                 getActivity(),
                 mContainerActivity,
-                this
+                this,
+                mContainerActivity.getStorageManager()
         );
         setListAdapter(mAdapter);
 
@@ -334,6 +340,11 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
                 removeFabLabels();
             }
         }
+
+        SearchEvent searchEvent = Parcels.unwrap(getArguments().getParcelable(OCFileListFragment.SEARCH_EVENT));
+        if (searchEvent != null){
+            onMessageEvent(searchEvent);
+        }
     }
 
     /**
@@ -940,6 +951,7 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
     }
 
     public void refreshDirectory() {
+        searchFragment = false;
         listDirectory(getCurrentFile(), MainApp.isOnlyOnDevice(), false);
     }
 
@@ -951,56 +963,58 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
      * @param directory File to be listed
      */
     public void listDirectory(OCFile directory, boolean onlyOnDevice, boolean fromSearch) {
-        FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
-        if (storageManager != null) {
+        if (!searchFragment) {
+            FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
+            if (storageManager != null) {
 
-            // Check input parameters for null
-            if (directory == null) {
-                if (mFile != null) {
-                    directory = mFile;
-                } else {
-                    directory = storageManager.getFileByPath("/");
-                    if (directory == null) {
-                        return; // no files, wait for sync
+                // Check input parameters for null
+                if (directory == null) {
+                    if (mFile != null) {
+                        directory = mFile;
+                    } else {
+                        directory = storageManager.getFileByPath("/");
+                        if (directory == null) {
+                            return; // no files, wait for sync
+                        }
                     }
                 }
-            }
 
 
-            // If that's not a directory -> List its parent
-            if (!directory.isFolder()) {
-                Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
-                directory = storageManager.getFileById(directory.getParentId());
-            }
+                // If that's not a directory -> List its parent
+                if (!directory.isFolder()) {
+                    Log_OC.w(TAG, "You see, that is not a directory -> " + directory.toString());
+                    directory = storageManager.getFileById(directory.getParentId());
+                }
 
 
-            if (searchView != null && !searchView.isIconified() && !fromSearch) {
+                if (searchView != null && !searchView.isIconified() && !fromSearch) {
 
-                searchView.post(new Runnable() {
-                    @Override
-                    public void run() {
-                        searchView.setQuery("", false);
-                        searchView.onActionViewCollapsed();
-                        Activity activity;
-                        if ((activity = getActivity()) != null && activity instanceof FileDisplayActivity) {
-                            FileDisplayActivity fileDisplayActivity = (FileDisplayActivity) activity;
-                            if (getCurrentFile() != null) {
-                                fileDisplayActivity.setDrawerIndicatorEnabled(fileDisplayActivity.isRoot(getCurrentFile()));
+                    searchView.post(new Runnable() {
+                        @Override
+                        public void run() {
+                            searchView.setQuery("", false);
+                            searchView.onActionViewCollapsed();
+                            Activity activity;
+                            if ((activity = getActivity()) != null && activity instanceof FileDisplayActivity) {
+                                FileDisplayActivity fileDisplayActivity = (FileDisplayActivity) activity;
+                                if (getCurrentFile() != null) {
+                                    fileDisplayActivity.setDrawerIndicatorEnabled(fileDisplayActivity.isRoot(getCurrentFile()));
+                                }
                             }
-                        }
 
-                    }
-                });
-            }
+                        }
+                    });
+                }
 
-            mAdapter.swapDirectory(directory, storageManager, onlyOnDevice);
-            if (mFile == null || !mFile.equals(directory)) {
-                mCurrentListView.setSelection(0);
-            }
-            mFile = directory;
+                mAdapter.swapDirectory(directory, storageManager, onlyOnDevice);
+                if (mFile == null || !mFile.equals(directory)) {
+                    mCurrentListView.setSelection(0);
+                }
+                mFile = directory;
 
-            updateLayout();
+                updateLayout();
 
+            }
         }
     }
 
@@ -1047,42 +1061,46 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
     }
 
     private String generateFooterText(int filesCount, int foldersCount) {
-        String output;
-        if (filesCount <= 0) {
-            if (foldersCount <= 0) {
-                output = "";
+        String output = "";
+
+        if (getActivity() != null) {
+            if (filesCount <= 0) {
+                if (foldersCount <= 0) {
+                    output = "";
 
-            } else if (foldersCount == 1) {
-                output = getResources().getString(R.string.file_list__footer__folder);
+                } else if (foldersCount == 1) {
+                    output = getResources().getString(R.string.file_list__footer__folder);
 
-            } else { // foldersCount > 1
-                output = getResources().getString(R.string.file_list__footer__folders, foldersCount);
-            }
+                } else { // foldersCount > 1
+                    output = getResources().getString(R.string.file_list__footer__folders, foldersCount);
+                }
 
-        } else if (filesCount == 1) {
-            if (foldersCount <= 0) {
-                output = getResources().getString(R.string.file_list__footer__file);
+            } else if (filesCount == 1) {
+                if (foldersCount <= 0) {
+                    output = getResources().getString(R.string.file_list__footer__file);
 
-            } else if (foldersCount == 1) {
-                output = getResources().getString(R.string.file_list__footer__file_and_folder);
+                } else if (foldersCount == 1) {
+                    output = getResources().getString(R.string.file_list__footer__file_and_folder);
 
-            } else { // foldersCount > 1
-                output = getResources().getString(R.string.file_list__footer__file_and_folders, foldersCount);
-            }
-        } else {    // filesCount > 1
-            if (foldersCount <= 0) {
-                output = getResources().getString(R.string.file_list__footer__files, filesCount);
+                } else { // foldersCount > 1
+                    output = getResources().getString(R.string.file_list__footer__file_and_folders, foldersCount);
+                }
+            } else {    // filesCount > 1
+                if (foldersCount <= 0) {
+                    output = getResources().getString(R.string.file_list__footer__files, filesCount);
 
-            } else if (foldersCount == 1) {
-                output = getResources().getString(R.string.file_list__footer__files_and_folder, filesCount);
+                } else if (foldersCount == 1) {
+                    output = getResources().getString(R.string.file_list__footer__files_and_folder, filesCount);
 
-            } else { // foldersCount > 1
-                output = getResources().getString(
-                        R.string.file_list__footer__files_and_folders, filesCount, foldersCount
-                );
+                } else { // foldersCount > 1
+                    output = getResources().getString(
+                            R.string.file_list__footer__files_and_folders, filesCount, foldersCount
+                    );
 
+                }
             }
         }
+
         return output;
     }
 
@@ -1245,6 +1263,7 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
 
     @Subscribe(threadMode = ThreadMode.BACKGROUND)
     public void onMessageEvent(SearchEvent event) {
+        searchFragment = true;
         setEmptyListLoadingMessage();
         mAdapter.setData(new ArrayList<>(), SearchType.NO_SEARCH);
 
@@ -1284,29 +1303,34 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
             }
         };
 
-        Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
+        final RemoteOperation remoteOperation;
+        if (!currentSearchType.equals(SearchType.SHARED_FILTER)) {
+            remoteOperation = new SearchOperation(event.getSearchQuery(), event.getSearchType());
+        } else {
+            remoteOperation = new GetRemoteSharesOperation();
+        }
 
-        try {
-            OwnCloudAccount ocAccount = new OwnCloudAccount(
-                    currentAccount,
-                    MainApp.getAppContext()
-            );
+        final Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext());
+
+        AsyncTask task = new AsyncTask() {
+            @Override
+            protected Object doInBackground(Object[] params) {
+                RemoteOperationResult remoteOperationResult = remoteOperation.execute(currentAccount, getContext());
 
-            OwnCloudClient mClient = OwnCloudClientManagerFactory.getDefaultSingleton().
-                    getClientFor(ocAccount, MainApp.getAppContext());
-            if (!currentSearchType.equals(SearchType.SHARED_FILTER)) {
-                SearchOperation operation = new SearchOperation(event.getSearchQuery(), event.getSearchType());
-                RemoteOperationResult remoteOperationResult = operation.execute(mClient);
-                if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null) {
-                    mAdapter.setData(remoteOperationResult.getData(), currentSearchType);
-                }
-            } else {
-                GetRemoteSharesOperation operation = new GetRemoteSharesOperation();
-                RemoteOperationResult remoteOperationResult = operation.execute(mClient);
                 if (remoteOperationResult.isSuccess() && remoteOperationResult.getData() != null) {
                     mAdapter.setData(remoteOperationResult.getData(), currentSearchType);
                 }
+
+                return remoteOperationResult.isSuccess();
+            }
+
+            @Override
+            protected void onPostExecute(Object o) {
+                mAdapter.notifyDataSetChanged();
             }
+        };
+
+        task.execute(true);
 
             if (event.getSearchType().equals(SearchOperation.SearchType.FILE_SEARCH)) {
                 setEmptyListMessage(SearchType.FILE_SEARCH);
@@ -1350,18 +1374,6 @@ public class OCFileListFragment extends ExtendedListFragment implements OCFileLi
             } else {
                 new Handler(Looper.getMainLooper()).post(switchViewsRunnable);
             }
-
-        } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
-            Log_OC.e(TAG, "Account not found", e);
-        } catch (AuthenticatorException e) {
-            Log_OC.e(TAG, "Authentication failed", e);
-        } catch (IOException e) {
-            Log_OC.e(TAG, "IO error", e);
-        } catch (OperationCanceledException e) {
-            Log_OC.e(TAG, "Operation has been canceled", e);
-        }
-
-
     }
 
     @Override