• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 import com.android.permissioncontroller.PermissionControllerApplication
28 import com.android.permissioncontroller.permission.utils.Utils
29 
30 /**
31  * Live data of the users of the current profile group.
32  *
33  *
34  * Data source: system server
35  */
36 object UsersLiveData : SmartUpdateMediatorLiveData<List<UserHandle>>() {
37 
38     @SuppressLint("StaticFieldLeak")
39     private val app = PermissionControllerApplication.get()
40 
41     /** Monitors changes to the users on this device  */
42     private val mUserMonitor = object : BroadcastReceiver() {
onReceivenull43         override fun onReceive(context: Context, intent: Intent) {
44             onUpdate()
45         }
46     }
47 
48     /**
49      * Update the encapsulated data with the current list of users.
50      */
onUpdatenull51     override fun onUpdate() {
52         value = app.getSystemService(UserManager::class.java)!!.userProfiles
53     }
54 
onActivenull55     override fun onActive() {
56         onUpdate()
57 
58         val userChangeFilter = IntentFilter()
59         userChangeFilter.addAction(Intent.ACTION_USER_ADDED)
60         userChangeFilter.addAction(Intent.ACTION_USER_REMOVED)
61 
62         app.registerReceiver(mUserMonitor, userChangeFilter)
63 
64         super.onActive()
65     }
66 
onInactivenull67     override fun onInactive() {
68         app.unregisterReceiver(mUserMonitor)
69         super.onInactive()
70     }
71 }
72