1 /*
2  * Copyright (C) 2018 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.car.settings.quicksettings;
18 
19 import android.annotation.DrawableRes;
20 import android.annotation.Nullable;
21 import android.app.UiModeManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.graphics.drawable.Drawable;
25 import android.view.View;
26 
27 import com.android.car.settings.R;
28 import com.android.car.settings.common.CarSettingActivities;
29 import com.android.car.settings.common.FragmentHost;
30 
31 /**
32  * Toggles auto or night mode tile on quick setting page.
33  */
34 public class DayNightTile implements QuickSettingGridAdapter.Tile {
35     private final Context mContext;
36     private final StateChangedListener mStateChangedListener;
37     private final UiModeManager mUiModeManager;
38     private final View.OnLongClickListener mLaunchDisplaySettings;
39 
40     @DrawableRes
41     private int mIconRes = R.drawable.ic_settings_night_display;
42 
43     private final String mText;
44 
45     private State mState = State.ON;
46 
DayNightTile( Context context, StateChangedListener stateChangedListener, FragmentHost fragmentHost)47     DayNightTile(
48             Context context,
49             StateChangedListener stateChangedListener,
50             FragmentHost fragmentHost) {
51         mStateChangedListener = stateChangedListener;
52         mContext = context;
53         mUiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
54         if (mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES) {
55             mState = State.ON;
56         } else {
57             mState = State.OFF;
58         }
59         mText = mContext.getString(R.string.night_mode_tile_label);
60         mLaunchDisplaySettings = v -> {
61             context.startActivity(new Intent(context,
62                     CarSettingActivities.DisplaySettingsActivity.class));
63             return true;
64         };
65     }
66 
67     @Nullable
getOnLongClickListener()68     public View.OnLongClickListener getOnLongClickListener() {
69         return mLaunchDisplaySettings;
70     }
71 
72     @Override
isAvailable()73     public boolean isAvailable() {
74         return true;
75     }
76 
77     @Override
getIcon()78     public Drawable getIcon() {
79         return mContext.getDrawable(mIconRes);
80     }
81 
82     @Override
83     @Nullable
getText()84     public String getText() {
85         return mText;
86     }
87 
88     @Override
getState()89     public State getState() {
90         return mState;
91     }
92 
93     @Override
start()94     public void start() {
95     }
96 
97     @Override
stop()98     public void stop() {
99     }
100 
101     @Override
onClick(View v)102     public void onClick(View v) {
103         if (mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES) {
104             mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
105         } else {
106             mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
107         }
108     }
109 }
110