1 /*
2  * Copyright (C) 2024 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.customization.picker.mode.shared.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.os.PowerManager
24 import android.text.TextUtils
25 import androidx.lifecycle.DefaultLifecycleObserver
26 import androidx.lifecycle.Lifecycle
27 import androidx.lifecycle.LifecycleOwner
28 import com.android.customization.picker.mode.data.repository.DarkModeRepository
29 import dagger.hilt.android.qualifiers.ActivityContext
30 import dagger.hilt.android.scopes.ActivityScoped
31 import javax.inject.Inject
32 
33 /**
34  * This class observes the activity lifecycle and updates the DarkModeRepositoryImpl based on
35  * lifecycle phases.
36  */
37 @ActivityScoped
38 class DarkModeLifecycleUtil
39 @Inject
40 constructor(
41     @ActivityContext private val activityContext: Context,
42     private val darkModeRepository: DarkModeRepository,
43 ) {
44     private val lifecycleOwner = activityContext as LifecycleOwner
45 
46     private val batterySaverStateReceiver =
47         object : BroadcastReceiver() {
onReceivenull48             override fun onReceive(context: Context?, intent: Intent?) {
49                 if (
50                     intent != null &&
51                         TextUtils.equals(intent.action, PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)
52                 ) {
53                     darkModeRepository.refreshIsPowerSaveModeActivated()
54                 }
55             }
56         }
57     private val lifecycleObserver =
58         object : DefaultLifecycleObserver {
59             @Synchronized
onStartnull60             override fun onStart(owner: LifecycleOwner) {
61                 super.onStart(owner)
62                 darkModeRepository.refreshIsDarkModeActivated()
63                 darkModeRepository.refreshIsPowerSaveModeActivated()
64                 if (lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) {
65                     activityContext.registerReceiver(
66                         batterySaverStateReceiver,
67                         IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED),
68                     )
69                 }
70             }
71 
72             @Synchronized
onStopnull73             override fun onStop(owner: LifecycleOwner) {
74                 super.onStop(owner)
75                 activityContext.unregisterReceiver(batterySaverStateReceiver)
76             }
77 
78             @Synchronized
onDestroynull79             override fun onDestroy(owner: LifecycleOwner) {
80                 super.onDestroy(owner)
81                 lifecycleOwner.lifecycle.removeObserver(this)
82             }
83         }
84 
85     init {
86         lifecycleOwner.lifecycle.addObserver(lifecycleObserver)
87     }
88 }
89