Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 54a5c444 authored by Raff Tsai's avatar Raff Tsai
Browse files

Fix crash in getMetricsTag

SettingsIntelligence put a null in intent extra bundle, caused the
crash. Check the null before use it.

Fixes: 137351833
Test: make RunSettingsRoboTests
Change-Id: I9630760396c72bddf6a11314b869873c3b83b45a
parent a2be917d
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -205,10 +205,14 @@ public class SettingsActivity extends SettingsBaseActivity
    }

    private String getMetricsTag() {
        String tag = getClass().getName();
        String tag = null;
        if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
            tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
        }
        if (TextUtils.isEmpty(tag)) {
            Log.w(LOG_TAG, "MetricsTag is invalid " + tag);
            tag = getClass().getName();
        }
        if (tag.startsWith("com.android.settings.")) {
            tag = tag.replace("com.android.settings.", "");
        }
+14 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.settings;

import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.anyInt;
@@ -34,6 +36,7 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.android.settings.core.OnActivityResultListener;
import com.android.settings.testutils.FakeFeatureFactory;

import org.junit.Before;
import org.junit.Test;
@@ -82,6 +85,17 @@ public class SettingsActivityTest {
        verify(mTaskDescription).setIcon(anyInt());
    }

    @Test
    public void getSharedPreferences_intentExtraIsNull_shouldNotCrash() {
        final Intent intent = new Intent();
        intent.putExtra(EXTRA_SHOW_FRAGMENT, (String)null);
        doReturn(intent).when(mActivity).getIntent();
        doReturn(mContext.getPackageName()).when(mActivity).getPackageName();
        FakeFeatureFactory.setupForTest();

        mActivity.getSharedPreferences(mContext.getPackageName() + "_preferences", 0);
    }

    @Test
    public void onActivityResult_shouldDelegateToListener() {
        final List<Fragment> fragments = new ArrayList<>();