BitstripsAnalyticsService.java 8.67 KB
package com.bitstrips.imoji.analytics;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import com.bitstrips.imoji.analytics.performance.AnalyticsLabelKey;
import com.bitstrips.imoji.api.BitmojiApi;
import com.bitstrips.imoji.behaviour.BehaviourHelper;
import com.bitstrips.imoji.experiments.Experiments;
import com.bitstrips.imoji.models.BatchAnalyticsEvent;
import com.bitstrips.imoji.monouser.oauth2.OAuth2Manager;
import com.bitstrips.imoji.util.PreferenceUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.json.JSONObject;
import retrofit.Callback;
import retrofit.RetrofitError;

@Singleton
public class BitstripsAnalyticsService
  extends BatchedAnalyticsService
  implements LegacyAnalyticsService
{
  private static final String d = BitstripsAnalyticsService.class.getSimpleName();
  private static final Type e = new TypeToken() {}.getType();
  private final BitmojiApi f;
  private final PreferenceUtils g;
  private final SharedPreferences h;
  private final Context i;
  private final BehaviourHelper j;
  private final Experiments k;
  private WeakReference<Activity> l;
  private String m;
  private AtomicInteger n = new AtomicInteger(0);
  
  @Inject
  public BitstripsAnalyticsService(BitmojiApi paramBitmojiApi, PreferenceUtils paramPreferenceUtils, @Named("persisted") SharedPreferences paramSharedPreferences, Context paramContext, BehaviourHelper paramBehaviourHelper, Experiments paramExperiments)
  {
    this.f = paramBitmojiApi;
    this.g = paramPreferenceUtils;
    this.h = paramSharedPreferences;
    this.i = paramContext;
    this.j = paramBehaviourHelper;
    this.k = paramExperiments;
  }
  
  private void a(AnalyticsEventBuilder.AnalyticsEvent paramAnalyticsEvent)
  {
    Category localCategory = paramAnalyticsEvent.getCategory();
    if ((localCategory == Category.AVATAR_UX) || (localCategory == Category.AVATAR)) {}
    for (int i1 = 1; i1 != 0; i1 = 0) {
      return;
    }
    i1 = this.n.getAndIncrement();
    paramAnalyticsEvent.setAvatarId(getAvatarId());
    paramAnalyticsEvent.setAppId(Integer.valueOf(getAppId()));
    paramAnalyticsEvent.setHasSnapchatInstalled(OAuth2Manager.hasSnapchatInstalled(this.i));
    paramAnalyticsEvent.setSessionId(this.m);
    paramAnalyticsEvent.setSequenceId(i1);
    this.h.edit().putInt(this.i.getString(2131231262), i1).apply();
    logEvent(paramAnalyticsEvent);
  }
  
  final void a(final List<AnalyticsEventBuilder.AnalyticsEvent> paramList)
  {
    if ((paramList == null) || (paramList.size() == 0)) {
      return;
    }
    BatchAnalyticsEvent localBatchAnalyticsEvent = new BatchAnalyticsEvent(paramList);
    this.f.sendAnalyticsEvents(localBatchAnalyticsEvent, new Callback()
    {
      public final void failure(RetrofitError arg1)
      {
        BitstripsAnalyticsService localBitstripsAnalyticsService = BitstripsAnalyticsService.this;
        Object localObject2 = paramList;
        synchronized (localBitstripsAnalyticsService.a)
        {
          localBitstripsAnalyticsService.b.removeAll((Collection)localObject2);
          localObject2 = ((List)localObject2).iterator();
          while (((Iterator)localObject2).hasNext())
          {
            AnalyticsEventBuilder.AnalyticsEvent localAnalyticsEvent = (AnalyticsEventBuilder.AnalyticsEvent)((Iterator)localObject2).next();
            if (localAnalyticsEvent.getRetryCount() <= 0) {
              localBitstripsAnalyticsService.a.add(localAnalyticsEvent);
            }
          }
        }
        ((BatchedAnalyticsService)localObject1).a();
      }
    });
  }
  
  final void b(List<AnalyticsEventBuilder.AnalyticsEvent> paramList)
  {
    this.h.edit().putString(this.i.getString(2131231301), new Gson().toJson(paramList)).apply();
  }
  
  protected int getAppId()
  {
    return this.i.getResources().getInteger(2131558411);
  }
  
  @VisibleForTesting
  protected String getAvatarId()
  {
    String str2 = this.g.getString(2131231270, null);
    String str1 = str2;
    if (TextUtils.isEmpty(str2)) {
      str1 = "0";
    }
    return str1;
  }
  
  public void init()
  {
    this.n.set(this.h.getInt(this.i.getString(2131231262), 0));
    ??? = this.h.getString(this.i.getString(2131231301), null);
    if (TextUtils.isEmpty((CharSequence)???)) {}
    List localList;
    do
    {
      return;
      localList = (List)new Gson().fromJson((String)???, e);
    } while (this.c);
    this.c = true;
    synchronized (this.a)
    {
      this.a.addAll(localList);
      return;
    }
  }
  
  public void reportStart(@NonNull Activity paramActivity)
  {
    int i2 = 0;
    super.reportStart(paramActivity);
    boolean bool = this.h.getBoolean(this.i.getString(2131231164), false);
    int i1;
    if (this.l == null)
    {
      i1 = 0;
      i2 = 1;
    }
    for (;;)
    {
      this.l = new WeakReference(paramActivity);
      if (i1 != 0)
      {
        return;
        if ((bool) && (this.l.get() != paramActivity)) {
          i1 = 1;
        }
      }
      else
      {
        HashMap localHashMap = new HashMap(3);
        long l1 = System.currentTimeMillis();
        long l2 = this.h.getLong(this.i.getString(2131231260), 0L);
        this.m = this.h.getString(this.i.getString(2131231261), null);
        AnalyticsLabelKey localAnalyticsLabelKey;
        if ((i2 != 0) && (bool))
        {
          localHashMap.put(AnalyticsLabelKey.APP_LAST_CLOSE_STATE, "unexpected");
          localHashMap.put(AnalyticsLabelKey.APP_LAST_SESSION_ID, this.m);
          resetSession();
          localAnalyticsLabelKey = AnalyticsLabelKey.APP_STATE_KEY;
          if (i2 == 0) {
            break label278;
          }
        }
        label278:
        for (paramActivity = "cold";; paramActivity = "warm")
        {
          localHashMap.put(localAnalyticsLabelKey, paramActivity);
          this.h.edit().putBoolean(this.i.getString(2131231164), true).apply();
          sendEvent(Category.APPLICATION, Action.APP_OPEN, localHashMap);
          return;
          if (l1 - l2 > 15000L)
          {
            resetSession();
            break;
          }
          if (!TextUtils.isEmpty(this.m)) {
            break;
          }
          resetSession();
          break;
        }
      }
      i1 = 0;
    }
  }
  
  public void reportStop(@NonNull Activity paramActivity)
  {
    if (paramActivity != this.l.get())
    {
      super.reportStop(paramActivity);
      return;
    }
    this.h.edit().putBoolean(this.i.getString(2131231164), false).putLong(this.i.getString(2131231260), System.currentTimeMillis()).apply();
    sendEvent(Category.APPLICATION, Action.APP_CLOSE);
    super.reportStop(paramActivity);
  }
  
  public void resetSession()
  {
    this.m = UUID.randomUUID().toString();
    this.h.edit().putString(this.i.getString(2131231261), this.m).apply();
  }
  
  public void sendEvent(Category paramCategory, Action paramAction)
  {
    a(new AnalyticsEventBuilder(paramCategory, paramAction, this.k).build());
  }
  
  public void sendEvent(Category paramCategory, Action paramAction, AnalyticsWrapper paramAnalyticsWrapper)
  {
    a(new AnalyticsEventBuilder(paramCategory, paramAction, this.k).withAnalyticsWrapper(paramAnalyticsWrapper).build());
  }
  
  @Deprecated
  public void sendEvent(Category paramCategory, Action paramAction, String paramString)
  {
    a(new AnalyticsEventBuilder(paramCategory, paramAction, this.k).withLabel(paramString).build());
  }
  
  public void sendEvent(Category paramCategory, Action paramAction, Map<AnalyticsLabelKey, Object> paramMap)
  {
    HashMap localHashMap = new HashMap(paramMap.size());
    paramMap = paramMap.entrySet().iterator();
    while (paramMap.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)paramMap.next();
      localHashMap.put(((AnalyticsLabelKey)localEntry.getKey()).toString(), localEntry.getValue());
    }
    a(new AnalyticsEventBuilder(paramCategory, paramAction, this.k).withLabel(new JSONObject(localHashMap).toString()).build());
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/bitstrips/imoji/analytics/BitstripsAnalyticsService.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */