1 /*
2  * Copyright (C) 2021 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;
18 
19 import android.content.Context;
20 import android.permission.PermissionControllerManager;
21 
22 import androidx.annotation.NonNull;
23 
24 /**
25  * Class for fetching and returning the number of hibernated apps. Largely derived from
26  * {@link com.android.settings.applications.HibernatedAppsPreferenceController}.
27  */
28 public class HibernatedAppsItemManager {
29     private final Context mContext;
30     private HibernatedAppsCountListener mListener;
31 
HibernatedAppsItemManager(Context context)32     public HibernatedAppsItemManager(Context context) {
33         mContext = context;
34     }
35 
36     /**
37      * Starts fetching recently used apps
38      */
startLoading()39     public void startLoading() {
40         PermissionControllerManager permController =
41                 mContext.getSystemService(PermissionControllerManager.class);
42         if (mListener != null && permController != null) {
43             // This executor is only used for returning the value
44             // The main logic happens on a background thread
45             permController.getUnusedAppCount(mContext.getMainExecutor(),
46                     mListener::onHibernatedAppsCountLoaded);
47         }
48     }
49 
50     /**
51      * Registers a listener that will be notified once the data is loaded.
52      */
setListener(@onNull HibernatedAppsCountListener listener)53     public void setListener(@NonNull HibernatedAppsCountListener listener) {
54         mListener = listener;
55     }
56 
57     /**
58      * Callback that is called once the count of hibernated apps has been fetched.
59      */
60     public interface HibernatedAppsCountListener {
61         /**
62          * Called when the count of hibernated apps has loaded.
63          */
onHibernatedAppsCountLoaded(int hibernatedAppsCount)64         void onHibernatedAppsCountLoaded(int hibernatedAppsCount);
65     }
66 }
67