1 /*
2  * Copyright (C) 2019 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.permissioncontroller.permission.data
18 
19 import android.annotation.SuppressLint
20 import android.content.BroadcastReceiver
21 import android.content.Context
22 import android.content.Intent
23 import android.content.IntentFilter
24 import android.os.UserHandle
25 import android.os.UserManager
26 import com.android.permissioncontroller.PermissionControllerApplication
27 
28 /**
29  * Live data of the users of the current profile group.
30  *
31  * Data source: system server
32  */
33 object UsersLiveData : SmartUpdateMediatorLiveData<List<UserHandle>>() {
34 
35     @SuppressLint("StaticFieldLeak") private val app = PermissionControllerApplication.get()
36 
37     /** Monitors changes to the users on this device */
38     private val mUserMonitor =
39         object : BroadcastReceiver() {
onReceivenull40             override fun onReceive(context: Context, intent: Intent) {
41                 onUpdate()
42             }
43         }
44 
45     /** Update the encapsulated data with the current list of users. */
onUpdatenull46     override fun onUpdate() {
47         value = app.getSystemService(UserManager::class.java)!!.userProfiles
48     }
49 
onActivenull50     override fun onActive() {
51         onUpdate()
52 
53         val userChangeFilter = IntentFilter()
54         userChangeFilter.addAction(Intent.ACTION_USER_ADDED)
55         userChangeFilter.addAction(Intent.ACTION_USER_REMOVED)
56 
57         app.registerReceiver(mUserMonitor, userChangeFilter)
58 
59         super.onActive()
60     }
61 
onInactivenull62     override fun onInactive() {
63         app.unregisterReceiver(mUserMonitor)
64         super.onInactive()
65     }
66 }
67