1 /*
2  * Copyright (C) 2023 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.keyguard.domain.interactor
18 
19 import android.content.Context
20 import android.database.ContentObserver
21 import android.os.Handler
22 import android.os.UserHandle
23 import android.provider.Settings
24 import android.provider.Settings.SettingNotFoundException
25 import com.android.systemui.dagger.SysUISingleton
26 import com.android.systemui.dagger.qualifiers.Main
27 import javax.inject.Inject
28 
29 @SysUISingleton
30 class NaturalScrollingSettingObserver
31 @Inject
32 constructor(
33     @Main private val handler: Handler,
34     private val context: Context,
35 ) {
36     var isNaturalScrollingEnabled = true
37         get() {
38             if (!isInitialized) {
39                 isInitialized = true
40                 update()
41             }
42             return field
43         }
44 
45     private var isInitialized = false
46 
47     private val contentObserver = object : ContentObserver(handler) {
onChangenull48         override fun onChange(selfChange: Boolean) {
49             update()
50         }
51     }
52 
53     init {
54         context.contentResolver.registerContentObserver(
55                 Settings.System.getUriFor(Settings.System.TOUCHPAD_NATURAL_SCROLLING), false,
56                 contentObserver)
57     }
58 
updatenull59     private fun update() {
60         isNaturalScrollingEnabled = try {
61             Settings.System.getIntForUser(context.contentResolver,
62                     Settings.System.TOUCHPAD_NATURAL_SCROLLING, UserHandle.USER_CURRENT) == 1
63         } catch (e: SettingNotFoundException) {
64             true
65         }
66     }
67 }
68