package com.worklight.common;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.DisplayMetrics;
import android.util.Patterns;
import android.view.Display;
import android.view.WindowManager;
import android.widget.EditText;
import com.worklight.nativeandroid.common.WLUtils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WLPreferences extends PreferenceActivity
{
  public static final String APP_ID_PREF_KEY = "appIdPref";
  public static final String APP_VERSION_PREF_KEY = "appVersionPref";
  private static final String CANCEL_ID = "WLClient.cancel";
  public static final String EDIT_WL_SERVER_URL_PREF_KEY = "editWLServerURLPref";
  public static final String LAST_SETTINGS_URL_STATE_PREF_KEY = "lastSettingsUrlStatePref";
  public static final String MODIFY_WL_SERVER_URL_PREF_KEY = "resetWLServerURLPref";
  private static final String NEW_APP_ID_PREF_KEY = "newAppIdPref";
  private static final String NEW_APP_VERSION_PREF_KEY = "newAppVersionPref";
  private static final String NEW_LINE = "\n";
  private static final String OK_ID = "WLClient.ok";
  private static final String RESOURCE_BUNDLE = "com/worklight/wlclient/messages";
  public static final String SHOULD_MODIFY_URL_PREF_KEY = "shouldModifyUrlPref";
  public static final String WL_SERVER_URL = "WLServerURL";
  private EditTextPreference appIdEditTextPreference = null;
  private EditTextPreference appVersionEditTextPreference = null;
  private CheckBoxPreference modifyServerURLCheckBoxPreference = null;
  private EditTextPreference serverURLEditTextPreference = null;
  private String serverURLafter = null;
  private String serverURLbefore = null;

  private void createModifyServerCheckbox()
  {
    this.modifyServerURLCheckBoxPreference = new CheckBoxPreference(this);
    this.modifyServerURLCheckBoxPreference.setTitle(WLUtils.getResourceString("serverAddress", this));
    this.modifyServerURLCheckBoxPreference.setSummary(WLUtils.getResourceString("changeServerAddress", this));
    this.modifyServerURLCheckBoxPreference.setKey("resetWLServerURLPref");
    this.modifyServerURLCheckBoxPreference.setPersistent(true);
  }

  private void createServerURLTextField()
  {
    String str1 = WLUtils.getResourceString("summaryWLServerUrl", this);
    this.serverURLEditTextPreference = new EditTextPreference(this);
    this.serverURLbefore = WLConfig.getInstance().readWLPref("WLServerURL");
    if ((this.serverURLbefore == null) || (this.serverURLbefore.trim().equals("")))
      this.serverURLbefore = WLConfig.getInstance().getRootURL();
    this.serverURLEditTextPreference.setTitle(WLUtils.getResourceString("titleWLServerUrl", this));
    this.serverURLEditTextPreference.setKey("editWLServerURLPref");
    this.serverURLEditTextPreference.setPositiveButtonText(WLUtils.getResourceString("OKTitleWLServerUrl", this));
    this.serverURLEditTextPreference.setNegativeButtonText(WLUtils.getResourceString("cancel", this));
    this.serverURLEditTextPreference.setDefaultValue(this.serverURLbefore);
    String str2 = WLConfig.getInstance().readWLPref("lastSettingsUrlStatePref");
    EditTextPreference localEditTextPreference1 = this.serverURLEditTextPreference;
    EditTextPreference localEditTextPreference2;
    if (str2 == null)
    {
      localEditTextPreference1.setSummary(str1);
      String str3 = WLConfig.getInstance().readWLPref("shouldModifyUrlPref");
      localEditTextPreference2 = this.serverURLEditTextPreference;
      if ((str3 != null) && (Boolean.valueOf(str3).booleanValue()))
        break label223;
    }
    label223: for (boolean bool = false; ; bool = true)
    {
      localEditTextPreference2.setEnabled(bool);
      this.serverURLEditTextPreference.setPersistent(true);
      return;
      str1 = str2 + "\n" + str1;
      break;
    }
  }

  private EditTextPreference createTextField(String paramString1, String paramString2, String paramString3)
  {
    EditTextPreference localEditTextPreference = new EditTextPreference(this);
    localEditTextPreference.setTitle(WLUtils.getResourceString(paramString1, this));
    String str = WLConfig.getInstance().readWLPref(paramString2);
    if (str == null)
      str = paramString3;
    WLConfig.getInstance().writeWLPref(paramString2, str);
    localEditTextPreference.setSummary(str);
    localEditTextPreference.setDefaultValue(str);
    localEditTextPreference.setKey(paramString2);
    localEditTextPreference.setPersistent(true);
    localEditTextPreference.setPositiveButtonText(WLUtils.getResourceString("OKTitleWLServerUrl", this));
    localEditTextPreference.setNegativeButtonText(WLUtils.getResourceString("cancel", this));
    DisplayMetrics localDisplayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
    int i = localDisplayMetrics.widthPixels;
    localEditTextPreference.getEditText().setWidth(i - 20);
    return localEditTextPreference;
  }

  private boolean isUrlChanged()
  {
    boolean bool = true;
    if (this.serverURLafter == null)
      bool = false;
    do
    {
      do
      {
        return bool;
        if (this.serverURLbefore != null)
          break;
      }
      while (this.serverURLafter != null);
      return false;
    }
    while (!this.serverURLbefore.equals(this.serverURLafter));
    return false;
  }

  private boolean isWebResourcesChanged()
  {
    int i;
    if (!WLConfig.getInstance().getAppId().equals(WLConfig.getInstance().readWLPref("newAppIdPref")))
    {
      i = 1;
      if (WLConfig.getInstance().getApplicationVersion().equals(WLConfig.getInstance().readWLPref("newAppVersionPref")))
        break label63;
    }
    label63: for (int j = 1; ; j = 0)
    {
      boolean bool;
      if (i == 0)
      {
        bool = false;
        if (j == 0);
      }
      else
      {
        bool = true;
      }
      return bool;
      i = 0;
      break;
    }
  }

  private void setServerURLFieldSummary()
  {
    String str = (String)this.serverURLEditTextPreference.getSummary();
    if (str.contains("\n"))
      str = str.substring(1 + str.lastIndexOf("\n"), str.length());
    if (this.serverURLafter == null)
    {
      this.serverURLEditTextPreference.setSummary(this.serverURLbefore + "\n" + str);
      return;
    }
    this.serverURLEditTextPreference.setSummary(this.serverURLafter + "\n" + str);
  }

  public void onBackPressed()
  {
    Intent localIntent = new Intent();
    localIntent.putExtra("isServerURLChanged", isUrlChanged());
    localIntent.putExtra("serverURL", this.serverURLafter);
    localIntent.putExtra("isWebResourcesChanged", isWebResourcesChanged());
    setResult(-1, localIntent);
    super.onBackPressed();
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    PreferenceScreen localPreferenceScreen = getPreferenceManager().createPreferenceScreen(this);
    localPreferenceScreen.setTitle(WLUtils.getResourceString("worklightSettingsTitle", this));
    PreferenceCategory localPreferenceCategory1 = new PreferenceCategory(this);
    localPreferenceCategory1.setTitle(WLUtils.getResourceString("networkSettingsTitleWLServerUrl", this));
    localPreferenceScreen.addPreference(localPreferenceCategory1);
    this.serverURLbefore = WLConfig.getInstance().readWLPref("WLServerURL");
    createModifyServerCheckbox();
    createServerURLTextField();
    localPreferenceCategory1.addPreference(this.modifyServerURLCheckBoxPreference);
    localPreferenceCategory1.addPreference(this.serverURLEditTextPreference);
    PreferenceCategory localPreferenceCategory2 = new PreferenceCategory(this);
    localPreferenceCategory2.setTitle(WLUtils.getResourceString("wlWebResourcesCategory", this));
    localPreferenceScreen.addPreference(localPreferenceCategory2);
    WLConfig localWLConfig = WLConfig.getInstance();
    this.appIdEditTextPreference = createTextField("wlAppIdTitle", "newAppIdPref", localWLConfig.getAppId());
    this.appVersionEditTextPreference = createTextField("wlAppVersionTitle", "newAppVersionPref", localWLConfig.getApplicationVersion());
    localPreferenceCategory2.addPreference(this.appIdEditTextPreference);
    localPreferenceCategory2.addPreference(this.appVersionEditTextPreference);
    setPreferenceScreen(localPreferenceScreen);
    this.modifyServerURLCheckBoxPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    {
      public boolean onPreferenceChange(Preference paramAnonymousPreference, Object paramAnonymousObject)
      {
        if (((Boolean)paramAnonymousObject).booleanValue())
        {
          WLConfig.getInstance().writeWLPref("shouldModifyUrlPref", "true");
          WLPreferences.this.serverURLEditTextPreference.setEnabled(true);
          WLPreferences.access$102(WLPreferences.this, WLPreferences.this.serverURLEditTextPreference.getText());
          WLConfig.getInstance().writeWLPref("WLServerURL", WLPreferences.this.serverURLafter);
          return true;
        }
        WLConfig.getInstance().writeWLPref("shouldModifyUrlPref", "false");
        WLPreferences.access$102(WLPreferences.this, WLConfig.getInstance().getRootURL());
        WLConfig.getInstance().writeWLPref("WLServerURL", WLPreferences.this.serverURLafter);
        WLPreferences.this.serverURLEditTextPreference.setEnabled(false);
        return true;
      }
    });
    this.serverURLEditTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    {
      public boolean onPreferenceChange(Preference paramAnonymousPreference, Object paramAnonymousObject)
      {
        WLPreferences.access$102(WLPreferences.this, (String)paramAnonymousObject);
        String str2;
        try
        {
          if (!WLPreferences.this.serverURLafter.startsWith("http://["))
          {
            boolean bool = WLPreferences.this.serverURLafter.startsWith("https://[");
            if (!bool)
              break label248;
          }
          try
          {
            String str1 = WLPreferences.this.serverURLafter.substring(1 + WLPreferences.this.serverURLafter.indexOf('['), WLPreferences.this.serverURLafter.indexOf(']'));
            str2 = WLPreferences.this.serverURLafter.substring(1 + WLPreferences.this.serverURLafter.indexOf(']'));
            if (!str1.matches("(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"))
              throw new MalformedURLException();
          }
          catch (StringIndexOutOfBoundsException localStringIndexOutOfBoundsException)
          {
            throw new MalformedURLException();
          }
        }
        catch (MalformedURLException localMalformedURLException)
        {
          new AlertDialog.Builder(WLPreferences.this).setTitle(WLUtils.getResourceString("titleInvalidWLServerUrl", WLPreferences.this)).setMessage(WLUtils.getResourceString("errorInvalidWLServerUrl", WLPreferences.this.serverURLafter, WLPreferences.this)).setNeutralButton(WLUtils.getResourceString("OKTitleWLServerUrl", WLPreferences.this), new DialogInterface.OnClickListener()
          {
            public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
            {
            }
          }).show();
          return false;
        }
        if (!Patterns.WEB_URL.matcher("http://9.8.7.6" + str2).matches())
        {
          throw new MalformedURLException();
          label248: new URL(WLPreferences.this.serverURLafter);
          if (!Patterns.WEB_URL.matcher(WLPreferences.this.serverURLafter).matches())
            throw new MalformedURLException();
        }
        WLConfig.getInstance().writeWLPref("WLServerURL", WLPreferences.this.serverURLafter);
        WLConfig.getInstance().writeWLPref("lastSettingsUrlStatePref", WLPreferences.this.serverURLafter);
        WLPreferences.this.setServerURLFieldSummary();
        return true;
      }
    });
    this.appIdEditTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    {
      public boolean onPreferenceChange(Preference paramAnonymousPreference, Object paramAnonymousObject)
      {
        String str = (String)paramAnonymousObject;
        WLConfig.getInstance().writeWLPref("newAppIdPref", str);
        WLPreferences.this.appIdEditTextPreference.setSummary(str);
        return true;
      }
    });
    this.appVersionEditTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    {
      public boolean onPreferenceChange(Preference paramAnonymousPreference, Object paramAnonymousObject)
      {
        String str = (String)paramAnonymousObject;
        WLConfig.getInstance().writeWLPref("newAppVersionPref", str);
        WLPreferences.this.appVersionEditTextPreference.setSummary(str);
        return true;
      }
    });
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.common.WLPreferences
 * JD-Core Version:    0.6.2
 */