android: Replace deprecated startActivityForResult/onActivityResult usage
This commit is contained in:
+16
-21
@@ -16,7 +16,6 @@
|
|||||||
package org.strongswan.android.ui;
|
package org.strongswan.android.ui;
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
@@ -37,6 +36,8 @@ import java.security.cert.CertificateException;
|
|||||||
import java.security.cert.CertificateFactory;
|
import java.security.cert.CertificateFactory;
|
||||||
import java.security.cert.X509Certificate;
|
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.AlertDialog;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.app.AppCompatDialogFragment;
|
import androidx.appcompat.app.AppCompatDialogFragment;
|
||||||
@@ -44,10 +45,21 @@ import androidx.fragment.app.FragmentTransaction;
|
|||||||
|
|
||||||
public class TrustedCertificateImportActivity extends AppCompatActivity
|
public class TrustedCertificateImportActivity extends AppCompatActivity
|
||||||
{
|
{
|
||||||
private static final int OPEN_DOCUMENT = 0;
|
|
||||||
private static final String DIALOG_TAG = "Dialog";
|
private static final String DIALOG_TAG = "Dialog";
|
||||||
private Uri mCertificateUri;
|
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)
|
@TargetApi(Build.VERSION_CODES.KITKAT)
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState)
|
||||||
@@ -71,7 +83,7 @@ public class TrustedCertificateImportActivity extends AppCompatActivity
|
|||||||
openIntent.setType("*/*");
|
openIntent.setType("*/*");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
startActivityForResult(openIntent, OPEN_DOCUMENT);
|
mOpenDocument.launch(openIntent);
|
||||||
}
|
}
|
||||||
catch (ActivityNotFoundException e)
|
catch (ActivityNotFoundException e)
|
||||||
{ /* some devices are unable to browse for files */
|
{ /* some devices are unable to browse for files */
|
||||||
@@ -81,23 +93,6 @@ public class TrustedCertificateImportActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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
|
@Override
|
||||||
protected void onPostResume()
|
protected void onPostResume()
|
||||||
{
|
{
|
||||||
@@ -214,7 +209,7 @@ public class TrustedCertificateImportActivity extends AppCompatActivity
|
|||||||
if (activity.storeCertificate(certificate))
|
if (activity.storeCertificate(certificate))
|
||||||
{
|
{
|
||||||
Toast.makeText(getActivity(), R.string.cert_imported_successfully, Toast.LENGTH_LONG).show();
|
Toast.makeText(getActivity(), R.string.cert_imported_successfully, Toast.LENGTH_LONG).show();
|
||||||
getActivity().setResult(Activity.RESULT_OK);
|
getActivity().setResult(RESULT_OK);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
+14
-19
@@ -15,7 +15,6 @@
|
|||||||
|
|
||||||
package org.strongswan.android.ui;
|
package org.strongswan.android.ui;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
@@ -34,6 +33,8 @@ import org.strongswan.android.ui.CertificateDeleteConfirmationDialog.OnCertifica
|
|||||||
|
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
|
|
||||||
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
@@ -45,11 +46,20 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
|
|||||||
{
|
{
|
||||||
public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
|
public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
|
||||||
private static final String DIALOG_TAG = "Dialog";
|
private static final String DIALOG_TAG = "Dialog";
|
||||||
private static final int IMPORT_CERTIFICATE = 0;
|
|
||||||
private TrustedCertificatesPagerAdapter mAdapter;
|
private TrustedCertificatesPagerAdapter mAdapter;
|
||||||
private ViewPager mPager;
|
private ViewPager mPager;
|
||||||
private boolean mSelect;
|
private boolean mSelect;
|
||||||
|
|
||||||
|
private final ActivityResultLauncher<Intent> mImportCertificate = registerForActivityResult(
|
||||||
|
new ActivityResultContracts.StartActivityForResult(),
|
||||||
|
result -> {
|
||||||
|
if (result.getResultCode() == RESULT_OK)
|
||||||
|
{
|
||||||
|
reloadCertificates();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
@@ -100,27 +110,12 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
|
|||||||
return true;
|
return true;
|
||||||
case R.id.menu_import_certificate:
|
case R.id.menu_import_certificate:
|
||||||
Intent intent = new Intent(this, TrustedCertificateImportActivity.class);
|
Intent intent = new Intent(this, TrustedCertificateImportActivity.class);
|
||||||
startActivityForResult(intent, IMPORT_CERTIFICATE);
|
mImportCertificate.launch(intent);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
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
|
@Override
|
||||||
public void onTrustedCertificateSelected(TrustedCertificateEntry selected)
|
public void onTrustedCertificateSelected(TrustedCertificateEntry selected)
|
||||||
{
|
{
|
||||||
@@ -129,7 +124,7 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
|
|||||||
/* the user selected a certificate, return to calling activity */
|
/* the user selected a certificate, return to calling activity */
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.putExtra(VpnProfileDataSource.KEY_CERTIFICATE, selected.getAlias());
|
intent.putExtra(VpnProfileDataSource.KEY_CERTIFICATE, selected.getAlias());
|
||||||
setResult(Activity.RESULT_OK, intent);
|
setResult(RESULT_OK, intent);
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
else if (mAdapter.getSource(mPager.getCurrentItem()) == TrustedCertificateSource.LOCAL)
|
else if (mAdapter.getSource(mPager.getCurrentItem()) == TrustedCertificateSource.LOCAL)
|
||||||
|
|||||||
+48
-47
@@ -44,6 +44,8 @@ import org.strongswan.android.logic.VpnStateService;
|
|||||||
import org.strongswan.android.logic.VpnStateService.State;
|
import org.strongswan.android.logic.VpnStateService.State;
|
||||||
import org.strongswan.android.utils.Constants;
|
import org.strongswan.android.utils.Constants;
|
||||||
|
|
||||||
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
@@ -59,8 +61,6 @@ public class VpnProfileControlActivity extends AppCompatActivity
|
|||||||
public static final String DISCONNECT = "org.strongswan.android.action.DISCONNECT";
|
public static final String DISCONNECT = "org.strongswan.android.action.DISCONNECT";
|
||||||
public static final String EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID";
|
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 WAITING_FOR_RESULT = "WAITING_FOR_RESULT";
|
||||||
private static final String PROFILE_NAME = "PROFILE_NAME";
|
private static final String PROFILE_NAME = "PROFILE_NAME";
|
||||||
private static final String PROFILE_REQUIRES_PASSWORD = "REQUIRES_PASSWORD";
|
private static final String PROFILE_REQUIRES_PASSWORD = "REQUIRES_PASSWORD";
|
||||||
@@ -87,6 +87,33 @@ public class VpnProfileControlActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
@@ -173,7 +200,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
mWaitingForResult = true;
|
mWaitingForResult = true;
|
||||||
startActivityForResult(intent, PREPARE_VPN_SERVICE);
|
mPrepareVpnService.launch(intent);
|
||||||
}
|
}
|
||||||
catch (ActivityNotFoundException ex)
|
catch (ActivityNotFoundException ex)
|
||||||
{
|
{
|
||||||
@@ -187,7 +214,23 @@ public class VpnProfileControlActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ /* user already granted permission to use VpnService */
|
{ /* 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,48 +262,6 @@ public class VpnProfileControlActivity extends AppCompatActivity
|
|||||||
return true;
|
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
|
* Check if we are currently connected to a VPN connection
|
||||||
*
|
*
|
||||||
@@ -597,7 +598,7 @@ public class VpnProfileControlActivity extends AppCompatActivity
|
|||||||
activity.mWaitingForResult = true;
|
activity.mWaitingForResult = true;
|
||||||
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
||||||
Uri.parse("package:" + activity.getPackageName()));
|
Uri.parse("package:" + activity.getPackageName()));
|
||||||
activity.startActivityForResult(intent, ADD_TO_POWER_WHITELIST);
|
activity.mAddToPowerWhitelist.launch(intent);
|
||||||
}).create();
|
}).create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+29
-32
@@ -74,6 +74,8 @@ import java.util.SortedSet;
|
|||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.app.AppCompatDialogFragment;
|
import androidx.appcompat.app.AppCompatDialogFragment;
|
||||||
@@ -82,9 +84,6 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|||||||
|
|
||||||
public class VpnProfileDetailActivity extends AppCompatActivity
|
public class VpnProfileDetailActivity extends AppCompatActivity
|
||||||
{
|
{
|
||||||
private static final int SELECT_TRUSTED_CERTIFICATE = 0;
|
|
||||||
private static final int SELECT_APPLICATIONS = 1;
|
|
||||||
|
|
||||||
private VpnProfileDataSource mDataSource;
|
private VpnProfileDataSource mDataSource;
|
||||||
private Long mId;
|
private Long mId;
|
||||||
private TrustedCertificateEntry mCertEntry;
|
private TrustedCertificateEntry mCertEntry;
|
||||||
@@ -144,6 +143,31 @@ public class VpnProfileDetailActivity extends AppCompatActivity
|
|||||||
private EditText mDnsServers;
|
private EditText mDnsServers;
|
||||||
private TextInputLayoutHelper mDnsServersWrap;
|
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
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
@@ -300,7 +324,7 @@ public class VpnProfileDetailActivity extends AppCompatActivity
|
|||||||
{
|
{
|
||||||
Intent intent = new Intent(VpnProfileDetailActivity.this, TrustedCertificatesActivity.class);
|
Intent intent = new Intent(VpnProfileDetailActivity.this, TrustedCertificatesActivity.class);
|
||||||
intent.setAction(TrustedCertificatesActivity.SELECT_CERTIFICATE);
|
intent.setAction(TrustedCertificatesActivity.SELECT_CERTIFICATE);
|
||||||
startActivityForResult(intent, SELECT_TRUSTED_CERTIFICATE);
|
mSelectTrustedCertificate.launch(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -334,7 +358,7 @@ public class VpnProfileDetailActivity extends AppCompatActivity
|
|||||||
{
|
{
|
||||||
Intent intent = new Intent(VpnProfileDetailActivity.this, SelectedApplicationsActivity.class);
|
Intent intent = new Intent(VpnProfileDetailActivity.this, SelectedApplicationsActivity.class);
|
||||||
intent.putExtra(VpnProfileDataSource.KEY_SELECTED_APPS_LIST, new ArrayList<>(mSelectedApps));
|
intent.putExtra(VpnProfileDataSource.KEY_SELECTED_APPS_LIST, new ArrayList<>(mSelectedApps));
|
||||||
startActivityForResult(intent, SELECT_APPLICATIONS);
|
mSelectApplications.launch(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -404,33 +428,6 @@ public class VpnProfileDetailActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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
|
* Update the UI to enter credentials depending on the type of VPN currently selected
|
||||||
*/
|
*/
|
||||||
|
|||||||
+27
-29
@@ -15,7 +15,6 @@
|
|||||||
|
|
||||||
package org.strongswan.android.ui;
|
package org.strongswan.android.ui;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -74,6 +73,8 @@ import java.util.UUID;
|
|||||||
|
|
||||||
import javax.net.ssl.SSLHandshakeException;
|
import javax.net.ssl.SSLHandshakeException;
|
||||||
|
|
||||||
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.loader.app.LoaderManager;
|
import androidx.loader.app.LoaderManager;
|
||||||
import androidx.loader.content.AsyncTaskLoader;
|
import androidx.loader.content.AsyncTaskLoader;
|
||||||
@@ -84,8 +85,6 @@ public class VpnProfileImportActivity extends AppCompatActivity
|
|||||||
{
|
{
|
||||||
private static final String PKCS12_INSTALLED = "PKCS12_INSTALLED";
|
private static final String PKCS12_INSTALLED = "PKCS12_INSTALLED";
|
||||||
private static final String PROFILE_URI = "PROFILE_URI";
|
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 PROFILE_LOADER = 0;
|
||||||
private static final int USER_CERT_LOADER = 1;
|
private static final int USER_CERT_LOADER = 1;
|
||||||
|
|
||||||
@@ -112,6 +111,29 @@ public class VpnProfileImportActivity extends AppCompatActivity
|
|||||||
private ViewGroup mRemoteCertificate;
|
private ViewGroup mRemoteCertificate;
|
||||||
private RelativeLayout mRemoteCert;
|
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>()
|
private LoaderManager.LoaderCallbacks<ProfileLoadResult> mProfileLoaderCallbacks = new LoaderManager.LoaderCallbacks<ProfileLoadResult>()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
@@ -200,7 +222,7 @@ public class VpnProfileImportActivity extends AppCompatActivity
|
|||||||
Intent intent = KeyChain.createInstallIntent();
|
Intent intent = KeyChain.createInstallIntent();
|
||||||
intent.putExtra(KeyChain.EXTRA_NAME, getString(R.string.profile_cert_alias, mProfile.getName()));
|
intent.putExtra(KeyChain.EXTRA_NAME, getString(R.string.profile_cert_alias, mProfile.getName()));
|
||||||
intent.putExtra(KeyChain.EXTRA_PKCS12, mProfile.PKCS12);
|
intent.putExtra(KeyChain.EXTRA_PKCS12, mProfile.PKCS12);
|
||||||
startActivityForResult(intent, INSTALL_PKCS12);
|
mImportPKCS12.launch(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -216,7 +238,7 @@ public class VpnProfileImportActivity extends AppCompatActivity
|
|||||||
openIntent.setType("*/*");
|
openIntent.setType("*/*");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
startActivityForResult(openIntent, OPEN_DOCUMENT);
|
mOpenDocument.launch(openIntent);
|
||||||
}
|
}
|
||||||
catch (ActivityNotFoundException e)
|
catch (ActivityNotFoundException e)
|
||||||
{ /* some devices are unable to browse for files */
|
{ /* some devices are unable to browse for files */
|
||||||
@@ -283,30 +305,6 @@ public class VpnProfileImportActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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)
|
private void loadProfile(Uri uri)
|
||||||
{
|
{
|
||||||
mProgressBar.show();
|
mProgressBar.show();
|
||||||
|
|||||||
+3
-5
@@ -55,8 +55,6 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|||||||
public class VpnProfileListFragment extends Fragment
|
public class VpnProfileListFragment extends Fragment
|
||||||
{
|
{
|
||||||
private static final String SELECTED_KEY = "SELECTED";
|
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 List<VpnProfile> mVpnProfiles;
|
||||||
private VpnProfileDataSource mDataSource;
|
private VpnProfileDataSource mDataSource;
|
||||||
@@ -212,7 +210,7 @@ public class VpnProfileListFragment extends Fragment
|
|||||||
case R.id.add_profile:
|
case R.id.add_profile:
|
||||||
Intent connectionIntent = new Intent(getActivity(),
|
Intent connectionIntent = new Intent(getActivity(),
|
||||||
VpnProfileDetailActivity.class);
|
VpnProfileDetailActivity.class);
|
||||||
startActivityForResult(connectionIntent, ADD_REQUEST);
|
startActivity(connectionIntent);
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
@@ -270,7 +268,7 @@ public class VpnProfileListFragment extends Fragment
|
|||||||
VpnProfile profile = (VpnProfile)mListView.getItemAtPosition(position);
|
VpnProfile profile = (VpnProfile)mListView.getItemAtPosition(position);
|
||||||
Intent connectionIntent = new Intent(getActivity(), VpnProfileDetailActivity.class);
|
Intent connectionIntent = new Intent(getActivity(), VpnProfileDetailActivity.class);
|
||||||
connectionIntent.putExtra(VpnProfileDataSource.KEY_ID, profile.getId());
|
connectionIntent.putExtra(VpnProfileDataSource.KEY_ID, profile.getId());
|
||||||
startActivityForResult(connectionIntent, EDIT_REQUEST);
|
startActivity(connectionIntent);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case R.id.copy_profile:
|
case R.id.copy_profile:
|
||||||
@@ -288,7 +286,7 @@ public class VpnProfileListFragment extends Fragment
|
|||||||
|
|
||||||
Intent connectionIntent = new Intent(getActivity(), VpnProfileDetailActivity.class);
|
Intent connectionIntent = new Intent(getActivity(), VpnProfileDetailActivity.class);
|
||||||
connectionIntent.putExtra(VpnProfileDataSource.KEY_ID, profile.getId());
|
connectionIntent.putExtra(VpnProfileDataSource.KEY_ID, profile.getId());
|
||||||
startActivityForResult(connectionIntent, EDIT_REQUEST);
|
startActivity(connectionIntent);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case R.id.delete_profile:
|
case R.id.delete_profile:
|
||||||
|
|||||||
Reference in New Issue
Block a user