package com.worklight.wlclient.api;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build.VERSION;
import com.google.android.gcm.GCMRegistrar;
import com.worklight.common.Logger;
import com.worklight.common.WLConfig;
import com.worklight.nativeandroid.common.WLUtils;
import com.worklight.push.GCMErrorCode;
import com.worklight.wlclient.WLRequest;
import com.worklight.wlclient.WLRequestListener;
import com.worklight.wlclient.push.GCMIntentService;
import com.worklight.wlclient.push.GCMIntentService.Message;
import com.worklight.wlclient.ui.UIActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class WLPush
{
  private static final String ERROR_ID = "WLClient.error";
  private static final String FROM_NOTIFICATION_BAR = "notificationBar";
  private static final String NOT_UPDATE_FAILURE = "WLClient.notificationUpdateFailure";
  private static final String OK = "WLClient.ok";
  private static final String RESOURCE_BUNDLE = "com/worklight/wlclient/messages";
  private static Logger logger = Logger.getInstance(WLPush.class.getName());
  private Activity activity;
  private WLConfig config;
  private Context context;
  private String deviceToken;
  private boolean isTokenUpdatedOnServer;
  private WLNotificationListener notificationListener;
  private BroadcastReceiver onMessage;
  private WLOnReadyToSubscribeListener onReadyToSubscribeListener;
  private BroadcastReceiver onRegister;
  private BroadcastReceiver onRegisterError;
  private ArrayList<GCMIntentService.Message> pending;
  private ArrayList<JSONObject> pendingPushEvents;
  private HashMap<String, RegisteredEventSource> registeredEventSources;
  private String serverToken;
  private ArrayList<String> subscribedEventSources;
  private ArrayList<String> subscribedTags;

  // ERROR //
  WLPush(WLConfig paramWLConfig, Context paramContext)
  {
    // Byte code:
    //   0: aload_0
    //   1: invokespecial 73	java/lang/Object:<init>	()V
    //   4: aload_0
    //   5: iconst_0
    //   6: putfield 75	com/worklight/wlclient/api/WLPush:isTokenUpdatedOnServer	Z
    //   9: aload_0
    //   10: new 77	java/util/HashMap
    //   13: dup
    //   14: invokespecial 78	java/util/HashMap:<init>	()V
    //   17: putfield 80	com/worklight/wlclient/api/WLPush:registeredEventSources	Ljava/util/HashMap;
    //   20: aload_0
    //   21: new 82	java/util/ArrayList
    //   24: dup
    //   25: invokespecial 83	java/util/ArrayList:<init>	()V
    //   28: putfield 85	com/worklight/wlclient/api/WLPush:subscribedEventSources	Ljava/util/ArrayList;
    //   31: aload_0
    //   32: new 82	java/util/ArrayList
    //   35: dup
    //   36: invokespecial 83	java/util/ArrayList:<init>	()V
    //   39: putfield 87	com/worklight/wlclient/api/WLPush:subscribedTags	Ljava/util/ArrayList;
    //   42: aload_0
    //   43: new 82	java/util/ArrayList
    //   46: dup
    //   47: invokespecial 83	java/util/ArrayList:<init>	()V
    //   50: putfield 89	com/worklight/wlclient/api/WLPush:pendingPushEvents	Ljava/util/ArrayList;
    //   53: aload_0
    //   54: new 82	java/util/ArrayList
    //   57: dup
    //   58: invokespecial 83	java/util/ArrayList:<init>	()V
    //   61: putfield 91	com/worklight/wlclient/api/WLPush:pending	Ljava/util/ArrayList;
    //   64: aload_0
    //   65: aconst_null
    //   66: putfield 93	com/worklight/wlclient/api/WLPush:serverToken	Ljava/lang/String;
    //   69: aload_0
    //   70: aconst_null
    //   71: putfield 95	com/worklight/wlclient/api/WLPush:deviceToken	Ljava/lang/String;
    //   74: aload_0
    //   75: aconst_null
    //   76: putfield 97	com/worklight/wlclient/api/WLPush:onReadyToSubscribeListener	Lcom/worklight/wlclient/api/WLOnReadyToSubscribeListener;
    //   79: aload_0
    //   80: aconst_null
    //   81: putfield 99	com/worklight/wlclient/api/WLPush:notificationListener	Lcom/worklight/wlclient/api/WLNotificationListener;
    //   84: aload_0
    //   85: new 101	com/worklight/wlclient/api/WLPush$3
    //   88: dup
    //   89: aload_0
    //   90: invokespecial 104	com/worklight/wlclient/api/WLPush$3:<init>	(Lcom/worklight/wlclient/api/WLPush;)V
    //   93: putfield 106	com/worklight/wlclient/api/WLPush:onRegister	Landroid/content/BroadcastReceiver;
    //   96: aload_0
    //   97: new 108	com/worklight/wlclient/api/WLPush$4
    //   100: dup
    //   101: aload_0
    //   102: invokespecial 109	com/worklight/wlclient/api/WLPush$4:<init>	(Lcom/worklight/wlclient/api/WLPush;)V
    //   105: putfield 111	com/worklight/wlclient/api/WLPush:onRegisterError	Landroid/content/BroadcastReceiver;
    //   108: aload_0
    //   109: new 113	com/worklight/wlclient/api/WLPush$5
    //   112: dup
    //   113: aload_0
    //   114: invokespecial 114	com/worklight/wlclient/api/WLPush$5:<init>	(Lcom/worklight/wlclient/api/WLPush;)V
    //   117: putfield 116	com/worklight/wlclient/api/WLPush:onMessage	Landroid/content/BroadcastReceiver;
    //   120: aload_2
    //   121: ifnonnull +13 -> 134
    //   124: new 118	java/lang/RuntimeException
    //   127: dup
    //   128: ldc 120
    //   130: invokespecial 123	java/lang/RuntimeException:<init>	(Ljava/lang/String;)V
    //   133: athrow
    //   134: getstatic 129	android/os/Build$VERSION:SDK_INT	I
    //   137: bipush 8
    //   139: if_icmpge +49 -> 188
    //   142: new 131	java/lang/StringBuilder
    //   145: dup
    //   146: invokespecial 132	java/lang/StringBuilder:<init>	()V
    //   149: ldc 134
    //   151: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   154: getstatic 141	android/os/Build$VERSION:RELEASE	Ljava/lang/String;
    //   157: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   160: ldc 143
    //   162: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   165: invokevirtual 146	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   168: astore 9
    //   170: getstatic 67	com/worklight/wlclient/api/WLPush:logger	Lcom/worklight/common/Logger;
    //   173: aload 9
    //   175: invokevirtual 149	com/worklight/common/Logger:error	(Ljava/lang/String;)V
    //   178: new 118	java/lang/RuntimeException
    //   181: dup
    //   182: aload 9
    //   184: invokespecial 123	java/lang/RuntimeException:<init>	(Ljava/lang/String;)V
    //   187: athrow
    //   188: aload_0
    //   189: aload_2
    //   190: checkcast 151	android/app/Activity
    //   193: putfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   196: aload_0
    //   197: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   200: invokestatic 159	com/google/android/gcm/GCMRegistrar:checkDevice	(Landroid/content/Context;)V
    //   203: aload_0
    //   204: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   207: invokestatic 162	com/google/android/gcm/GCMRegistrar:checkManifest	(Landroid/content/Context;)V
    //   210: aload_2
    //   211: ldc 164
    //   213: ldc 166
    //   215: invokestatic 172	com/worklight/nativeandroid/common/WLUtils:getResourceId	(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)I
    //   218: pop
    //   219: aload_0
    //   220: aload_1
    //   221: putfield 174	com/worklight/wlclient/api/WLPush:config	Lcom/worklight/common/WLConfig;
    //   224: aload_0
    //   225: aload_2
    //   226: putfield 176	com/worklight/wlclient/api/WLPush:context	Landroid/content/Context;
    //   229: aload_0
    //   230: iconst_1
    //   231: invokevirtual 180	com/worklight/wlclient/api/WLPush:setForeground	(Z)V
    //   234: aload_0
    //   235: invokevirtual 183	com/worklight/wlclient/api/WLPush:unregisterReceivers	()V
    //   238: aload_0
    //   239: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   242: aload_0
    //   243: getfield 116	com/worklight/wlclient/api/WLPush:onMessage	Landroid/content/BroadcastReceiver;
    //   246: new 185	android/content/IntentFilter
    //   249: dup
    //   250: new 131	java/lang/StringBuilder
    //   253: dup
    //   254: invokespecial 132	java/lang/StringBuilder:<init>	()V
    //   257: aload_0
    //   258: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   261: invokestatic 189	com/worklight/nativeandroid/common/WLUtils:getFullAppName	(Landroid/content/Context;)Ljava/lang/String;
    //   264: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   267: ldc 191
    //   269: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   272: invokevirtual 146	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   275: invokespecial 192	android/content/IntentFilter:<init>	(Ljava/lang/String;)V
    //   278: invokevirtual 196	android/app/Activity:registerReceiver	(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;
    //   281: pop
    //   282: new 131	java/lang/StringBuilder
    //   285: dup
    //   286: invokespecial 132	java/lang/StringBuilder:<init>	()V
    //   289: aload_0
    //   290: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   293: invokestatic 189	com/worklight/nativeandroid/common/WLUtils:getFullAppName	(Landroid/content/Context;)Ljava/lang/String;
    //   296: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   299: ldc 198
    //   301: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   304: invokevirtual 146	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   307: aload_0
    //   308: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   311: invokevirtual 202	android/app/Activity:getIntent	()Landroid/content/Intent;
    //   314: invokevirtual 207	android/content/Intent:getAction	()Ljava/lang/String;
    //   317: invokevirtual 213	java/lang/String:equals	(Ljava/lang/Object;)Z
    //   320: ifeq +43 -> 363
    //   323: getstatic 67	com/worklight/wlclient/api/WLPush:logger	Lcom/worklight/common/Logger;
    //   326: ldc 215
    //   328: invokevirtual 218	com/worklight/common/Logger:debug	(Ljava/lang/String;)V
    //   331: aload_0
    //   332: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   335: invokevirtual 202	android/app/Activity:getIntent	()Landroid/content/Intent;
    //   338: ldc 11
    //   340: iconst_1
    //   341: invokevirtual 222	android/content/Intent:putExtra	(Ljava/lang/String;Z)Landroid/content/Intent;
    //   344: pop
    //   345: aload_0
    //   346: getfield 116	com/worklight/wlclient/api/WLPush:onMessage	Landroid/content/BroadcastReceiver;
    //   349: aload_0
    //   350: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   353: aload_0
    //   354: getfield 153	com/worklight/wlclient/api/WLPush:activity	Landroid/app/Activity;
    //   357: invokevirtual 202	android/app/Activity:getIntent	()Landroid/content/Intent;
    //   360: invokevirtual 228	android/content/BroadcastReceiver:onReceive	(Landroid/content/Context;Landroid/content/Intent;)V
    //   363: aload_0
    //   364: invokespecial 231	com/worklight/wlclient/api/WLPush:dispatchPending	()V
    //   367: return
    //   368: astore_3
    //   369: new 118	java/lang/RuntimeException
    //   372: dup
    //   373: new 131	java/lang/StringBuilder
    //   376: dup
    //   377: invokespecial 132	java/lang/StringBuilder:<init>	()V
    //   380: ldc 233
    //   382: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   385: aload_3
    //   386: invokevirtual 236	java/lang/Exception:getMessage	()Ljava/lang/String;
    //   389: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   392: invokevirtual 146	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   395: invokespecial 123	java/lang/RuntimeException:<init>	(Ljava/lang/String;)V
    //   398: athrow
    //   399: astore 4
    //   401: new 118	java/lang/RuntimeException
    //   404: dup
    //   405: new 131	java/lang/StringBuilder
    //   408: dup
    //   409: invokespecial 132	java/lang/StringBuilder:<init>	()V
    //   412: ldc 238
    //   414: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   417: aload 4
    //   419: invokevirtual 236	java/lang/Exception:getMessage	()Ljava/lang/String;
    //   422: invokevirtual 138	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   425: invokevirtual 146	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   428: invokespecial 123	java/lang/RuntimeException:<init>	(Ljava/lang/String;)V
    //   431: athrow
    //   432: astore 5
    //   434: new 118	java/lang/RuntimeException
    //   437: dup
    //   438: ldc 240
    //   440: invokespecial 123	java/lang/RuntimeException:<init>	(Ljava/lang/String;)V
    //   443: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   196	203	368	java/lang/Exception
    //   203	210	399	java/lang/Exception
    //   210	219	432	java/lang/Exception
  }

  private void cancelAllNotification()
  {
    ((NotificationManager)this.activity.getSystemService("notification")).cancelAll();
  }

  private void dispatch()
  {
    dispatchPending();
  }

  private void dispatchPending()
  {
    while (true)
    {
      GCMIntentService.Message localMessage;
      synchronized (this.pending)
      {
        int i = this.pending.size();
        localMessage = null;
        if (i > 0)
          localMessage = (GCMIntentService.Message)this.pending.remove(0);
        if (localMessage == null)
          return;
      }
      onMessage(localMessage.getProps(), localMessage.getPayload());
    }
  }

  private void dispatchPendings()
  {
    Iterator localIterator = this.pendingPushEvents.iterator();
    while (localIterator.hasNext())
    {
      JSONObject localJSONObject = (JSONObject)localIterator.next();
      while (true)
      {
        try
        {
          if (!localJSONObject.isNull("alias"))
            break label167;
          localObject = null;
          if (localObject == null)
            break label182;
          if ((!this.subscribedEventSources.contains(localObject)) || (!this.registeredEventSources.containsKey(localObject)) || (((RegisteredEventSource)this.registeredEventSources.get(localObject)).eventSourceListener == null))
            break;
          ((RegisteredEventSource)this.registeredEventSources.get(localObject)).eventSourceListener.onReceive(localJSONObject.getString("props"), localJSONObject.getString("payload"));
          localIterator.remove();
          cancelAllNotification();
        }
        catch (JSONException localJSONException)
        {
          logger.error("Failed processing pending push events." + localJSONException.getMessage());
        }
        break;
        label167: Object localObject = (String)localJSONObject.get("alias");
      }
      label182: if (this.notificationListener != null)
      {
        this.notificationListener.onMessage(localJSONObject.getString("props"), localJSONObject.getString("payload"));
        localIterator.remove();
        cancelAllNotification();
      }
    }
  }

  private boolean hasPendings()
  {
    return (this.pendingPushEvents != null) && (this.pendingPushEvents.size() > 0);
  }

  private void internalSubscribe(WLRequestOptions paramWLRequestOptions, WLPushOptions paramWLPushOptions, WLRequestListener paramWLRequestListener, WLResponseListener paramWLResponseListener, final String paramString)
  {
    if (paramWLPushOptions == null)
      paramWLPushOptions = new WLPushOptions();
    StringBuffer localStringBuffer = new StringBuffer();
    Map localMap = paramWLPushOptions.getSubscriptionParameters();
    localStringBuffer.append("{");
    if ((localMap != null) && (!localMap.isEmpty()))
    {
      Iterator localIterator = localMap.keySet().iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        localStringBuffer.append("\"");
        localStringBuffer.append(str);
        localStringBuffer.append("\":\"");
        localStringBuffer.append((String)localMap.get(str));
        localStringBuffer.append("\"");
        if (localIterator.hasNext())
          localStringBuffer.append(",");
      }
    }
    localStringBuffer.append("}");
    paramWLRequestOptions.addParameter("subscribe", localStringBuffer.toString());
    if (paramWLResponseListener == null)
      paramWLResponseListener = new WLResponseListener()
      {
        public void onFailure(WLFailResponse paramAnonymousWLFailResponse)
        {
          WLPush.logger.error(paramString + ": error subscribing for notifications");
        }

        public void onSuccess(WLResponse paramAnonymousWLResponse)
        {
        }
      };
    paramWLRequestOptions.setResponseListener(paramWLResponseListener);
    new WLRequest(paramWLRequestListener, paramWLRequestOptions, this.config, this.context).makeRequest("notifications");
    dispatch();
  }

  private void internalUnsubscribe(WLRequestOptions paramWLRequestOptions, WLRequestListener paramWLRequestListener, WLResponseListener paramWLResponseListener, final String paramString)
  {
    if (paramWLResponseListener == null)
      paramWLResponseListener = new WLResponseListener()
      {
        public void onFailure(WLFailResponse paramAnonymousWLFailResponse)
        {
          WLPush.logger.error(paramString + ": error unsubscribing from notifications");
        }

        public void onSuccess(WLResponse paramAnonymousWLResponse)
        {
        }
      };
    paramWLRequestOptions.setResponseListener(paramWLResponseListener);
    paramWLRequestOptions.addParameter("unsubscribe", "{}");
    new WLRequest(paramWLRequestListener, paramWLRequestOptions, this.config, this.context).makeRequest("notifications");
  }

  private boolean isAbleToSubscribe()
  {
    if (!isDeviceSupportPush())
    {
      logger.error("The current Android version " + Build.VERSION.RELEASE + " does not support push notifications.");
      return false;
    }
    if (!this.isTokenUpdatedOnServer)
    {
      logger.error("Can't subscribe, notification token is not updated on the server");
      return false;
    }
    return true;
  }

  private boolean isAbleToSubscribeEventSource(String paramString)
  {
    if (!isAbleToSubscribe())
      return false;
    if ((this.registeredEventSources == null) || (this.registeredEventSources.get(paramString) == null))
    {
      logger.error("No registered push event source for alias '" + paramString + "'.");
      return false;
    }
    return true;
  }

  private boolean isDeviceSupportPush()
  {
    return Build.VERSION.SDK_INT >= 10;
  }

  private void moveQueueToPending()
  {
    synchronized (GCMIntentService.intentsQueue)
    {
      Iterator localIterator = GCMIntentService.intentsQueue.descendingIterator();
      if (localIterator.hasNext())
      {
        GCMIntentService.Message localMessage = (GCMIntentService.Message)((Intent)localIterator.next()).getParcelableExtra("message");
        this.pending.add(localMessage);
      }
    }
    GCMIntentService.intentsQueue.clear();
  }

  private void onMessage(JSONObject paramJSONObject1, JSONObject paramJSONObject2)
  {
    try
    {
      if (!paramJSONObject2.isNull("alias"))
      {
        String str = (String)paramJSONObject2.get("alias");
        logger.debug("WLPush received notification for alias: " + str);
        if ((this.subscribedEventSources.contains(str)) && (this.registeredEventSources.containsKey(str)) && (((RegisteredEventSource)this.registeredEventSources.get(str)).eventSourceListener != null))
        {
          ((RegisteredEventSource)this.registeredEventSources.get(str)).eventSourceListener.onReceive(paramJSONObject1.toString(), paramJSONObject2.toString());
          return;
        }
        this.pendingPushEvents.add(new JSONObject("{\"alias\":" + str + ", \"props\":" + paramJSONObject1.toString() + ", \"payload\":" + paramJSONObject2.toString() + "}"));
        return;
      }
    }
    catch (Exception localException)
    {
      logger.error("Failed processing pending push events, because " + localException.getMessage());
      return;
    }
    if (this.notificationListener != null)
    {
      this.notificationListener.onMessage(paramJSONObject1.toString(), paramJSONObject2.toString());
      return;
    }
    this.pendingPushEvents.add(new JSONObject("{\"props\":" + paramJSONObject1.toString() + ", \"payload\":" + paramJSONObject2.toString() + "}"));
  }

  private void removeOldSubscribedAliases(String paramString)
  {
    RegisteredEventSource localRegisteredEventSource1 = (RegisteredEventSource)this.registeredEventSources.get(paramString);
    String str1 = localRegisteredEventSource1.eventSource;
    String str2 = localRegisteredEventSource1.adapter;
    Iterator localIterator = this.registeredEventSources.entrySet().iterator();
    while (localIterator.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      RegisteredEventSource localRegisteredEventSource2 = (RegisteredEventSource)localEntry.getValue();
      if ((localRegisteredEventSource2.eventSource.equals(str1)) && (localRegisteredEventSource2.adapter.equals(str2)) && (!((String)localEntry.getKey()).equals(paramString)))
        this.subscribedEventSources.remove(localEntry.getKey());
    }
  }

  private void showErrorDialogue(String paramString1, String paramString2, String paramString3)
  {
    Intent localIntent = new Intent(this.context, UIActivity.class);
    localIntent.putExtra("action", "exit");
    localIntent.putExtra("dialogue_message", paramString2);
    localIntent.putExtra("dialogue_title", paramString1);
    localIntent.putExtra("positive_button_text", paramString3);
    this.context.startActivity(localIntent);
  }

  private void updateRegisteredEventSources(String paramString1, String paramString2, String paramString3, WLEventSourceListener paramWLEventSourceListener)
  {
    if (this.registeredEventSources == null)
      this.registeredEventSources = new HashMap();
    RegisteredEventSource localRegisteredEventSource = (RegisteredEventSource)this.registeredEventSources.get(paramString1);
    if (localRegisteredEventSource == null)
      localRegisteredEventSource = new RegisteredEventSource();
    localRegisteredEventSource.setAdapter(paramString2);
    localRegisteredEventSource.setEventSource(paramString3);
    localRegisteredEventSource.setEventSourceListener(paramWLEventSourceListener);
    this.registeredEventSources.put(paramString1, localRegisteredEventSource);
  }

  private void updateTokenCallback(String paramString)
  {
    if ((this.serverToken == null) || (!this.serverToken.equals(paramString)))
    {
      logger.debug("Push notification device token has changed, Updating on server [serverToken: " + this.serverToken + ", deviceToken: " + paramString + "]");
      WLRequestOptions localWLRequestOptions = new WLRequestOptions();
      localWLRequestOptions.addParameter("updateToken", paramString);
      new WLRequest(new UpdateTokenListener(), localWLRequestOptions, this.config, this.context).makeRequest("notifications");
    }
    while (true)
    {
      this.serverToken = null;
      return;
      this.isTokenUpdatedOnServer = true;
      if (this.onReadyToSubscribeListener != null)
        this.onReadyToSubscribeListener.onReadyToSubscribe();
      if (hasPendings())
        dispatchPendings();
    }
  }

  public void clearSubscribedEventSources()
  {
    logger.debug("Clearing notification subscriptions.");
    this.subscribedEventSources.clear();
  }

  public void clearSubscribedTags()
  {
    logger.debug("Clearing tag notification subscriptions.");
    this.subscribedTags.clear();
  }

  WLOnReadyToSubscribeListener getOnReadyToSubscribeListener()
  {
    return this.onReadyToSubscribeListener;
  }

  public WLNotificationListener getWLNotificationListener()
  {
    return this.notificationListener;
  }

  public boolean isForeground()
  {
    return GCMIntentService.isAppForeground();
  }

  public boolean isPushSupported()
  {
    return isDeviceSupportPush();
  }

  public boolean isSubscribed(String paramString)
  {
    return (this.subscribedEventSources != null) && (this.subscribedEventSources.contains(paramString));
  }

  public boolean isTagSubscribed(String paramString)
  {
    return (this.subscribedTags != null) && (this.subscribedTags.contains(paramString));
  }

  public void registerEventSourceCallback(String paramString1, String paramString2, String paramString3, WLEventSourceListener paramWLEventSourceListener)
  {
    if ((WLUtils.isStringEmpty(paramString1)) || (WLUtils.isStringEmpty(paramString2)) || (WLUtils.isStringEmpty(paramString3)))
      logger.error("Cannot register to event source callback with alias '" + paramString1 + "', adapter '" + paramString2 + "' and eventSource '" + paramString3 + "'. Use concrete values which are not null or empty.");
    do
    {
      return;
      if (this.registeredEventSources.get(paramString1) != null)
      {
        logger.warn("Event source callback is already registered with alias: " + paramString1);
        return;
      }
    }
    while (!isAbleToSubscribe());
    updateRegisteredEventSources(paramString1, paramString2, paramString3, paramWLEventSourceListener);
  }

  public void setForeground(boolean paramBoolean)
  {
    GCMIntentService.setAppForeground(paramBoolean);
    if (paramBoolean)
    {
      moveQueueToPending();
      dispatchPending();
      if (this.pendingPushEvents.isEmpty())
        cancelAllNotification();
    }
  }

  public void setOnReadyToSubscribeListener(WLOnReadyToSubscribeListener paramWLOnReadyToSubscribeListener)
  {
    this.onReadyToSubscribeListener = paramWLOnReadyToSubscribeListener;
  }

  public void setWLNotificationListener(WLNotificationListener paramWLNotificationListener)
  {
    this.notificationListener = paramWLNotificationListener;
    if (hasPendings())
      dispatchPendings();
  }

  public void subscribe(String paramString, WLPushOptions paramWLPushOptions, WLResponseListener paramWLResponseListener)
  {
    if (WLUtils.isStringEmpty(paramString))
      logger.error("Cannot subscribe to event source with alias '" + paramString + "', because it is either null or empty.");
    while (!isAbleToSubscribeEventSource(paramString))
      return;
    RegisteredEventSource localRegisteredEventSource = (RegisteredEventSource)this.registeredEventSources.get(paramString);
    if (localRegisteredEventSource == null)
    {
      logger.error("Event source with alias '" + paramString + "' is not registered. Register before subscribing to event source.");
      return;
    }
    WLRequestOptions localWLRequestOptions = new WLRequestOptions();
    localWLRequestOptions.addParameter("alias", paramString);
    localWLRequestOptions.addParameter("adapter", localRegisteredEventSource.adapter);
    localWLRequestOptions.addParameter("eventSource", localRegisteredEventSource.eventSource);
    internalSubscribe(localWLRequestOptions, paramWLPushOptions, new EventSourceSubscribeRequestListener(paramString), paramWLResponseListener, "WLPush.subscribe");
  }

  public void subscribeTag(String paramString, WLPushOptions paramWLPushOptions, WLResponseListener paramWLResponseListener)
  {
    if (WLUtils.isStringEmpty(paramString))
      logger.error("Cannot subscribe to tag with name '" + paramString + "', because it is either null or empty.");
    while (!isAbleToSubscribe())
      return;
    WLRequestOptions localWLRequestOptions = new WLRequestOptions();
    localWLRequestOptions.addParameter("tag", paramString);
    internalSubscribe(localWLRequestOptions, paramWLPushOptions, new TagSubscribeRequestListener(paramString), paramWLResponseListener, "WLPush.subscribeTag");
  }

  public void unregisterReceivers()
  {
    if (this.activity != null);
    try
    {
      this.activity.unregisterReceiver(this.onRegister);
    }
    catch (Exception localException2)
    {
      try
      {
        this.activity.unregisterReceiver(this.onRegisterError);
      }
      catch (Exception localException2)
      {
        try
        {
          while (true)
          {
            this.activity.unregisterReceiver(this.onMessage);
            if (GCMRegistrar.isRegistered(this.context.getApplicationContext()))
              GCMRegistrar.onDestroy(this.context.getApplicationContext());
            return;
            localException1 = localException1;
            logger.warn("unregisterReceivers:" + localException1.getMessage());
            continue;
            localException2 = localException2;
            logger.warn("unregisterReceivers:" + localException2.getMessage());
          }
        }
        catch (Exception localException3)
        {
          while (true)
            logger.warn("unregisterReceivers:" + localException3.getMessage());
        }
      }
    }
  }

  public void unsubscribe(String paramString, WLResponseListener paramWLResponseListener)
  {
    if (WLUtils.isStringEmpty(paramString))
      logger.error("Cannot unsubscribe from event source with alias '" + paramString + "'.Please check if the input alias is valid.");
    while (!isAbleToSubscribeEventSource(paramString))
      return;
    RegisteredEventSource localRegisteredEventSource = (RegisteredEventSource)this.registeredEventSources.get(paramString);
    WLRequestOptions localWLRequestOptions = new WLRequestOptions();
    localWLRequestOptions.addParameter("alias", paramString);
    localWLRequestOptions.addParameter("adapter", localRegisteredEventSource.adapter);
    localWLRequestOptions.addParameter("eventSource", localRegisteredEventSource.eventSource);
    internalUnsubscribe(localWLRequestOptions, new EventSourceUnSubscribeRequestListener(paramString), paramWLResponseListener, "WLPush.unsubscribe");
  }

  public void unsubscribeTag(String paramString, WLResponseListener paramWLResponseListener)
  {
    if (WLUtils.isStringEmpty(paramString))
      logger.error("Cannot unsubscribe from tag with name '" + paramString + "'.Please check if the input tag name is valid.");
    while (!isAbleToSubscribe())
      return;
    WLRequestOptions localWLRequestOptions = new WLRequestOptions();
    localWLRequestOptions.addParameter("tag", paramString);
    internalUnsubscribe(localWLRequestOptions, new TagUnSubscribeRequestListener(paramString), paramWLResponseListener, "WLPush.unsubscribeTag");
  }

  public void updateSubscribedEventSources(JSONArray paramJSONArray)
  {
    logger.debug("Updating notification subscriptions.");
    int i = 0;
    try
    {
      int j = paramJSONArray.length();
      while (i < j)
      {
        String str = (String)((JSONObject)paramJSONArray.get(i)).get("alias");
        if (!this.subscribedEventSources.contains(str))
          this.subscribedEventSources.add(str);
        i++;
      }
    }
    catch (JSONException localJSONException)
    {
      logger.error("Updating notification subscriptions failed, because " + localJSONException.getMessage());
    }
  }

  public void updateSubscribedTags(JSONArray paramJSONArray)
  {
    logger.debug("Updating tag notification subscriptions.");
    int i = 0;
    try
    {
      int j = paramJSONArray.length();
      while (i < j)
      {
        String str = (String)paramJSONArray.get(i);
        if (!this.subscribedTags.contains(str))
          this.subscribedTags.add(str);
        i++;
      }
    }
    catch (JSONException localJSONException)
    {
      logger.error("Updating tag notification subscriptions failed, because " + localJSONException.getMessage());
    }
  }

  public void updateToken(String paramString)
  {
    this.serverToken = paramString;
    try
    {
      this.activity.unregisterReceiver(this.onRegister);
      try
      {
        label16: this.activity.unregisterReceiver(this.onRegisterError);
        label27: this.activity.registerReceiver(this.onRegister, new IntentFilter(WLUtils.getFullAppName(this.activity) + ".C2DM_REGISTERED"));
        this.activity.registerReceiver(this.onRegisterError, new IntentFilter(WLUtils.getFullAppName(this.activity) + ".C2DM_ERROR"));
        logger.debug("Registering at the GCM server.");
        Context localContext = this.context.getApplicationContext();
        String[] arrayOfString = new String[1];
        arrayOfString[0] = this.config.getGCMSender();
        GCMRegistrar.register(localContext, arrayOfString);
        return;
      }
      catch (Exception localException2)
      {
        break label27;
      }
    }
    catch (Exception localException1)
    {
      break label16;
    }
  }

  class EventSourceSubscribeRequestListener extends WLPush.SubscribeRequestListener
  {
    private String alias;

    public EventSourceSubscribeRequestListener(String arg2)
    {
      super();
      Object localObject;
      this.alias = localObject;
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      WLPush.this.removeOldSubscribedAliases(this.alias);
      if (!WLPush.this.subscribedEventSources.contains(this.alias))
        WLPush.this.subscribedEventSources.add(this.alias);
      super.onSuccess(paramWLResponse);
    }
  }

  class EventSourceUnSubscribeRequestListener extends WLPush.UnSubscribeRequestListener
  {
    private String alias;

    public EventSourceUnSubscribeRequestListener(String arg2)
    {
      super();
      Object localObject;
      this.alias = localObject;
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      WLPush.this.removeOldSubscribedAliases(this.alias);
      if (WLPush.this.subscribedEventSources.contains(this.alias))
        WLPush.this.subscribedEventSources.remove(this.alias);
      super.onSuccess(paramWLResponse);
    }
  }

  class RegisteredEventSource
  {
    private String adapter;
    private String eventSource;
    private WLEventSourceListener eventSourceListener;

    RegisteredEventSource()
    {
    }

    public String getAdapter()
    {
      return this.adapter;
    }

    public String getEventSource()
    {
      return this.eventSource;
    }

    public WLEventSourceListener getEventSourceListener()
    {
      return this.eventSourceListener;
    }

    public void setAdapter(String paramString)
    {
      this.adapter = paramString;
    }

    public void setEventSource(String paramString)
    {
      this.eventSource = paramString;
    }

    public void setEventSourceListener(WLEventSourceListener paramWLEventSourceListener)
    {
      this.eventSourceListener = paramWLEventSourceListener;
    }
  }

  class SubscribeRequestListener
    implements WLRequestListener
  {
    SubscribeRequestListener()
    {
    }

    public void onFailure(WLFailResponse paramWLFailResponse)
    {
      paramWLFailResponse.getOptions().getResponseListener().onFailure(paramWLFailResponse);
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      paramWLResponse.getOptions().getResponseListener().onSuccess(paramWLResponse);
      if (WLPush.this.hasPendings())
        WLPush.this.dispatchPendings();
    }
  }

  class TagSubscribeRequestListener extends WLPush.SubscribeRequestListener
  {
    private String tagName;

    public TagSubscribeRequestListener(String arg2)
    {
      super();
      Object localObject;
      this.tagName = localObject;
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      if (!WLPush.this.subscribedTags.contains(this.tagName))
        WLPush.this.subscribedTags.add(this.tagName);
      super.onSuccess(paramWLResponse);
    }
  }

  class TagUnSubscribeRequestListener extends WLPush.UnSubscribeRequestListener
  {
    private String tagName;

    public TagUnSubscribeRequestListener(String arg2)
    {
      super();
      Object localObject;
      this.tagName = localObject;
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      if (WLPush.this.subscribedTags.contains(this.tagName))
        WLPush.this.subscribedTags.remove(this.tagName);
      super.onSuccess(paramWLResponse);
    }
  }

  class UnSubscribeRequestListener
    implements WLRequestListener
  {
    UnSubscribeRequestListener()
    {
    }

    public void onFailure(WLFailResponse paramWLFailResponse)
    {
      paramWLFailResponse.getOptions().getResponseListener().onFailure(paramWLFailResponse);
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      paramWLResponse.getOptions().getResponseListener().onSuccess(paramWLResponse);
    }
  }

  class UpdateTokenListener
    implements WLRequestListener
  {
    public UpdateTokenListener()
    {
    }

    public void onFailure(WLFailResponse paramWLFailResponse)
    {
      WLPush.access$1802(WLPush.this, false);
      WLPush.logger.error("Failed to update token on server");
    }

    public void onSuccess(WLResponse paramWLResponse)
    {
      WLPush.access$1802(WLPush.this, true);
      if (WLPush.this.onReadyToSubscribeListener != null)
        WLPush.this.onReadyToSubscribeListener.onReadyToSubscribe();
      while (true)
      {
        if (WLPush.this.hasPendings())
          WLPush.this.dispatchPendings();
        return;
        WLPush.logger.debug("onReadyToSubscribeListener is NULL");
      }
    }
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.wlclient.api.WLPush
 * JD-Core Version:    0.6.2
 */