blob: fa1b0267fafa5f0929e80f7438ddac28e9a2ad63 [file] [log] [blame]
Evan Laird31ca5472020-04-08 17:45:24 -04001/*
2 * Copyright (C) 2020 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
17package com.android.systemui.settings
18
19import android.content.Context
20import android.os.UserHandle
21import com.android.systemui.broadcast.BroadcastDispatcher
22import com.android.systemui.util.Assert
23import javax.inject.Inject
24import javax.inject.Singleton
25
26/**
27 * Tracks a reference to the context for the current user
28 */
29@Singleton
30class CurrentUserContextTracker @Inject constructor(
31 private val sysuiContext: Context,
32 broadcastDispatcher: BroadcastDispatcher
33) {
34 private val userTracker: CurrentUserTracker
35 var currentUserContext: Context
36
37 init {
38 userTracker = object : CurrentUserTracker(broadcastDispatcher) {
39 override fun onUserSwitched(newUserId: Int) {
40 handleUserSwitched(newUserId)
41 }
42 }
43
44 currentUserContext = makeUserContext(userTracker.currentUserId)
45 }
46
47 fun initialize() {
48 userTracker.startTracking()
49 }
50
51 private fun handleUserSwitched(newUserId: Int) {
52 currentUserContext = makeUserContext(newUserId)
53 }
54
55 private fun makeUserContext(uid: Int): Context {
56 Assert.isMainThread()
57 return sysuiContext.createContextAsUser(
58 UserHandle.getUserHandleForUid(userTracker.currentUserId), 0)
59 }
60}