Back to project page android-sdk.
The source code is released under:
Copyright (c) 2013 Adcash OU. All rights reserved under Creative Commons Attribution 3.0 Unported http://creativecommons.org/licenses/by/3.0/ Redistribution and use in source and binary forms, with or...
If you think the Android project android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.adcash.mobileads; // w ww .ja v a 2s . c o m import static com.adcash.mobileads.util.Reflection.MethodBuilder; import static com.adcash.mobileads.util.Reflection.classFound; import java.lang.ref.WeakReference; import com.adcash.mobileads.util.AsyncTasks; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; public class GpsHelper { static public final int GOOGLE_PLAY_SUCCESS_CODE = 0; static public final String ADVERTISING_ID_KEY = "advertisingId"; static public final String IS_LIMIT_AD_TRACKING_ENABLED_KEY = "isLimitAdTrackingEnabled"; private static String sPlayServicesUtilClassName = "com.google.android.gms.common.GooglePlayServicesUtil"; private static String sAdvertisingIdClientClassName = "com.google.android.gms.ads.identifier.AdvertisingIdClient"; public interface GpsHelperListener { public void onFetchAdInfoCompleted(); } static boolean isGpsAvailable(final Context context) { try { Object result = new MethodBuilder(null, "isGooglePlayServicesAvailable") .setStatic(Class.forName(sPlayServicesUtilClassName)) .addParam(Context.class, context).execute(); return (result != null && (Integer) result == GOOGLE_PLAY_SUCCESS_CODE); } catch (Exception exception) { Log.i("Adcash", exception.toString()); return false; } } static public void asyncFetchAdvertisingInfoIfNotCached(final Context context, final GpsHelperListener gpsHelperListener) { // This method guarantees that the Google Play Services (GPS) advertising info will // be populated if GPS is available and the ad info is not already cached // The above will happen before the callback is run if (isGpsAvailable(context) && !isSharedPreferencesPopluated(context)) { asyncFetchAdvertisingInfo(context, gpsHelperListener); } else { gpsHelperListener.onFetchAdInfoCompleted(); } } static public void asyncFetchAdvertisingInfo(final Context context) { asyncFetchAdvertisingInfo(context, null); } static String getAdvertisingId(final Context context) { final String defaultValue = null; if (isGpsAvailable(context)) { return SharedPreferencesHelper.getSharedPreferences(context) .getString(ADVERTISING_ID_KEY, defaultValue); } else { return defaultValue; } } static public boolean isLimitAdTrackingEnabled(Context context) { final boolean defaultValue = false; if (isGpsAvailable(context)) { return SharedPreferencesHelper.getSharedPreferences(context) .getBoolean(IS_LIMIT_AD_TRACKING_ENABLED_KEY, defaultValue); } else { return defaultValue; } } static boolean isSharedPreferencesPopluated(final Context context) { SharedPreferences sharedPreferences = SharedPreferencesHelper.getSharedPreferences(context); return sharedPreferences.contains(ADVERTISING_ID_KEY) && sharedPreferences.contains(IS_LIMIT_AD_TRACKING_ENABLED_KEY); } static private class FetchAdvertisingInfoTask extends AsyncTask<Void, Void, Void> { private WeakReference<Context> mContextWeakReference; private WeakReference<GpsHelperListener> mGpsHelperListenerWeakReference; public FetchAdvertisingInfoTask(Context context, GpsHelperListener gpsHelperListener) { mContextWeakReference = new WeakReference<Context>(context); mGpsHelperListenerWeakReference = new WeakReference<GpsHelperListener>(gpsHelperListener); } @Override protected Void doInBackground(Void... voids) { try { Context context = mContextWeakReference.get(); if (context == null) { return null; } Object adInfo = new MethodBuilder(null, "getAdvertisingIdInfo") .setStatic(Class.forName(sAdvertisingIdClientClassName)) .addParam(Context.class, context).execute(); if (adInfo != null) { updateSharedPreferences(context, adInfo); } } catch (Exception exception) { Log.i("Adcash", "Unable to obtain AdvertisingIdClient.getAdvertisingIdInfo()"); } return null; } @Override protected void onPostExecute(Void aVoid) { GpsHelperListener gpsHelperListener = mGpsHelperListenerWeakReference.get(); if (gpsHelperListener != null) { gpsHelperListener.onFetchAdInfoCompleted(); } } } static public void asyncFetchAdvertisingInfo(final Context context, final GpsHelperListener gpsHelperListener) { if (!classFound(sAdvertisingIdClientClassName)) { if (gpsHelperListener != null) { gpsHelperListener.onFetchAdInfoCompleted(); } return; } try { AsyncTasks.safeExecuteOnExecutor(new FetchAdvertisingInfoTask(context, gpsHelperListener)); } catch (Exception exception) { if (gpsHelperListener != null) { gpsHelperListener.onFetchAdInfoCompleted(); } } } static void updateSharedPreferences(final Context context, final Object adInfo) { String advertisingId = reflectedGetAdvertisingId(adInfo, null); Log.i("Adcasg", advertisingId); boolean isLimitAdTrackingEnabled = reflectedIsLimitAdTrackingEnabled(adInfo, false); /* * Committing using the editor is atomic; a single editor must always commit * to ensure that the state of the GPS variables are in sync. */ SharedPreferencesHelper.getSharedPreferences(context) .edit() .putString(ADVERTISING_ID_KEY, advertisingId) .putBoolean(IS_LIMIT_AD_TRACKING_ENABLED_KEY, isLimitAdTrackingEnabled) .commit(); } static String reflectedGetAdvertisingId(final Object adInfo, final String defaultValue) { try { return (String) new MethodBuilder(adInfo, "getId").execute(); } catch (Exception exception) { Log.i("Adcash", exception.toString()); return defaultValue; } } static boolean reflectedIsLimitAdTrackingEnabled(final Object adInfo, final boolean defaultValue) { try { Boolean result = (Boolean) new MethodBuilder(adInfo, "isLimitAdTrackingEnabled").execute(); return (result != null) ? result : defaultValue; } catch (Exception exception) { Log.i("Adcash", exception.toString()); return defaultValue; } } }