AppIndexingService.java 11.2 KB
package com.bitstrips.imoji.firebase;

import android.app.IntentService;
import android.app.job.JobParameters;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import com.bitstrips.imoji.InjectorApplication;
import com.bitstrips.imoji.behaviour.BehaviourHelper;
import com.bitstrips.imoji.firebase.models.IndexableSticker;
import com.bitstrips.imoji.firebase.models.IndexableStickerPack;
import com.bitstrips.imoji.manager.AppIndexingManager;
import com.bitstrips.imoji.manager.StickerPacksManager;
import com.bitstrips.imoji.models.AvatarInfo;
import com.bitstrips.imoji.models.StickerPacks;
import com.bitstrips.imoji.monouser.oauth2.OAuth2Manager;
import com.bitstrips.imoji.util.AuthorizationException;
import com.bitstrips.imoji.util.AvatarInfoUtils;
import com.bitstrips.imoji.util.FileUtil;
import com.bitstrips.imoji.util.PreferenceTimer;
import com.bitstrips.imoji.util.PreferenceUtils;
import com.crashlytics.android.Crashlytics;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class AppIndexingService
  extends IntentService
{
  private static final int DEFAULT_INDEX_BATCH_SIZE = 400;
  private static final String INDEXED_STICKERS_CACHE_FILE = "firebase_indexed_stickers";
  private static final String INDEXED_STICKER_PACKS_CACHE_FILE = "firebase_indexed_sticker_packs";
  public static final String INTENT_FLAG_FORCE_FLUSH_FIREBASE = "forceFlushFirebase";
  public static final String INTENT_FLAG_FORCE_REQUEST_AVATAR_INFO = "forceRequestAvatarInfo";
  public static final String INTENT_FLAG_FORCE_REQUEST_STICKER_PACKS = "forceRequestStickerPacks";
  public static final String INTENT_FLAG_INDEX_REASON = "reason";
  public static final String INTENT_FLAG_JOB_PARAMS = "jobParams";
  public static final String INTENT_FLAG_MESSENGER = "messenger";
  private static final String LOG_TAG = "AppIndexingService";
  private static final int SCHEDULED_INDEX_BATCH_SIZE = 200;
  private static final long SHOULD_UPDATE_STICKERS_INTERVAL = TimeUnit.HOURS.toMillis(1L);
  public static final String UPDATE_INDEX_REASON_APP_OPEN = "app_open";
  public static final String UPDATE_INDEX_REASON_LOCALE_CHANGED = "locale_changed";
  public static final String UPDATE_INDEX_REASON_SCHEDULED = "scheduled";
  public static final String UPDATE_INDEX_REASON_STICKERS_CHANGED = "stickers_changed";
  public static final String UPDATE_INDEX_REASON_THIRD_PARTY_OPEN = "third_party_open";
  @Inject
  AppIndexingManager mAppIndexingManager;
  @Inject
  AvatarInfoUtils mAvatarInfoUtils;
  @Inject
  BehaviourHelper mBehaviourHelper;
  @Inject
  FileUtil mFileUtil;
  @Inject
  OAuth2Manager mOAuth2Manager;
  @Inject
  PreferenceUtils mPreferenceUtils;
  @Inject
  StickerPacksManager mStickerPacksManager;
  
  public AppIndexingService()
  {
    super("AppIndexingService");
  }
  
  private void clearAllIndexables()
  {
    long l = System.currentTimeMillis();
    clearCachedIndexableUrls();
    FirebaseUtils.clearFirebaseSync();
    Log.i("AppIndexingService", MessageFormat.format("Successfully removed all app index - {0}ms", new Object[] { Long.valueOf(System.currentTimeMillis() - l) }));
  }
  
  private void clearCachedIndexableUrls()
  {
    this.mFileUtil.saveGsonToFile("firebase_indexed_sticker_packs", null);
    this.mFileUtil.saveGsonToFile("firebase_indexed_stickers", null);
  }
  
  private void clearLastSuccessfulUpdateTimer()
  {
    this.mPreferenceUtils.getTimer(2131231266).setTimestamp(0L);
  }
  
  private String getAvatarId(boolean paramBoolean)
  {
    String str = this.mPreferenceUtils.getString(2131231175, null);
    if (!paramBoolean) {}
    for (;;)
    {
      return str;
      try
      {
        Object localObject = this.mAvatarInfoUtils.getLatestAvatarInfoSync();
        if (localObject != null)
        {
          localObject = ((AvatarInfo)localObject).getId();
          return (String)localObject;
        }
      }
      catch (AuthorizationException localAuthorizationException)
      {
        return null;
      }
      catch (Exception localException)
      {
        Log.e("AppIndexingService", "Avatar info fetching failed", localException);
      }
    }
    return localAuthorizationException;
  }
  
  private Map<String, IndexableSticker> getCachedStickerMap()
  {
    IndexableSticker[] arrayOfIndexableSticker = (IndexableSticker[])this.mFileUtil.readGsonFromFile("firebase_indexed_stickers", IndexableSticker[].class);
    HashMap localHashMap = new HashMap();
    if ((arrayOfIndexableSticker == null) || (arrayOfIndexableSticker.length == 0)) {
      return localHashMap;
    }
    int j = arrayOfIndexableSticker.length;
    int i = 0;
    while (i < j)
    {
      IndexableSticker localIndexableSticker = arrayOfIndexableSticker[i];
      localHashMap.put(localIndexableSticker.getGeneratedUrl(), localIndexableSticker);
      i += 1;
    }
    return localHashMap;
  }
  
  private Map<String, IndexableStickerPack> getCachedStickerPackMap()
  {
    IndexableStickerPack[] arrayOfIndexableStickerPack = (IndexableStickerPack[])this.mFileUtil.readGsonFromFile("firebase_indexed_sticker_packs", IndexableStickerPack[].class);
    HashMap localHashMap = new HashMap();
    if ((arrayOfIndexableStickerPack == null) || (arrayOfIndexableStickerPack.length == 0)) {
      return localHashMap;
    }
    int j = arrayOfIndexableStickerPack.length;
    int i = 0;
    while (i < j)
    {
      IndexableStickerPack localIndexableStickerPack = arrayOfIndexableStickerPack[i];
      localHashMap.put(localIndexableStickerPack.getUrl(), localIndexableStickerPack);
      i += 1;
    }
    return localHashMap;
  }
  
  private static int getIndexingMaxBatchSize(String paramString)
  {
    if ("scheduled".equals(paramString)) {
      return 200;
    }
    return 400;
  }
  
  private void setLastSuccessfulUpdateTimer(String paramString)
  {
    if (!"scheduled".equals(paramString)) {
      this.mPreferenceUtils.getTimer(2131231266).setTimestamp(System.currentTimeMillis());
    }
  }
  
  private void updateFirebaseStickerPacks(StickerPacks paramStickerPacks, String paramString, Map<String, IndexableStickerPack> paramMap, int paramInt)
  {
    long l = System.currentTimeMillis();
    paramStickerPacks = AppIndexablesFactory.generateIndexableStickerPacks(paramStickerPacks.get(), paramString, paramMap);
    FirebaseUtils.batchUpdateFirebaseSync(paramStickerPacks.getIndexableUrlsToRemove(), paramStickerPacks.getIndexablesToUpdate(), paramInt);
    Log.i("AppIndexingService", MessageFormat.format("Successfully updated app index - sticker packs - {0}ms", new Object[] { Long.valueOf(System.currentTimeMillis() - l) }));
    this.mFileUtil.saveGsonToFile("firebase_indexed_sticker_packs", paramStickerPacks.getSerializables().toArray());
  }
  
  private void updateFirebaseStickers(StickerPacks paramStickerPacks, String paramString, Map<String, IndexableSticker> paramMap, int paramInt)
  {
    long l = System.currentTimeMillis();
    paramStickerPacks = AppIndexablesFactory.generateIndexableStickers(paramStickerPacks.get(), paramString, paramMap);
    FirebaseUtils.batchUpdateFirebaseSync(paramStickerPacks.getIndexableUrlsToRemove(), paramStickerPacks.getIndexablesToUpdate(), paramInt);
    Log.i("AppIndexingService", MessageFormat.format("Successfully updated app index - stickers - {0}ms", new Object[] { Long.valueOf(System.currentTimeMillis() - l) }));
    this.mFileUtil.saveGsonToFile("firebase_indexed_stickers", paramStickerPacks.getSerializables().toArray());
  }
  
  private boolean updateIndex(boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3, int paramInt)
  {
    try
    {
      String str = getAvatarId(paramBoolean1);
      if (TextUtils.isEmpty(str))
      {
        Log.w("AppIndexingService", "User is not logged in. Stop indexing..");
        return true;
      }
    }
    catch (Exception localException1)
    {
      Log.e("AppIndexingService", "Avatar info fetching failed", localException1);
      Crashlytics.logException(localException1);
      return false;
    }
    Map localMap1 = getCachedStickerPackMap();
    Map localMap2 = getCachedStickerMap();
    StickerPacks localStickerPacks;
    try
    {
      localStickerPacks = this.mStickerPacksManager.updateStickerPacksIfNecessarySync(paramBoolean2, SHOULD_UPDATE_STICKERS_INTERVAL);
      if (localStickerPacks == null) {
        return false;
      }
    }
    catch (Exception localException2)
    {
      Log.e("AppIndexingService", "Sticker packs fetching failed", localException2);
      Crashlytics.logException(localException2);
      return false;
    }
    if (!paramBoolean3) {}
    try
    {
      if ((localMap1.isEmpty()) || (localMap2.isEmpty())) {
        clearAllIndexables();
      }
      updateFirebaseStickerPacks(localStickerPacks, localException2, localMap1, paramInt);
      updateFirebaseStickers(localStickerPacks, localException2, localMap2, paramInt);
      return true;
    }
    catch (Exception localException3)
    {
      clearCachedIndexableUrls();
      clearLastSuccessfulUpdateTimer();
      Log.e("AppIndexingService", "Templates update failed", localException3);
      Crashlytics.logException(localException3);
    }
    return false;
  }
  
  public void onCreate()
  {
    super.onCreate();
    ((InjectorApplication)getApplication()).inject(this);
  }
  
  protected void onHandleIntent(Intent paramIntent)
  {
    if (paramIntent == null) {
      return;
    }
    boolean bool1 = paramIntent.getBooleanExtra("forceRequestAvatarInfo", false);
    boolean bool2 = paramIntent.getBooleanExtra("forceRequestStickerPacks", false);
    boolean bool3 = paramIntent.getBooleanExtra("forceFlushFirebase", false);
    Object localObject = paramIntent.getStringExtra("reason");
    Messenger localMessenger = (Messenger)paramIntent.getParcelableExtra("messenger");
    if (((bool1) || (this.mBehaviourHelper.isTokenRefreshForced())) && (this.mOAuth2Manager.isUserLoggedIn())) {
      this.mOAuth2Manager.syncRefreshToken(this.mBehaviourHelper.isTokenRefreshForced());
    }
    if (updateIndex(bool1, bool2, bool3, getIndexingMaxBatchSize((String)localObject)))
    {
      setLastSuccessfulUpdateTimer((String)localObject);
      this.mAppIndexingManager.notifyFirebaseIndexCompleteOnMainThread();
    }
    for (int i = 1;; i = 0)
    {
      this.mAppIndexingManager.setIsUpdatingIndex(false);
      if ((Build.VERSION.SDK_INT < 21) || (localMessenger == null)) {
        break;
      }
      paramIntent = (JobParameters)paramIntent.getParcelableExtra("jobParams");
      localObject = Message.obtain();
      ((Message)localObject).what = 100;
      ((Message)localObject).arg1 = i;
      ((Message)localObject).obj = paramIntent;
      try
      {
        localMessenger.send((Message)localObject);
        return;
      }
      catch (RemoteException paramIntent)
      {
        Log.e("AppIndexingService", "Couldn't send message to finish job!", paramIntent);
        return;
      }
    }
  }
}


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