|
| 1 | +package com.android.settings.applications |
| 2 | + |
| 3 | +import android.app.compat.gms.GmsUtils |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.content.pm.ApplicationInfo |
| 7 | +import android.content.pm.GosPackageState |
| 8 | +import android.content.pm.GosPackageStateFlag |
| 9 | +import android.content.pm.PackageManager.NameNotFoundException |
| 10 | +import android.ext.PackageId |
| 11 | +import android.ext.settings.app.AswBlockPlayIntegrityApi |
| 12 | +import android.net.Uri |
| 13 | +import android.os.Bundle |
| 14 | +import androidx.appcompat.app.AlertDialog |
| 15 | +import androidx.compose.runtime.Composable |
| 16 | +import androidx.compose.ui.platform.LocalContext |
| 17 | +import androidx.preference.Preference |
| 18 | +import androidx.preference.PreferenceScreen |
| 19 | +import androidx.preference.SwitchPreferenceCompat |
| 20 | +import com.android.settings.R |
| 21 | +import com.android.settings.spa.app.appinfo.AswPreference |
| 22 | +import com.android.settingslib.spaprivileged.model.app.userId |
| 23 | +import com.android.settingslib.widget.TopIntroPreference |
| 24 | + |
| 25 | +object AswAdapterManagePlayIntegrityApi : AswAdapter<AswBlockPlayIntegrityApi>() { |
| 26 | + override fun getAppSwitch() = AswBlockPlayIntegrityApi.I |
| 27 | + |
| 28 | + override fun getAswTitle(ctx: Context) = ctx.getText(R.string.app_play_integrity_api) |
| 29 | + override fun getOnTitle(ctx: Context) = ctx.getText(R.string.app_play_integrity_api_blocked) |
| 30 | + override fun getOffTitle(ctx: Context) = ctx.getText(R.string.app_play_integrity_api_not_blocked) |
| 31 | + |
| 32 | + override fun getDetailFragmentClass() = AppManagePlayIntegrityApiFragment::class |
| 33 | + |
| 34 | + override fun shouldIncludeInAppListPage(app: ApplicationInfo, gosPs: GosPackageState): Boolean { |
| 35 | + return gosPs.hasFlag(GosPackageStateFlag.PLAY_INTEGRITY_API_USED_AT_LEAST_ONCE) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +@Composable |
| 40 | +fun AppManagePlayIntegrityApiPreference(app: ApplicationInfo) { |
| 41 | + if (GosPackageState.get(app.packageName, app.userId) |
| 42 | + .hasFlag(GosPackageStateFlag.PLAY_INTEGRITY_API_USED_AT_LEAST_ONCE)) { |
| 43 | + AswPreference(LocalContext.current, app, AswAdapterManagePlayIntegrityApi) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class AppManagePlayIntegrityApiFragment : AppInfoWithHeader() { |
| 48 | + private lateinit var showUsageNotifsSwitch: SwitchPreferenceCompat |
| 49 | + private lateinit var blockUsageAttemptsSwitch: SwitchPreferenceCompat |
| 50 | + |
| 51 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 52 | + super.onCreate(savedInstanceState) |
| 53 | + |
| 54 | + requireActivity().setTitle(R.string.app_play_integrity_api) |
| 55 | + |
| 56 | + val prefCtx = prefContext |
| 57 | + val screen: PreferenceScreen = preferenceManager.createPreferenceScreen(prefCtx) |
| 58 | + |
| 59 | + TopIntroPreference(prefCtx).apply { |
| 60 | + setTitle(R.string.app_play_integrity_top_intro) |
| 61 | + screen.addPreference(this) |
| 62 | + } |
| 63 | + Preference(prefCtx).apply { |
| 64 | + setTitle(R.string.app_play_integrity_contact_app_developer) |
| 65 | + setOnPreferenceClickListener { |
| 66 | + val pkgName = mPackageName |
| 67 | + val intent: Intent |
| 68 | + if (isPlayStoreAvailable()) { |
| 69 | + intent = GmsUtils.createAppPlayStoreIntent(pkgName) |
| 70 | + } else { |
| 71 | + val uri = Uri.parse("https://play.google.com/store/apps/details?id=$pkgName") |
| 72 | + intent = Intent.createChooser(Intent(Intent.ACTION_VIEW, uri), null) |
| 73 | + } |
| 74 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
| 75 | + startActivity(intent) |
| 76 | + true |
| 77 | + } |
| 78 | + screen.addPreference(this) |
| 79 | + } |
| 80 | + showUsageNotifsSwitch = SwitchPreferenceCompat(prefCtx).apply { |
| 81 | + setTitle(R.string.app_play_integrity_show_usage_notifications) |
| 82 | + setOnPreferenceChangeListener { _, value -> |
| 83 | + val ed = GosPackageState.edit(mPackageName, mUserId) |
| 84 | + AswBlockPlayIntegrityApi.I.setNotificationEnabled(ed, value as Boolean) |
| 85 | + ed.apply() |
| 86 | + } |
| 87 | + screen.addPreference(this) |
| 88 | + } |
| 89 | + blockUsageAttemptsSwitch = SwitchPreferenceCompat(prefCtx).apply { |
| 90 | + setTitle(R.string.app_play_integrity_block_usage_attempts) |
| 91 | + setSummaryOn(R.string.app_play_integrity_block_usage_attempts_summary_on) |
| 92 | + setOnPreferenceChangeListener { _, value -> |
| 93 | + val ed = GosPackageState.edit(mPackageName, mUserId) |
| 94 | + AswBlockPlayIntegrityApi.I.set(ed, value as Boolean) |
| 95 | + ed.apply() |
| 96 | + } |
| 97 | + screen.addPreference(this) |
| 98 | + } |
| 99 | + AswAdapterManagePlayIntegrityApi.addAppListPageLink( |
| 100 | + screen, getText(R.string.app_play_integrity_see_all_apps)) |
| 101 | + |
| 102 | + setPreferenceScreen(screen) |
| 103 | + } |
| 104 | + |
| 105 | + override fun refreshUi(): Boolean { |
| 106 | + val gosPs = GosPackageState.get(mPackageName, mUserId) |
| 107 | + |
| 108 | + showUsageNotifsSwitch.isChecked = AswBlockPlayIntegrityApi.I.isNotificationEnabled(gosPs) |
| 109 | + |
| 110 | + blockUsageAttemptsSwitch.isChecked = AswBlockPlayIntegrityApi.I |
| 111 | + .get(requireContext(), mUserId, mPackageInfo.applicationInfo!!, gosPs) |
| 112 | + |
| 113 | + return true |
| 114 | + } |
| 115 | + |
| 116 | + private fun isPlayStoreAvailable(): Boolean { |
| 117 | + val pkgManager = requireContext().packageManager |
| 118 | + val appInfo = try { |
| 119 | + pkgManager.getApplicationInfoAsUser(PackageId.PLAY_STORE_NAME, 0, mUserId) |
| 120 | + } catch (e: NameNotFoundException) { return false } |
| 121 | + |
| 122 | + if (!appInfo.enabled) { |
| 123 | + return false |
| 124 | + } |
| 125 | + return appInfo.ext().packageId == PackageId.PLAY_STORE |
| 126 | + } |
| 127 | + |
| 128 | + override fun createDialog(id: Int, errorCode: Int): AlertDialog? = null |
| 129 | + |
| 130 | + override fun getMetricsCategory(): Int = METRICS_CATEGORY_UNKNOWN |
| 131 | +} |
0 commit comments