Android Open Source - playnomics-android Util






From Project

Back to project page playnomics-android.

License

The source code is released under:

Apache License

If you think the Android project playnomics-android 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

package com.playnomics.android.util;
// w ww .java  2s  .  com
import java.io.File;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.TimeZone;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Window;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.playnomics.android.session.TouchEventHandler;
import com.playnomics.android.session.WindowCallbackProxy;
import com.playnomics.android.util.Logger.LogLevel;

public class Util implements IRandomGenerator {

  public static final TimeZone TIME_ZONE_GMT = TimeZone.getTimeZone("GMT");
  public static final String UT8_ENCODING = "UTF-8";
  public static final String CONTENT_TYPE_HTML = "text/html";

  private Logger logger;

  public Util(Logger logger) {
    this.logger = logger;
  }

  public long generatePositiveRandomLong() {
    Random rand = new Random();
    return Math.abs(rand.nextLong());
  }

  public String getDeviceIdFromContext(Context context) {
    return Settings.Secure.getString(context.getContentResolver(),
        Settings.Secure.ANDROID_ID);
  }

  public int getApplicationVersionFromContext(Context context) {
    try {
      PackageManager packageManager = context.getPackageManager();
      PackageInfo info;
      info = packageManager.getPackageInfo(context.getPackageName(), 0);
      return info.versionCode;
    } catch (NameNotFoundException ex) {
      // according to Google's docs this should never happen
      logger.log(LogLevel.WARNING, ex,
          "Could not obtain the application version from the package manager");
      // in the event of a failure always return a -1
      return -1;
    }
  }

  public String getApplicationVersionStringFromContext(Context context) {
    try {
      PackageManager packageManager = context.getPackageManager();
      PackageInfo info;
      info = packageManager.getPackageInfo(context.getPackageName(), 0);
      return info.versionName;
    } catch (NameNotFoundException ex) {
      // according to Google's docs this should never happen
      logger.log(LogLevel.WARNING, ex,
          "Could not obtain the application version from the package manager");
      // in the event of a failure always return a -1
      return null;
    }
  }

  public void startUriIntent(String uri, Context context) {
    
    try{
      Intent uriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
      
      PackageManager packageManager = context.getPackageManager();
      List<ResolveInfo> activities = packageManager.queryIntentActivities(uriIntent, 0);
      boolean isIntentSafe = activities.size() > 0;
      
      if(isIntentSafe){
        context.startActivity(uriIntent);
      } else {
        logger.log(LogLevel.WARNING, "Failed to navigate to URI %s. There is nothing to repond to this URI.", uri);
      }
    } catch(Exception ex){
      logger.log(LogLevel.WARNING, "Failed to navigate to URI %s", uri);
      logger.log(LogLevel.WARNING, ex);
    }
  }

  public String getDeviceLanguage() {
    Locale defaultLocale = Locale.getDefault();
    if (defaultLocale == null) {
      return null;
    }
    return defaultLocale.getLanguage();
  }

  public boolean isGooglePlaySdkAvailable(){
    try{
      Class.forName("com.google.android.gms.common.GooglePlayServicesUtil");
      return true;
    } catch(ClassNotFoundException exception){
      logger.log(LogLevel.WARNING, "Google Play Services are not available on this device.");
    }
    return false;
  }
  
  public int getGooglePlayServiceStatus(Context context){
    return GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
  }
  
  public GoogleCloudMessaging getGCMFromContext(Context context){
    return GoogleCloudMessaging.getInstance(context);
  }
  
  public File getContextCacheFile(Context context, String fileName) {
    return new File(context.getCacheDir(), fileName);
  }

  public static boolean stringIsNullOrEmpty(String value) {
    return (value == null || value.isEmpty());
  }

  public static boolean isEmptyObject(JSONObject object) {
    return object.names() == null;
  }

  public static Map<String, Object> getMap(JSONObject object, String key)
      throws JSONException {
    return toMap(object.getJSONObject(key));
  }

  public static Map<String, Object> toMap(JSONObject object)
      throws JSONException {
    Map<String, Object> map = new HashMap<String, Object>();
    @SuppressWarnings("rawtypes")
    Iterator keys = object.keys();
    while (keys.hasNext()) {
      String key = (String) keys.next();
      map.put(key, fromJson(object.get(key)));
    }
    return map;
  }

  @SuppressWarnings({ "rawtypes", "unchecked" })
  public static List toList(JSONArray array) throws JSONException {
    List list = new ArrayList();
    for (int i = 0; i < array.length(); i++) {
      list.add(fromJson(array.get(i)));
    }
    return list;
  }

  private static Object fromJson(Object json) throws JSONException {
    if (json == JSONObject.NULL) {
      return null;
    } else if (json instanceof JSONObject) {
      return toMap((JSONObject) json);
    } else if (json instanceof JSONArray) {
      return toList((JSONArray) json);
    } else {
      return json;
    }
  }

  public void overrideActivityWindowCallback(Activity activity,
      TouchEventHandler handler) {
    Window.Callback currentCallback = activity.getWindow().getCallback();
    activity.getWindow().setCallback(
        WindowCallbackProxy.newCallbackProxyForActivity(
            currentCallback, handler, logger));
  }

  public void removeWindowCallback(Activity activity) {
    Window.Callback callbackProxy = activity.getWindow().getCallback();
    WindowCallbackProxy proxy = (WindowCallbackProxy) Proxy
        .getInvocationHandler(callbackProxy);
    activity.getWindow().setCallback(proxy.getOriginalCallback());
  }

  public void runTaskOnActivityUIThread(Runnable task, Activity activity) {
    activity.runOnUiThread(task);
  }
  
  public Thread startTaskOnBackgroundThread(Runnable task){
    Thread thread = new Thread(task);
    thread.start();
    return thread;
  }

  public static String getDeviceModel() {
    return Build.MODEL;
  }

  public static String getDeviceManufacturer() {
    return Build.MANUFACTURER;
  }

  public static String getAndroidOSVersion() {
    return Build.VERSION.RELEASE;
  }

  public static int safeStringCompare(String s1, String s2) {
    if (s1==null || s2==null)
      return -1;
    return s1.compareTo(s2);
  }
}




Java Source Code List

com.playnomics.PlaynomicsMoreTestActivity.java
com.playnomics.PlaynomicsTestAppActivity.java
com.playnomics.RichDataFrameDelegate.java
com.playnomics.android.client.AssetClient.java
com.playnomics.android.client.EventQueue.java
com.playnomics.android.client.EventWorker.java
com.playnomics.android.client.HttpConnectionFactory.java
com.playnomics.android.client.IEventQueue.java
com.playnomics.android.client.IEventWorker.java
com.playnomics.android.client.IHttpConnectionFactory.java
com.playnomics.android.client.PlacementDataClient.java
com.playnomics.android.client.SegmentationClient.java
com.playnomics.android.events.AppPageEvent.java
com.playnomics.android.events.AppPauseEvent.java
com.playnomics.android.events.AppResumeEvent.java
com.playnomics.android.events.AppRunningEvent.java
com.playnomics.android.events.AppStartEvent.java
com.playnomics.android.events.CustomEvent.java
com.playnomics.android.events.ExplicitEvent.java
com.playnomics.android.events.ImplicitEvent.java
com.playnomics.android.events.PlaynomicsEvent.java
com.playnomics.android.events.TransactionEvent.java
com.playnomics.android.events.UserInfoEvent.java
com.playnomics.android.messaging.CloseButton.java
com.playnomics.android.messaging.HtmlAdFactory.java
com.playnomics.android.messaging.HtmlAd.java
com.playnomics.android.messaging.HtmlCloseButton.java
com.playnomics.android.messaging.MessagingManager.java
com.playnomics.android.messaging.NativeCloseButton.java
com.playnomics.android.messaging.Placement.java
com.playnomics.android.messaging.Position.java
com.playnomics.android.messaging.Target.java
com.playnomics.android.messaging.ui.IPlayViewFactory.java
com.playnomics.android.messaging.ui.PlayDialog.java
com.playnomics.android.messaging.ui.PlayViewFactory.java
com.playnomics.android.messaging.ui.PlayWebView.java
com.playnomics.android.messaging.ui.RenderTaskFactory.java
com.playnomics.android.push.GcmBroadcastReceiver.java
com.playnomics.android.push.GcmIntentService.java
com.playnomics.android.push.GcmManager.java
com.playnomics.android.sdk.IGoogleCloudMessageConfig.java
com.playnomics.android.sdk.IPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementRawDelegate.java
com.playnomics.android.sdk.IPlaynomicsSegmentationDelegate.java
com.playnomics.android.sdk.IPushConfig.java
com.playnomics.android.sdk.IPushNotificationDelegate.java
com.playnomics.android.sdk.Playnomics.java
com.playnomics.android.segments.UserSegmentIds.java
com.playnomics.android.session.ActivityObserver.java
com.playnomics.android.session.GameSessionInfo.java
com.playnomics.android.session.HeartBeatHandler.java
com.playnomics.android.session.HeartBeatProducer.java
com.playnomics.android.session.IActivityObserver.java
com.playnomics.android.session.ICallbackProcessor.java
com.playnomics.android.session.IHeartBeatProducer.java
com.playnomics.android.session.SessionStateMachine.java
com.playnomics.android.session.Session.java
com.playnomics.android.session.TouchEventHandler.java
com.playnomics.android.session.WindowCallbackProxy.java
com.playnomics.android.util.AndroidLogger.java
com.playnomics.android.util.AsyncTaskRunner.java
com.playnomics.android.util.CacheFile.java
com.playnomics.android.util.Config.java
com.playnomics.android.util.ContextWrapper.java
com.playnomics.android.util.EventTime.java
com.playnomics.android.util.IAsyncCall.java
com.playnomics.android.util.IConfig.java
com.playnomics.android.util.IRandomGenerator.java
com.playnomics.android.util.LargeGeneratedId.java
com.playnomics.android.util.LogWriter.java
com.playnomics.android.util.Logger.java
com.playnomics.android.util.Util.java