com.albedinsky.android.support.intent.PlayIntent.java Source code

Java tutorial

Introduction

Here is the source code for com.albedinsky.android.support.intent.PlayIntent.java

Source

/*
 * =================================================================================================
 *                             Copyright (C) 2015 Martin Albedinsky
 * =================================================================================================
 *         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.albedinsky.android.support.intent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

/**
 * A {@link BaseIntent} builder implementation providing API for building of intents targeting
 * the <b>Android Play Store</b> application.
 * <p>
 * This intent builder does not requires any of its parameters to be set. By default will be used
 * package name of the current Android application.
 *
 * @author Martin Albedinsky
 */
public final class PlayIntent extends BaseIntent<PlayIntent> {

    /**
     * Interface ===================================================================================
     */

    /**
     * Constants ===================================================================================
     */

    /**
     * Log TAG.
     */
    // private static final String TAG = "PlayIntent";

    /**
     * Base for the url to view a particular application within Play Store.
     */
    @VisibleForTesting
    static final String VIEW_URL_BASE = "https://play.google.com/store/apps/details?id=";

    /**
     * Static members ==============================================================================
     */

    /**
     * Members =====================================================================================
     */

    /**
     * Package name of an Android application to view in Play Store.
     */
    private String mPackageName;

    /**
     * Constructors ================================================================================
     */

    /**
     * Creates a new instance of PlayIntent for the given <var>activity</var> context.
     * See {@link com.albedinsky.android.support.intent.BaseIntent#BaseIntent(Activity)}
     * for additional info.
     */
    public PlayIntent(@NonNull Activity activity) {
        super(activity);
        this.mPackageName = activity.getPackageName();
    }

    /**
     * Creates a new instance of PlayIntent for the given <var>fragment</var> context.
     * See {@link com.albedinsky.android.support.intent.BaseIntent#BaseIntent(android.support.v4.app.Fragment)}
     * for additional info.
     */
    public PlayIntent(@NonNull Fragment fragment) {
        super(fragment);
        this.mPackageName = fragment.getActivity().getPackageName();
    }

    /**
     * Methods =====================================================================================
     */

    /**
     * Sets a package name of an Android application to be viewed in Play Store.
     *
     * @param packageName Package name of the desired application to view in store.
     * @return This intent builder to allow methods chaining.
     * @see #packageName()
     */
    public PlayIntent packageName(@NonNull @IntRange(from = 0) String packageName) {
        this.mPackageName = packageName;
        return this;
    }

    /**
     * Returns the package name of Android application that can be viewed in Play Store.
     *
     * @return Package name specified via {@link #packageName(String)} or current application's
     * package name.
     */
    @NonNull
    @IntRange(from = 0)
    public String packageName() {
        return mPackageName;
    }

    /**
     */
    @Override
    protected void ensureCanBuildOrThrow() {
        super.ensureCanBuildOrThrow();
        if (TextUtils.isEmpty(mPackageName)) {
            throw cannotBuildIntentException("No package name specified.");
        }
    }

    /**
     */
    @Override
    protected Intent onBuild() {
        return new Intent(Intent.ACTION_VIEW).setData(Uri.parse(VIEW_URL_BASE + mPackageName));
    }

    /**
     * Inner classes ===============================================================================
     */
}