package com.ikea.shared.shopping.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.ikea.shared.cart.model.SyncEventModel;
import com.ikea.shared.util.AppExecutors;
import com.ikea.shared.util.L;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public class ShoppingCartSyncService extends Service
{
  public static final String EVENT_DATA = "event_data";
  private static final String ROOT_PATH = "sync_cart_service";
  private final ExecutorService mExecutorService = AppExecutors.highPrio();
  private Future<Boolean> mFuture;
  private PersistentQueue mQueue;

  private void loadEventQueueIfRequired()
  {
    if (!this.mQueue.isLoaded())
      this.mQueue.load();
  }

  private void startProcessingIfRequired()
  {
    L.D(this, "Lets start the sync service background thread ");
    if ((this.mFuture == null) || (this.mFuture.isDone()))
      this.mFuture = this.mExecutorService.submit(new Callable()
      {
        public Boolean call()
          throws Exception
        {
          ShoppingCartSyncService.this.loadEventQueueIfRequired();
          L.D(this, "pending event in event queue are " + ShoppingCartSyncService.this.mQueue.size());
          int i = 0;
          while ((!ShoppingCartSyncService.this.mQueue.isEmpty()) && (i <= 0))
          {
            if (!ShoppingCartService.i(ShoppingCartSyncService.this).processEventQueue(ShoppingCartSyncService.this.mQueue))
              i++;
            try
            {
              Thread.sleep(100L);
            }
            catch (Exception localException)
            {
              localException.printStackTrace();
            }
          }
          ShoppingCartSyncService.this.stopSelf();
          ShoppingCartSyncService.access$202(ShoppingCartSyncService.this, null);
          ShoppingCartService.i(ShoppingCartSyncService.this).notifyShoppingList(ShoppingCartSyncService.this.mQueue.isEmpty());
          System.gc();
          return Boolean.valueOf(true);
        }
      });
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  public void onCreate()
  {
    super.onCreate();
    this.mQueue = new PersistentQueue(this, "sync_cart_service");
  }

  public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2)
  {
    L.I("ShoppingCartSyncService.onHandleIntent()");
    loadEventQueueIfRequired();
    if ((paramIntent == null) || (paramIntent.getExtras() == null))
    {
      startProcessingIfRequired();
      return 2;
    }
    SyncEventModel localSyncEventModel = (SyncEventModel)paramIntent.getSerializableExtra("event_data");
    if (localSyncEventModel != null)
    {
      if (!localSyncEventModel.getEvent().equals("event_clear"))
        break label89;
      this.mQueue.reset();
    }
    while (true)
    {
      L.D(this, "sync service request for event processing. with data " + localSyncEventModel);
      break;
      label89: if (localSyncEventModel != null)
      {
        this.mQueue.push(localSyncEventModel);
        startProcessingIfRequired();
      }
    }
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.shared.shopping.service.ShoppingCartSyncService
 * JD-Core Version:    0.6.2
 */