package org.strongswan.android.ui;
import android.annotation.TargetApi;
-import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDialogFragment;
public class TrustedCertificateImportActivity extends AppCompatActivity
{
- private static final int OPEN_DOCUMENT = 0;
private static final String DIALOG_TAG = "Dialog";
private Uri mCertificateUri;
+ private final ActivityResultLauncher<Intent> mOpenDocument = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ if (result.getResultCode() == RESULT_OK && result.getData() != null)
+ {
+ mCertificateUri = result.getData().getData();
+ return;
+ }
+ finish();
+ }
+ );
+
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onCreate(Bundle savedInstanceState)
openIntent.setType("*/*");
try
{
- startActivityForResult(openIntent, OPEN_DOCUMENT);
+ mOpenDocument.launch(openIntent);
}
catch (ActivityNotFoundException e)
{ /* some devices are unable to browse for files */
}
}
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode)
- {
- case OPEN_DOCUMENT:
- if (resultCode == Activity.RESULT_OK && data != null)
- {
- mCertificateUri = data.getData();
- return;
- }
- finish();
- return;
- }
- }
-
@Override
protected void onPostResume()
{
if (activity.storeCertificate(certificate))
{
Toast.makeText(getActivity(), R.string.cert_imported_successfully, Toast.LENGTH_LONG).show();
- getActivity().setResult(Activity.RESULT_OK);
+ getActivity().setResult(RESULT_OK);
}
else
{
package org.strongswan.android.ui;
-import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import java.security.KeyStore;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
{
public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
private static final String DIALOG_TAG = "Dialog";
- private static final int IMPORT_CERTIFICATE = 0;
private TrustedCertificatesPagerAdapter mAdapter;
private ViewPager mPager;
private boolean mSelect;
+ private final ActivityResultLauncher<Intent> mImportCertificate = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ if (result.getResultCode() == RESULT_OK)
+ {
+ reloadCertificates();
+ }
+ }
+ );
+
@Override
public void onCreate(Bundle savedInstanceState)
{
return true;
case R.id.menu_import_certificate:
Intent intent = new Intent(this, TrustedCertificateImportActivity.class);
- startActivityForResult(intent, IMPORT_CERTIFICATE);
+ mImportCertificate.launch(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- switch (requestCode)
- {
- case IMPORT_CERTIFICATE:
- if (resultCode == Activity.RESULT_OK)
- {
- reloadCertificates();
- }
- return;
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
-
@Override
public void onTrustedCertificateSelected(TrustedCertificateEntry selected)
{
/* the user selected a certificate, return to calling activity */
Intent intent = new Intent();
intent.putExtra(VpnProfileDataSource.KEY_CERTIFICATE, selected.getAlias());
- setResult(Activity.RESULT_OK, intent);
+ setResult(RESULT_OK, intent);
finish();
}
else if (mAdapter.getSource(mPager.getCurrentItem()) == TrustedCertificateSource.LOCAL)
import org.strongswan.android.logic.VpnStateService.State;
import org.strongswan.android.utils.Constants;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public static final String DISCONNECT = "org.strongswan.android.action.DISCONNECT";
public static final String EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID";
- private static final int PREPARE_VPN_SERVICE = 0;
- private static final int ADD_TO_POWER_WHITELIST = 1;
private static final String WAITING_FOR_RESULT = "WAITING_FOR_RESULT";
private static final String PROFILE_NAME = "PROFILE_NAME";
private static final String PROFILE_REQUIRES_PASSWORD = "REQUIRES_PASSWORD";
}
};
+ private final ActivityResultLauncher<Intent> mPrepareVpnService = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ mWaitingForResult = false;
+ if (result.getResultCode() == RESULT_OK && mProfileInfo != null)
+ {
+ onVpnServicePrepared();
+ }
+ else
+ { /* this happens if the always-on VPN feature is activated by a different app or the user declined */
+ VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported_no_permission);
+ }
+ }
+ );
+
+ private final ActivityResultLauncher<Intent> mAddToPowerWhitelist = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ mWaitingForResult = false;
+ if (mProfileInfo != null && mService != null)
+ {
+ mService.connect(mProfileInfo, true);
+ }
+ finish();
+ }
+ );
+
@Override
public void onCreate(Bundle savedInstanceState)
{
try
{
mWaitingForResult = true;
- startActivityForResult(intent, PREPARE_VPN_SERVICE);
+ mPrepareVpnService.launch(intent);
}
catch (ActivityNotFoundException ex)
{
}
else
{ /* user already granted permission to use VpnService */
- onActivityResult(PREPARE_VPN_SERVICE, RESULT_OK, null);
+ onVpnServicePrepared();
+ }
+ }
+
+ /**
+ * Called once the VpnService has been prepared and permission has been granted
+ * by the user.
+ */
+ protected void onVpnServicePrepared()
+ {
+ if (checkPowerWhitelist())
+ {
+ if (mService != null)
+ {
+ mService.connect(mProfileInfo, true);
+ }
+ finish();
}
}
return true;
}
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- switch (requestCode)
- {
- case PREPARE_VPN_SERVICE:
- mWaitingForResult = false;
- if (resultCode == RESULT_OK && mProfileInfo != null)
- {
- if (checkPowerWhitelist())
- {
- if (mService != null)
- {
- mService.connect(mProfileInfo, true);
- }
- finish();
- }
- }
- else
- { /* this happens if the always-on VPN feature is activated by a different app or the user declined */
- if (getSupportFragmentManager().isStateSaved())
- { /* onActivityResult() might be called when we aren't active anymore e.g. if the
- * user pressed the home button, if the activity is started again we land here
- * before onNewIntent() is called */
- return;
- }
- VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported_no_permission);
- }
- break;
- case ADD_TO_POWER_WHITELIST:
- mWaitingForResult = false;
- if (mProfileInfo != null && mService != null)
- {
- mService.connect(mProfileInfo, true);
- }
- finish();
- break;
- default:
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
-
/**
* Check if we are currently connected to a VPN connection
*
activity.mWaitingForResult = true;
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + activity.getPackageName()));
- activity.startActivityForResult(intent, ADD_TO_POWER_WHITELIST);
+ activity.mAddToPowerWhitelist.launch(intent);
}).create();
}
import java.util.TreeSet;
import java.util.UUID;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDialogFragment;
public class VpnProfileDetailActivity extends AppCompatActivity
{
- private static final int SELECT_TRUSTED_CERTIFICATE = 0;
- private static final int SELECT_APPLICATIONS = 1;
-
private VpnProfileDataSource mDataSource;
private Long mId;
private TrustedCertificateEntry mCertEntry;
private EditText mDnsServers;
private TextInputLayoutHelper mDnsServersWrap;
+ private final ActivityResultLauncher<Intent> mSelectTrustedCertificate = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ if (result.getResultCode() == RESULT_OK)
+ {
+ String alias = result.getData().getStringExtra(VpnProfileDataSource.KEY_CERTIFICATE);
+ X509Certificate certificate = TrustedCertificateManager.getInstance().getCACertificateFromAlias(alias);
+ mCertEntry = certificate == null ? null : new TrustedCertificateEntry(alias, certificate);
+ updateCertificateSelector();
+ }
+ }
+ );
+
+ private final ActivityResultLauncher<Intent> mSelectApplications = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ if (result.getResultCode() == RESULT_OK)
+ {
+ ArrayList<String> selection = result.getData().getStringArrayListExtra(VpnProfileDataSource.KEY_SELECTED_APPS_LIST);
+ mSelectedApps = new TreeSet<>(selection);
+ updateAppsSelector();
+ }
+ }
+ );
+
@Override
public void onCreate(Bundle savedInstanceState)
{
{
Intent intent = new Intent(VpnProfileDetailActivity.this, TrustedCertificatesActivity.class);
intent.setAction(TrustedCertificatesActivity.SELECT_CERTIFICATE);
- startActivityForResult(intent, SELECT_TRUSTED_CERTIFICATE);
+ mSelectTrustedCertificate.launch(intent);
}
});
{
Intent intent = new Intent(VpnProfileDetailActivity.this, SelectedApplicationsActivity.class);
intent.putExtra(VpnProfileDataSource.KEY_SELECTED_APPS_LIST, new ArrayList<>(mSelectedApps));
- startActivityForResult(intent, SELECT_APPLICATIONS);
+ mSelectApplications.launch(intent);
}
});
}
}
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- switch (requestCode)
- {
- case SELECT_TRUSTED_CERTIFICATE:
- if (resultCode == RESULT_OK)
- {
- String alias = data.getStringExtra(VpnProfileDataSource.KEY_CERTIFICATE);
- X509Certificate certificate = TrustedCertificateManager.getInstance().getCACertificateFromAlias(alias);
- mCertEntry = certificate == null ? null : new TrustedCertificateEntry(alias, certificate);
- updateCertificateSelector();
- }
- break;
- case SELECT_APPLICATIONS:
- if (resultCode == RESULT_OK)
- {
- ArrayList<String> selection = data.getStringArrayListExtra(VpnProfileDataSource.KEY_SELECTED_APPS_LIST);
- mSelectedApps = new TreeSet<>(selection);
- updateAppsSelector();
- }
- break;
- default:
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
-
/**
* Update the UI to enter credentials depending on the type of VPN currently selected
*/
package org.strongswan.android.ui;
-import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import javax.net.ssl.SSLHandshakeException;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
{
private static final String PKCS12_INSTALLED = "PKCS12_INSTALLED";
private static final String PROFILE_URI = "PROFILE_URI";
- private static final int INSTALL_PKCS12 = 0;
- private static final int OPEN_DOCUMENT = 1;
private static final int PROFILE_LOADER = 0;
private static final int USER_CERT_LOADER = 1;
private ViewGroup mRemoteCertificate;
private RelativeLayout mRemoteCert;
+ private final ActivityResultLauncher<Intent> mImportPKCS12 = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ if (result.getResultCode() == RESULT_OK)
+ { /* no need to import twice */
+ mImportUserCert.setEnabled(false);
+ mSelectUserCert.performClick();
+ }
+ }
+ );
+
+ private final ActivityResultLauncher<Intent> mOpenDocument = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ if (result.getResultCode() == RESULT_OK && result.getData() != null)
+ {
+ loadProfile(result.getData().getData());
+ return;
+ }
+ finish();
+ }
+ );
+
private LoaderManager.LoaderCallbacks<ProfileLoadResult> mProfileLoaderCallbacks = new LoaderManager.LoaderCallbacks<ProfileLoadResult>()
{
@Override
Intent intent = KeyChain.createInstallIntent();
intent.putExtra(KeyChain.EXTRA_NAME, getString(R.string.profile_cert_alias, mProfile.getName()));
intent.putExtra(KeyChain.EXTRA_PKCS12, mProfile.PKCS12);
- startActivityForResult(intent, INSTALL_PKCS12);
+ mImportPKCS12.launch(intent);
}
});
openIntent.setType("*/*");
try
{
- startActivityForResult(openIntent, OPEN_DOCUMENT);
+ mOpenDocument.launch(openIntent);
}
catch (ActivityNotFoundException e)
{ /* some devices are unable to browse for files */
}
}
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode)
- {
- case INSTALL_PKCS12:
- if (resultCode == Activity.RESULT_OK)
- { /* no need to import twice */
- mImportUserCert.setEnabled(false);
- mSelectUserCert.performClick();
- }
- break;
- case OPEN_DOCUMENT:
- if (resultCode == Activity.RESULT_OK && data != null)
- {
- loadProfile(data.getData());
- return;
- }
- finish();
- break;
- }
- }
-
private void loadProfile(Uri uri)
{
mProgressBar.show();
public class VpnProfileListFragment extends Fragment
{
private static final String SELECTED_KEY = "SELECTED";
- private static final int ADD_REQUEST = 1;
- private static final int EDIT_REQUEST = 2;
private List<VpnProfile> mVpnProfiles;
private VpnProfileDataSource mDataSource;
case R.id.add_profile:
Intent connectionIntent = new Intent(getActivity(),
VpnProfileDetailActivity.class);
- startActivityForResult(connectionIntent, ADD_REQUEST);
+ startActivity(connectionIntent);
return true;
default:
return super.onOptionsItemSelected(item);
VpnProfile profile = (VpnProfile)mListView.getItemAtPosition(position);
Intent connectionIntent = new Intent(getActivity(), VpnProfileDetailActivity.class);
connectionIntent.putExtra(VpnProfileDataSource.KEY_ID, profile.getId());
- startActivityForResult(connectionIntent, EDIT_REQUEST);
+ startActivity(connectionIntent);
break;
}
case R.id.copy_profile:
Intent connectionIntent = new Intent(getActivity(), VpnProfileDetailActivity.class);
connectionIntent.putExtra(VpnProfileDataSource.KEY_ID, profile.getId());
- startActivityForResult(connectionIntent, EDIT_REQUEST);
+ startActivity(connectionIntent);
break;
}
case R.id.delete_profile: