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

Commit 64bd1286 authored by William Leshner's avatar William Leshner Committed by Android (Google) Code Review
Browse files

Merge "Guard against a nonexistent dream settings activity." into main

parents 353ed172 f771c1ef
Loading
Loading
Loading
Loading
+20 −7
Original line number Original line Diff line number Diff line
@@ -1405,7 +1405,8 @@ public class DreamService extends Service implements Window.Callback {
                        convertToComponentName(
                        convertToComponentName(
                                rawMetadata.getString(
                                rawMetadata.getString(
                                        com.android.internal.R.styleable.Dream_settingsActivity),
                                        com.android.internal.R.styleable.Dream_settingsActivity),
                                serviceInfo),
                                serviceInfo,
                                packageManager),
                        rawMetadata.getDrawable(
                        rawMetadata.getDrawable(
                                com.android.internal.R.styleable.Dream_previewImage),
                                com.android.internal.R.styleable.Dream_previewImage),
                        rawMetadata.getBoolean(R.styleable.Dream_showClockAndComplications,
                        rawMetadata.getBoolean(R.styleable.Dream_showClockAndComplications,
@@ -1420,26 +1421,38 @@ public class DreamService extends Service implements Window.Callback {
    }
    }


    @Nullable
    @Nullable
    private static ComponentName convertToComponentName(@Nullable String flattenedString,
    private static ComponentName convertToComponentName(
            ServiceInfo serviceInfo) {
            @Nullable String flattenedString,
            ServiceInfo serviceInfo,
            PackageManager packageManager) {
        if (flattenedString == null) {
        if (flattenedString == null) {
            return null;
            return null;
        }
        }


        if (!flattenedString.contains("/")) {
        final ComponentName cn =
            return new ComponentName(serviceInfo.packageName, flattenedString);
                flattenedString.contains("/")
                        ? ComponentName.unflattenFromString(flattenedString)
                        : new ComponentName(serviceInfo.packageName, flattenedString);

        if (cn == null) {
            return null;
        }
        }


        // Ensure that the component is from the same package as the dream service. If not,
        // Ensure that the component is from the same package as the dream service. If not,
        // treat the component as invalid and return null instead.
        // treat the component as invalid and return null instead.
        final ComponentName cn = ComponentName.unflattenFromString(flattenedString);
        if (cn == null) return null;
        if (!cn.getPackageName().equals(serviceInfo.packageName)) {
        if (!cn.getPackageName().equals(serviceInfo.packageName)) {
            Log.w(TAG,
            Log.w(TAG,
                    "Inconsistent package name in component: " + cn.getPackageName()
                    "Inconsistent package name in component: " + cn.getPackageName()
                            + ", should be: " + serviceInfo.packageName);
                            + ", should be: " + serviceInfo.packageName);
            return null;
            return null;
        }
        }

        // Ensure that the activity exists. If not, treat the component as invalid and return null.
        if (new Intent().setComponent(cn).resolveActivityInfo(packageManager, 0) == null) {
            Log.w(TAG, "Dream settings activity not found: " + cn);
            return null;
        }

        return cn;
        return cn;
    }
    }


+29 −0
Original line number Original line Diff line number Diff line
@@ -53,6 +53,35 @@
                android:name="android.service.dream"
                android:name="android.service.dream"
                android:resource="@xml/test_dream_metadata_invalid" />
                android:resource="@xml/test_dream_metadata_invalid" />
        </service>
        </service>

        <service
            android:name="com.android.server.dreams.TestDreamServiceWithNonexistentSettings"
            android:exported="false"
            android:label="Test Dream" >
            <intent-filter>
                <action android:name="android.service.dreams.DreamService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data
                android:name="android.service.dream"
                android:resource="@xml/test_dream_metadata_nonexistent_settings" />
        </service>

        <service
            android:name="com.android.server.dreams.TestDreamServiceNoPackageNonexistentSettings"
            android:exported="false"
            android:label="Test Dream" >
            <intent-filter>
                <action android:name="android.service.dreams.DreamService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data
                android:name="android.service.dream"
                android:resource="@xml/test_dream_metadata_nopackage_nonexistent_settings" />
        </service>

        <activity android:name=".TestDreamSettingsActivity">
        </activity>
    </application>
    </application>


    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+20 −0
Original line number Original line Diff line number Diff line
<!--
  ~ Copyright (C) 2024 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.
  -->

<!-- The settings activity does not exist, which is invalid. -->
<dream xmlns:android="http://schemas.android.com/apk/res/android"
    android:settingsActivity="com.android.frameworks.dreamservicetests/.TestDreamSettingsActivityNonexistent"
    android:showClockAndComplications="false"/>
+20 −0
Original line number Original line Diff line number Diff line
<!--
  ~ Copyright (C) 2024 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.
  -->

<!-- The settings activity's ComponentName is invalid. -->
<dream xmlns:android="http://schemas.android.com/apk/res/android"
    android:settingsActivity="com.android.frameworks.dreamservicetests.TestDreamSettingsActivityNonexistentNoPackage"
    android:showClockAndComplications="false"/>
+22 −0
Original line number Original line Diff line number Diff line
@@ -90,6 +90,28 @@ public class DreamServiceTest {
        assertThat(metadata.dreamCategory).isEqualTo(DreamService.DREAM_CATEGORY_DEFAULT);
        assertThat(metadata.dreamCategory).isEqualTo(DreamService.DREAM_CATEGORY_DEFAULT);
    }
    }


    @Test
    public void testMetadataParsing_nonexistentSettingsActivity()
            throws PackageManager.NameNotFoundException {
        final String testDreamClassName =
                "com.android.server.dreams.TestDreamServiceWithNonexistentSettings";
        final DreamService.DreamMetadata metadata = getDreamMetadata(testDreamClassName);

        assertThat(metadata.settingsActivity).isNull();
        assertThat(metadata.dreamCategory).isEqualTo(DreamService.DREAM_CATEGORY_DEFAULT);
    }

    @Test
    public void testMetadataParsing_noPackage_nonexistentSettingsActivity()
            throws PackageManager.NameNotFoundException {
        final String testDreamClassName =
                "com.android.server.dreams.TestDreamServiceNoPackageNonexistentSettings";
        final DreamService.DreamMetadata metadata = getDreamMetadata(testDreamClassName);

        assertThat(metadata.settingsActivity).isNull();
        assertThat(metadata.dreamCategory).isEqualTo(DreamService.DREAM_CATEGORY_DEFAULT);
    }

    @Test
    @Test
    public void testMetadataParsing_exceptionReading() {
    public void testMetadataParsing_exceptionReading() {
        final PackageManager packageManager = Mockito.mock(PackageManager.class);
        final PackageManager packageManager = Mockito.mock(PackageManager.class);
Loading