@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == START_VPN_PROFILE) {
      if (resultCode == Activity.RESULT_OK) {
        int needpw = mSelectedProfile.needUserPWInput(false);
        if (needpw != 0) {
          VpnStatus.updateStateString(
              "USER_VPN_PASSWORD",
              "",
              R.string.openvpn_state_user_vpn_password,
              ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT);
          askForPW(needpw);
        } else {
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
          boolean showLogWindow = prefs.getBoolean("showlogwindow", true);

          if (!mhideLog && showLogWindow) showLogWindow();
          new startOpenVpnThread().start();
        }
      } else if (resultCode == Activity.RESULT_CANCELED) {
        // User does not want us to start, so we just vanish
        VpnStatus.updateStateString(
            "USER_VPN_PERMISSION_CANCELLED",
            "",
            R.string.openvpn_state_user_vpn_permission_cancelled,
            ConnectionStatus.LEVEL_NOTCONNECTED);

        finish();
      }
    }
  }
  private void execeuteSUcmd(String command) {
    ProcessBuilder pb = new ProcessBuilder("su", "-c", command);
    try {
      Process p = pb.start();
      int ret = p.waitFor();
      if (ret == 0) mCmfixed = true;
    } catch (InterruptedException e) {
      VpnStatus.logException("SU command", e);

    } catch (IOException e) {
      VpnStatus.logException("SU command", e);
    }
  }
  @Override
  protected void onStart() {
    super.onStart();
    // Resolve the intent

    final Intent intent = getIntent();
    final String action = intent.getAction();

    // If the intent is a request to create a shortcut, we'll do that and exit

    if (Intent.ACTION_MAIN.equals(action)) {
      // we got called to be the starting point, most likely a shortcut
      String shortcutUUID = intent.getStringExtra(EXTRA_KEY);
      String shortcutName = intent.getStringExtra(EXTRA_NAME);
      mhideLog = intent.getBooleanExtra(EXTRA_HIDELOG, false);

      VpnProfile profileToConnect = ProfileManager.get(this, shortcutUUID);
      if (shortcutName != null && profileToConnect == null)
        profileToConnect = ProfileManager.getInstance(this).getProfileByName(shortcutName);

      if (profileToConnect == null) {
        VpnStatus.logError(R.string.openvpn_shortcut_profile_notfound);
        // show Log window to display error
        showLogWindow();
        finish();
        return;
      }

      mSelectedProfile = profileToConnect;
      launchVPN();
    }
  }
  void launchVPN() {
    int vpnok = mSelectedProfile.checkProfile(this);
    if (vpnok != R.string.openvpn_no_error_found) {
      showConfigErrorDialog(vpnok);
      return;
    }

    Intent intent = VpnService.prepare(this);
    // Check if we want to fix /dev/tun
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean usecm9fix = prefs.getBoolean("useCM9Fix", false);
    boolean loadTunModule = prefs.getBoolean("loadTunModule", false);

    if (loadTunModule) execeuteSUcmd("insmod /system/lib/modules/tun.ko");

    if (usecm9fix && !mCmfixed) {
      execeuteSUcmd("chown system /dev/tun");
    }

    if (intent != null) {
      VpnStatus.updateStateString(
          "USER_VPN_PERMISSION",
          "",
          R.string.openvpn_state_user_vpn_permission,
          ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT);
      // Start the query
      try {
        startActivityForResult(intent, START_VPN_PROFILE);
      } catch (ActivityNotFoundException ane) {
        // Shame on you Sony! At least one user reported that
        // an official Sony Xperia Arc S image triggers this exception
        VpnStatus.logError(R.string.openvpn_no_vpn_support_image);
        showLogWindow();
      }
    } else {
      onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null);
    }
  }
  public void emailMiniDumps() {
    // need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("*/*");
    emailIntent.putExtra(
        android.content.Intent.EXTRA_EMAIL, new String[] {"Arne Schwabe <*****@*****.**>"});

    String version;
    String name = "ics-openvpn";
    try {
      PackageInfo packageinfo =
          getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
      version = packageinfo.versionName;
      name = packageinfo.applicationInfo.name;
    } catch (NameNotFoundException e) {
      version = "error fetching version";
    }

    emailIntent.putExtra(
        Intent.EXTRA_SUBJECT,
        String.format("%s(%s) %s Minidump", name, getActivity().getPackageName(), version));

    emailIntent.putExtra(Intent.EXTRA_TEXT, "Please describe the issue you have experienced");

    ArrayList<Uri> uris = new ArrayList<>();

    Pair<File, Long> ldump = getLastestDump(getActivity());
    if (ldump == null) {
      VpnStatus.logError("No Minidump found!");
    }

    uris.add(Uri.parse("content://de.blinkt.openvpn.FileProvider/" + ldump.first.getName()));
    uris.add(
        Uri.parse("content://de.blinkt.openvpn.FileProvider/" + ldump.first.getName() + ".log"));

    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    startActivity(emailIntent);
  }