Make local variables final where possible in CredentialStorage

This speeds up the run-time of credential settings.
Test: this is tested at compile time.

Change-Id: I77d577cace07826596eb40223a36a8d327b3d72b
This commit is contained in:
Irina Dumitrescu
2018-05-11 19:03:42 +01:00
parent d3ff522a0b
commit ea8406f75d

View File

@@ -127,9 +127,9 @@ public final class CredentialStorage extends Activity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Intent intent = getIntent(); final Intent intent = getIntent();
String action = intent.getAction(); final String action = intent.getAction();
UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); final UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
if (!userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS)) { if (!userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS)) {
if (ACTION_RESET.equals(action)) { if (ACTION_RESET.equals(action)) {
new ResetDialog(); new ResetDialog();
@@ -208,18 +208,18 @@ public final class CredentialStorage extends Activity {
* Returns true if the currently set key guard matches our minimum quality requirements. * Returns true if the currently set key guard matches our minimum quality requirements.
*/ */
private boolean checkKeyGuardQuality() { private boolean checkKeyGuardQuality() {
int credentialOwner = final int credentialOwner =
UserManager.get(this).getCredentialOwnerProfile(UserHandle.myUserId()); UserManager.get(this).getCredentialOwnerProfile(UserHandle.myUserId());
int quality = new LockPatternUtils(this).getActivePasswordQuality(credentialOwner); final int quality = new LockPatternUtils(this).getActivePasswordQuality(credentialOwner);
return (quality >= MIN_PASSWORD_QUALITY); return (quality >= MIN_PASSWORD_QUALITY);
} }
private boolean isHardwareBackedKey(byte[] keyData) { private boolean isHardwareBackedKey(byte[] keyData) {
try { try {
ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(keyData)); final ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(keyData));
PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject()); final PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject());
String algOid = pki.getAlgorithmId().getAlgorithm().getId(); final String algOid = pki.getAlgorithmId().getAlgorithm().getId();
String algName = new AlgorithmId(new ObjectIdentifier(algOid)).getName(); final String algName = new AlgorithmId(new ObjectIdentifier(algOid)).getName();
return KeyChain.isBoundKeyAlgorithm(algName); return KeyChain.isBoundKeyAlgorithm(algName);
} catch (IOException e) { } catch (IOException e) {
@@ -236,14 +236,13 @@ public final class CredentialStorage extends Activity {
return; return;
} }
Bundle bundle = mInstallBundle; final Bundle bundle = mInstallBundle;
mInstallBundle = null; mInstallBundle = null;
final int uid = bundle.getInt(Credentials.EXTRA_INSTALL_AS_UID, KeyStore.UID_SELF); final int uid = bundle.getInt(Credentials.EXTRA_INSTALL_AS_UID, KeyStore.UID_SELF);
if (uid != KeyStore.UID_SELF && !UserHandle.isSameUser(uid, Process.myUid())) { if (uid != KeyStore.UID_SELF && !UserHandle.isSameUser(uid, Process.myUid())) {
int dstUserId = UserHandle.getUserId(uid); final int dstUserId = UserHandle.getUserId(uid);
int myUserId = UserHandle.myUserId();
// Restrict install target to the wifi uid. // Restrict install target to the wifi uid.
if (uid != Process.WIFI_UID) { if (uid != Process.WIFI_UID) {
@@ -252,7 +251,7 @@ public final class CredentialStorage extends Activity {
return; return;
} }
Intent installIntent = new Intent(ACTION_INSTALL) final Intent installIntent = new Intent(ACTION_INSTALL)
.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT) .setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT)
.putExtras(bundle); .putExtras(bundle);
startActivityAsUser(installIntent, new UserHandle(dstUserId)); startActivityAsUser(installIntent, new UserHandle(dstUserId));
@@ -260,8 +259,8 @@ public final class CredentialStorage extends Activity {
} }
if (bundle.containsKey(Credentials.EXTRA_USER_PRIVATE_KEY_NAME)) { if (bundle.containsKey(Credentials.EXTRA_USER_PRIVATE_KEY_NAME)) {
String key = bundle.getString(Credentials.EXTRA_USER_PRIVATE_KEY_NAME); final String key = bundle.getString(Credentials.EXTRA_USER_PRIVATE_KEY_NAME);
byte[] value = bundle.getByteArray(Credentials.EXTRA_USER_PRIVATE_KEY_DATA); final byte[] value = bundle.getByteArray(Credentials.EXTRA_USER_PRIVATE_KEY_DATA);
int flags = KeyStore.FLAG_ENCRYPTED; int flags = KeyStore.FLAG_ENCRYPTED;
if (uid == Process.WIFI_UID && isHardwareBackedKey(value)) { if (uid == Process.WIFI_UID && isHardwareBackedKey(value)) {
@@ -287,11 +286,11 @@ public final class CredentialStorage extends Activity {
} }
} }
int flags = KeyStore.FLAG_NONE; final int flags = KeyStore.FLAG_NONE;
if (bundle.containsKey(Credentials.EXTRA_USER_CERTIFICATE_NAME)) { if (bundle.containsKey(Credentials.EXTRA_USER_CERTIFICATE_NAME)) {
String certName = bundle.getString(Credentials.EXTRA_USER_CERTIFICATE_NAME); final String certName = bundle.getString(Credentials.EXTRA_USER_CERTIFICATE_NAME);
byte[] certData = bundle.getByteArray(Credentials.EXTRA_USER_CERTIFICATE_DATA); final byte[] certData = bundle.getByteArray(Credentials.EXTRA_USER_CERTIFICATE_DATA);
if (!mKeyStore.put(certName, certData, uid, flags)) { if (!mKeyStore.put(certName, certData, uid, flags)) {
Log.e(TAG, "Failed to install " + certName + " as uid " + uid); Log.e(TAG, "Failed to install " + certName + " as uid " + uid);
@@ -300,8 +299,8 @@ public final class CredentialStorage extends Activity {
} }
if (bundle.containsKey(Credentials.EXTRA_CA_CERTIFICATES_NAME)) { if (bundle.containsKey(Credentials.EXTRA_CA_CERTIFICATES_NAME)) {
String caListName = bundle.getString(Credentials.EXTRA_CA_CERTIFICATES_NAME); final String caListName = bundle.getString(Credentials.EXTRA_CA_CERTIFICATES_NAME);
byte[] caListData = bundle.getByteArray(Credentials.EXTRA_CA_CERTIFICATES_DATA); final byte[] caListData = bundle.getByteArray(Credentials.EXTRA_CA_CERTIFICATES_DATA);
if (!mKeyStore.put(caListName, caListData, uid, flags)) { if (!mKeyStore.put(caListName, caListData, uid, flags)) {
Log.e(TAG, "Failed to install " + caListName + " as uid " + uid); Log.e(TAG, "Failed to install " + caListName + " as uid " + uid);
@@ -310,7 +309,7 @@ public final class CredentialStorage extends Activity {
} }
// Send the broadcast. // Send the broadcast.
Intent broadcast = new Intent(KeyChain.ACTION_KEYCHAIN_CHANGED); final Intent broadcast = new Intent(KeyChain.ACTION_KEYCHAIN_CHANGED);
sendBroadcast(broadcast); sendBroadcast(broadcast);
setResult(RESULT_OK); setResult(RESULT_OK);
@@ -324,7 +323,7 @@ public final class CredentialStorage extends Activity {
private boolean mResetConfirmed; private boolean mResetConfirmed;
private ResetDialog() { private ResetDialog() {
AlertDialog dialog = new AlertDialog.Builder(CredentialStorage.this) final AlertDialog dialog = new AlertDialog.Builder(CredentialStorage.this)
.setTitle(android.R.string.dialog_alert_title) .setTitle(android.R.string.dialog_alert_title)
.setMessage(R.string.credentials_reset_hint) .setMessage(R.string.credentials_reset_hint)
.setPositiveButton(android.R.string.ok, this) .setPositiveButton(android.R.string.ok, this)
@@ -364,7 +363,7 @@ public final class CredentialStorage extends Activity {
new LockPatternUtils(CredentialStorage.this).resetKeyStore(UserHandle.myUserId()); new LockPatternUtils(CredentialStorage.this).resetKeyStore(UserHandle.myUserId());
try { try {
KeyChainConnection keyChainConnection = KeyChain.bind(CredentialStorage.this); final KeyChainConnection keyChainConnection = KeyChain.bind(CredentialStorage.this);
try { try {
return keyChainConnection.getService().reset(); return keyChainConnection.getService().reset();
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -393,7 +392,7 @@ public final class CredentialStorage extends Activity {
} }
private void clearLegacyVpnIfEstablished() { private void clearLegacyVpnIfEstablished() {
boolean isDone = VpnUtils.disconnectLegacyVpn(getApplicationContext()); final boolean isDone = VpnUtils.disconnectLegacyVpn(getApplicationContext());
if (isDone) { if (isDone) {
Toast.makeText(CredentialStorage.this, R.string.vpn_disconnected, Toast.makeText(CredentialStorage.this, R.string.vpn_disconnected,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
@@ -407,7 +406,7 @@ public final class CredentialStorage extends Activity {
private class MarkKeyAsUserSelectable extends AsyncTask<Void, Void, Boolean> { private class MarkKeyAsUserSelectable extends AsyncTask<Void, Void, Boolean> {
final String mAlias; final String mAlias;
public MarkKeyAsUserSelectable(String alias) { MarkKeyAsUserSelectable(String alias) {
mAlias = alias; mAlias = alias;
} }
@@ -440,7 +439,7 @@ public final class CredentialStorage extends Activity {
final int launchedFromUserId; final int launchedFromUserId;
try { try {
int launchedFromUid = android.app.ActivityManager.getService() final int launchedFromUid = android.app.ActivityManager.getService()
.getLaunchedFromUid(getActivityToken()); .getLaunchedFromUid(getActivityToken());
if (launchedFromUid == -1) { if (launchedFromUid == -1) {
Log.e(TAG, ACTION_INSTALL + " must be started with startActivityForResult"); Log.e(TAG, ACTION_INSTALL + " must be started with startActivityForResult");
@@ -456,8 +455,8 @@ public final class CredentialStorage extends Activity {
return false; return false;
} }
UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); final UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
UserInfo parentInfo = userManager.getProfileParent(launchedFromUserId); final UserInfo parentInfo = userManager.getProfileParent(launchedFromUserId);
if (parentInfo == null || parentInfo.id != UserHandle.myUserId()) { if (parentInfo == null || parentInfo.id != UserHandle.myUserId()) {
// Caller is not running in a profile of this user // Caller is not running in a profile of this user
return false; return false;
@@ -469,7 +468,7 @@ public final class CredentialStorage extends Activity {
* Confirm existing key guard, returning password via onActivityResult. * Confirm existing key guard, returning password via onActivityResult.
*/ */
private boolean confirmKeyGuard(int requestCode) { private boolean confirmKeyGuard(int requestCode) {
Resources res = getResources(); final Resources res = getResources();
boolean launched = new ChooseLockSettingsHelper(this) boolean launched = new ChooseLockSettingsHelper(this)
.launchConfirmationActivity(requestCode, .launchConfirmationActivity(requestCode,
res.getText(R.string.credentials_title), true); res.getText(R.string.credentials_title), true);
@@ -485,7 +484,7 @@ public final class CredentialStorage extends Activity {
*/ */
if (requestCode == CONFIRM_KEY_GUARD_REQUEST) { if (requestCode == CONFIRM_KEY_GUARD_REQUEST) {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
String password = data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD); final String password = data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
if (!TextUtils.isEmpty(password)) { if (!TextUtils.isEmpty(password)) {
// success // success
mKeyStore.unlock(password); mKeyStore.unlock(password);
@@ -520,9 +519,10 @@ public final class CredentialStorage extends Activity {
private final TextView mError; private final TextView mError;
private UnlockDialog() { private UnlockDialog() {
View view = View.inflate(CredentialStorage.this, R.layout.credentials_dialog, null); final View view = View.inflate(
CredentialStorage.this, R.layout.credentials_dialog, null);
CharSequence text; final CharSequence text;
if (mRetriesRemaining == -1) { if (mRetriesRemaining == -1) {
text = getResources().getText(R.string.credentials_unlock_hint); text = getResources().getText(R.string.credentials_unlock_hint);
} else if (mRetriesRemaining > 3) { } else if (mRetriesRemaining > 3) {
@@ -539,7 +539,7 @@ public final class CredentialStorage extends Activity {
mOldPassword.addTextChangedListener(this); mOldPassword.addTextChangedListener(this);
mError = (TextView) view.findViewById(R.id.error); mError = (TextView) view.findViewById(R.id.error);
AlertDialog dialog = new AlertDialog.Builder(CredentialStorage.this) final AlertDialog dialog = new AlertDialog.Builder(CredentialStorage.this)
.setView(view) .setView(view)
.setTitle(R.string.credentials_unlock) .setTitle(R.string.credentials_unlock)
.setPositiveButton(android.R.string.ok, this) .setPositiveButton(android.R.string.ok, this)
@@ -575,7 +575,7 @@ public final class CredentialStorage extends Activity {
mUnlockConfirmed = false; mUnlockConfirmed = false;
mError.setVisibility(View.VISIBLE); mError.setVisibility(View.VISIBLE);
mKeyStore.unlock(mOldPassword.getText().toString()); mKeyStore.unlock(mOldPassword.getText().toString());
int error = mKeyStore.getLastError(); final int error = mKeyStore.getLastError();
if (error == KeyStore.NO_ERROR) { if (error == KeyStore.NO_ERROR) {
mRetriesRemaining = -1; mRetriesRemaining = -1;
Toast.makeText(CredentialStorage.this, Toast.makeText(CredentialStorage.this,