android: Simplified handling of error dialog that is displayed if VpnService API is not supported

This commit is contained in:
Tobias Brunner
2012-08-27 10:48:13 +02:00
parent aa55040192
commit 8a9956762c
@@ -46,10 +46,8 @@ import android.widget.EditText;
public class MainActivity extends Activity implements OnVpnProfileSelectedListener
{
public static final String CONTACT_EMAIL = "[email protected]";
private static final String SHOW_ERROR_DIALOG = "errordialog";
private static final int PREPARE_VPN_SERVICE = 0;
private AlertDialog mErrorDialog;
private Bundle mProfileInfo;
@Override
@@ -62,32 +60,10 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
ActionBar bar = getActionBar();
bar.setDisplayShowTitleEnabled(false);
if (savedInstanceState != null && savedInstanceState.getBoolean(SHOW_ERROR_DIALOG))
{
showVpnNotSupportedError();
}
/* load CA certificates in a background task */
new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, false);
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putBoolean(SHOW_ERROR_DIALOG, mErrorDialog != null);
}
@Override
protected void onDestroy()
{
super.onDestroy();
if (mErrorDialog != null)
{ /* avoid any errors about leaked windows */
mErrorDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
@@ -134,7 +110,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
* don't have the VPN components built into the system image.
* com.android.vpndialogs/com.android.vpndialogs.ConfirmDialog
* will not be found then */
showVpnNotSupportedError();
new VpnNotSupportedError().show(getFragmentManager(), "ErrorDialog");
}
}
else
@@ -180,25 +156,6 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
}
}
/**
* Show an error dialog if case the device lacks VPN support.
*/
private void showVpnNotSupportedError()
{
mErrorDialog = new AlertDialog.Builder(this)
.setTitle(R.string.vpn_not_supported_title)
.setMessage(getString(R.string.vpn_not_supported))
.setCancelable(false)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
mErrorDialog = null;
dialog.dismiss();
}
}).show();
}
/**
* Class that loads or reloads the cached CA certificates.
*/
@@ -263,4 +220,27 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
return adb.create();
}
}
/**
* Class representing an error message which is displayed if VpnService is
* not supported on the current device.
*/
public static class VpnNotSupportedError extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.vpn_not_supported_title)
.setMessage(getString(R.string.vpn_not_supported))
.setCancelable(false)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
}
}).create();
}
}
}