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.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.location.LocationManager; 23 import android.provider.Settings.Secure; 24 import android.support.annotation.VisibleForTesting; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.PreferenceScreen; 27 import com.android.settings.R; 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settingslib.core.AbstractPreferenceController; 30 import com.android.settings.search.DatabaseIndexingUtils; 31 import com.android.settings.search.InlineListPayload; 32 import com.android.settings.search.ResultPayload; 33 import com.android.settingslib.core.lifecycle.Lifecycle; 34 import com.android.settingslib.core.lifecycle.LifecycleObserver; 35 import com.android.settingslib.core.lifecycle.events.OnPause; 36 import com.android.settingslib.core.lifecycle.events.OnResume; 37 38 public class LocationPreferenceController extends AbstractPreferenceController 39 implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause { 40 41 private static final String KEY_LOCATION = "location"; 42 private Context mContext; 43 private Preference mPreference; 44 45 @VisibleForTesting 46 BroadcastReceiver mLocationProvidersChangedReceiver; 47 48 public LocationPreferenceController(Context context, Lifecycle lifecycle) { 49 super(context); 50 mContext = context; 51 mLocationProvidersChangedReceiver = new BroadcastReceiver() { 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 if (intent.getAction().equals(LocationManager.PROVIDERS_CHANGED_ACTION)) { 55 updateSummary(); 56 } 57 } 58 }; 59 if (lifecycle != null) { 60 lifecycle.addObserver(this); 61 } 62 } 63 64 @Override 65 public void displayPreference(PreferenceScreen screen) { 66 super.displayPreference(screen); 67 mPreference = screen.findPreference(KEY_LOCATION); 68 } 69 70 @Override 71 public void onResume() { 72 if (mLocationProvidersChangedReceiver != null) { 73 mContext.registerReceiver(mLocationProvidersChangedReceiver, new IntentFilter( 74 LocationManager.PROVIDERS_CHANGED_ACTION)); 75 } 76 } 77 78 @Override 79 public void onPause() { 80 if (mLocationProvidersChangedReceiver != null) { 81 mContext.unregisterReceiver(mLocationProvidersChangedReceiver); 82 } 83 } 84 85 @Override 86 public void updateState(Preference preference) { 87 preference.setSummary(getLocationSummary(mContext)); 88 } 89 90 @Override 91 public String getPreferenceKey() { 92 return KEY_LOCATION; 93 } 94 95 @Override 96 public boolean isAvailable() { 97 return true; 98 } 99 100 public void updateSummary() { 101 updateState(mPreference); 102 } 103 104 public static String getLocationSummary(Context context) { 105 int mode = Secure.getInt(context.getContentResolver(), 106 Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF); 107 if (mode != Secure.LOCATION_MODE_OFF) { 108 return context.getString(R.string.location_on_summary); 109 } 110 return context.getString(R.string.location_off_summary); 111 } 112 113 @Override 114 public ResultPayload getResultPayload() { 115 final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext, 116 LocationSettings.class.getName(), KEY_LOCATION, 117 mContext.getString(R.string.location_settings_title)); 118 119 return new InlineListPayload(Secure.LOCATION_MODE, 120 ResultPayload.SettingsSource.SECURE, intent, isAvailable(), 121 Secure.LOCATION_MODE_HIGH_ACCURACY + 1, Secure.LOCATION_MODE_OFF); 122 } 123 } 124