1 /* 2 * 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.car.settings.applications.performance; 18 19 import android.content.Context; 20 21 import androidx.annotation.NonNull; 22 23 import com.android.settingslib.utils.ThreadUtils; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** 29 * Class used to get the disabled packages due to resource overuse. 30 */ 31 public class PerfImpactingAppsItemManager { 32 33 private Context mContext; 34 private final List<PerfImpactingAppsListener> mPerfImpactingAppsListeners; 35 PerfImpactingAppsItemManager(Context context)36 public PerfImpactingAppsItemManager(Context context) { 37 mContext = context; 38 mPerfImpactingAppsListeners = new ArrayList<>(); 39 } 40 41 /** 42 * Registers a listener that will be notified once the data is loaded. 43 */ addListener(@onNull PerfImpactingAppsListener listener)44 public void addListener(@NonNull PerfImpactingAppsListener listener) { 45 mPerfImpactingAppsListeners.add(listener); 46 } 47 48 /** 49 * Starts fetching installed apps and counting the non-system apps 50 */ startLoading()51 public void startLoading() { 52 ThreadUtils.postOnBackgroundThread(() -> { 53 int disabledPackagesCount = PerfImpactingAppsUtils.getDisabledPackages(mContext).size(); 54 for (PerfImpactingAppsListener listener : mPerfImpactingAppsListeners) { 55 ThreadUtils.postOnMainThread(() -> listener 56 .onPerfImpactingAppsLoaded(disabledPackagesCount)); 57 } 58 }); 59 } 60 61 /** 62 * Callback that is called once the performance-impacting apps are loaded. 63 */ 64 public interface PerfImpactingAppsListener { 65 /** 66 * Called when the apps are successfully loaded from secure settings strings and 67 * PackageManager. 68 */ onPerfImpactingAppsLoaded(int disabledPackagesCount)69 void onPerfImpactingAppsLoaded(int disabledPackagesCount); 70 } 71 } 72