1 /* 2 * Copyright (C) 2017 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 package com.android.settings.location; 17 18 import android.content.Context; 19 import android.provider.Settings.Secure; 20 import android.support.v7.preference.Preference; 21 import android.support.v7.preference.PreferenceScreen; 22 import com.android.settings.R; 23 import com.android.settings.core.PreferenceController; 24 25 public class LocationPreferenceController extends PreferenceController { 26 27 private static final String KEY_LOCATION = "location"; 28 private Preference mPreference; 29 LocationPreferenceController(Context context)30 public LocationPreferenceController(Context context) { 31 super(context); 32 } 33 34 @Override displayPreference(PreferenceScreen screen)35 public void displayPreference(PreferenceScreen screen) { 36 super.displayPreference(screen); 37 mPreference = screen.findPreference(KEY_LOCATION); 38 } 39 40 @Override updateState(Preference preference)41 public void updateState(Preference preference) { 42 preference.setSummary(getLocationSummary(mContext)); 43 } 44 45 @Override getPreferenceKey()46 public String getPreferenceKey() { 47 return KEY_LOCATION; 48 } 49 50 @Override isAvailable()51 public boolean isAvailable() { 52 return true; 53 } 54 updateSummary()55 public void updateSummary() { 56 updateState(mPreference); 57 } 58 getLocationSummary(Context context)59 public static String getLocationSummary(Context context) { 60 int mode = Secure.getInt(context.getContentResolver(), 61 Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF); 62 if (mode != Secure.LOCATION_MODE_OFF) { 63 return context.getString(R.string.location_on_summary, 64 context.getString(getLocationString(mode))); 65 } 66 return context.getString(R.string.location_off_summary); 67 } 68 getLocationString(int mode)69 public static int getLocationString(int mode) { 70 switch (mode) { 71 case Secure.LOCATION_MODE_OFF: 72 return R.string.location_mode_location_off_title; 73 case Secure.LOCATION_MODE_SENSORS_ONLY: 74 return R.string.location_mode_sensors_only_title; 75 case Secure.LOCATION_MODE_BATTERY_SAVING: 76 return R.string.location_mode_battery_saving_title; 77 case Secure.LOCATION_MODE_HIGH_ACCURACY: 78 return R.string.location_mode_high_accuracy_title; 79 } 80 return 0; 81 } 82 83 } 84