1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.calendar;
18 
19 import android.net.Uri;
20 
21 public class QueryParameterUtils {
22 
readBooleanQueryParameter(Uri uri, String name, boolean defaultValue)23     public static boolean readBooleanQueryParameter(Uri uri, String name,
24             boolean defaultValue) {
25         final String flag = getQueryParameter(uri, name);
26         return flag == null
27                 ? defaultValue
28                 : (!"false".equals(flag.toLowerCase()) && !"0".equals(flag.toLowerCase()));
29     }
30 
31     // Duplicated from ContactsProvider2.
32     /**
33      * A fast re-implementation of {@link android.net.Uri#getQueryParameter}
34      */
getQueryParameter(Uri uri, String parameter)35     public static String getQueryParameter(Uri uri, String parameter) {
36         String query = uri.getEncodedQuery();
37         if (query == null) {
38             return null;
39         }
40 
41         int queryLength = query.length();
42         int parameterLength = parameter.length();
43 
44         String value;
45         int index = 0;
46         while (true) {
47             index = query.indexOf(parameter, index);
48             if (index == -1) {
49                 return null;
50             }
51 
52             index += parameterLength;
53 
54             if (queryLength == index) {
55                 return null;
56             }
57 
58             if (query.charAt(index) == '=') {
59                 index++;
60                 break;
61             }
62         }
63 
64         int ampIndex = query.indexOf('&', index);
65         if (ampIndex == -1) {
66             value = query.substring(index);
67         } else {
68             value = query.substring(index, ampIndex);
69         }
70 
71         return Uri.decode(value);
72     }
73 }
74