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

Commit 3eddcc67 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Add AppLocalePreference for Spa

Also let SettingsSpaUnitTests depends on the
mockito-target-extended-minus-junit4 to support mock static methods.

Bug: 236346018
Test: Unit test & Manual with Settings App
Change-Id: Ib7a3022b20e30c8292713f52d29785eb78899c70
parent 1cbf8c78
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ import com.android.settings.applications.AppInfoBase;
import com.android.settings.applications.AppLocaleUtil;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.widget.LayoutPreference;

import java.util.Locale;
@@ -206,10 +205,10 @@ public class AppLocaleDetails extends SettingsPreferenceFragment {
     * TODO (b209962418) Do a performance test to low end device.
     * @return Return the summary to show the current app's language.
     */
    public static CharSequence getSummary(Context context, AppEntry entry) {
        final UserHandle userHandle = UserHandle.getUserHandleForUid(entry.info.uid);
    public static CharSequence getSummary(Context context, ApplicationInfo app) {
        final UserHandle userHandle = UserHandle.getUserHandleForUid(app.uid);
        final Context contextAsUser = context.createContextAsUser(userHandle, 0);
        Locale appLocale = getAppDefaultLocale(contextAsUser, entry.info.packageName);
        Locale appLocale = getAppDefaultLocale(contextAsUser, app.packageName);
        if (appLocale == null) {
            return context.getString(R.string.preference_of_system_locale_summary);
        } else {
+1 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ public class AppLocalePreferenceController extends AppInfoPreferenceControllerBa

    @Override
    public CharSequence getSummary() {
        return AppLocaleDetails.getSummary(mContext, mParent.getAppEntry());
        return AppLocaleDetails.getSummary(mContext, mParent.getAppEntry().info);
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -1707,7 +1707,7 @@ public class ManageApplications extends InstrumentedFragment
                    holder.setSummary(MediaManagementAppsDetails.getSummary(mContext, entry));
                    break;
                case LIST_TYPE_APPS_LOCALE:
                    holder.setSummary(AppLocaleDetails.getSummary(mContext, entry));
                    holder.setSummary(AppLocaleDetails.getSummary(mContext, entry.info));
                    break;
                case LIST_TYPE_BATTERY_OPTIMIZATION:
                    holder.setSummary(null);
+1 −1
Original line number Diff line number Diff line
@@ -96,7 +96,7 @@ private fun AppInfoSettings(packageInfoPresenter: PackageInfoPresenter) {
        // TODO: data_settings
        AppTimeSpentPreference(app)
        // TODO: battery
        // TODO: app_language_setting
        AppLocalePreference(app)
        AppOpenByDefaultPreference(app)
        DefaultAppShortcuts(app)

+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.spa.app.appinfo

import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.ResolveInfoFlags
import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.android.settings.R
import com.android.settings.applications.AppInfoBase
import com.android.settings.applications.AppLocaleUtil
import com.android.settings.applications.appinfo.AppLocaleDetails
import com.android.settings.localepicker.AppLocalePickerActivity
import com.android.settingslib.spa.framework.compose.collectAsStateWithLifecycle
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.PreferenceModel
import com.android.settingslib.spaprivileged.model.app.userHandle
import com.android.settingslib.spaprivileged.model.app.userId
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext

@Composable
fun AppLocalePreference(app: ApplicationInfo) {
    val context = LocalContext.current
    val presenter = remember { AppLocalePresenter(context, app) }
    if (!presenter.isAvailableFlow.collectAsStateWithLifecycle(initialValue = false).value) return

    Preference(object : PreferenceModel {
        override val title = stringResource(R.string.app_locale_preference_title)
        override val summary = presenter.summaryFlow.collectAsStateWithLifecycle(
            initialValue = stringResource(R.string.summary_placeholder),
        )
        override val onClick = presenter::startActivity
    })
}

private class AppLocalePresenter(
    private val context: Context,
    private val app: ApplicationInfo,
) {
    private val packageManager = context.packageManager

    val isAvailableFlow = flow { emit(isAvailable()) }

    private suspend fun isAvailable(): Boolean = withContext(Dispatchers.IO) {
        val resolveInfos = packageManager.queryIntentActivitiesAsUser(
            AppLocaleUtil.LAUNCHER_ENTRY_INTENT,
            ResolveInfoFlags.of(PackageManager.GET_META_DATA.toLong()),
            app.userId,
        )
        AppLocaleUtil.canDisplayLocaleUi(context, app.packageName, resolveInfos)
    }

    val summaryFlow = flow { emit(getSummary()) }

    private suspend fun getSummary() = withContext(Dispatchers.IO) {
        AppLocaleDetails.getSummary(context, app).toString()
    }

    fun startActivity() {
        val intent = Intent(context, AppLocalePickerActivity::class.java).apply {
            data = Uri.parse("package:" + app.packageName)
            putExtra(AppInfoBase.ARG_PACKAGE_UID, app.uid)
        }
        context.startActivityAsUser(intent, app.userHandle)
    }
}
Loading