Ported 'View App' and 'Search' intents from FDroid to MainActivity

This commit is contained in:
Peter Serwylo 2017-02-21 11:26:54 +11:00
parent b8de545179
commit 3bc97d931b
3 changed files with 248 additions and 121 deletions

View File

@ -136,125 +136,6 @@
android:windowSoftInputMode="adjustResize"
android:configChanges="layoutDirection|locale|keyboardHidden|orientation|screenSize" >
<!-- App URLs -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="fdroid.app" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="f-droid.org" />
<data android:host="www.f-droid.org" />
<data android:pathPrefix="/app/" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="f-droid.org" />
<data android:host="www.f-droid.org" />
<data android:pathPrefix="/repository/browse" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="market" android:host="details" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="play.google.com" /> <!-- they don't do www. -->
<data android:path="/store/apps/details" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="amzn" android:host="apps" android:path="/android" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="amazon.com" />
<data android:host="www.amazon.com" />
<data android:path="/gp/mas/dl/android" />
</intent-filter>
<!-- Search URLs -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="fdroid.search" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="market" android:host="search" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="play.google.com" /> <!-- they don't do www. -->
<data android:path="/store/search" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<!-- Handle NFC tags detected from outside our application -->
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
@ -506,6 +387,129 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- App URLs -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="fdroid.app" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="f-droid.org" />
<data android:host="www.f-droid.org" />
<data android:pathPrefix="/app/" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="f-droid.org" />
<data android:host="www.f-droid.org" />
<data android:pathPrefix="/repository/browse" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="market" android:host="details" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="play.google.com" /> <!-- they don't do www. -->
<data android:path="/store/apps/details" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="amzn" android:host="apps" android:path="/android" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="amazon.com" />
<data android:host="www.amazon.com" />
<data android:path="/gp/mas/dl/android" />
</intent-filter>
<!-- Search URLs -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="fdroid.search" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="market" android:host="search" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="play.google.com" /> <!-- they don't do www. -->
<data android:path="/store/search" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity android:name=".views.apps.AppListActivity" />

View File

@ -25,6 +25,7 @@ import org.fdroid.fdroid.data.Schema;
public class AppListActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, CategoryTextWatcher.SearchTermsChangedListener {
public static final String EXTRA_CATEGORY = "org.fdroid.fdroid.views.apps.AppListActivity.EXTRA_CATEGORY";
public static final String EXTRA_SEARCH_TERMS = "org.fdroid.fdroid.views.apps.AppListActivity.EXTRA_SEARCH_TERMS";
private RecyclerView appView;
private AppListAdapter appAdapter;
private String category;
@ -85,8 +86,9 @@ public class AppListActivity extends AppCompatActivity implements LoaderManager.
Intent intent = getIntent();
category = intent.hasExtra(EXTRA_CATEGORY) ? intent.getStringExtra(EXTRA_CATEGORY) : null;
searchTerms = intent.hasExtra(EXTRA_SEARCH_TERMS) ? intent.getStringExtra(EXTRA_SEARCH_TERMS) : null;
searchInput.setText(getSearchText(category, null));
searchInput.setText(getSearchText(category, searchTerms));
searchInput.setSelection(searchInput.getText().length());
if (category != null) {

View File

@ -1,20 +1,28 @@
package org.fdroid.fdroid.views.main;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.MenuItem;
import android.support.v7.widget.RecyclerView;
import org.fdroid.fdroid.AppDetails;
import org.fdroid.fdroid.AppDetails2;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.NfcHelper;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.UpdateService;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.compat.UriCompat;
import org.fdroid.fdroid.views.apps.AppListActivity;
/**
* Main view shown to users upon starting F-Droid.
@ -56,6 +64,9 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV
bottomNavigation.setOnNavigationItemSelectedListener(this);
initialRepoUpdateIfRequired();
Intent intent = getIntent();
handleSearchOrAppViewIntent(intent);
}
/**
@ -85,16 +96,126 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV
pager.scrollToPosition(adapter.adapterPositionFromItemId(R.id.my_apps));
}
// AppDetails and RepoDetailsActivity set different NFC actions, so reset here
// AppDetails 2 and RepoDetailsActivity set different NFC actions, so reset here
NfcHelper.setAndroidBeam(this, getApplication().getPackageName());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleSearchOrAppViewIntent(intent);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
pager.scrollToPosition(((MainViewAdapter) pager.getAdapter()).adapterPositionFromItemId(item.getItemId()));
return true;
}
private void handleSearchOrAppViewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
performSearch(query);
return;
}
final Uri data = intent.getData();
if (data == null) {
return;
}
final String scheme = data.getScheme();
final String path = data.getPath();
String packageName = null;
String query = null;
if (data.isHierarchical()) {
final String host = data.getHost();
if (host == null) {
return;
}
switch (host) {
case "f-droid.org":
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=packageName
packageName = UriCompat.getQueryParameter(data, "fdid");
} else if (path.startsWith("/app")) {
// http://f-droid.org/app/packageName
packageName = data.getLastPathSegment();
if ("app".equals(packageName)) {
packageName = null;
}
}
break;
case "details":
// market://details?id=app.id
packageName = UriCompat.getQueryParameter(data, "id");
break;
case "search":
// market://search?q=query
query = UriCompat.getQueryParameter(data, "q");
break;
case "play.google.com":
if (path.startsWith("/store/apps/details")) {
// http://play.google.com/store/apps/details?id=app.id
packageName = UriCompat.getQueryParameter(data, "id");
} else if (path.startsWith("/store/search")) {
// http://play.google.com/store/search?q=foo
query = UriCompat.getQueryParameter(data, "q");
}
break;
case "apps":
case "amazon.com":
case "www.amazon.com":
// amzn://apps/android?p=app.id
// http://amazon.com/gp/mas/dl/android?s=app.id
packageName = UriCompat.getQueryParameter(data, "p");
query = UriCompat.getQueryParameter(data, "s");
break;
}
} else if ("fdroid.app".equals(scheme)) {
// fdroid.app:app.id
packageName = data.getSchemeSpecificPart();
} else if ("fdroid.search".equals(scheme)) {
// fdroid.search:query
query = data.getSchemeSpecificPart();
}
if (!TextUtils.isEmpty(query)) {
// an old format for querying via packageName
if (query.startsWith("pname:")) {
packageName = query.split(":")[1];
}
// sometimes, search URLs include pub: or other things before the query string
if (query.contains(":")) {
query = query.split(":")[1];
}
}
if (!TextUtils.isEmpty(packageName)) {
Utils.debugLog(TAG, "FDroid launched via app link for '" + packageName + "'");
Intent intentToInvoke = new Intent(this, AppDetails2.class);
intentToInvoke.putExtra(AppDetails.EXTRA_APPID, packageName);
startActivity(intentToInvoke);
finish();
} else if (!TextUtils.isEmpty(query)) {
Utils.debugLog(TAG, "FDroid launched via search link for '" + query + "'");
performSearch(query);
}
}
/**
* Initiates the {@link AppListActivity} with the relevant search terms passed in via the query arg.
*/
private void performSearch(String query) {
Intent searchIntent = new Intent(this, AppListActivity.class);
searchIntent.putExtra(AppListActivity.EXTRA_SEARCH_TERMS, query);
startActivity(searchIntent);
}
private static class NonScrollingHorizontalLayoutManager extends LinearLayoutManager {
NonScrollingHorizontalLayoutManager(Context context) {
super(context, LinearLayoutManager.HORIZONTAL, false);