DeviceCredentialHandlerActivity.java
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.biometric;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.appcompat.app.AppCompatActivity;
/**
* Transparent activity that is responsible for re-launching the {@link BiometricPrompt} and
* handling results from {@link android.app.KeyguardManager#createConfirmDeviceCredentialIntent(
* CharSequence, CharSequence)} in order to allow device credential authentication prior to Q.
*
* @hide
*/
@RestrictTo(RestrictTo.Scope.LIBRARY)
@SuppressLint("SyntheticAccessor")
public class DeviceCredentialHandlerActivity extends AppCompatActivity {
private static final String TAG = "DeviceCredentialHandler";
static final String EXTRA_PROMPT_INFO_BUNDLE = "prompt_info_bundle";
@Nullable
private DeviceCredentialHandlerBridge mBridge;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// Apply the client activity's theme to ensure proper dialog styling.
DeviceCredentialHandlerBridge bridge =
DeviceCredentialHandlerBridge.getInstanceIfNotNull();
if (bridge != null && bridge.getClientThemeResId() != 0) {
setTheme(bridge.getClientThemeResId());
getTheme().applyStyle(R.style.TransparentStyle, true /* force */);
}
// Must be called after setting the theme.
super.onCreate(savedInstanceState);
setTitle(null);
setContentView(R.layout.device_credential_handler_activity);
mBridge = DeviceCredentialHandlerBridge.getInstance();
if (mBridge.getExecutor() == null || mBridge.getAuthenticationCallback() == null) {
Log.e(TAG, "onCreate: Executor and/or callback was null!");
} else {
// (Re)connect to and launch a biometric prompt within this activity.
final BiometricPrompt biometricPrompt = new BiometricPrompt(this,
mBridge.getExecutor(), mBridge.getAuthenticationCallback());
final Bundle infoBundle = getIntent().getBundleExtra(EXTRA_PROMPT_INFO_BUNDLE);
final BiometricPrompt.PromptInfo info = new BiometricPrompt.PromptInfo(infoBundle);
biometricPrompt.authenticate(info);
}
}
@Override
protected void onPause() {
super.onPause();
// Prevent the client from resetting the bridge in onPause if just changing configuration.
if (isChangingConfigurations() && mBridge != null) {
mBridge.ignoreNextReset();
}
}
// Handles the result of startActivity invoked by the attached BiometricPrompt.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Handle result from ConfirmDeviceCredentialActivity.
if (mBridge == null || mBridge.getAuthenticationCallback() == null) {
Log.e(TAG, "onActivityResult: Bridge or callback was null!");
} else if (resultCode == RESULT_OK) {
mBridge.setDeviceCredentialResult(DeviceCredentialHandlerBridge.RESULT_SUCCESS);
} else {
// Treat any non-OK result as a user cancellation.
mBridge.setDeviceCredentialResult(DeviceCredentialHandlerBridge.RESULT_ERROR);
}
finish();
}
}