Java tutorial
/* * ================================================================================================= * Copyright (C) 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.wit.android.support.content.intent; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.util.Log; import com.wit.android.support.content.ContentConfig; /** * <h3>Class Overview</h3> * todo: description * * @author Martin Albedinsky */ public class MapIntent extends BaseIntent<MapIntent> { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ private static final String TAG = "MapIntent"; /** * Flag indicating whether the output trough log-cat is enabled or not. */ // private static final boolean LOG_ENABLED = true; /** * Flag indicating whether the debug output trough log-cat is enabled or not. */ private static final boolean DEBUG_ENABLED = ContentConfig.LIBRARY_DEBUG_LOG_ENABLED; /** * Uri scheme for <b>map</b> targeting intents. * <p> * Constant value: <b>geo:</b> */ public static final String SCHEME = "geo:"; /** * Simple location Uri format for <b>latitude</b> and <b>longitude</b>. * <p> * Constant value: <b>geo:%f,%f</b> */ public static final String GEO_LAT_LNG_FORMAT = SCHEME + "%f,%f"; /** * Simple location Uri format for <b>latitude, longitude</b> and <b>zoom level</b>. * <p> * Constant value: <b>geo:%f,%f?z=%.2f</b> */ public static final String GEO_LAT_LNG_ZOOM_FORMAT = SCHEME + "%f,%f?z=%.2f"; /** * Simple location Uri format for <b>latitude, longitude</b> and <b>label</b>. * <p> * Constant value: <b>geo:0,0?q=%f,%f(%s)</b> */ public static final String GEO_LAT_LNG_LABEL_FORMAT = SCHEME + "0,0?q=%f,%f(%s)"; /** * Simple location Uri format for <b>custom location query</b>. * <p> * Constant value: <b>geo:0,0?q=%s</b> */ public static final String GEO_LOCATION_QUERY_FORMAT = SCHEME + "0,0?q=%s"; /** * Simple location Uri format for <b>custom location query</b> with <b>label</b>. * <p> * Constant value: <b>geo:0,0?q=%s</b> */ public static final String GEO_LOCATION_QUERY_LABEL_FORMAT = SCHEME + "0,0?q=%s(%s)"; /** * Static members ============================================================================== */ /** * Members ===================================================================================== */ /** * Longitude value for map locationQuery. */ private double mLng; /** * Latitude value for map locationQuery. */ private double mLat; /** * Zoom level for map camera. */ private float mZoomLevel; /** * Label for map locationQuery. */ private CharSequence mLabel; /** * Location query for map data. */ private String mLocationQuery; /** * Flag indicating whether there was latitude + longitude set or not. */ private boolean mLatLngSet; /** * Constructors ================================================================================ */ /** * Creates a new instance of MapIntent for the given <var>activity</var> context. * See {@link com.wit.android.support.content.intent.BaseIntent#BaseIntent(android.app.Activity)} * for additional info. */ public MapIntent(@NonNull Activity activity) { super(activity); } /** * Creates a new instance of MapIntent for the given <var>fragment</var> context. * See {@link com.wit.android.support.content.intent.BaseIntent#BaseIntent(android.support.v4.app.Fragment)} * for additional info. */ public MapIntent(@NonNull Fragment fragment) { super(fragment); } /** * Methods ===================================================================================== */ /** * Public -------------------------------------------------------------------------------------- */ /** */ @NonNull @Override public Intent buildIntent() { final Intent intent = new Intent(Intent.ACTION_VIEW); Uri locationUri = Uri.parse(SCHEME); if (!TextUtils.isEmpty(mLocationQuery)) { if (!TextUtils.isEmpty(mLabel)) { locationUri = Uri.parse(String.format(GEO_LOCATION_QUERY_LABEL_FORMAT, mLocationQuery, mLabel)); } else { locationUri = Uri.parse(String.format(GEO_LOCATION_QUERY_FORMAT, mLocationQuery)); } } else if (mLatLngSet) { if (!TextUtils.isEmpty(mLabel)) { locationUri = Uri.parse(String.format(GEO_LAT_LNG_LABEL_FORMAT, mLat, mLng, mLabel)); } else if (mZoomLevel > 0) { locationUri = Uri.parse(String.format(GEO_LAT_LNG_ZOOM_FORMAT, mLat, mLng, mZoomLevel)); } else { locationUri = Uri.parse(String.format(GEO_LAT_LNG_FORMAT, mLat, mLng)); } } if (DEBUG_ENABLED) { Log.d(TAG, "Created data for map with uri('" + locationUri.toString() + "')."); } return intent.setData(locationUri); } /** * Getters + Setters --------------------------------------------------------------------------- */ /** * Sets the latitude and longitude of location to show on map. * * @param latitude Location latitude. * @param longitude Location longitude. * @return This intent builder to allow methods chaining. */ public MapIntent location(double latitude, double longitude) { this.mLat = latitude; this.mLng = longitude; this.mLocationQuery = null; this.mLatLngSet = true; return this; } /** * Returns the current latitude of location to show on map. * * @return Location latitude value. */ public double getLat() { return mLat; } /** * Returns the current longitude of location to show on map. * * @return Location longitude value. */ public double getLng() { return mLng; } /** * Sets the location query for to pass to the map. * * @param query Custom location query. Can be for example name of town with street address and * address number to show on map. * @return This intent builder to allow methods chaining. */ public MapIntent locationQuery(@Nullable String query) { this.mLocationQuery = query; mLat = mLng = 0; this.mLatLngSet = false; return this; } /** * Returns the current location query to pass to the map. * * @return Custom location query. */ @Nullable public String getLocationQuery() { return mLocationQuery; } /** * Sets the zoom level which should be set when map is loaded and showed to user. * * @param level The desired zoom level in range <1, 23>. * @return This intent builder instance. */ public MapIntent zoomLevel(float level) { if (level >= 1 && level < 23) { this.mZoomLevel = level; } return this; } /** * Returns the zoom level which should be set when map is loaded and showed to user. * * @return Set zoom level or {@code 0} by default. */ public float getZoomLevel() { return mZoomLevel; } /** * Same as {@link #label(CharSequence)}, where label will be obtain from the current context wrapper * (activity/fragment). * * @param resId Resource id of the desired label text placed within an application resources. */ public MapIntent label(@StringRes int resId) { return label(obtainText(resId)); } /** * Sets the label by which should be labeled location on map. * * @param label The desired label text. * @return This intent builder to allow methods chaining. */ public MapIntent label(@Nullable CharSequence label) { this.mLabel = label; return this; } /** * Returns the current label for map location. * * @return Simple label text. */ @Nullable public CharSequence getLabel() { return mLabel; } /** * Protected ----------------------------------------------------------------------------------- */ /** */ @Override protected boolean onStart(@NonNull Intent intent) { final Context context = mContextWrapper.getContext(); if (context == null) { return false; } if (intent.resolveActivity(context.getPackageManager()) != null) { return super.onStart(intent); } Log.e(TAG, "Maps not found."); return false; } /** * Private ------------------------------------------------------------------------------------- */ /** * Inner classes =============================================================================== */ }