Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using query parameters for deep linking #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ local.properties
/local.properties
/.idea/workspace.xml
.ds_store
MobileDeepLinking-Android-SDK/mobiledeeplinking_lib.iml
10 changes: 1 addition & 9 deletions MobileDeepLinking-Android-SDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ apply plugin: 'android-library'

android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
buildToolsVersion "19.1"

defaultConfig {
minSdkVersion 8
targetSdkVersion 16
versionCode 1
versionName "1.0"
}
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}

dependencies {
compile 'com.android.support:appcompat-v7:+'
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MobileDeepLinking extends Activity
{
Expand Down Expand Up @@ -102,6 +105,15 @@ private void routeUsingUrl(Uri deeplink) throws JSONException
try
{
Map<String, String> routeParameters = new HashMap<String, String>();
Set<String> queryParameterNames = getQueryParameterNames(deeplink);
if ( !queryParameterNames.isEmpty() )
{
for (String paramName : queryParameterNames)
{
routeParameters.put(paramName, deeplink.getQueryParameter(paramName));
}
}
routeParameters.put("route", deeplink.getHost());
routeParameters = DeeplinkMatcher.match(route, routeOptions, routeParameters, deeplink);
if (routeParameters != null)
{
Expand Down Expand Up @@ -214,4 +226,43 @@ private String readConfigFile() throws IOException
}
return sb.toString();
}

/**
* Returns a set of the unique names of all query parameters. Iterating
* over the set will return the names in order of their first occurrence.
*
* @throws UnsupportedOperationException if this isn't a hierarchical URI
*
* @return a set of decoded names
*/
private Set<String> getQueryParameterNames(Uri uri) {
if (uri.isOpaque()) {
throw new UnsupportedOperationException("This isn't a hierarchical URI.");
}

String query = uri.getEncodedQuery();
if (query == null) {
return Collections.emptySet();
}

Set<String> names = new LinkedHashSet<String>();
int start = 0;
do {
int next = query.indexOf('&', start);
int end = (next == -1) ? query.length() : next;

int separator = query.indexOf('=', start);
if (separator > end || separator == -1) {
separator = end;
}

String name = query.substring(start, separator);
names.add(Uri.decode(name));

// Move start to end of name.
start = end + 1;
} while (start < query.length());

return Collections.unmodifiableSet(names);
}
}