Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * The MIT License (MIT)
 * Copyright  2016 Steve Guidetti
 *
 * 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.
 */

import android.content.Context;

import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class Main {
    /**
     * The package names for the apps
     */
    private static final String[] sPackageNames = new String[] { "com.ultramegasoft.flavordex2.lite",
            "com.flavordex.beer", "com.flavordex.wine", "com.flavordex.whiskey", "com.flavordex.coffee" };

    /**
     * Check if an app is installed on the device.
     *
     * @param context The Context
     * @param app     The app
     * @return Whether the app is installed
     */
    private static boolean isAppInstalled(@NonNull Context context, int app) {
        return isAppInstalled(context, app, false);
    }

    /**
     * Check if an app is installed on the device.
     *
     * @param context      The Context
     * @param app          The app
     * @param checkSupport Check whether the app supports importing
     * @return Whether the app is installed
     */
    public static boolean isAppInstalled(@NonNull Context context, int app, boolean checkSupport) {
        final ProviderInfo pi = getProviderInfo(context.getPackageManager(), app);
        return pi != null && (!checkSupport || pi.exported);
    }

    /**
     * Get the information about an app's ContentProvider.
     *
     * @param pm  The PackageManager
     * @param app The app
     * @return The ProviderInfo for the app's ContentProvider
     */
    @Nullable
    private static ProviderInfo getProviderInfo(@NonNull PackageManager pm, int app) {
        return pm.resolveContentProvider(sPackageNames[app] + ".provider", 0);
    }
}