Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.*;
import android.content.pm.PackageManager;
import android.net.Uri;
import java.util.List;

public class Main {
    private static final String URL_SCHEME = "goparche://";
    private static final String OPEN_ENDPOINT = "open";
    private static final String NO_DISCOUNT_FORMAT = "?api_key=%s";

    /**
     * Creates an Intent to open the Parche application without a discount, but indicating what app the request
     * is coming from.
     *
     * @param aContext  The current context.
     * @param aAPIKey   The partner application's Parche API key.
     *
     * @return The intent to open the Parche application, or null if the app needs to be updated or installed.
     */
    public static Intent openParcheIntent(Context aContext, String aAPIKey) {
        Intent returnIntent = null;
        if (!parcheNeedsToBeUpdatedOrInstalled(aContext)) {
            String urlString = URL_SCHEME + OPEN_ENDPOINT + String.format(NO_DISCOUNT_FORMAT, aAPIKey);
            returnIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlString));
        }

        return returnIntent;
    }

    /**
     * Determines if there is a version of Parche on the user's device which responds
     * to this URL scheme.
     *
     * @param aContext The current context.
     *
     * @return true if the application needs to be updated or installed, false if you're clear
     *         to go ahead and open the application.
     */
    public static boolean parcheNeedsToBeUpdatedOrInstalled(Context aContext) {
        return !urlCanBeHandled(aContext, URL_SCHEME + OPEN_ENDPOINT);
    }

    /*******************
     * PRIVATE METHODS *
     *******************/

    private static boolean urlCanBeHandled(Context aContext, String aURLString) {
        PackageManager packageManager = aContext.getPackageManager();
        Intent openAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(aURLString));
        List activitiesCanHandle = packageManager.queryIntentActivities(openAppIntent,
                PackageManager.MATCH_DEFAULT_ONLY);
        return activitiesCanHandle.size() != 0;
    }
}