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