Back to project page AndroidPlaces.
The source code is released under:
Apache License
If you think the Android project AndroidPlaces listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2014 Brian Lee/*from ww w. j av a 2s. co m*/ * * 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 com.tigerpenguin.places.request; import android.content.Context; import com.tigerpenguin.places.PlacesTest; import com.tigerpenguin.places.model.Language; import com.tigerpenguin.places.model.PlaceType; import com.tigerpenguin.places.model.PriceLevel; import com.tigerpenguin.places.model.RankBy; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.regex.Pattern; public abstract class RequestTest extends PlacesTest { protected static final String PATH_NEARBY = "nearbysearch/json?"; protected static final String PATH_DETAIL = "details/json?"; protected static final String PARAM_LOCATION = "location"; protected static final String PARAM_RADIUS = "radius"; protected static final String PARAM_NAME = "name"; protected static final String PARAM_TYPES = "types"; protected static final String PARAM_KEYWORD = "keyword"; protected static final String PARAM_LANGUAGE = "language"; protected static final String PARAM_MIN_PRICE = "minprice"; protected static final String PARAM_MAX_PRICE = "maxprice"; protected static final String PARAM_OPEN_NOW = "opennow"; protected static final String PARAM_RANK_BY = "rankby"; protected static final String PARAM_PAGE_TOKEN = "pagetoken"; protected static final String PARAM_PLACE_ID = "placeid"; private static final String BASE_URL = "https://maps.googleapis.com/maps/api/place/"; private static final String PARAM_API_KEY = "key"; protected static final void checkUrlEmpty(String url) { assertTrue("Url is not empty! url: " + url, url.isEmpty()); } protected static final void checkValidation(PlacesRequest placesRequest) { boolean exceptionCaught = false; try { placesRequest.validate(); } catch (InvalidRequestException ire) { exceptionCaught = true; } assertFalse("Validation failed", exceptionCaught); } protected static final void checkExecutionValidated(PlacesRequest placesRequest) { boolean exceptionCaught = false; try { placesRequest.execute(); } catch (InvalidRequestException ire) { exceptionCaught = true; } assertTrue("Execution validation failed", exceptionCaught); } protected static final String checkAndRemoveBaseParams(Context context, String url, String path) { url = checkAndRemoveString(url, BASE_URL); if (path != null && !path.isEmpty()) { url = checkAndRemoveString(url, path); } url = checkAndRemoveString(url, PARAM_API_KEY + "=" + getApiKey(context)); return url; } protected static final String checkAndRemoveParam(String url, String param, String value) { try { value = URLEncoder.encode(value, "UTF-8"); } catch (UnsupportedEncodingException e) { fail("Unable to encode value for testing"); } return checkAndRemoveString(url, "&" + param + "=" + value); } protected static final String checkAndRemoveString(String source, String searchString) { assertNotNull(source); assertNotNull(searchString); assertTrue("Source string doesn't contain search string! Source: " + source + " searchString: " + searchString, source.contains(searchString)); return source.replaceFirst(Pattern.quote(searchString), ""); } protected static final String checkAndRemoveLocation(String url, double latitude, double longitude) { return checkAndRemoveParam(url, PARAM_LOCATION, String.valueOf(latitude) + "," + String.valueOf(longitude)); } protected static final String checkAndRemovePlaceTypes(String url, PlaceType... placeTypes) { StringBuilder sb = new StringBuilder(); boolean firstItem = true; for (PlaceType placeType : placeTypes) { if (firstItem) { firstItem = false; } else { sb.append("|"); } sb.append(placeType.getJsonValue()); } return checkAndRemoveParam(url, PARAM_TYPES, sb.toString()); } protected static final String checkAndRemoveParam(String url, String param, int value) { return checkAndRemoveParam(url, param, String.valueOf(value)); } protected static final String checkAndRemoveParam(String url, String param, PriceLevel priceLevel) { return checkAndRemoveParam(url, param, String.valueOf(priceLevel.getJsonValue())); } protected static final String checkAndRemoveParam(String url, String param, boolean value) { return checkAndRemoveParam(url, param, String.valueOf(value)); } protected static final String checkAndRemoveParam(String url, String param, RankBy rankBy) { return checkAndRemoveParam(url, param, rankBy.getValue()); } protected static final String checkAndRemoveParam(String url, String param, Language language) { return checkAndRemoveParam(url, param, language.getCode()); } }