Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 18ab0647 authored by Stephen Bird's avatar Stephen Bird
Browse files

Search: Fix race condition with results display

Since results are now run through an asynctask before they are added
this created a race condition which lead to the screen sometimes
showing 'no results found' when results are actually available

Ticket: QRDL-995
Change-Id: I5b7ddb44c16603fac7ceca3f955038cc3d646de8
(cherry picked from commit 1a23b92d)
parent 7ad3eb4c
Loading
Loading
Loading
Loading
+10 −26
Original line number Diff line number Diff line
@@ -249,14 +249,10 @@ public class SearchActivity extends Activity
                    try {
                        mExecutable = null;
                        mAdapter.stopStreaming();
                        int resultsSize = mAdapter.resultsSize();
                        mStreamingSearchProgress.setVisibility(View.INVISIBLE);
                        if (mMimeTypeCategories != null && mMimeTypeCategories.size() > 1) {
                            mMimeTypeSpinner.setVisibility(View.VISIBLE);
                        }
                        mSearchListView.setVisibility(resultsSize > 0 ? View.VISIBLE : View.GONE);
                        mEmptyListMsg.setVisibility(resultsSize > 0 ? View.GONE : View.VISIBLE);

                    } catch (Throwable ex) {
                        // hide the search progress spinner if the search fails
                        mStreamingSearchProgress.setVisibility(View.INVISIBLE);
@@ -273,7 +269,6 @@ public class SearchActivity extends Activity
        @SuppressWarnings("unchecked")
        public void onConcurrentPartialResult(final Object partialResults) {
            //Saved in the global result list, for save at the end
            FileSystemObject result = null;
            if (partialResults instanceof FileSystemObject) {
                FileSystemObject fso = (FileSystemObject) partialResults;
                if (mMimeTypeCategories == null || mMimeTypeCategories.contains(MimeTypeHelper
@@ -291,24 +286,6 @@ public class SearchActivity extends Activity
                    }
                }
            }

            //Notify progress
            mSearchListView.post(new Runnable() {
                @Override
                public void run() {
                    int progress = mAdapter.resultsSize();
                    String foundItems =
                            getResources().
                                    getQuantityString(
                                            R.plurals.search_found_items, progress,
                                            Integer.valueOf(progress) );
                    mSearchFoundItems.setText(
                            getString(
                                    R.string.search_found_items_in_directory,
                                    foundItems,
                                    mSearchDirectory));
                }
            });
        }

        /**
@@ -839,7 +816,7 @@ public class SearchActivity extends Activity
                    DialogHelper.showToast(
                            SearchActivity.this,
                            R.string.search_error_msg, Toast.LENGTH_SHORT);
                    SearchActivity.this.mSearchListView.setVisibility(View.GONE);
                    toggleResults(false, true);
                }
            }
        });
@@ -882,14 +859,21 @@ public class SearchActivity extends Activity
        }

        @Override
        protected void onPostExecute(Boolean sucess) {
        protected void onPostExecute(Boolean success) {
            SearchActivity activity = mActivity.get();
            if (activity == null) {
                return;
            }
            if (sucess) {
            if (success) {
                // add to adapter
                activity.mAdapter.addNewItem(mHolder);
                int progress = activity.mAdapter.resultsSize();
                activity.toggleResults(progress > 0, false);
                String foundItems = activity.getResources().getQuantityString(
                        R.plurals.search_found_items, progress, progress);
                activity.mSearchFoundItems.setText(activity.getString(
                        R.string.search_found_items_in_directory,
                        foundItems, activity.mSearchDirectory));
            }
        }
    }