Android Open Source - snowplow-android-tracker Util






From Project

Back to project page snowplow-android-tracker.

License

The source code is released under:

Apache License

If you think the Android project snowplow-android-tracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2014 Snowplow Analytics Ltd. All rights reserved.
 */*w ww . j  av a 2 s  .co  m*/
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

package com.snowplowanalytics.snowplow.tracker.android;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.lang.reflect.Method;

public class Util extends com.snowplowanalytics.snowplow.tracker.core.Util {

    public static String getAdvertisingID(Context context) {
        return getPlayAdId(context);
    }

    public static String getCarrier(Context context) {
        String carrierName = "";
        TelephonyManager telephonyManager =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            carrierName = telephonyManager.getNetworkOperatorName();
        }
        return carrierName;
    }

    public static Location getLocation(Context context) {
        Location location = null;
        LocationManager locationManager =
                (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);

        String provider = locationManager.getBestProvider(criteria, true);
        if (provider != null) {
            try {
                location = locationManager.getLastKnownLocation(provider);
            } catch (SecurityException ex) {
                Log.d(Util.class.toString(), "No permission to retrieve location.");
                location = null;
            }
        }
        return location;
    }

    public static String getPlayAdId(Context context) {
        try {
            Object AdvertisingInfoObject = getAdvertisingInfoObject(context);

            String playAdid = (String) invokeInstanceMethod(AdvertisingInfoObject, "getId", null);

            return playAdid;
        }
        catch (Throwable t) {
            return null;
        }
    }

    private static Object getAdvertisingInfoObject(Context context)
            throws Exception {
        return invokeStaticMethod("com.google.android.gms.ads.identifier.AdvertisingIdClient",
                "getAdvertisingIdInfo",
                new Class[] {Context.class} , context
        );
    }

    private static Object invokeStaticMethod(String className, String methodName,
                                             Class[] cArgs, Object... args)
            throws Exception {
        Class classObject = Class.forName(className);

        return invokeMethod(classObject, methodName, null, cArgs, args);
    }

    private static Object invokeInstanceMethod(Object instance, String methodName,
                                               Class[] cArgs, Object... args)
            throws Exception {
        Class classObject = instance.getClass();

        return invokeMethod(classObject, methodName, instance, cArgs, args);
    }

    private static Object invokeMethod(Class classObject, String methodName, Object instance,
                                       Class[] cArgs, Object... args)
            throws Exception {
        Method methodObject = classObject.getMethod(methodName, cArgs);

        Object resultObject = methodObject.invoke(instance, args);

        return resultObject;
    }


}




Java Source Code List

com.snowplowanalytics.snowplow.tracker.android.ApplicationTest.java
com.snowplowanalytics.snowplow.tracker.android.Constants.java
com.snowplowanalytics.snowplow.tracker.android.EventStoreHelper.java
com.snowplowanalytics.snowplow.tracker.android.EventStoreTest.java
com.snowplowanalytics.snowplow.tracker.android.EventStore.java
com.snowplowanalytics.snowplow.tracker.android.Parameter.java
com.snowplowanalytics.snowplow.tracker.android.Subject.java
com.snowplowanalytics.snowplow.tracker.android.Tracker.java
com.snowplowanalytics.snowplow.tracker.android.Util.java
com.snowplowanalytics.snowplow.tracker.android.Version.java
com.snowplowanalytics.snowplow.tracker.android.emitter.EmitterTest.java
com.snowplowanalytics.snowplow.tracker.android.emitter.Emitter.java
com.snowplowanalytics.snowplow.tracker.android.payload.SchemaPayload.java
com.snowplowanalytics.snowplow.tracker.android.payload.TrackerPayload.java