From 50d76124493e6a971e109742c8e79d773650ec69 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 11 Jan 2022 10:29:18 +0100 Subject: [PATCH] Get option search suggestions in the background --- .../eu/faircode/email/FragmentOptions.java | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptions.java b/app/src/main/java/eu/faircode/email/FragmentOptions.java index 617b29ae5b..d3829c7aaa 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptions.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptions.java @@ -21,7 +21,6 @@ package eu.faircode.email; import android.Manifest; import android.app.Activity; -import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -62,12 +61,16 @@ import com.google.android.material.tabs.TabLayout; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.concurrent.ExecutorService; public class FragmentOptions extends FragmentBase { private ViewPager pager; private PagerAdapter adapter; private String searching = null; + private final ExecutorService executor = + Helper.getBackgroundExecutor(1, "suggest"); + private static final int[] TAB_PAGES = { R.layout.fragment_setup, R.layout.fragment_options_synchronize, @@ -321,8 +324,7 @@ public class FragmentOptions extends FragmentBase { searchView.setOnSuggestionListener(onSuggestionListener); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { - private String[] titles = null; - private View[] views = null; + private SuggestData data = null; @Override public boolean onQueryTextSubmit(String query) { @@ -343,22 +345,46 @@ public class FragmentOptions extends FragmentBase { } private void suggest(String query) { + if (data == null) + new SimpleTask() { + @Override + protected SuggestData onExecute(Context context, Bundle args) throws Throwable { + SuggestData data = new SuggestData(); + data.titles = new String[TAB_PAGES.length]; + data.views = new View[TAB_PAGES.length]; + + LayoutInflater inflater = LayoutInflater.from(context); + for (int tab = 0; tab < TAB_PAGES.length; tab++) { + data.titles[tab] = getString(PAGE_TITLES[tab]); + data.views[tab] = inflater.inflate(TAB_PAGES[tab], null); + } + + return data; + } + + @Override + protected void onExecuted(Bundle args, SuggestData result) { + data = result; + _suggest(query); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }.setExecutor(executor) + .execute(FragmentOptions.this, new Bundle(), "option:suggest"); + else + _suggest(query); + } + + private void _suggest(String query) { MatrixCursor cursor = new MatrixCursor(new String[]{"_id", "tab", "resid", "title"}); if (query != null && query.length() > 1) { - if (titles == null || views == null) { - titles = new String[TAB_PAGES.length]; - views = new View[TAB_PAGES.length]; - LayoutInflater inflater = LayoutInflater.from(searchView.getContext()); - for (int tab = 0; tab < TAB_PAGES.length; tab++) { - titles[tab] = getString(PAGE_TITLES[tab]); - views[tab] = inflater.inflate(TAB_PAGES[tab], null); - } - } - int id = 0; for (int tab = 0; tab < TAB_PAGES.length; tab++) - id = getSuggestions(query.toLowerCase(), id, tab, titles[tab], views[tab], cursor); + id = getSuggestions(query.toLowerCase(), id, tab, data.titles[tab], data.views[tab], cursor); } searchView.setSuggestionsAdapter(new SimpleCursorAdapter( @@ -504,4 +530,9 @@ public class FragmentOptions extends FragmentBase { return POSITION_NONE; // always recreate fragment } } + + private static class SuggestData { + private String[] titles; + private View[] views; + } }