Fix crash in ChooseLockGenericFragment.

- in onActivityResult(), the intent data can be null. Check for non null
intent data before trying to read the extra string from the intent.

Change-Id: I14c42725a7885a84688ae39fde63e30ad0536001
Fixes: 109675331
Test: make RunSettingsRoboTests
This commit is contained in:
Doris Ling
2018-06-12 16:33:05 -07:00
parent cc7c1418cc
commit 06d264de19
2 changed files with 50 additions and 3 deletions

View File

@@ -39,6 +39,7 @@ import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.os.storage.StorageManager; import android.os.storage.StorageManager;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import android.text.TextUtils; import android.text.TextUtils;
@@ -124,7 +125,8 @@ public class ChooseLockGeneric extends SettingsActivity {
*/ */
public static final String EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS = "choose_lock_generic_extras"; public static final String EXTRA_CHOOSE_LOCK_GENERIC_EXTRAS = "choose_lock_generic_extras";
private static final int CONFIRM_EXISTING_REQUEST = 100; @VisibleForTesting
static final int CONFIRM_EXISTING_REQUEST = 100;
private static final int ENABLE_ENCRYPTION_REQUEST = 101; private static final int ENABLE_ENCRYPTION_REQUEST = 101;
private static final int CHOOSE_LOCK_REQUEST = 102; private static final int CHOOSE_LOCK_REQUEST = 102;
private static final int CHOOSE_LOCK_BEFORE_FINGERPRINT_REQUEST = 103; private static final int CHOOSE_LOCK_BEFORE_FINGERPRINT_REQUEST = 103;
@@ -329,7 +331,9 @@ public class ChooseLockGeneric extends SettingsActivity {
mWaitingForConfirmation = false; mWaitingForConfirmation = false;
if (requestCode == CONFIRM_EXISTING_REQUEST && resultCode == Activity.RESULT_OK) { if (requestCode == CONFIRM_EXISTING_REQUEST && resultCode == Activity.RESULT_OK) {
mPasswordConfirmed = true; mPasswordConfirmed = true;
mUserPassword = data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD); mUserPassword = data != null
? data.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD)
: null;
updatePreferencesOrFinish(false /* isRecreatingActivity */); updatePreferencesOrFinish(false /* isRecreatingActivity */);
if (mForChangeCredRequiredForBoot) { if (mForChangeCredRequiredForBoot) {
if (!TextUtils.isEmpty(mUserPassword)) { if (!TextUtils.isEmpty(mUserPassword)) {
@@ -394,7 +398,8 @@ public class ChooseLockGeneric extends SettingsActivity {
} }
} }
private void updatePreferencesOrFinish(boolean isRecreatingActivity) { @VisibleForTesting
void updatePreferencesOrFinish(boolean isRecreatingActivity) {
Intent intent = getActivity().getIntent(); Intent intent = getActivity().getIntent();
int quality = intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY, -1); int quality = intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY, -1);
if (quality == -1) { if (quality == -1) {

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2018 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 com.android.settings.password;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.spy;
import android.app.Activity;
import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(SettingsRobolectricTestRunner.class)
public class ChooseLockGenericTest {
@Test
public void onActivityResult_nullIntentData_shouldNotCrash() {
ChooseLockGenericFragment fragment = spy(new ChooseLockGenericFragment());
doNothing().when(fragment).updatePreferencesOrFinish(anyBoolean());
fragment.onActivityResult(
fragment.CONFIRM_EXISTING_REQUEST, Activity.RESULT_OK, null /* data */);
// no crash
}
}