/* * Copyright (C) 2007 The Android Open Source Project * Copyright (C) 2012 Paul Watts (paulcwatts@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.onebusaway.android.mock; import android.annotation.TargetApi; import android.net.Uri; import android.os.Build; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class UriCompat { @TargetApi(value = 11) public static Set<String> getQueryParameterNames(Uri uri) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { return uri.getQueryParameterNames(); } else { return getQueryParameterNames_10(uri); } } // // Copied from newer Uri sources, useable with < 11 APIs. // private static Set<String> getQueryParameterNames_10(Uri 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); } }