Created
September 24, 2019 14:14
-
-
Save Aksi0m/5c51632d43390c0018151b3cbc14bd8c to your computer and use it in GitHub Desktop.
This is the mainactivity/first activity of the application that's there long enough to make the async install ing of the SecurityProvider work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity implements ProviderInstaller.ProviderInstallListener { | |
private static final int ERROR_DIALOG_REQUEST_CODE = 1; | |
private boolean mRetryProviderInstall; | |
//Update the security provider when the activity is created. | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ProviderInstaller.installIfNeededAsync(this, this); | |
} | |
/** | |
* This method is only called if the provider is successfully updated | |
* (or is already up-to-date). | |
*/ | |
@Override | |
protected void onProviderInstalled() { | |
// Provider is up-to-date, app can make secure network calls. | |
} | |
/** | |
* This method is called if updating fails; the error code indicates | |
* whether the error is recoverable. | |
*/ | |
@Override | |
protected void onProviderInstallFailed(int errorCode, Intent recoveryIntent) { | |
if (GooglePlayServicesUtil.isUserRecoverableError(errorCode)) { | |
// Recoverable error. Show a dialog prompting the user to | |
// install/update/enable Google Play services. | |
GooglePlayServicesUtil.showErrorDialogFragment( | |
errorCode, | |
this, | |
ERROR_DIALOG_REQUEST_CODE, | |
new DialogInterface.OnCancelListener() { | |
@Override | |
public void onCancel(DialogInterface dialog) { | |
// The user chose not to take the recovery action | |
onProviderInstallerNotAvailable(); | |
} | |
}); | |
} else { | |
// Google Play services is not available. | |
onProviderInstallerNotAvailable(); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, | |
Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == ERROR_DIALOG_REQUEST_CODE) { | |
// Adding a fragment via GooglePlayServicesUtil.showErrorDialogFragment | |
// before the instance state is restored throws an error. So instead, | |
// set a flag here, which will cause the fragment to delay until | |
// onPostResume. | |
mRetryProviderInstall = true; | |
} | |
} | |
/** | |
* On resume, check to see if we flagged that we need to reinstall the | |
* provider. | |
*/ | |
@Override | |
protected void onPostResume() { | |
super.onPostResult(); | |
if (mRetryProviderInstall) { | |
// We can now safely retry installation. | |
ProviderInstall.installIfNeededAsync(this, this); | |
mRetryProviderInstall = false; | |
} | |
private void onProviderInstallerNotAvailable() { | |
// This is reached if the provider cannot be updated for some reason. | |
// App should consider all HTTP communication to be vulnerable, and take | |
// appropriate action (e.g. inform backend, block certain high-risk actions, etc.). | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment