• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.systemui.util
18 
19 import android.content.BroadcastReceiver
20 import android.content.Context
21 import android.content.Intent
22 import android.content.IntentFilter
23 import android.media.AudioManager
24 import android.os.UserHandle
25 import androidx.lifecycle.LiveData
26 import androidx.lifecycle.MutableLiveData
27 import com.android.systemui.broadcast.BroadcastDispatcher
28 import com.android.systemui.dagger.SysUISingleton
29 import com.android.systemui.dagger.qualifiers.Background
30 import java.util.concurrent.Executor
31 import javax.inject.Inject
32 
33 @SysUISingleton
34 class RingerModeTrackerImpl @Inject constructor(
35     audioManager: AudioManager,
36     broadcastDispatcher: BroadcastDispatcher,
37     @Background executor: Executor
38 ) : RingerModeTracker {
39 
40     override val ringerMode: LiveData<Int> = RingerModeLiveData(
41             broadcastDispatcher,
42             executor,
43             AudioManager.RINGER_MODE_CHANGED_ACTION,
44             audioManager::getRingerMode
45     )
46 
47     override val ringerModeInternal: LiveData<Int> = RingerModeLiveData(
48             broadcastDispatcher,
49             executor,
50             AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION,
51             audioManager::getRingerModeInternal
52     )
53 }
54 
55 class RingerModeLiveData(
56     private val broadcastDispatcher: BroadcastDispatcher,
57     private val executor: Executor,
58     intent: String,
59     private val getter: () -> Int
60 ) : MutableLiveData<Int>() {
61 
62     private val filter = IntentFilter(intent)
63     var initialSticky: Boolean = false
64         private set
65 
66     private val receiver = object : BroadcastReceiver() {
onReceivenull67         override fun onReceive(context: Context, intent: Intent) {
68             initialSticky = isInitialStickyBroadcast
69             postValue(intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1))
70         }
71     }
72 
getValuenull73     override fun getValue(): Int {
74         return super.getValue() ?: -1
75     }
76 
onActivenull77     override fun onActive() {
78         super.onActive()
79         broadcastDispatcher.registerReceiver(receiver, filter, executor, UserHandle.ALL)
80         executor.execute {
81             postValue(getter())
82         }
83     }
84 
onInactivenull85     override fun onInactive() {
86         super.onInactive()
87         broadcastDispatcher.unregisterReceiver(receiver)
88     }
89 }
90