package com.ikea.shared.geofence;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.Builder;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingApi;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.GeofencingRequest.Builder;
import com.google.android.gms.location.LocationServices;
import com.ikea.shared.util.L;
import com.ikea.shared.util.ServiceCallback;
import java.util.ArrayList;
import java.util.List;

public class GeofenceRequester
  implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
  private GoogleApiClient googleApiClient;
  private ServiceCallback<Integer> mCallback;
  private final Context mCtx;
  private ArrayList<Geofence> mCurrentGeofences;
  private PendingIntent mGeofencePendingIntent;
  private boolean mInProgress;

  public GeofenceRequester(Context paramContext)
  {
    this.mCtx = paramContext;
    this.mGeofencePendingIntent = null;
    this.googleApiClient = null;
    this.mInProgress = false;
  }

  private PendingIntent createRequestPendingIntent()
  {
    if (this.mGeofencePendingIntent != null)
      return this.mGeofencePendingIntent;
    Intent localIntent = new Intent("com.ikea.shared.geofence.ACTION_RECEIVE_GEOFENCE");
    return PendingIntent.getService(this.mCtx, 0, localIntent, 134217728);
  }

  private GeofencingRequest getAddGeofencingRequest()
  {
    GeofencingRequest.Builder localBuilder = new GeofencingRequest.Builder();
    localBuilder.setInitialTrigger(1);
    localBuilder.addGeofences(this.mCurrentGeofences);
    return localBuilder.build();
  }

  private GoogleApiClient getLocationClient()
  {
    if (this.googleApiClient == null)
      this.googleApiClient = new GoogleApiClient.Builder(this.mCtx).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    return this.googleApiClient;
  }

  private void requestConnection()
  {
    try
    {
      if (getLocationClient() != null)
        getLocationClient().connect();
      return;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  private void requestDisconnection()
  {
    this.mInProgress = false;
    getLocationClient().disconnect();
  }

  public void addGeofences(List<Geofence> paramList, ServiceCallback<Integer> paramServiceCallback)
    throws UnsupportedOperationException
  {
    this.mCurrentGeofences = ((ArrayList)paramList);
    this.mCallback = paramServiceCallback;
    if (!this.mInProgress)
    {
      this.mInProgress = true;
      requestConnection();
    }
    while (this.mCallback == null)
      return;
    this.mCallback.callbackDone(Integer.valueOf(-1), null, null);
    this.mCallback = null;
  }

  public boolean getInProgressFlag()
  {
    return this.mInProgress;
  }

  public PendingIntent getRequestPendingIntent()
  {
    return createRequestPendingIntent();
  }

  public void onConnected(Bundle paramBundle)
  {
    this.mGeofencePendingIntent = createRequestPendingIntent();
    if ((this.googleApiClient != null) && (this.mGeofencePendingIntent != null));
    try
    {
      LocationServices.GeofencingApi.addGeofences(this.googleApiClient, getAddGeofencingRequest(), this.mGeofencePendingIntent).setResultCallback(new ResultCallback()
      {
        public void onResult(Status paramAnonymousStatus)
        {
          if (paramAnonymousStatus.isSuccess())
          {
            L.D("geofnce add");
            if (GeofenceRequester.this.mCallback != null)
            {
              GeofenceRequester.this.mCallback.callbackDone(Integer.valueOf(0), null, null);
              GeofenceRequester.access$002(GeofenceRequester.this, null);
            }
            GeofenceRequester.this.requestDisconnection();
            return;
          }
          if (GeofenceRequester.this.mCallback != null)
          {
            GeofenceRequester.this.mCallback.callbackDone(Integer.valueOf(-1), null, null);
            GeofenceRequester.access$002(GeofenceRequester.this, null);
          }
          L.E("Registering geofence failed: " + paramAnonymousStatus.getStatusMessage() + " : " + paramAnonymousStatus.getStatusCode());
        }
      });
      return;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void onConnectionFailed(ConnectionResult paramConnectionResult)
  {
    this.mInProgress = false;
    if (this.mCallback != null)
    {
      this.mCallback.callbackDone(Integer.valueOf(-1), null, null);
      this.mCallback = null;
    }
  }

  public void onConnectionSuspended(int paramInt)
  {
    L.E("Connecting to GoogleApiClient suspended.");
    this.mInProgress = false;
    if (this.mCallback != null)
    {
      this.mCallback.callbackDone(Integer.valueOf(-1), null, null);
      this.mCallback = null;
    }
    Log.d("Geofence Detection", "Client disconnected");
    if (this.googleApiClient.isConnected())
      this.googleApiClient.disconnect();
    this.googleApiClient = null;
  }

  public void setInProgressFlag(boolean paramBoolean)
  {
    this.mInProgress = paramBoolean;
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.shared.geofence.GeofenceRequester
 * JD-Core Version:    0.6.2
 */