Merge "Migrate the Legal Info page to Catalyst" into main

This commit is contained in:
Treehugger Robot
2024-09-25 00:39:10 +00:00
committed by Android (Google) Code Review
20 changed files with 461 additions and 1 deletions

View File

@@ -17,8 +17,13 @@
package com.android.settings;
import android.app.settings.SettingsEnums;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.legal.LegalSettingsScreen;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
@@ -44,4 +49,10 @@ public class LegalSettings extends DashboardFragment {
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.about_legal);
@Nullable
@Override
public String getPreferenceScreenBindingKey(@NonNull Context context) {
return LegalSettingsScreen.KEY;
}
}

View File

@@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal;
import android.content.Context;
import android.content.Intent;
// LINT.IfChange
public class CopyrightPreferenceController extends LegalPreferenceController {
private static final Intent INTENT = new Intent("android.settings.COPYRIGHT");
@@ -30,3 +31,4 @@ public class CopyrightPreferenceController extends LegalPreferenceController {
return INTENT;
}
}
// LINT.ThenChange(LegalPreference.kt)

View File

@@ -0,0 +1,58 @@
/*
* 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.
*/
package com.android.settings.deviceinfo.legal
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.ResolveInfo
import androidx.annotation.StringRes
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.metadata.PreferenceTitleProvider
// LINT.IfChange
class LegalPreference(
override val key: String,
@StringRes val defaultTitle: Int = 0,
val intentAction: String,
) : PreferenceMetadata, PreferenceTitleProvider, PreferenceAvailabilityProvider {
override fun getTitle(context: Context): CharSequence? {
val resolveInfo =
findMatchingSpecificActivity(context) ?: return context.getText(defaultTitle)
return resolveInfo.loadLabel(context.packageManager)
}
override fun isAvailable(context: Context) = (findMatchingSpecificActivity(context) != null)
override fun intent(context: Context) =
findMatchingSpecificActivity(context)?.let {
Intent()
.setClassName(it.activityInfo.packageName, it.activityInfo.name)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
private fun findMatchingSpecificActivity(context: Context): ResolveInfo? {
val intent = Intent(intentAction)
// Find the activity that is in the system image
val list: List<ResolveInfo> = context.packageManager.queryIntentActivities(intent, 0)
return list.firstOrNull {
(it.activityInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0
}
}
}
// LINT.ThenChange(LegalPreferenceController.java)

View File

@@ -27,6 +27,7 @@ import com.android.settings.core.BasePreferenceController;
import java.util.List;
// LINT.IfChange
public abstract class LegalPreferenceController extends BasePreferenceController {
private final PackageManager mPackageManager;
private Preference mPreference;
@@ -94,3 +95,4 @@ public abstract class LegalPreferenceController extends BasePreferenceController
mPreference.setTitle(resolveInfo.loadLabel(mPackageManager));
}
}
// LINT.ThenChange(LegalPreference.kt)

View File

@@ -0,0 +1,55 @@
/*
* 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.
*/
package com.android.settings.deviceinfo.legal
import android.content.Context
import com.android.settings.LegalSettings
import com.android.settings.R
import com.android.settings.flags.Flags
import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.preferenceHierarchy
import com.android.settingslib.preference.PreferenceScreenCreator
@ProvidePreferenceScreen
open class LegalSettingsScreen : PreferenceScreenCreator {
override val key: String
get() = KEY
override val title: Int
get() = R.string.legal_information
override fun isFlagEnabled(context: Context) = Flags.catalystLegalInformation()
override fun fragmentClass() = LegalSettings::class.java
override fun getPreferenceHierarchy(context: Context) =
preferenceHierarchy(this) {
+LegalPreference("copyright", R.string.copyright_title, "android.settings.COPYRIGHT")
+LegalPreference("license", R.string.license_title, "android.settings.LICENSE")
+LegalPreference("terms", R.string.terms_title, "android.settings.TERMS")
+ModuleLicensesScreen.KEY // Use screen key in case it is overlaid.
+LegalPreference(
"webview_license",
R.string.webview_license_title,
"android.settings.WEBVIEW_LICENSE",
)
+WallpaperAttributionsPreference()
}
companion object {
const val KEY = "legal_information"
}
}

View File

@@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal;
import android.content.Context;
import android.content.Intent;
// LINT.IfChange
public class LicensePreferenceController extends LegalPreferenceController {
private static final Intent INTENT = new Intent("android.settings.LICENSE");
@@ -30,3 +31,4 @@ public class LicensePreferenceController extends LegalPreferenceController {
return INTENT;
}
}
// LINT.ThenChange(LegalPreference.kt)

View File

@@ -24,6 +24,7 @@ import com.android.settings.core.BasePreferenceController;
import java.util.List;
// LINT.IfChange
public class ModuleLicensesListPreferenceController extends BasePreferenceController {
public ModuleLicensesListPreferenceController(Context context,
String preferenceKey) {
@@ -39,3 +40,4 @@ public class ModuleLicensesListPreferenceController extends BasePreferenceContro
: CONDITIONALLY_UNAVAILABLE;
}
}
// LINT.ThenChange(ModuleLicensesScreen.kt)

View File

@@ -0,0 +1,61 @@
/*
* 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.
*/
package com.android.settings.deviceinfo.legal
import android.content.Context
import com.android.settings.R
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.ProvidePreferenceScreen
import com.android.settingslib.metadata.preferenceHierarchy
import com.android.settingslib.preference.PreferenceScreenCreator
// LINT.IfChange
@ProvidePreferenceScreen
class ModuleLicensesScreen : PreferenceScreenCreator, PreferenceAvailabilityProvider {
override val key: String
get() = KEY
override val title: Int
get() = R.string.module_license_title
// We need to avoid directly assign fragment attribute in the bind() API. So we need to create
// a screen and provide it to its parent screen LegalSettingsScreen.
// By the way, we also need to set the isFlagEnabled() as false. Let system render the legacy
// UI. The hierarchy will be added while migrating this page.
override fun isFlagEnabled(context: Context) = false
override fun fragmentClass() = ModuleLicensesDashboard::class.java
override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) {}
override fun isAvailable(context: Context): Boolean {
val modules = context.packageManager.getInstalledModules(/* flags= */ 0)
return modules.any {
try {
ModuleLicenseProvider.getPackageAssetManager(context.packageManager, it.packageName)
.list("")
?.contains(ModuleLicenseProvider.GZIPPED_LICENSE_FILE_NAME) == true
} catch (e: Exception) {
false
}
}
}
companion object {
const val KEY = "module_license"
}
}
// LINT.ThenChange(ModuleLicensesListPreferenceController.java)

View File

@@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal;
import android.content.Context;
import android.content.Intent;
// LINT.IfChange
public class TermsPreferenceController extends LegalPreferenceController {
private static final Intent INTENT = new Intent("android.settings.TERMS");
@@ -30,3 +31,4 @@ public class TermsPreferenceController extends LegalPreferenceController {
return INTENT;
}
}
// LINT.ThenChange(LegalPreference.kt)

View File

@@ -0,0 +1,49 @@
/*
* 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.
*/
package com.android.settings.deviceinfo.legal
import android.content.Context
import androidx.preference.Preference
import com.android.settings.R
import com.android.settingslib.metadata.PreferenceAvailabilityProvider
import com.android.settingslib.metadata.PreferenceMetadata
import com.android.settingslib.preference.PreferenceBinding
// LINT.IfChange
class WallpaperAttributionsPreference :
PreferenceMetadata, PreferenceBinding, PreferenceAvailabilityProvider {
override val key: String
get() = KEY
override val title: Int
get() = R.string.wallpaper_attributions
override val summary: Int
get() = R.string.wallpaper_attributions_values
override fun bind(preference: Preference, metadata: PreferenceMetadata) {
super.bind(preference, metadata)
preference.isSelectable = false
}
override fun isAvailable(context: Context) =
context.resources.getBoolean(R.bool.config_show_wallpaper_attribution)
companion object {
const val KEY = "wallpaper_attributions"
}
}
// LINT.ThenChange(WallpaperAttributionsPreferenceController.java)

View File

@@ -20,6 +20,7 @@ import android.content.Context;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
// LINT.IfChange
public class WallpaperAttributionsPreferenceController extends BasePreferenceController {
public WallpaperAttributionsPreferenceController(Context context, String key) {
@@ -33,3 +34,4 @@ public class WallpaperAttributionsPreferenceController extends BasePreferenceCon
: UNSUPPORTED_ON_DEVICE;
}
}
// LINT.ThenChange(WallpaperAttributionsPreference.kt)

View File

@@ -17,6 +17,7 @@ package com.android.settings.deviceinfo.legal;
import android.content.Context;
import android.content.Intent;
// LINT.IfChange
public class WebViewLicensePreferenceController extends LegalPreferenceController {
private static final Intent INTENT = new Intent("android.settings.WEBVIEW_LICENSE");
@@ -30,3 +31,4 @@ public class WebViewLicensePreferenceController extends LegalPreferenceControlle
return INTENT;
}
}
// LINT.ThenChange(LegalPreference.kt)

View File

@@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
// LINT.IfChange
@RunWith(RobolectricTestRunner.class)
public class CopyrightPreferenceControllerTest {
@@ -119,3 +120,4 @@ public class CopyrightPreferenceControllerTest {
return testResolveInfo;
}
}
// LINT.ThenChange(LegalPreferenceTest.kt)

View File

@@ -0,0 +1,92 @@
/*
* 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.
*/
package com.android.settings.deviceinfo.legal
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import androidx.test.core.app.ApplicationProvider
import com.android.settings.R
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.stub
import org.robolectric.RobolectricTestRunner
// LINT.IfChange
@RunWith(RobolectricTestRunner::class)
class LegalPreferenceTest {
private val pkgManager = mock<PackageManager>()
private val context: Context =
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
override fun getPackageManager(): PackageManager = pkgManager
}
private val copyrightPreference =
LegalPreference("copyright", R.string.copyright_title, "android.settings.COPYRIGHT")
@Test
fun isAvailable_systemApp_shouldReturnTrue() {
val testResolveInfos: MutableList<ResolveInfo> = ArrayList()
testResolveInfos.add(getTestResolveInfo(/* isSystemApp= */ true))
pkgManager.stub {
on { queryIntentActivities(any(Intent::class.java), anyInt()) } doReturn
testResolveInfos
}
assertThat(copyrightPreference.isAvailable(context)).isTrue()
}
@Test
fun isAvailable_nonSystemApp_shouldReturnFalse() {
val testResolveInfos: MutableList<ResolveInfo> = ArrayList()
testResolveInfos.add(getTestResolveInfo(/* isSystemApp= */ false))
pkgManager.stub {
on { queryIntentActivities(any(Intent::class.java), anyInt()) } doReturn
testResolveInfos
}
assertThat(copyrightPreference.isAvailable(context)).isFalse()
}
private fun getTestResolveInfo(isSystemApp: Boolean): ResolveInfo {
val testResolveInfo = ResolveInfo()
val testAppInfo = ApplicationInfo()
if (isSystemApp) {
testAppInfo.flags = testAppInfo.flags or ApplicationInfo.FLAG_SYSTEM
}
testResolveInfo.activityInfo =
ActivityInfo().apply {
name = "TestActivityName"
packageName = "TestPackageName"
applicationInfo = testAppInfo
}
return testResolveInfo
}
}
// LINT.ThenChange(CopyrightPreferenceControllerTest.java)

View File

@@ -0,0 +1,53 @@
/*
* 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.
*/
package com.android.settings.deviceinfo.legal
import android.content.Context
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import android.text.TextUtils
import androidx.test.core.app.ApplicationProvider
import com.android.settings.flags.Flags
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class LegalSettingsScreenTest {
@get:Rule val setFlagsRule = SetFlagsRule()
private val context: Context = ApplicationProvider.getApplicationContext()
private val legalSettingsScreen = LegalSettingsScreen()
@Test
fun screenKey_exist() {
assertThat(TextUtils.equals(legalSettingsScreen.key, LegalSettingsScreen.KEY)).isTrue()
}
@Test
@EnableFlags(Flags.FLAG_CATALYST_LEGAL_INFORMATION)
fun isFlagEnabled_returnTrue() {
assertThat(legalSettingsScreen.isFlagEnabled(context)).isTrue()
}
@Test
@DisableFlags(Flags.FLAG_CATALYST_LEGAL_INFORMATION)
fun isFlagDisabled_returnTrue() {
assertThat(legalSettingsScreen.isFlagEnabled(context)).isFalse()
}
}

View File

@@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
// LINT.IfChange
@RunWith(RobolectricTestRunner.class)
public class LicensePreferenceControllerTest {
@@ -119,3 +120,4 @@ public class LicensePreferenceControllerTest {
return testResolveInfo;
}
}
// LINT.ThenChange(LegalPreferenceTest.kt)

View File

@@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
// LINT.IfChange
@RunWith(RobolectricTestRunner.class)
public class TermsPreferenceControllerTest {
@@ -119,3 +120,4 @@ public class TermsPreferenceControllerTest {
return testResolveInfo;
}
}
// LINT.ThenChange(LegalPreferenceTest.kt)

View File

@@ -29,6 +29,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
// LINT.IfChange
@RunWith(RobolectricTestRunner.class)
public class WallpaperAttributionsPreferenceControllerTest {
@@ -54,4 +55,5 @@ public class WallpaperAttributionsPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
}
}
}
// LINT.ThenChange(WallpaperAttributionsPreferenceTest.kt)

View File

@@ -0,0 +1,57 @@
/*
* 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.
*/
package com.android.settings.deviceinfo.legal
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Resources
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.stub
import org.robolectric.RobolectricTestRunner
// LINT.IfChange
@RunWith(RobolectricTestRunner::class)
class WallpaperAttributionsPreferenceTest {
private val mockResources = mock<Resources>()
private val context: Context =
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
override fun getResources(): Resources = mockResources
}
private val wallpaperAttributionsPreference = WallpaperAttributionsPreference()
@Test
fun isAvailable_configTrue_shouldReturnTrue() {
mockResources.stub { on { getBoolean(anyInt()) } doReturn true }
assertThat(wallpaperAttributionsPreference.isAvailable(context)).isTrue()
}
@Test
fun isAvailable_configFalse_shouldReturnFalse() {
mockResources.stub { on { getBoolean(anyInt()) } doReturn false }
assertThat(wallpaperAttributionsPreference.isAvailable(context)).isFalse()
}
}
// LINT.ThenChange(WallpaperAttributionsPreferenceControllerTest.java)

View File

@@ -45,6 +45,7 @@ import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
// LINT.IfChange
@RunWith(RobolectricTestRunner.class)
public class WebViewLicensePreferenceControllerTest {
@@ -119,3 +120,4 @@ public class WebViewLicensePreferenceControllerTest {
return testResolveInfo;
}
}
// LINT.ThenChange(LegalPreferenceTest.kt)