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

Commit 1a23b92d 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
parent 450f3d55
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) {
                        Log.e(TAG, "onAsyncEnd method fails", ex); //$NON-NLS-1$
                    }
@@ -271,7 +267,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
@@ -289,24 +284,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));
                }
            });
        }

        /**
@@ -837,7 +814,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);
                }
            }
        });
@@ -880,14 +857,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));
            }
        }
    }