private void startTethering() {
   switch (mTetherChoice) {
     case BLUETOOTH_TETHERING:
       // turn on Bluetooth first
       BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
       if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
         mBluetoothEnableForTether = true;
         adapter.enable();
         mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
         mBluetoothTether.setEnabled(false);
       } else {
         BluetoothPan bluetoothPan = mBluetoothPan.get();
         if (bluetoothPan != null) {
           bluetoothPan.setBluetoothTethering(true);
         }
         /// M: set blue tooth dun tethering to true @{
         BluetoothDun bluetoothDun = BluetoothDunGetProxy();
         if (bluetoothDun != null) {
           bluetoothDun.setBluetoothTethering(true);
         }
         String summary = getString(R.string.bluetooth_tethering_available_subtext);
         if (FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
           mBluetoothTether.setSummary(summary + getIPV6String(mBtErrorIpv4, mBtErrorIpv6));
         } else {
           mBluetoothTether.setSummary(summary);
         }
         /// @}
       }
       break;
     case USB_TETHERING:
       setUsbTethering(true);
       break;
     default:
       // should not happen
       break;
   }
 }
  @Override
  public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    if (preference == mUsbTether) {
      if (!mUsbTethering) {
        boolean newState = mUsbTether.isChecked();

        /// M: update usb tethering @{
        mUsbTether.setEnabled(false);
        mUsbTetherType.setEnabled(false);
        mUsbTethering = true;
        mUsbTetherCheckEnable = false;
        if (newState) {
          mUsbTetherDone = false;
        } else {
          mUsbUnTetherDone = false;
        }
        mUsbTetherFail = false;

        Xlog.d(
            TAG,
            "onPreferenceTreeClick - setusbTethering("
                + newState
                + ") mUsbTethering:  "
                + mUsbTethering);
        /// @}

        if (newState) {
          startProvisioningIfNecessary(USB_TETHERING);
        } else {
          setUsbTethering(newState);
        }
      } else {
        return true;
      }
    } else if (preference == mBluetoothTether) {
      boolean bluetoothTetherState = mBluetoothTether.isChecked();

      if (bluetoothTetherState) {
        startProvisioningIfNecessary(BLUETOOTH_TETHERING);
      } else {
        boolean errored = false;

        String[] tethered = cm.getTetheredIfaces();
        String bluetoothIface = findIface(tethered, mBluetoothRegexs);
        if (bluetoothIface != null
            && cm.untether(bluetoothIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
          errored = true;
        }

        BluetoothPan bluetoothPan = mBluetoothPan.get();
        if (bluetoothPan != null) {
          bluetoothPan.setBluetoothTethering(false);
        }
        /// M: set bluetooth tethering to false @{
        BluetoothDun bluetoothDun = BluetoothDunGetProxy();
        if (bluetoothDun != null) {
          bluetoothDun.setBluetoothTethering(false);
        }
        /// @}
        if (errored) {
          mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
        } else {
          mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
        }
      }
      /// M: @{
      if (FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
        updateIpv6Preference();
      }
      /// @}
    }

    return super.onPreferenceTreeClick(screen, preference);
  }
    @Override
    public void onReceive(Context content, Intent intent) {
      String action = intent.getAction();
      /// M:
      Xlog.d(TAG, "TetherChangeReceiver - onReceive, action is " + action);

      if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
        // TODO - this should understand the interface types
        ArrayList<String> available =
            intent.getStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER);
        ArrayList<String> active =
            intent.getStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER);
        ArrayList<String> errored =
            intent.getStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER);

        /** M: for bug solving, ALPS00331223 */
        mUsbUnTetherDone = intent.getBooleanExtra("UnTetherDone", false);
        mUsbTetherDone = intent.getBooleanExtra("TetherDone", false);
        mUsbTetherFail = intent.getBooleanExtra("TetherFail", false);

        /// M: print log
        Xlog.d(
            TAG,
            "mUsbUnTetherDone? :"
                + mUsbUnTetherDone
                + " , mUsbTetherDonel? :"
                + mUsbTetherDone
                + " , tether fail? :"
                + mUsbTetherFail);
        updateState(
            available.toArray(new String[available.size()]),
            active.toArray(new String[active.size()]),
            errored.toArray(new String[errored.size()]));
      } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
        mMassStorageActive = true;
        updateState();
      } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
        mMassStorageActive = false;
        updateState();
      } else if (action.equals(UsbManager.ACTION_USB_STATE)) {
        mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
        /// M: @{
        mUsbConfigured = intent.getBooleanExtra(UsbManager.USB_CONFIGURED, false);
        mUsbHwDisconnected = intent.getBooleanExtra("USB_HW_DISCONNECTED", false);
        mIsPcKnowMe = intent.getBooleanExtra("USB_IS_PC_KNOW_ME", true);

        Xlog.d(
            TAG,
            "TetherChangeReceiver - ACTION_USB_STATE mUsbConnected: "
                + mUsbConnected
                + ", mUsbConfigured:  "
                + mUsbConfigured
                + ", mUsbHwDisconnected: "
                + mUsbHwDisconnected);
        /// @}
        updateState();
      } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        if (mBluetoothEnableForTether) {
          switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
            case BluetoothAdapter.STATE_ON:
              BluetoothPan bluetoothPan = mBluetoothPan.get();
              if (bluetoothPan != null) {
                bluetoothPan.setBluetoothTethering(true);
                mBluetoothEnableForTether = false;
              }
              /// M: @{
              BluetoothDun bluetoothDun = BluetoothDunGetProxy();
              if (bluetoothDun != null) {
                bluetoothDun.setBluetoothTethering(true);
                mBluetoothEnableForTether = false;
              }
              /// @}
              break;

            case BluetoothAdapter.STATE_OFF:
            case BluetoothAdapter.ERROR:
              mBluetoothEnableForTether = false;
              break;

            default:
              // ignore transition states
          }
        }
        updateState();
      } else if (action.equals(USB_DATA_STATE)) {
        String dataApnKey = intent.getStringExtra(PhoneConstants.DATA_APN_KEY);
        PhoneConstants.DataState state =
            Enum.valueOf(
                PhoneConstants.DataState.class, intent.getStringExtra(PhoneConstants.STATE_KEY));
        Xlog.d(TAG, "receive USB_DATA_STATE");
        Xlog.d(TAG, "dataApnKey = " + dataApnKey + ", state = " + state);
        if ("internet".equals(dataApnKey)) {
          if (state == PhoneConstants.DataState.CONNECTED) {
            mUsbInternetSharing = true;
          } else {
            mUsbInternetSharing = false;
          }
          updateState();
        }
      } else if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) {
        /// M: update ipv4 & ipv6 preference @{
        int state =
            intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED);
        if ((state == WifiManager.WIFI_AP_STATE_ENABLED
                || state == WifiManager.WIFI_AP_STATE_DISABLED)
            && FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
          updateIpv6Preference();
        }
        /// @}
      } else if (action.equals(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED)
          || action.equals(BluetoothDun.STATE_CHANGED_ACTION)) {
        updateState();
      }
    }
  private void updateBluetoothState(String[] available, String[] tethered, String[] errored) {
    /// M:   @{
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    if (FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
      mBtErrorIpv4 = ConnectivityManager.TETHER_ERROR_NO_ERROR;
      mBtErrorIpv6 = ConnectivityManager.TETHER_ERROR_IPV6_NO_ERROR;
      for (String s : available) {
        for (String regex : mBluetoothRegexs) {
          if (s.matches(regex) && cm != null) {
            if (mBtErrorIpv4 == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
              mBtErrorIpv4 = (cm.getLastTetherError(s) & 0x0f);
            }
            if (mBtErrorIpv6 == ConnectivityManager.TETHER_ERROR_IPV6_NO_ERROR) {
              mBtErrorIpv6 = (cm.getLastTetherError(s) & 0xf0);
            }
          }
        }
      }
    }
    /// @}

    boolean bluetoothErrored = false;
    for (String s : errored) {
      for (String regex : mBluetoothRegexs) {
        if (s.matches(regex)) {
          bluetoothErrored = true;
        }
      }
    }

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
      return;
    }
    int btState = adapter.getState();
    Xlog.d(TAG, "btState = " + btState);
    if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
      mBluetoothTether.setEnabled(false);
      mBluetoothTether.setSummary(R.string.bluetooth_turning_off);
    } else if (btState == BluetoothAdapter.STATE_TURNING_ON) {
      mBluetoothTether.setEnabled(false);
      mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
    } else {
      BluetoothPan bluetoothPan = mBluetoothPan.get();
      /// M:
      BluetoothDun bluetoothDun = BluetoothDunGetProxy();
      if (btState == BluetoothAdapter.STATE_ON
          && ((bluetoothPan != null && bluetoothPan.isTetheringOn())
              || (bluetoothDun != null && bluetoothDun.isTetheringOn()))) {
        mBluetoothTether.setChecked(true);
        mBluetoothTether.setEnabled(true);
        int bluetoothTethered = 0;
        if (bluetoothPan != null && bluetoothPan.isTetheringOn()) {
          bluetoothTethered = bluetoothPan.getConnectedDevices().size();
          Xlog.d(TAG, "bluetooth Tethered PAN devices = " + bluetoothTethered);
        }
        if (bluetoothDun != null && bluetoothDun.isTetheringOn()) {
          bluetoothTethered += bluetoothDun.getConnectedDevices().size();
          Xlog.d(TAG, "bluetooth tethered total devices = " + bluetoothTethered);
        }

        if (bluetoothTethered > 1) {
          String summary =
              getString(R.string.bluetooth_tethering_devices_connected_subtext, bluetoothTethered);
          if (FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
            /// M:
            mBluetoothTether.setSummary(summary + getIPV6String(mBtErrorIpv4, mBtErrorIpv6));
          } else {
            mBluetoothTether.setSummary(summary);
          }
        } else if (bluetoothTethered == 1) {
          /// M: @{
          String summary = getString(R.string.bluetooth_tethering_device_connected_subtext);
          if (FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
            mBluetoothTether.setSummary(summary + getIPV6String(mBtErrorIpv4, mBtErrorIpv6));
          } else {
            mBluetoothTether.setSummary(summary);
          }
          /// @}
        } else if (bluetoothErrored) {
          mBluetoothTether.setSummary(R.string.bluetooth_tethering_errored_subtext);
        } else {
          /// M: @{
          String summary = getString(R.string.bluetooth_tethering_available_subtext);
          if (FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
            mBluetoothTether.setSummary(summary + getIPV6String(mBtErrorIpv4, mBtErrorIpv6));
          } else {
            mBluetoothTether.setSummary(summary);
          }
          /// @}
        }
      } else {
        mBluetoothTether.setEnabled(true);
        mBluetoothTether.setChecked(false);
        mBluetoothTether.setSummary(R.string.bluetooth_tethering_off_subtext);
      }
    }
  }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    addPreferencesFromResource(R.xml.tether_prefs);

    /// M: get plugin
    mExt = Utils.getApnSettingsPlugin(getActivity());

    final Activity activity = getActivity();
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
      adapter.getProfileProxy(
          activity.getApplicationContext(), mProfileServiceListener, BluetoothProfile.PAN);
    }
    /// M: @{
    mBluetoothDunProxy =
        new BluetoothDun(getActivity().getApplicationContext(), mDunServiceListener);
    mWifiTether = (HotspotSwitchPreference) findPreference(WIFI_SWITCH_SETTINGS);
    mWifiTether.setChecked(false);
    /// @}

    mUsbTether = (CheckBoxPreference) findPreference(USB_TETHER_SETTINGS);
    mBluetoothTether = (CheckBoxPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);

    mTetherIpv6 = (ListPreference) findPreference(TETHERED_IPV6);
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    /// M: @{
    mUsbTetherType = (ListPreference) findPreference(USB_TETHERING_TYPE);
    if (!FeatureOption.MTK_TETHERING_EEM_SUPPORT) {
      getPreferenceScreen().removePreference(mUsbTetherType);
    }

    /// @}
    mUsbRegexs = cm.getTetherableUsbRegexs();
    mWifiRegexs = cm.getTetherableWifiRegexs();
    mBluetoothRegexs = cm.getTetherableBluetoothRegexs();

    final boolean usbAvailable = mUsbRegexs.length != 0;
    final boolean wifiAvailable = mWifiRegexs.length != 0;
    final boolean bluetoothAvailable = mBluetoothRegexs.length != 0;

    if (!usbAvailable || Utils.isMonkeyRunning()) {
      getPreferenceScreen().removePreference(mUsbTether);
      getPreferenceScreen().removePreference(mUsbTetherType);
    }

    if (wifiAvailable && !Utils.isMonkeyRunning()) {
      mWifiApEnabler = new WifiApEnabler(activity, mWifiTether);
      mWifiApEnabler.setTetherSettings(this);
    } else {
      getPreferenceScreen().removePreference(mWifiTether);
    }

    if (!bluetoothAvailable) {
      getPreferenceScreen().removePreference(mBluetoothTether);
    } else {
      BluetoothPan pan = mBluetoothPan.get();
      /// M:
      BluetoothDun dun = BluetoothDunGetProxy();
      if ((pan != null && pan.isTetheringOn()) || (dun != null && dun.isTetheringOn())) {
        mBluetoothTether.setChecked(true);
      } else {
        mBluetoothTether.setChecked(false);
      }
    }

    /// M: add tether apn settings
    mExt.customizeTetherApnSettings(getPreferenceScreen());

    mProvisionApp =
        getResources()
            .getStringArray(com.android.internal.R.array.config_mobile_hotspot_provision_app);

    mView = new WebView(activity);

    /// M: @{
    if (FeatureOption.MTK_TETHERINGIPV6_SUPPORT) {
      if (mTetherIpv6 != null) {
        mTetherIpv6.setOnPreferenceChangeListener(this);
      }
    } else {
      getPreferenceScreen().removePreference(mTetherIpv6);
    }
    getMountService();
    /// @}
  }