1 /* <lambda>null2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.spa.app.appinfo 18 19 import android.content.Context 20 import android.content.Intent 21 import android.content.pm.ApplicationInfo 22 import android.content.pm.PackageManager.ResolveInfoFlags 23 import android.provider.Settings 24 import androidx.compose.runtime.Composable 25 import androidx.compose.runtime.getValue 26 import androidx.compose.runtime.livedata.observeAsState 27 import androidx.compose.runtime.remember 28 import androidx.compose.ui.platform.LocalContext 29 import androidx.compose.ui.res.stringResource 30 import androidx.lifecycle.liveData 31 import com.android.settings.R 32 import com.android.settings.overlay.FeatureFactory.Companion.featureFactory 33 import com.android.settingslib.spa.widget.preference.Preference 34 import com.android.settingslib.spa.widget.preference.PreferenceModel 35 import com.android.settingslib.spaprivileged.model.app.hasFlag 36 import com.android.settingslib.spaprivileged.model.app.userHandle 37 import com.android.settingslib.spaprivileged.model.app.userId 38 import kotlinx.coroutines.Dispatchers 39 40 @Composable 41 fun AppTimeSpentPreference(app: ApplicationInfo) { 42 val context = LocalContext.current 43 val presenter = remember(app) { AppTimeSpentPresenter(context, app) } 44 if (!presenter.isAvailable()) return 45 46 val summary by presenter.summaryLiveData.observeAsState( 47 initial = stringResource(R.string.summary_placeholder), 48 ) 49 Preference(object : PreferenceModel { 50 override val title = stringResource(R.string.time_spent_in_app_pref_title) 51 override val summary = { summary } 52 override val enabled = { presenter.isEnabled() } 53 override val onClick = presenter::startActivity 54 }) 55 } 56 57 private class AppTimeSpentPresenter( 58 private val context: Context, 59 private val app: ApplicationInfo, 60 ) { <lambda>null61 private val intent = Intent(Settings.ACTION_APP_USAGE_SETTINGS).apply { 62 putExtra(Intent.EXTRA_PACKAGE_NAME, app.packageName) 63 } 64 private val appFeatureProvider = featureFactory.applicationFeatureProvider 65 isAvailablenull66 fun isAvailable() = context.packageManager.queryIntentActivitiesAsUser( 67 intent, ResolveInfoFlags.of(0), app.userId 68 ).any { resolveInfo -> 69 resolveInfo?.activityInfo?.applicationInfo?.isSystemApp == true 70 } 71 isEnablednull72 fun isEnabled() = app.hasFlag(ApplicationInfo.FLAG_INSTALLED) 73 74 val summaryLiveData = liveData(Dispatchers.IO) { 75 emit(appFeatureProvider.getTimeSpentInApp(app.packageName).toString()) 76 } 77 startActivitynull78 fun startActivity() { 79 context.startActivityAsUser(intent, app.userHandle) 80 } 81 } 82