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.dagger;
18 
19 import android.content.Context;
20 import android.hardware.display.NightDisplayListener;
21 import android.os.Handler;
22 import android.os.UserHandle;
23 
24 import com.android.systemui.dagger.qualifiers.Background;
25 
26 import dagger.Module;
27 import dagger.Provides;
28 
29 import javax.inject.Inject;
30 
31 /**
32  * Module for providing a {@link NightDisplayListener}.
33  */
34 @Module
35 public class NightDisplayListenerModule {
36 
37     /**
38      * Provides a {@link NightDisplayListener}.
39      *
40      * The provided listener is associated with the user as returned by
41      * {@link android.app.ActivityManager#getCurrentUser}, making an IPC call on its creation.
42      * If the current user is known, prefer using a {@link Builder}.
43      */
44     @Provides
provideNightDisplayListener(Context context, @Background Handler bgHandler)45     public NightDisplayListener provideNightDisplayListener(Context context,
46             @Background Handler bgHandler) {
47         return new NightDisplayListener(context, bgHandler);
48     }
49 
50     /**
51      * Builder to create instances of {@link NightDisplayListener}.
52      *
53      * It uses {@link UserHandle#USER_SYSTEM} as the default user.
54      */
55     public static class Builder {
56         private final Context mContext;
57         private final Handler mBgHandler;
58         private int mUserId = UserHandle.USER_SYSTEM;
59 
60         @Inject
Builder(Context context, @Background Handler bgHandler)61         public Builder(Context context, @Background Handler bgHandler) {
62             mContext = context;
63             mBgHandler = bgHandler;
64         }
65 
66         /**
67          * Set the userId for this builder
68          */
setUser(int userId)69         public Builder setUser(int userId) {
70             mUserId = userId;
71             return this;
72         }
73 
74         /**
75          * Build a {@link NightDisplayListener} for the set user.
76          */
build()77         public NightDisplayListener build() {
78             return new NightDisplayListener(mContext, mUserId, mBgHandler);
79         }
80     }
81 }
82