Make the SearchView get shown when SearchDialog is submitted.

This commit is contained in:
Peter Serwylo 2015-12-08 00:09:08 +11:00
parent bfb90ef655
commit 7b5160df47
3 changed files with 120 additions and 23 deletions

View File

@ -31,7 +31,6 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
@ -45,7 +44,9 @@ import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.fdroid.fdroid.compat.Compatibility;
import org.fdroid.fdroid.compat.TabManager; import org.fdroid.fdroid.compat.TabManager;
import org.fdroid.fdroid.compat.UriCompat;
import org.fdroid.fdroid.data.AppProvider; import org.fdroid.fdroid.data.AppProvider;
import org.fdroid.fdroid.data.NewRepoConfig; import org.fdroid.fdroid.data.NewRepoConfig;
import org.fdroid.fdroid.privileged.install.InstallExtensionDialogActivity; import org.fdroid.fdroid.privileged.install.InstallExtensionDialogActivity;
@ -75,6 +76,9 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
private AppListFragmentPagerAdapter adapter; private AppListFragmentPagerAdapter adapter;
@Nullable
private MenuItem searchMenuItem;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -92,8 +96,9 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
Intent intent = getIntent(); Intent intent = getIntent();
// If the intent can be handled via AppDetails or SearchResults, it // If the intent can be handled via AppDetails it will call finish()
// will call finish() and the rest of the code won't execute // and the rest of the code won't execute. If it looks like a search term,
// this will prompt the SearchView to update itself.
handleIntent(intent); handleIntent(intent);
if (intent.hasExtra(EXTRA_TAB_UPDATE)) { if (intent.hasExtra(EXTRA_TAB_UPDATE)) {
@ -116,6 +121,17 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
// } // }
} }
private void performSearch(String query) {
if (searchMenuItem == null) {
Log.e(TAG, "Tried to search, but search view has not yet been created.");
return;
}
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
MenuItemCompat.expandActionView(searchMenuItem);
searchView.setQuery(query, true);
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@ -124,11 +140,24 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
checkForAddRepoIntent(); checkForAddRepoIntent();
} }
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) { private void handleIntent(Intent intent) {
final Uri data = intent.getData(); final Uri data = intent.getData();
if (data == null) { if (data == null) {
return; return;
} }
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
performSearch(query);
return;
}
final String scheme = data.getScheme(); final String scheme = data.getScheme();
final String path = data.getPath(); final String path = data.getPath();
String appId = null; String appId = null;
@ -140,11 +169,14 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
} }
switch (host) { switch (host) {
case "f-droid.org": case "f-droid.org":
// http://f-droid.org/app/app.id
if (path.startsWith("/repository/browse")) { if (path.startsWith("/repository/browse")) {
// http://f-droid.org/repository/browse?fdfilter=search+query
query = UriCompat.getQueryParameter(data, "fdfilter");
// http://f-droid.org/repository/browse?fdid=app.id // http://f-droid.org/repository/browse?fdid=app.id
appId = data.getQueryParameter("fdid"); appId = UriCompat.getQueryParameter(data, "fdid");
} else if (path.startsWith("/app")) { } else if (path.startsWith("/app")) {
// http://f-droid.org/app/app.id
appId = data.getLastPathSegment(); appId = data.getLastPathSegment();
if ("app".equals(appId)) { if ("app".equals(appId)) {
appId = null; appId = null;
@ -153,28 +185,28 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
break; break;
case "details": case "details":
// market://details?id=app.id // market://details?id=app.id
appId = data.getQueryParameter("id"); appId = UriCompat.getQueryParameter(data, "id");
break; break;
case "search": case "search":
// market://search?q=query // market://search?q=query
query = data.getQueryParameter("q"); query = UriCompat.getQueryParameter(data, "q");
break; break;
case "play.google.com": case "play.google.com":
if (path.startsWith("/store/apps/details")) { if (path.startsWith("/store/apps/details")) {
// http://play.google.com/store/apps/details?id=app.id // http://play.google.com/store/apps/details?id=app.id
appId = data.getQueryParameter("id"); appId = UriCompat.getQueryParameter(data, "id");
} else if (path.startsWith("/store/search")) { } else if (path.startsWith("/store/search")) {
// http://play.google.com/store/search?q=foo // http://play.google.com/store/search?q=foo
query = data.getQueryParameter("q"); query = UriCompat.getQueryParameter(data, "q");
} }
break; break;
case "apps": case "apps":
case "amazon.com": case "amazon.com":
case "www.amazon.com": case "www.amazon.com":
// amzn://apps/android?p=app.id // amzn://apps/android?p=app.id
// http://amazon.com/gp/mas/dl/android?p=app.id // http://amazon.com/gp/mas/dl/android?s=app.id
appId = data.getQueryParameter("p"); appId = UriCompat.getQueryParameter(data, "p");
query = data.getQueryParameter("s"); query = UriCompat.getQueryParameter(data, "s");
break; break;
} }
} else if ("fdroid.app".equals(scheme)) { } else if ("fdroid.app".equals(scheme)) {
@ -182,7 +214,7 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
appId = data.getSchemeSpecificPart(); appId = data.getSchemeSpecificPart();
} else if ("fdroid.search".equals(scheme)) { } else if ("fdroid.search".equals(scheme)) {
// fdroid.search:query // fdroid.search:query
query = data.getSchemeSpecificPart(); query = UriCompat.replacePlusWithSpace(data.getSchemeSpecificPart());
} }
if (!TextUtils.isEmpty(query)) { if (!TextUtils.isEmpty(query)) {
@ -202,10 +234,9 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
call.putExtra(AppDetails.EXTRA_APPID, appId); call.putExtra(AppDetails.EXTRA_APPID, appId);
} else if (!TextUtils.isEmpty(query)) { } else if (!TextUtils.isEmpty(query)) {
Utils.debugLog(TAG, "FDroid launched via search link for '" + query + "'"); Utils.debugLog(TAG, "FDroid launched via search link for '" + query + "'");
call = new Intent(this, SearchResults.class); performSearch(query);
call.setAction(Intent.ACTION_SEARCH);
call.putExtra(SearchManager.QUERY, query);
} }
if (call != null) { if (call != null) {
startActivity(call); startActivity(call);
finish(); finish();
@ -250,8 +281,8 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
} }
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search); searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// LayoutParams.MATCH_PARENT does not work, use a big value instead // LayoutParams.MATCH_PARENT does not work, use a big value instead
searchView.setMaxWidth(1000000); searchView.setMaxWidth(1000000);
@ -374,15 +405,14 @@ public class FDroid extends AppCompatActivity implements SearchView.OnQueryTextL
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
return false; // Do nothing, because we respond to the query being changed as it is updated
// via onQueryTextChange(...)
return true;
} }
private static final String SEARCH_CHANGED = "fdroid.SearchChanged";
private static final String SEARCH_QUERY = "fdroid.SearchQuery";
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
this.adapter.updateSearchQuery(newText, getTabManager().getSelectedIndex()); adapter.updateSearchQuery(newText, getTabManager().getSelectedIndex());
return true; return true;
} }

View File

@ -0,0 +1,25 @@
package org.fdroid.fdroid.compat;
import android.net.Uri;
import android.os.Build;
public class UriCompat {
/**
* Uri#getQueryParameter(String) has the following warning:
*
* > Prior to Ice Cream Sandwich, this decoded the '+' character as '+' rather than ' '.
*/
public static String getQueryParameter(Uri uri, String key) {
String value = uri.getQueryParameter(key);
if (value != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
value = replacePlusWithSpace(value);
}
return value;
}
public static String replacePlusWithSpace(String input) {
return input.replaceAll("\\+", " ");
}
}

View File

@ -0,0 +1,42 @@
#!/bin/sh
function view {
DESCRIPTION=$1
DATA=$2
wait "$DESCRIPTION"
CMD="adb shell am start -a android.intent.action.VIEW -d $DATA"
$CMD
echo ""
sleep 1
}
function wait {
DESCRIPTION=$1
echo "$DESCRIPTION [Y/n]"
read -n 1 RESULT
# Lower case the result.
RESULT=`echo "$RESULT" | tr '[:upper:]' '[:lower:]'`
echo ""
if [ "$RESULT" != 'y' ]; then
exit;
fi
}
APP_TO_SHOW=org.fdroid.fdroid
SEARCH_QUERY=book+reader
view "Search for '$SEARCH_QUERY' (fdroid web)" http://f-droid.org/repository/browse?fdfilter=$SEARCH_QUERY
view "Search for '$SEARCH_QUERY' (market)" market://search?q=$SEARCH_QUERY
view "Search for '$SEARCH_QUERY' (play)" http://play.google.com/store/search?q=$SEARCH_QUERY
view "Search for '$SEARCH_QUERY' (amazon)" http://amazon.com/gp/mas/dl/android?s=$SEARCH_QUERY
view "Search for '$SEARCH_QUERY' (fdroid)" fdroid.search:$SEARCH_QUERY
view "View '$APP_TO_SHOW' (fdroid web fdid)" http://f-droid.org/repository/browse?fdid=$APP_TO_SHOW
view "View '$APP_TO_SHOW' (fdroid web /app/ path)" http://f-droid.org/app/$APP_TO_SHOW
view "View '$APP_TO_SHOW' (market)" market://details?id=$APP_TO_SHOW
view "View '$APP_TO_SHOW' (play)" http://play.google.com/store/apps/details?id=$APP_TO_SHOW
view "View '$APP_TO_SHOW' (amazon)" amzn://apps/android?p=$APP_TO_SHOW
view "View '$APP_TO_SHOW' (fdroid)" fdroid.app:$APP_TO_SHOW