public void onDestroy() {
   super.onDestroy();
   MainMenu.basicWirelessEvent = null;
   if (dialWPSsession != null && dialWPSsession.isShowing()) {
     dialWPSsession.cancel();
   }
   if (dialWPSpinentry != null && dialWPSpinentry.isShowing()) {
     dialWPSpinentry.cancel();
   }
   if (bwsalertdialog != null && bwsalertdialog.isShowing()) {
     bwsalertdialog.cancel();
   }
   if (timr != null) {
     timr.cancel();
   }
   if (looper != null) {
     looper.quit();
   }
   defPrefEditor.putString(L10NConstants.CONFIG_KEY, "");
   defPrefEditor.commit();
 }
 /** This method starts the timer counter */
 public void startTimer() {
   if (timr != null) timr.cancel();
   timr = new Timer(L10NConstants.MINUTE, 1000);
   timr.start();
 }
  /**
   * This method implements change event handler in Basic wireless setting
   *
   * @param preference The changed Preference.
   * @param newValue The new value of the Preference.
   * @return boolean True to update the state of the Preference with the new value.
   */
  public boolean onPreferenceChange(final Preference preference, Object newValue) {
    bPrefChStatus = true;
    Log.d(L10NConstants.TAG_BWS, "New Changed Value : " + newValue);
    if (preference instanceof ListPreference) {
      ListPreference lstPref = (ListPreference) preference;
      int index = lstPref.findIndexOfValue((String) newValue);
      String sLstEntry = (String) lstPref.getEntries()[index];

      if (preference == wpsEnrollLst) {
        lstPref.setSummary(getString(R.string.str_bws_enroll));
      } else if (preference != networkLst) {
        lstPref.setSummary(sLstEntry);
      }
      if (preference == wpsEnrollLst) {
        if (newValue.equals(L10NConstants.VAL_ONE)) {
          TextView view;
          // Inflate EditText and TextView on the Dialog box for WPS Pin
          LayoutInflater factory = LayoutInflater.from(BasicWirelessSettings.this);
          final View textEntryView = factory.inflate(R.layout.wps_dialog_layout, null);
          newPin = (EditText) textEntryView.findViewById(R.id.editText);
          view = (TextView) textEntryView.findViewById(R.id.txt_view);
          view.setText("Enter 8 digits");
          newPin.setHint("Enter PIN");

          bwsbuilder = new AlertDialog.Builder(BasicWirelessSettings.this);
          bwsbuilder.setTitle("Enter WPS PIN");
          bwsbuilder.setView(textEntryView);
          bwsalertdialog =
              bwsbuilder
                  .setPositiveButton(
                      getString(R.string.alert_dialog_rename_ok),
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                          sPinValue = newPin.getText().toString();
                          if (!sPinValue.equals("")) {
                            if (sPinValue.matches(L10NConstants.PIN_PATTERN)) {
                              // Extract 8th digit
                              String sLastDigit =
                                  sPinValue.substring(sPinValue.length() - 1, sPinValue.length());
                              Log.d("Checksum", "sLastDigit = " + sLastDigit);

                              // Compute checksum for 7 digits
                              int pinCheckSumValue =
                                  ComputeChecksum(
                                      Integer.parseInt(
                                          sPinValue.substring(0, sPinValue.length() - 1)));
                              Log.d("Checksum", "pinValueCheck = " + pinCheckSumValue);

                              // compare checksum value with 8th digit
                              if (pinCheckSumValue != Integer.parseInt(sLastDigit)) {
                                Toast.makeText(
                                        getApplicationContext(),
                                        "PIN checksum failed. Please enter a valid PIN",
                                        1)
                                    .show();
                              } else {
                                Log.d(
                                    L10NConstants.TAG_BWS,
                                    "Sending Command "
                                        + L10NConstants.SET_CMD_PREFIX
                                        + "config_methods=1 "
                                        + sPinValue);
                                try {
                                  sResponse =
                                      mSoftAPCfg.SapSendCommand(
                                          L10NConstants.SET_CMD_PREFIX
                                              + "config_methods=1 "
                                              + sPinValue);
                                } catch (Exception e) {
                                  Log.d(L10NConstants.TAG_BWS, "Exception :" + e);
                                }
                                Log.d(L10NConstants.TAG_BWS, "Response " + sResponse);
                                sResponse = L10NConstants.SUCCESS;
                                if (sResponse.contains(L10NConstants.SUCCESS)) {
                                  UpdateChanges(defSharPref, orgSharPref, preference.getKey());
                                  sPinEntryMsg =
                                      "Enter PIN number"
                                          + " "
                                          + sPinValue.toString()
                                          + " "
                                          + "on the client to connect...";
                                  new DialogThrd(L10NConstants.DIALOG_WPS_PINENTRY);
                                  Log.d(
                                      L10NConstants.TAG_BWS,
                                      "Response From Config_methods Success Reply " + sResponse);
                                } else {
                                  sWpsKey = orgSharPref.getString("wpsKey", "");
                                  UpdateChanges(orgSharPref, defSharPref, preference.getKey());
                                  bPrefChStatus = false;
                                  Log.d(
                                      L10NConstants.TAG_BWS,
                                      "Response From COnfig_methods UnSuccess Reply " + sResponse);
                                }
                              }
                            } else {
                              Toast.makeText(getApplicationContext(), "Invalid PIN", 1).show();
                            }
                          } else {
                            Toast.makeText(getApplicationContext(), "Value cannot be null", 1)
                                .show();
                          }
                        }
                      })
                  .setNegativeButton(
                      "Cancel",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                          // do nothing
                        }
                      })
                  .create();
          bwsalertdialog.show();
        } else if (newValue.equals(L10NConstants.VAL_ZERO)) {
          Log.d(
              L10NConstants.TAG_BWS,
              "Sending Command "
                  + L10NConstants.SET_CMD_PREFIX
                  + preference.getKey()
                  + "="
                  + newValue);
          try {
            sResponse =
                mSoftAPCfg.SapSendCommand(
                    L10NConstants.SET_CMD_PREFIX + preference.getKey() + "=" + newValue);
          } catch (Exception e) {
            Log.d(L10NConstants.TAG_BWS, "Exception :" + e);
          }
          Log.d(L10NConstants.TAG_BWS, "Response " + sResponse);
          if (sResponse.contains(L10NConstants.SUCCESS)) {
            // start counter
            UpdateChanges(defSharPref, orgSharPref, preference.getKey());
            new DialogThrd(L10NConstants.DIALOG_WPS_SESSION);
          } else {
            UpdateChanges(orgSharPref, defSharPref, preference.getKey());
          }
        }
        bPrefChStatus = false;
      } else if (preference == freqLst) {
        String sChannelCheck = defSharPref.getString(L10NConstants.CHNL_KEY, "");
        if (!sChannelCheck.equals(newValue)) {
          MainMenu.bPreferenceChanged = true;
        }
        // if channel selected is 12,13 or 14 then set network_mode=b
        defPrefEditor.putString(L10NConstants.CHNL_KEY, newValue.toString());
        defPrefEditor.commit();
        if (newValue.equals("12") || newValue.equals("13") || newValue.equals("14")) {
          onPreferenceChange(networkLst, "b");
        }
        if (newValue.equals(L10NConstants.VAL_ZERO)) {
          String sAutoChannel = defSharPref.getString("autoChannel", "");
          if (!sAutoChannel.equals(""))
            freqLst.setSummary(sLstEntry + "-Current Channel " + sAutoChannel);
          else freqLst.setSummary(sLstEntry);
        } else {
          freqLst.setSummary(newValue.toString());
        }

      } else if (preference == networkLst) {
        String sDataRate = defSharPref.getString(L10NConstants.DATA_RATE_KEY, "");
        String[] sDr = null;

        if (newValue.equals(L10NConstants.SM_B)) {
          sDr = getResources().getStringArray(R.array.dataRatesValuesB);
        } else if ((newValue.equals(L10NConstants.SM_G_ONLY))
            || (newValue.equals(L10NConstants.SM_G))) {
          sDr = getResources().getStringArray(R.array.dataRatesValuesGBG);
        } else if ((newValue.equals(L10NConstants.SM_N_ONLY))
            || (newValue.equals(L10NConstants.SM_N))) {
          sDr = getResources().getStringArray(R.array.dataRatesValuesNBGN);
        }
        if (!isAvailable(sDr, sDataRate)) {
          defPrefEditor.putString("data_rate", L10NConstants.VAL_ZERO);
        }

        sRsn = defSharPref.getString(L10NConstants.RSN_PAIR_KEY, "");
        sWpa = defSharPref.getString(L10NConstants.WPA_PAIR_KEY, "");
        sSM = defSharPref.getString(L10NConstants.SEC_MODE_KEY, "");

        if (newValue.equals(L10NConstants.SM_N_ONLY)) {
          if (sSM.equals(L10NConstants.VAL_ONE)) {
            sNwModeWarning =
                getString(R.string.bws_screen_alert_N_WEP)
                    + " "
                    + getString(R.string.common_append_alert_wep);
            bNModeVerifyCheck = true;
            verifyNModeAlgorithm(sNwModeWarning, lstPref, sLstEntry);
          } else {
            sNwModeWarning =
                getString(R.string.bws_screen_alert_N_TKIPorMIXED)
                    + " "
                    + getString(R.string.common_append_alert_wpa);
            bNModeVerifyCheck = false;
            verifyNModeAlgorithm(sNwModeWarning, lstPref, sLstEntry);
          }
        } else if (newValue.equals(L10NConstants.SM_N)) {
          if (sSM.equals(L10NConstants.VAL_ONE)) {
            sNwModeWarning =
                getString(R.string.bws_screen_alert_BGN_WEP)
                    + " "
                    + getString(R.string.common_append_alert_wep);
            bNModeVerifyCheck = true;
            verifyNModeAlgorithm(sNwModeWarning, lstPref, sLstEntry);
          } else {
            sNwModeWarning =
                getString(R.string.bws_screen_alert_BGN_TKIPorMIXED)
                    + " "
                    + getString(R.string.common_append_alert_wpa);
            bNModeVerifyCheck = false;
            verifyNModeAlgorithm(sNwModeWarning, lstPref, sLstEntry);
          }
        } else lstPref.setSummary(sLstEntry);
      }
      if (!bPrefChStatus == false) {
        if (preference == networkLst) {
          String sNetworkCheck = defSharPref.getString(L10NConstants.HW_MODE_KEY, "");
          if (!sNetworkCheck.equals(newValue)) {
            MainMenu.bPreferenceChanged = true;
          }
        } else if (preference == authLst) {
          String sAuthCheck = defSharPref.getString(L10NConstants.AUTH_MODE_KEY, "");
          if (!sAuthCheck.equals(newValue)) {
            MainMenu.bPreferenceChanged = true;
          }
        }
      }
    } else if (preference instanceof EditTextPreference) {
      EditTextPreference editPref = (EditTextPreference) preference;
      if ((preference == ssidEdit)) {
        if (!newValue.equals("")) {
          editPref.setSummary(newValue.toString());
          MainMenu.bPreferenceChanged = true;
        } else {
          Toast.makeText(this, "Value cannot be null", 1).show();
          bPrefChStatus = false;
        }
      }
    } else if (preference == broadChk) {
      if (!broadChk.isChecked()) {
        defPrefEditor.putString(L10NConstants.IGNORE_BROAD_SSID_KEY, L10NConstants.VAL_ZERO);
      } else {
        defPrefEditor.putString(L10NConstants.IGNORE_BROAD_SSID_KEY, L10NConstants.VAL_ONE);
      }
      defPrefEditor.commit();
      MainMenu.bPreferenceChanged = true;
    } else if (preference == configLst) {
      if (!configLst.isChecked()) {
        sSM = defSharPref.getString(L10NConstants.SEC_MODE_KEY, "");
        if (sSM.equals(L10NConstants.VAL_ONE)) {
          sWpsWarning = getString(R.string.wpa_screen_alert_wep);
          showAlertDialog(sWpsWarning);
          return false;
        }
        new DialogThrd(L10NConstants.DIALOG_WPS);
        defPrefEditor.putString(L10NConstants.WPS_KEY, L10NConstants.VAL_ONE);
        defPrefEditor.commit();
        mainMenuStngs.saveChanges(defSharPref, orgSharPref, "BWS");
        if (dialWPS != null) dialWPS.cancel();
        if (MainMenu.sWpsResponse != null) {
          if (MainMenu.sWpsResponse.contains(L10NConstants.SUCCESS)) {
            if (MainMenu.sCommitResponse.contains(L10NConstants.SUCCESS)) {
              configLst.setChecked(true);
            } else {
              bPrefChStatus = setPrevValue();
            }
          } else {
            bPrefChStatus = setPrevValue();
          }
        } else {
          configLst.setChecked(false);
        }
        MainMenu.btnSave.setEnabled(false);
      } else {
        new DialogThrd(L10NConstants.DIALOG_WPS);
        try {
          sResponse =
              mSoftAPCfg.SapSendCommand(
                  L10NConstants.SET_CMD_PREFIX + L10NConstants.WPS_KEY + "=0");
          Log.d(L10NConstants.TAG_BWS, "Response " + sResponse);
          sResponse = mSoftAPCfg.SapSendCommand(L10NConstants.SET_CMD_PREFIX + "commit");
        } catch (Exception e) {
          Log.d(L10NConstants.TAG_BWS, "Exception :" + e);
        }

        if (sResponse.equals(L10NConstants.SUCCESS)) {
          defPrefEditor.putString(L10NConstants.WPS_KEY, L10NConstants.VAL_ZERO);
          orgPrefEditor.putString(L10NConstants.WPS_KEY, L10NConstants.VAL_ZERO);
          if (timr != null) {
            timr.cancel();
          }
        } else {
          defPrefEditor.putString(L10NConstants.WPS_KEY, L10NConstants.VAL_ONE);
          orgPrefEditor.putString(L10NConstants.WPS_KEY, L10NConstants.VAL_ONE);
        }
        defPrefEditor.commit();
        orgPrefEditor.commit();
        if (dialWPS != null) dialWPS.cancel();
      }
    }
    return bPrefChStatus;
  }