com.usepropeller.routable.Router.java Source code

Java tutorial

Introduction

Here is the source code for com.usepropeller.routable.Router.java

Source

/*
Routable for Android
Copyright (c) 2013 Turboprop, Inc. <clay@usepropeller.com>
http://usepropeller.com
    
Licensed under the MIT License.
    
Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
    
   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.
    
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

package com.usepropeller.routable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

public class Router {
    private static final Router _router = new Router();
    public static final String INTENT_PARAM_URL = "routable_url";
    public static final String INTENT_PARAM_QUERY_STRING = "routable_query_string";

    /**
     * A globally accessible Router instance that will work for
     * most use cases.
     */
    public static Router sharedRouter() {
        return _router;
    }

    /**
     * The class used when you want to map a function (given in `run`)
     * to a Router URL.
     */
    public static abstract class RouterCallback {
        public abstract void run(Map<String, String> params, String queryString, Bundle extras);
    }

    /**
     * The class used to determine behavior when opening a URL.
     * If you want to extend Routable to handle things like transition
     * animations or fragments, this class should be augmented.
     */
    public static class RouterOptions {
        Class<? extends Activity> _klass;
        RouterCallback _callback;
        Map<String, String> _defaultParams;

        public RouterOptions() {

        }

        public RouterOptions(Class<? extends Activity> klass) {
            this.setOpenClass(klass);
        }

        public RouterOptions(Map<String, String> defaultParams) {
            this.setDefaultParams(defaultParams);
        }

        public RouterOptions(Map<String, String> defaultParams, Class<? extends Activity> klass) {
            this.setDefaultParams(defaultParams);
            this.setOpenClass(klass);
        }

        public void setOpenClass(Class<? extends Activity> klass) {
            this._klass = klass;
        }

        public Class<? extends Activity> getOpenClass() {
            return this._klass;
        }

        public RouterCallback getCallback() {
            return this._callback;
        }

        public void setCallback(RouterCallback callback) {
            this._callback = callback;
        }

        public void setDefaultParams(Map<String, String> defaultParams) {
            this._defaultParams = defaultParams;
        }

        public Map<String, String> getDefaultParams() {
            return this._defaultParams;
        }
    }

    public class RouterParams {
        public RouterOptions routerOptions;
        public Map<String, String> openParams;
        public String matchingRoute;
        public String queryString;
    }

    private final Map<String, RouterOptions> _routes = new HashMap<String, RouterOptions>();
    private String _rootUrl = null;
    private final Map<String, RouterParams> _cachedRoutes = new HashMap<String, RouterParams>();
    private Context _context;

    /**
     * Creates a new Router
     */
    public Router() {

    }

    /**
     * Creates a new Router
     *
     * @param context {@link Context} that all {@link Intent}s generated by the router will use
     */
    public Router(Context context) {
        this.setContext(context);
    }

    /**
     * @param context {@link Context} that all {@link Intent}s generated by the router will use
     */
    public void setContext(Context context) {
        this._context = context;
    }

    /**
     * @return The context for the router
     */
    public Context getContext() {
        return this._context;
    }

    /**
     * Map a URL to a callback
     *
     * @param format   The URL being mapped; for example, "users/:id" or "groups/:id/topics/:topic_id"
     * @param callback {@link RouterCallback} instance which contains the code to execute when the URL is opened
     */
    public void map(String format, RouterCallback callback) {
        RouterOptions options = new RouterOptions();
        options.setCallback(callback);
        this.map(format, null, options);
    }

    /**
     * Map a URL to open an {@link Activity}
     *
     * @param format The URL being mapped; for example, "users/:id" or "groups/:id/topics/:topic_id"
     * @param klass  The {@link Activity} class to be opened with the URL
     */
    public void map(String format, Class<? extends Activity> klass) {
        this.map(format, klass, null);
    }

    /**
     * Map a URL to open an {@link Activity}
     *
     * @param format  The URL being mapped; for example, "users/:id" or "groups/:id/topics/:topic_id"
     * @param klass   The {@link Activity} class to be opened with the URL
     * @param options The {@link RouterOptions} to be used for more granular and customized options for when the URL is opened
     */
    public void map(String format, Class<? extends Activity> klass, RouterOptions options) {
        if (options == null) {
            options = new RouterOptions();
        }
        options.setOpenClass(klass);
        this._routes.put(format, options);
    }

    /**
     * Set the root url; used when opening an activity or callback via RouterActivity
     *
     * @param rootUrl The URL format to use as the root
     */
    public void setRootUrl(String rootUrl) {
        this._rootUrl = rootUrl;
    }

    /**
     * @return The router's root URL, or null.
     */
    public String getRootUrl() {
        return this._rootUrl;
    }

    /**
     * Open a URL using the operating system's configuration (such as opening a link to Chrome or a video to YouTube)
     *
     * @param url The URL; for example, "http://www.youtube.com/watch?v=oHg5SJYRHA0"
     */
    public void openExternal(String url) {
        this.openExternal(url, this._context);
    }

    /**
     * Open a URL using the operating system's configuration (such as opening a link to Chrome or a video to YouTube)
     *
     * @param url     The URL; for example, "http://www.youtube.com/watch?v=oHg5SJYRHA0"
     * @param context The context which is used in the generated {@link Intent}
     */
    public void openExternal(String url, Context context) {
        this.openExternal(url, null, context);
    }

    /**
     * Open a URL using the operating system's configuration (such as opening a link to Chrome or a video to YouTube)
     *
     * @param url    The URL; for example, "http://www.youtube.com/watch?v=oHg5SJYRHA0"
     * @param extras The {@link Bundle} which contains the extras to be assigned to the generated {@link Intent}
     */
    public void openExternal(String url, Bundle extras) {
        this.openExternal(url, extras, this._context);
    }

    /**
     * Open a URL using the operating system's configuration (such as opening a link to Chrome or a video to YouTube)
     *
     * @param url     The URL; for example, "http://www.youtube.com/watch?v=oHg5SJYRHA0"
     * @param extras  The {@link Bundle} which contains the extras to be assigned to the generated {@link Intent}
     * @param context The context which is used in the generated {@link Intent}
     */
    public void openExternal(String url, Bundle extras, Context context) {
        if (context == null) {
            throw new ContextNotProvided("You need to supply a context for Router " + this.toString());
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        this.addFlagsToIntent(intent, context);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    public void open(Route route, Bundle extras, Context context) {
        this.open(route.toString(), extras, context);
    }

    public void open(Route route, Bundle extras, Context context, int intentFlags) {
        this.open(route.toString(), extras, context, intentFlags);
    }

    public void open(Route route, Context context) {
        this.open(route.toString(), context);
    }

    public void open(Route route, Bundle extras, int intentFlags) {
        this.open(route.toString(), extras, intentFlags);
    }

    public void open(Route route, Bundle extras) {
        this.open(route.toString(), extras);
    }

    public void open(Route route) {
        this.open(route.toString());
    }

    /**
     * Open a map'd URL set using {@link #map(String, Class)} or {@link #map(String, RouterCallback)}
     *
     * @param url The URL; for example, "users/16" or "groups/5/topics/20"
     */
    public void open(String url) {
        this.open(url, this._context);
    }

    /**
     * Open a map'd URL set using {@link #map(String, Class)} or {@link #map(String, RouterCallback)}
     *
     * @param url    The URL; for example, "users/16" or "groups/5/topics/20"
     * @param extras The {@link Bundle} which contains the extras to be assigned to the generated {@link Intent}
     */
    public void open(String url, Bundle extras) {
        this.open(url, extras, this._context);
    }

    /**
     * @param url
     * @param extras
     * @param intentFlags
     */
    public void open(String url, Bundle extras, int intentFlags) {
        this.open(url, extras, this._context, intentFlags);
    }

    /**
     * Open a map'd URL set using {@link #map(String, Class)} or {@link #map(String, RouterCallback)}
     *
     * @param url     The URL; for example, "users/16" or "groups/5/topics/20"
     * @param context The context which is used in the generated {@link Intent}
     */
    public void open(String url, Context context) {
        this.open(url, null, context);
    }

    /**
     * @param url
     * @param extras
     * @param context
     */
    public void open(String url, Bundle extras, Context context) {
        this.open(url, extras, context, 0);
    }

    /**
     * Open a map'd URL set using {@link #map(String, Class)} or {@link #map(String, RouterCallback)}
     *
     * @param url     The URL; for example, "users/16" or "groups/5/topics/20"
     * @param extras  The {@link Bundle} which contains the extras to be assigned to the generated {@link Intent}
     * @param context The context which is used in the generated {@link Intent}
     */
    public void open(String url, Bundle extras, Context context, int intentFlags) {
        if (context == null) {
            throw new ContextNotProvided("You need to supply a context for Router " + this.toString());
        }
        RouterParams params = this.paramsForUrl(url);
        RouterOptions options = params.routerOptions;
        if (options.getCallback() != null) {
            options.getCallback().run(params.openParams, params.queryString, extras);
            return;
        }

        Intent intent = this.intentFor(context, url);
        if (intent == null) {
            // Means the options weren't opening a new activity
            return;
        }
        if (extras != null) {
            intent.putExtras(extras);
        }
        intent.addFlags(intentFlags);
        context.startActivity(intent);
    }

    /**
     * Open a map'd URL for result
     *
     * @param url         The URL; for example, "users/16" or "groups/5/topics/20"
     * @param extras      The {@link Bundle} which contains the extras to be assigned to the generated {@link Intent}
     * @param context     The context which is used in the generated {@link Intent}
     * @param requestCode The request code, to handle within onActivityResult
     */
    public void openForResult(String url, Bundle extras, Context context, int requestCode) {
        openForResult(url, extras, context, requestCode, 0);
    }

    public void openForResult(String url, Bundle extras, Context context, int requestCode, int intentFlags) {
        openForResult(url, extras, context, requestCode, intentFlags, null);
    }

    /**
     * Open a map'd URL for result
     *
     * @param url         The URL; for example, "users/16" or "groups/5/topics/20"
     * @param extras      The {@link Bundle} which contains the extras to be assigned to the generated {@link Intent}
     * @param context     The context which is used in the generated {@link Intent}
     * @param requestCode The request code, to handle within onActivityResult
     */
    public void openForResult(String url, Bundle extras, Context context, int requestCode, int intentFlags,
            Intent o) {
        if (context == null || !(context instanceof Activity)) {
            throw new ContextNotProvided(
                    "You need to supply a context for Router and it should be instance of activity "
                            + this.toString());
        }
        RouterParams params = this.paramsForUrl(url);
        RouterOptions options = params.routerOptions;
        checkState(options.getCallback() == null, "openForResult cannot be used for callback routes");

        Intent intent = this.intentFor(context, url, o);
        if (intent == null) {
            // Means the options weren't opening a new activity
            return;
        }
        if (extras != null) {
            intent.putExtras(extras);
        }
        intent.addFlags(intentFlags);
        ((Activity) context).startActivityForResult(intent, requestCode);
    }

    public void openForResult(String url, Bundle extras, Fragment fragment, int requestCode) {
        openForResult(url, extras, fragment, requestCode, 0);
    }

    public void openForResult(String url, Bundle extras, Fragment fragment, int requestCode, int intentFlags) {
        openForResult(url, extras, fragment, requestCode, intentFlags, null);
    }

    public void openForResult(String url, Bundle extras, Fragment fragment, int requestCode, int intentFlags,
            Intent o) {
        if (fragment == null) {
            throw new ContextNotProvided("You need to supply a fragment for Router" + this.toString());
        }
        RouterParams params = this.paramsForUrl(url);
        RouterOptions options = params.routerOptions;
        checkState(options.getCallback() == null, "openForResult cannot be used for callback routes");

        Intent intent = this.intentFor(fragment.getActivity(), url, o);
        if (intent == null) {
            // Means the options weren't opening a new activity
            return;
        }
        if (extras != null) {
            intent.putExtras(extras);
        }
        intent.addFlags(intentFlags);
        fragment.getActivity().startActivityFromFragment(fragment, intent, requestCode);
    }

    /**
     * @param url The URL to check
     * @return Whether or not the URL refers to an anonymous callback function
     */
    public boolean isCallbackUrl(String url) {
        RouterParams params = this.paramsForUrl(url);
        RouterOptions options = params.routerOptions;
        return options.getCallback() != null;
    }

    /*
     * Allows Intents to be spawned regardless of what context they were opened with.
     */
    private void addFlagsToIntent(Intent intent, Context context) {
        if (context == this._context) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    }

    /**
     * @param url The URL; for example, "users/16" or "groups/5/topics/20"
     * @return The {@link Intent} for the url
     */
    public Intent intentFor(String url) {
        return intentFor(url, null);
    }

    /**
     * @param url The URL; for example, "users/16" or "groups/5/topics/20"
     * @return The {@link Intent} for the url
     */
    public Intent intentFor(String url, Intent o) {
        RouterParams params = this.paramsForUrl(url);
        RouterOptions options = params.routerOptions;
        Intent intent = o == null ? new Intent() : new Intent(o);
        if (options.getDefaultParams() != null) {
            for (Entry<String, String> entry : options.getDefaultParams().entrySet()) {
                intent.putExtra(entry.getKey(), entry.getValue());
            }
        }
        for (Entry<String, String> entry : params.openParams.entrySet()) {
            intent.putExtra(entry.getKey(), entry.getValue());
        }
        intent.putExtra(INTENT_PARAM_URL, url);
        if (null != params.queryString) {
            intent.putExtra(INTENT_PARAM_QUERY_STRING, params.queryString);
        }
        return intent;
    }

    /**
     * @param context The context which is spawning the intent
     * @param url     The URL; for example, "users/16" or "groups/5/topics/20"
     * @return The {@link Intent} for the url, with the correct {@link Activity} set, or null.
     */
    public Intent intentFor(Context context, String url) {
        return intentFor(context, url, null);
    }

    /**
     * @param context The context which is spawning the intent
     * @param url     The URL; for example, "users/16" or "groups/5/topics/20"
     * @return The {@link Intent} for the url, with the correct {@link Activity} set, or null.
     */
    public Intent intentFor(Context context, String url, Intent o) {
        RouterParams params = this.paramsForUrl(url);
        RouterOptions options = params.routerOptions;
        if (options.getCallback() != null) {
            return null;
        }

        Intent intent = intentFor(url, o);
        intent.setClass(context, options.getOpenClass());
        this.addFlagsToIntent(intent, context);
        return intent;
    }

    public Boolean urlMatchRoute(String url, String route) {
        try {
            return checkNotNull(route).equals(paramsForUrl(url).matchingRoute);
        } catch (RouteNotFoundException e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    /*
     * Takes a url (i.e. "/users/16/hello") and breaks it into a {@link RouterParams} instance where
     * each of the parameters (like ":id") has been parsed.
     */
    public RouterParams paramsForUrl(String url) {
        if (this._cachedRoutes.get(checkNotNull(url)) != null) {
            return this._cachedRoutes.get(url);
        }

        final int qsPos = url.indexOf('?');
        String queryString = null;

        if (qsPos != -1) {
            queryString = url.substring(qsPos + 1);
            url = url.substring(0, qsPos);
        }

        String[] givenParts = url.split("/");

        RouterOptions openOptions = null;
        RouterParams openParams = null;
        for (Entry<String, RouterOptions> entry : this._routes.entrySet()) {
            String routerUrl = entry.getKey();
            RouterOptions routerOptions = entry.getValue();
            String[] routerParts = routerUrl.split("/");

            if (routerParts.length != givenParts.length) {
                continue;
            }

            Map<String, String> givenParams = urlToParamsMap(givenParts, routerParts);
            if (givenParams == null) {
                continue;
            }

            openOptions = routerOptions;
            openParams = new RouterParams();
            openParams.openParams = givenParams;
            openParams.routerOptions = routerOptions;
            openParams.queryString = queryString;
            openParams.matchingRoute = routerUrl;
            break;
        }

        if (openOptions == null || openParams == null) {
            throw new RouteNotFoundException("No route found for url " + url);
        }

        this._cachedRoutes.put(url, openParams);
        return openParams;
    }

    /**
     *
     */
    public static class Route {
        private String mRoute;
        private Map<String, Object> mParams;

        public Route(String route) {
            this(route, new HashMap<String, Object>());
        }

        public Route(String route, Object... params) {
            mRoute = checkNotNull(route);
            mParams = paramsMap(params);
        }

        public Route(String route, HashMap<String, Object> params) {
            mRoute = checkNotNull(route);
            mParams = checkNotNull(params);
        }

        public static String renderRoute(Route route) {
            return renderRoute(route.mRoute, route.mParams);
        }

        public static String renderRoute(String url, Object... params) {
            return renderRoute(url, paramsMap(params));
        }

        public static String renderRoute(String url, Map<String, Object> params) {
            int qsPos = checkNotNull(url).indexOf('?');
            Optional<String> queryString;

            if (qsPos != -1) {
                queryString = Optional.of(url.substring(qsPos + 1));
                url = url.substring(0, qsPos);
            } else {
                queryString = Optional.absent();
            }

            Iterable<String> givenParts = Splitter.on("/").split(url);
            List<String> renderParts = new ArrayList<String>();

            int paramIndex = 0;
            for (String part : givenParts) {
                if (":".equals(part.substring(0, 1))) {
                    String pName = part.substring(1);
                    String pIdx = String.valueOf(paramIndex);
                    if (params.containsKey(pName)) {
                        renderParts.add(String.valueOf(params.get(pName)));
                    } else if (params.containsKey(pIdx)) {
                        renderParts.add(String.valueOf(params.get(pIdx)));
                    } else {
                        throw new RuntimeException(String.format("No value found for \"%s\"", part));
                    }
                    paramIndex++;
                } else {
                    renderParts.add(part);
                }
            }

            return Joiner.on('/').join(renderParts).concat(queryString.isPresent() ? "?" + queryString.get() : "");
        }

        private static Map<String, Object> paramsMap(Object... params) {
            Map<String, Object> paramsHash = new HashMap<String, Object>();
            for (int i = 0; i < params.length; i++) {
                paramsHash.put(String.valueOf(i), params[i]);
            }
            return paramsHash;
        }

        public String toString() {
            return renderRoute(mRoute, mParams);
        }
    }

    /**
     * @param givenUrlSegments  An array representing the URL path attempting to be opened (i.e. ["users", "42"])
     * @param routerUrlSegments An array representing a possible URL match for the router (i.e. ["users", ":id"])
     * @return A map of URL parameters if it's a match (i.e. {"id" => "42"}) or null if there is no match
     */
    private Map<String, String> urlToParamsMap(String[] givenUrlSegments, String[] routerUrlSegments) {
        Map<String, String> formatParams = new HashMap<String, String>();
        for (int index = 0; index < routerUrlSegments.length; index++) {
            String routerPart = routerUrlSegments[index];
            String givenPart = givenUrlSegments[index];

            if (routerPart.charAt(0) == ':') {
                String key = routerPart.substring(1, routerPart.length());
                formatParams.put(key, givenPart);
                continue;
            }

            if (!routerPart.equals(givenPart)) {
                return null;
            }
        }

        return formatParams;
    }

    /**
     * Thrown if a given route is not found.
     */
    public static class RouteNotFoundException extends RuntimeException {
        private static final long serialVersionUID = -2278644339983544651L;

        public RouteNotFoundException(String message) {
            super(message);
        }
    }

    /**
     * Thrown if no context has been found.
     */
    public static class ContextNotProvided extends RuntimeException {
        private static final long serialVersionUID = -1381427067387547157L;

        public ContextNotProvided(String message) {
            super(message);
        }
    }
}