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.settings.display;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.location.LocationManager;
22 
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.R;
26 import com.android.settings.Settings;
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.overlay.FeatureFactory;
29 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
30 import com.android.settingslib.widget.BannerMessagePreference;
31 
32 /**
33  * Controller to take the user to location settings page
34  */
35 public class TwilightLocationPreferenceController extends BasePreferenceController {
36     private final LocationManager mLocationManager;
37     private final MetricsFeatureProvider mMetricsFeatureProvider;
38 
TwilightLocationPreferenceController(Context context, String preferenceKey)39     public TwilightLocationPreferenceController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41         mLocationManager = context.getSystemService(LocationManager.class);
42         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
43     }
44 
45     @Override
displayPreference(PreferenceScreen screen)46     public void displayPreference(PreferenceScreen screen) {
47         super.displayPreference(screen);
48         final BannerMessagePreference preference =
49                 (BannerMessagePreference) screen.findPreference(getPreferenceKey());
50         preference
51                 .setPositiveButtonText(R.string.twilight_mode_launch_location)
52                 .setPositiveButtonOnClickListener(v -> {
53                     mMetricsFeatureProvider.logClickedPreference(preference, getMetricsCategory());
54                     launchLocationSettings();
55                 });
56     }
57 
58     @Override
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         return mLocationManager.isLocationEnabled() ? CONDITIONALLY_UNAVAILABLE
61                 : AVAILABLE_UNSEARCHABLE;
62     }
63 
launchLocationSettings()64     private void launchLocationSettings() {
65         final Intent intent = new Intent();
66         intent.setClass(mContext, Settings.LocationSettingsActivity.class);
67         mContext.startActivity(intent);
68     }
69 }
70