Java tutorial
/* * ================================================================================================= * Copyright (C) 2013 - 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.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 android.util.Patterns; import com.wit.android.support.content.ContentConfig; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <h3>Class Overview</h3> * Intent builder specialized to create WEB intents. * <p> * Valid web URL for this intent must be in one of these formats: * <ul> * <li><b>http://URL</b></li> * <li><b>https://URL</b></li> * </ul> * * @author Martin Albedinsky */ public class WebIntent extends BaseIntent<WebIntent> { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ private static final String TAG = "WebIntent"; /** * Flag indicating whether the debug output trough log-cat is enabled or not. */ // private static final boolean DEBUG_ENABLED = true; /** * Flag indicating whether the output trough log-cat is enabled or not. */ private static final boolean LOG_ENABLED = ContentConfig.LIBRARY_LOG_ENABLED; /** * Static members ============================================================================== */ /** * Matcher for the valid web URL. See {@link Patterns#WEB_URL}.matcher() for more info. */ protected static final Matcher WEB_URL_MATCHER = Patterns.WEB_URL.matcher(""); /** * Matcher for valid HTTP format. Checks for the "http://" or "https://" prefix at the begin of * the passed web URL. */ protected static final Matcher HTTP_FORMAT_MATCHER = Pattern.compile("^(http|https):\\/\\/(.+)").matcher(""); /** * HTTP prefix for the valid web URL. */ private static final String HTTP_PREFIX = "http://"; /** * Members ===================================================================================== */ /** * URL to show in a web browser. */ private String mUrl; /** * Constructors ================================================================================ */ /** * Creates a new instance of WebIntent for the given <var>activity</var> context. * See {@link com.wit.android.support.content.intent.BaseIntent#BaseIntent(android.app.Activity)} * for additional info. */ public WebIntent(@NonNull Activity activity) { super(activity); } /** * Creates a new instance of WebIntent 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 WebIntent(@NonNull Fragment fragment) { super(fragment); } /** * Methods ===================================================================================== */ /** * Public -------------------------------------------------------------------------------------- */ /** */ @Nullable @Override public Intent buildIntent() { if (TextUtils.isEmpty(mUrl)) { Log.e(TAG, "Can not to create a WEB intent. No URL specified."); return null; } else if (!Patterns.WEB_URL.matcher(mUrl).matches()) { Log.e(TAG, "Can not to create a WEB intent. Invalid URL('" + mUrl + "')."); return null; } /** * Build and start the intent. */ return new Intent(Intent.ACTION_VIEW, Uri.parse(mUrl)); } /** * Getters + Setters --------------------------------------------------------------------------- */ /** * Same as {@link #url(String)}, where url will be obtain from the current context wrapper (activity/fragment). * * @param resId Resource id of the desired web url placed within an application resources. */ public WebIntent url(@StringRes int resId) { return url(obtainString(resId)); } /** * Sets the URL which should be loaded into a web browser. <b>Note</b>, that the given <var>url</var> * must be valid web URL otherwise will be not accepted and {@link #buildIntent()} will return * {@code null}. If the given <var>url</var> is valid, but doesn't contains HTTP or HTTPS prefix, * then the HTTP (http://) prefix will be assigned to it. * * @param url The string with URL to load into web browser. * @return This intent builder to allow methods chaining. * @see #getUrl() */ public WebIntent url(@NonNull String url) { if (!TextUtils.isEmpty(url) && WEB_URL_MATCHER.reset(url).matches()) { if (!HTTP_FORMAT_MATCHER.reset(url).matches()) { this.mUrl = HTTP_PREFIX + url; } else { this.mUrl = url; } } else { if (LOG_ENABLED) Log.v(TAG, "Invalid url('" + url + "')"); } return this; } /** * Returns string with web URL. * * @return A string with the current URL which should be loaded into a web browser. * @see #url(String) */ @Nullable public String getUrl() { return mUrl; } /** * Protected ----------------------------------------------------------------------------------- */ /** * Private ------------------------------------------------------------------------------------- */ /** * Inner classes =============================================================================== */ }