Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Azure Mobile Engagement Android SDK
 * Copyright (c) Microsoft Corporation
 *
 * All rights reserved.
 *
 * 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.
 */

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Process;

public class Main {
    /** Service class name */
    private static final String SERVICE_CLASS = "com.microsoft.azure.engagement.service.EngagementService";

    /**
     * Return <tt>true</tt> if the caller runs in a process dedicated to the Engagement service.<br/>
     * Return <tt>false</tt> otherwise, e.g. if it's the application process (even if the Engagement
     * service is running in it) or another process.<br/>
     * This method is useful when the <b>android:process</b> attribute has been set on the Engagement
     * service, if this method return <tt>true</tt>, application initialization must not be done in
     * that process. This method is used by {@link EngagementApplication}.
     * @param context the application context.
     * @return <tt>true</tt> if the caller is running in a process dedicated to the Engagement
     *         service, <tt>false</tt> otherwise.
     * @see EngagementApplication
     */
    public static boolean isInDedicatedEngagementProcess(Context context) {
        /* Get our package info */
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                    PackageManager.GET_SERVICES);
        } catch (Exception e) {
            /*
             * NameNotFoundException (uninstalling?) or in some rare scenario an undocumented
             * "RuntimeException: Package manager has died.", probably caused by a system app process
             * crash.
             */
            return false;
        }

        /* Get main process name */
        String mainProcess = packageInfo.applicationInfo.processName;

        /* Get embedded Engagement process name */
        String engagementProcess = null;
        if (packageInfo.services != null)
            for (ServiceInfo serviceInfo : packageInfo.services)
                if (SERVICE_CLASS.equals(serviceInfo.name)) {
                    engagementProcess = serviceInfo.processName;
                    break;
                }

        /* If the embedded Engagement service runs on its own process */
        if (engagementProcess != null && !engagementProcess.equals(mainProcess)) {
            /* The result is to check if the current process is the engagement process */
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            for (RunningAppProcessInfo rapInfo : activityManager.getRunningAppProcesses())
                if (rapInfo.pid == Process.myPid())
                    return rapInfo.processName.equals(engagementProcess);
        }

        /* Otherwise engagement is not running in a separate process (or not running at all) */
        return false;
    }
}