• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.settings.display.darkmode;
18 
19 import static android.app.UiModeManager.MODE_NIGHT_AUTO;
20 
21 import static com.google.common.base.Preconditions.checkNotNull;
22 
23 import android.app.UiModeManager;
24 import android.content.Context;
25 import android.location.LocationManager;
26 
27 import androidx.annotation.NonNull;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settingslib.widget.FooterPreference;
34 
35 /** Controller for the Twilight location custom footer. */
36 public class DarkModePendingLocationPreferenceController extends BasePreferenceController {
37     private final UiModeManager mUiModeManager;
38     private final LocationManager mLocationManager;
39 
DarkModePendingLocationPreferenceController(@onNull Context context, @NonNull String key)40     public DarkModePendingLocationPreferenceController(@NonNull Context context,
41             @NonNull String key) {
42         super(context, key);
43         mUiModeManager = context.getSystemService(UiModeManager.class);
44         mLocationManager = context.getSystemService(LocationManager.class);
45     }
46 
47     @Override
getAvailabilityStatus()48     public int getAvailabilityStatus() {
49         return AVAILABLE_UNSEARCHABLE;
50     }
51 
52     @Override
displayPreference(@onNull PreferenceScreen screen)53     public void displayPreference(@NonNull PreferenceScreen screen) {
54         super.displayPreference(screen);
55         FooterPreference footerPreference = checkNotNull(screen.findPreference(getPreferenceKey()));
56         footerPreference.setIcon(R.drawable.ic_settings_location_filled);
57     }
58 
59     @Override
updateState(Preference preference)60     public void updateState(Preference preference) {
61         preference.setVisible(isActive());
62     }
63 
isActive()64     public boolean isActive() {
65         return mUiModeManager.getNightMode() == MODE_NIGHT_AUTO
66                 && mLocationManager.isLocationEnabled()
67                 && mLocationManager.getLastLocation() == null;
68     }
69 }
70