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 {
    public static final String PARCHE_PACKAGE_NAME = "com.parche.parchemobile";
    public static final String PLAY_STORE_URL_SCHEME = "market://details?id=";
    public static final String PLAY_STORE_WEB_URL = "http://play.google.com/store/apps/details?id=";

    /**
     * Creates an intent to open either the Play Store or the web page for the play store which will show Parche,
     * depending on whether the user has the Play Store installed or not.
     *
     * @param aContext The current context.
     * @return An Intent which can be used to launch the appropriate route to the Play Store.
     */
    //Intent.FLAG_ACTIVITY_CLEAR_TASK_WHEN_RESET deprecated in API 21, but still needed before that.
    @SuppressWarnings("deprecation")
    public static Intent showParcheInPlayStoreIntent(Context aContext) {
        String marketURLString = PLAY_STORE_URL_SCHEME + PARCHE_PACKAGE_NAME;
        Intent returnIntent;
        if (urlCanBeHandled(aContext, marketURLString)) {
            returnIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketURLString));
        } else {
            //This user does not have the Play app installed - show them the webpage.
            String webURLString = PLAY_STORE_WEB_URL + PARCHE_PACKAGE_NAME;
            returnIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURLString));
        }

        return returnIntent;
    }

    /*******************
     * 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;
    }
}