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