Example usage for android.content Context getClass

List of usage examples for android.content Context getClass

Introduction

In this page you can find the example usage for android.content Context getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:edu.channel4.mobilemetrics.sdk.android.MMSession.java

/**
 * Constructs a new {@link MMSession} object.
 *
 * @param context The context used to access resources on behalf of the app. It is recommended to use
 *            {@link Context#getApplicationContext()} to avoid the potential memory leak incurred by maintaining references to
 *            {@code Activity} instances. Cannot be null.
 * @param key The key unique for each application generated at www.localytics.com. Cannot be null or empty.
 * @throws IllegalArgumentException if {@code context} is null
 * @throws IllegalArgumentException if {@code key} is null or empty
 *//*w ww. jav a 2s  .c o m*/
public MMSession(final Context context, final String key) {
    if (context == null) {
        throw new IllegalArgumentException("context cannot be null"); //$NON-NLS-1$
    }
    if (TextUtils.isEmpty(key)) {
        throw new IllegalArgumentException("key cannot be null or empty"); //$NON-NLS-1$
    }

    /*
     * Get the application context to avoid having the Localytics object holding onto an Activity object. Using application
     * context is very important to prevent the customer from giving the library multiple different contexts with different
     * package names, which would corrupt the events in the database.
     *
     * Although RenamingDelegatingContext is part of the Android SDK, the class isn't present in the ClassLoader unless the
     * process is being run as a unit test. For that reason, comparing class names is necessary instead of doing instanceof.
     *
     * Note that getting the application context may have unpredictable results for apps sharing a process running Android 2.1
     * and earlier. See <http://code.google.com/p/android/issues/detail?id=4469> for details.
     */
    mContext = !(context.getClass().getName().equals("android.test.RenamingDelegatingContext")) //$NON-NLS-1$
            && Constants.CURRENT_API_LEVEL >= 8 ? context.getApplicationContext() : context;
    mLocalyticsKey = key;

    /**
     *  Start the process of swapping credentials for an access token to load data into the Salesforce.com database.
     */
    new AuthenticateSalesforceTask().execute();

    mSessionHandler = new SessionHandler(mContext, mLocalyticsKey, sSessionHandlerThread.getLooper());

    /*
     * Complete Handler initialization on a background thread. Note that this is not generally a good best practice, as the
     * MMSession object (and its child objects) should be fully initialized by the time the constructor returns.
     * However this implementation is safe, as the Handler will process this initialization message before any other message.
     */
    mSessionHandler.sendMessage(mSessionHandler.obtainMessage(SessionHandler.MESSAGE_INIT));

}

From source file:com.sidekickApp.analytics.LocalyticsSession.java

/**
 * Constructs a new {@link LocalyticsSession} object.
 *
 * @param context The context used to access resources on behalf of the app. It is recommended to use
 *            {@link Context#getApplicationContext()} to avoid the potential memory leak incurred by maintaining references to
 *            {@code Activity} instances. Cannot be null.
 * @param key The key unique for each application generated at www.localytics.com. Cannot be null or empty.
 * @throws IllegalArgumentException if {@code context} is null
 * @throws IllegalArgumentException if {@code key} is null or empty
 *//*from w ww  .  ja va 2 s .com*/
public LocalyticsSession(final Context context, final String key) {
    if (context == null) {
        throw new IllegalArgumentException("context cannot be null"); //$NON-NLS-1$
    }
    if (TextUtils.isEmpty(key)) {
        throw new IllegalArgumentException("key cannot be null or empty"); //$NON-NLS-1$
    }

    /*
     * Prevent the client from providing a subclass of Context that returns the Localytics package name.
     *
     * Note that because getPackageName() is a method and could theoretically return different results with each invocation,
     * this check doesn't guarantee that a nefarious caller will be detected.
     */
    if (Constants.LOCALYTICS_PACKAGE_NAME.equals(context.getPackageName())
            && !context.getClass().getName().equals("android.test.IsolatedContext") //$NON-NLS-1$
            && !context.getClass().getName().equals("android.test.RenamingDelegatingContext")) //$NON-NLS-1$
    {
        throw new IllegalArgumentException(
                String.format("context.getPackageName() returned %s", context.getPackageName())); //$NON-NLS-1$
    }

    /*
     * Get the application context to avoid having the Localytics object holding onto an Activity object. Using application
     * context is very important to prevent the customer from giving the library multiple different contexts with different
     * package names, which would corrupt the events in the database.
     *
     * Although RenamingDelegatingContext is part of the Android SDK, the class isn't present in the ClassLoader unless the
     * process is being run as a unit test. For that reason, comparing class names is necessary instead of doing instanceof.
     *
     * Note that getting the application context may have unpredictable results for apps sharing a process running Android 2.1
     * and earlier. See <http://code.google.com/p/android/issues/detail?id=4469> for details.
     */
    mContext = !(context.getClass().getName().equals("android.test.RenamingDelegatingContext")) //$NON-NLS-1$
            && Constants.CURRENT_API_LEVEL >= 8 ? context.getApplicationContext() : context;

    synchronized (sLocalyticsSessionIntrinsicLock) {
        SessionHandler handler = sLocalyticsSessionHandlerMap.get(key);

        if (null == handler) {
            handler = new SessionHandler(mContext, key, sSessionHandlerThread.getLooper());
            sLocalyticsSessionHandlerMap.put(key, handler);

            /*
             * Complete Handler initialization on a background thread. Note that this is not generally a good best practice,
             * as the LocalyticsSession object (and its child objects) should be fully initialized by the time the constructor
             * returns. However this implementation is safe, as the Handler will process this initialization message before
             * any other message.
             */
            handler.sendMessage(handler.obtainMessage(SessionHandler.MESSAGE_INIT));
        }

        mSessionHandler = handler;
    }
}

From source file:com.localytics.android.LocalyticsSession.java

/**
 * Constructs a new {@link com.localytics.android.LocalyticsSession} object.
 *
 * @param context The context used to access resources on behalf of the app. It is recommended to use
 *            {@link android.content.Context#getApplicationContext()} to avoid the potential memory leak incurred by maintaining references to
 *            {@code Activity} instances. Cannot be null.
 * @param key The key unique for each application generated at www.localytics.com. Cannot be null or empty.
 * @throws IllegalArgumentException if {@code context} is null
 * @throws IllegalArgumentException if {@code key} is null or empty
 *///from w ww.j a  va 2  s.  c  o  m
public LocalyticsSession(final Context context, final String key) {
    if (context == null) {
        throw new IllegalArgumentException("context cannot be null"); //$NON-NLS-1$
    }

    String appKey = key;
    if (TextUtils.isEmpty(appKey)) {
        appKey = DatapointHelper.getLocalyticsAppKeyOrNull(context);
    }

    if (TextUtils.isEmpty(appKey)) {
        throw new IllegalArgumentException("key cannot be null or empty"); //$NON-NLS-1$
    }

    /*
     * Prevent the client from providing a subclass of Context that returns the Localytics package name.
     *
     * Note that because getPackageName() is a method and could theoretically return different results with each invocation,
     * this check doesn't guarantee that a nefarious caller will be detected.
     */
    if (Constants.LOCALYTICS_PACKAGE_NAME.equals(context.getPackageName())
            && !context.getClass().getName().equals("android.test.IsolatedContext") //$NON-NLS-1$
            && !context.getClass().getName().equals("android.test.RenamingDelegatingContext")) //$NON-NLS-1$
    {
        throw new IllegalArgumentException(
                String.format("context.getPackageName() returned %s", context.getPackageName())); //$NON-NLS-1$
    }

    /*
     * Get the application context to avoid having the Localytics object holding onto an Activity object. Using application
     * context is very important to prevent the customer from giving the library multiple different contexts with different
     * package names, which would corrupt the events in the database.
     *
     * Although RenamingDelegatingContext is part of the Android SDK, the class isn't present in the ClassLoader unless the
     * process is being run as a unit test. For that reason, comparing class names is necessary instead of doing instanceof.
     *
     * Note that getting the application context may have unpredictable results for apps sharing a process running Android 2.1
     * and earlier. See <http://code.google.com/p/android/issues/detail?id=4469> for details.
     */
    mContext = !(context.getClass().getName().equals("android.test.RenamingDelegatingContext")) //$NON-NLS-1$
            && Constants.CURRENT_API_LEVEL >= 8 ? context.getApplicationContext() : context;

    synchronized (sLocalyticsSessionIntrinsicLock) {
        SessionHandler handler = sLocalyticsSessionHandlerMap.get(appKey);

        if (null == handler) {
            handler = new SessionHandler(mContext, appKey, sSessionHandlerThread.getLooper());
            sLocalyticsSessionHandlerMap.put(appKey, handler);

            /*
             * Complete Handler initialization on a background thread. Note that this is not generally a good best practice,
             * as the LocalyticsSession object (and its child objects) should be fully initialized by the time the constructor
             * returns. However this implementation is safe, as the Handler will process this initialization message before
             * any other message.
             */
            handler.sendMessage(handler.obtainMessage(SessionHandler.MESSAGE_INIT));
        }

        mSessionHandler = handler;
    }
}