Android Open Source - growthpush-android Growth Push






From Project

Back to project page growthpush-android.

License

The source code is released under:

Apache License

If you think the Android project growthpush-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.growthpush;
//ww  w. jav  a2 s.c o  m
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

import android.content.Context;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.growthpush.handler.DefaultReceiveHandler;
import com.growthpush.handler.ReceiveHandler;
import com.growthpush.model.Client;
import com.growthpush.model.Environment;
import com.growthpush.model.Event;
import com.growthpush.model.Tag;
import com.growthpush.utils.DeviceUtils;

/**
 * Created by Shigeru Ogawa on 13/08/12.
 */
public class GrowthPush {

  public static final String BASE_URL = "https://api.growthpush.com/";

  private static final GrowthPush instance = new GrowthPush();

  private Logger logger = new Logger();
  private Client client = null;
  private Semaphore semaphore = new Semaphore(1);
  private CountDownLatch latch = new CountDownLatch(1);
  private ReceiveHandler receiveHandler = new DefaultReceiveHandler();

  private Context context = null;
  private int applicationId;
  private String secret;
  private Environment environment = null;

  private GrowthPush() {
    super();
  }

  public static GrowthPush getInstance() {
    return instance;
  }

  public GrowthPush initialize(Context context, int applicationId, String secret) {
    return initialize(context, applicationId, secret, Environment.production, false);
  }

  public GrowthPush initialize(Context context, int applicationId, String secret, Environment environment) {
    return initialize(context, applicationId, secret, environment, false);
  }

  public GrowthPush initialize(Context context, int applicationId, String secret, Environment environment, boolean debug) {

    if (this.context != null)
      return this;

    this.context = context;
    this.applicationId = applicationId;
    this.secret = secret;
    this.environment = environment;

    this.logger.setDebug(debug);
    Preference.getInstance().setContext(context);

    client = Preference.getInstance().fetchClient();
    if (client != null && client.getApplicationId() != applicationId)
      this.clearClient();

    return this;

  }

  public GrowthPush register(final String senderId) {

    new Thread(new Runnable() {

      @Override
      public void run() {

        if (context == null)
          throw new IllegalStateException("GrowthPush is not initialized.");

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        try {
          String registrationId = gcm.register(senderId);
          registerClient(registrationId);
        } catch (IOException e) {
        }

      }

    }).start();

    return this;

  }

  public void registerClient(final String registrationId) {

    new Thread(new Runnable() {

      @Override
      public void run() {

        try {

          semaphore.acquire();

          client = Preference.getInstance().fetchClient();
          if (client == null || client.getApplicationId() != applicationId) {
            createClient(registrationId);
            return;
          }

          if ((registrationId != null && !registrationId.equals(client.getToken())) || environment != client.getEnvironment()) {
            updateClient(registrationId);
            return;
          }

          logger.info("Client already registered.");
          latch.countDown();

        } catch (InterruptedException e) {
        } finally {
          semaphore.release();
        }

      }

    }).start();

  }

  private void createClient(final String registrationId) {

    try {

      logger.info(String.format("Registering client... (applicationId: %d, environment: %s)", applicationId, environment));
      GrowthPush.this.client = new Client(registrationId, environment).save(GrowthPush.this);
      logger.info(String.format("Registering client success (clientId: %d)", GrowthPush.this.client.getId()));

      logger.info(String
          .format("See https://growthpush.com/applications/%d/clients to check the client registration.", applicationId));
      Preference.getInstance().saveClient(GrowthPush.this.client);
      latch.countDown();

    } catch (GrowthPushException e) {
      logger.error(String.format("Registering client fail. %s", e.getMessage()));
    }

  }

  private void updateClient(final String registrationId) {

    try {

      logger.info(String.format("Updating client... (applicationId: %d, token: %s, environment: %s)", applicationId, registrationId,
          environment));
      GrowthPush.this.client.setToken(registrationId);
      GrowthPush.this.client.setEnvironment(environment);
      GrowthPush.this.client = GrowthPush.this.client.update();
      logger.info(String.format("Update client success (clientId: %d)", GrowthPush.this.client.getId()));

      Preference.getInstance().saveClient(GrowthPush.this.client);
      latch.countDown();

    } catch (GrowthPushException e) {
      logger.error(String.format("Updating client fail. %s", e.getMessage()));
    }

  }

  public void trackEvent(final String name) {
    trackEvent(name, null);
  }

  public void trackEvent(final String name, final String value) {

    new Thread(new Runnable() {

      @Override
      public void run() {

        if (name == null) {
          logger.warning("Event name cannot be null.");
          return;
        }

        waitClientRegistration();

        logger.info(String.format("Sending event ... (name: %s)", name));
        try {
          Event event = new Event(name, value).save(GrowthPush.this);
          logger.info(String.format("Sending event success. (timestamp: %s)", event.getTimeStamp()));
        } catch (GrowthPushException e) {
          logger.error(String.format("Sending event fail. %s", e.getMessage()));
        }

      }

    }).start();

  }

  public void setTag(final String name) {
    setTag(name, null);
  }

  public void setTag(final String name, final String value) {

    new Thread(new Runnable() {

      @Override
      public void run() {

        if (name == null) {
          logger.warning("Tag name cannot be null.");
          return;
        }

        Tag tag = Preference.getInstance().fetchTag(name);
        if (tag != null && value.equalsIgnoreCase(tag.getValue()))
          return;

        waitClientRegistration();

        logger.info(String.format("Sending tag... (key: %s, value: %s)", name, value));
        try {
          Tag createdTag = new Tag(name, value).save(GrowthPush.this);
          logger.info(String.format("Sending tag success"));
          Preference.getInstance().saveTag(createdTag);
        } catch (GrowthPushException e) {
          logger.error(String.format("Sending tag fail. %s", e.getMessage()));
        }

      }

    }).start();

  }

  public void setDeviceTags() {

    new Thread(new Runnable() {

      @Override
      public void run() {

        if (context == null)
          throw new IllegalStateException("GrowthPush is not initialized.");

        setTag("Device", DeviceUtils.getDevice());
        setTag("OS", DeviceUtils.getOs());
        setTag("Language", DeviceUtils.getLanguage());
        setTag("Time Zone", DeviceUtils.getTimeZone());
        setTag("Version", DeviceUtils.getVersion(context));
        setTag("Build", DeviceUtils.getBuild(context));

      }

    }).start();

  }

  public void setReceiveHandler(ReceiveHandler receiveHandler) {
    this.receiveHandler = receiveHandler;
  }

  public ReceiveHandler getReceiveHandler() {
    return receiveHandler;
  }

  public int getApplicationId() {
    return applicationId;
  }

  public String getSecret() {
    return secret;
  }

  public Logger getLogger() {
    return logger;
  }

  public Client getClient() {
    return client;
  }

  private void waitClientRegistration() {

    if (client == null) {
      try {
        latch.await();
      } catch (InterruptedException e) {
      }
    }

  }

  private void clearClient() {

    this.client = null;
    Preference.getInstance().deleteClient();
    Preference.getInstance().deleteTags();

  }

}




Java Source Code List

com.growthpush.BroadcastReceiver.java
com.growthpush.GrowthPushException.java
com.growthpush.GrowthPush.java
com.growthpush.Logger.java
com.growthpush.Preference.java
com.growthpush.Thread.java
com.growthpush.bridge.ExternalFrameworkBridge.java
com.growthpush.bridge.ExternalFrameworkBroadcastReceiver.java
com.growthpush.growthpushsample.MainActivity.java
com.growthpush.handler.BaseReceiveHandler.java
com.growthpush.handler.DefaultReceiveHandler.java
com.growthpush.handler.OnlyAlertReceiveHandler.java
com.growthpush.handler.OnlyNotificationReceiveHandler.java
com.growthpush.handler.ReceiveHandler.java
com.growthpush.model.ClientStatus.java
com.growthpush.model.Client.java
com.growthpush.model.Environment.java
com.growthpush.model.Error.java
com.growthpush.model.Event.java
com.growthpush.model.Model.java
com.growthpush.model.Tag.java
com.growthpush.utils.DeviceUtils.java
com.growthpush.utils.IOUtils.java
com.growthpush.utils.PermissionUtils.java
com.growthpush.utils.SystemUtils.java
com.growthpush.view.AlertActivity.java
com.growthpush.view.AlertFragment.java
com.growthpush.view.DialogCallback.java