Files
app_Settings/src/com/android/settings/applications/HibernatedAppsPreferenceController.java
xinghailu b5293a2d1c Cleanup plurals in Settings, change <one> to <1> in string res file.
"One" and "1" are not same, such as "1st place" vs "a
place". Also in many languages, plurals expression is different with English, for more detail please check: go/android-i18n-plurals.
So in string res file, replace "one" with excat value "1" is a more
proper way.

Test: Existing unit tests still pass.
Bug: 199230342
Change-Id: I832abc38afc5d8816fa803865c25e6017cffa2c6
2022-12-13 18:31:08 +08:00

121 lines
4.0 KiB
Java

/*
* Copyright (C) 2021 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.applications;
import static android.provider.DeviceConfig.NAMESPACE_APP_HIBERNATION;
import static com.android.settings.Utils.PROPERTY_APP_HIBERNATION_ENABLED;
import android.content.Context;
import android.icu.text.MessageFormat;
import android.permission.PermissionControllerManager;
import android.provider.DeviceConfig;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.google.common.annotations.VisibleForTesting;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executor;
/**
* A preference controller handling the logic for updating summary of hibernated apps.
*/
public final class HibernatedAppsPreferenceController extends BasePreferenceController
implements LifecycleObserver {
private static final String TAG = "HibernatedAppsPrefController";
private PreferenceScreen mScreen;
private int mUnusedCount = 0;
private boolean mLoadingUnusedApps;
private boolean mLoadedUnusedCount;
private final Executor mMainExecutor;
public HibernatedAppsPreferenceController(Context context, String preferenceKey) {
this(context, preferenceKey, context.getMainExecutor());
}
@VisibleForTesting
HibernatedAppsPreferenceController(Context context, String preferenceKey,
Executor mainExecutor) {
super(context, preferenceKey);
mMainExecutor = mainExecutor;
}
@Override
public int getAvailabilityStatus() {
return isHibernationEnabled() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
@Override
public CharSequence getSummary() {
MessageFormat msgFormat = new MessageFormat(
mContext.getResources().getString(R.string.unused_apps_summary),
Locale.getDefault());
Map<String, Object> arguments = new HashMap<>();
arguments.put("count", mUnusedCount);
return mLoadedUnusedCount
? msgFormat.format(arguments)
: mContext.getResources().getString(R.string.summary_placeholder);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mScreen = screen;
}
/**
* On lifecycle resume event.
*/
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume() {
updatePreference();
}
private void updatePreference() {
if (mScreen == null) {
return;
}
if (!mLoadingUnusedApps) {
final PermissionControllerManager permController =
mContext.getSystemService(PermissionControllerManager.class);
permController.getUnusedAppCount(mMainExecutor, unusedCount -> {
mUnusedCount = unusedCount;
mLoadingUnusedApps = false;
mLoadedUnusedCount = true;
Preference pref = mScreen.findPreference(mPreferenceKey);
refreshSummary(pref);
});
mLoadingUnusedApps = true;
}
}
private static boolean isHibernationEnabled() {
return DeviceConfig.getBoolean(
NAMESPACE_APP_HIBERNATION, PROPERTY_APP_HIBERNATION_ENABLED, true);
}
}