1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.settings.location; 15 16 import android.content.BroadcastReceiver; 17 import android.content.Context; 18 import android.content.Intent; 19 import android.content.IntentFilter; 20 import android.location.SettingInjectorService; 21 import android.os.UserHandle; 22 import android.support.annotation.VisibleForTesting; 23 import android.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceCategory; 25 import android.support.v7.preference.PreferenceScreen; 26 import android.util.Log; 27 28 import com.android.settings.widget.RestrictedAppPreference; 29 import com.android.settingslib.core.lifecycle.Lifecycle; 30 import com.android.settingslib.core.lifecycle.LifecycleObserver; 31 import com.android.settingslib.core.lifecycle.events.OnPause; 32 import com.android.settingslib.core.lifecycle.events.OnResume; 33 34 import java.util.List; 35 36 public class LocationServicePreferenceController extends LocationBasePreferenceController 37 implements LifecycleObserver, OnResume, OnPause { 38 39 private static final String TAG = "LocationServicePrefCtrl"; 40 /** Key for preference category "Location services" */ 41 private static final String KEY_LOCATION_SERVICES = "location_services"; 42 @VisibleForTesting 43 static final IntentFilter INTENT_FILTER_INJECTED_SETTING_CHANGED = 44 new IntentFilter(SettingInjectorService.ACTION_INJECTED_SETTING_CHANGED); 45 46 private PreferenceCategory mCategoryLocationServices; 47 private final LocationSettings mFragment; 48 private final SettingsInjector mInjector; 49 /** Receives UPDATE_INTENT */ 50 @VisibleForTesting 51 BroadcastReceiver mInjectedSettingsReceiver; 52 53 public LocationServicePreferenceController(Context context, LocationSettings fragment, 54 Lifecycle lifecycle) { 55 this(context, fragment, lifecycle, new SettingsInjector(context)); 56 } 57 58 @VisibleForTesting 59 LocationServicePreferenceController(Context context, LocationSettings fragment, 60 Lifecycle lifecycle, SettingsInjector injector) { 61 super(context, lifecycle); 62 mFragment = fragment; 63 mInjector = injector; 64 if (lifecycle != null) { 65 lifecycle.addObserver(this); 66 } 67 } 68 69 @Override 70 public String getPreferenceKey() { 71 return KEY_LOCATION_SERVICES; 72 } 73 74 @Override 75 public boolean isAvailable() { 76 // If managed profile has lock-down on location access then its injected location services 77 // must not be shown. 78 return mInjector.hasInjectedSettings(mLocationEnabler.isManagedProfileRestrictedByBase() 79 ? UserHandle.myUserId() : UserHandle.USER_CURRENT); 80 } 81 82 @Override 83 public void displayPreference(PreferenceScreen screen) { 84 super.displayPreference(screen); 85 mCategoryLocationServices = 86 (PreferenceCategory) screen.findPreference(KEY_LOCATION_SERVICES); 87 } 88 89 @Override 90 public void updateState(Preference preference) { 91 mCategoryLocationServices.removeAll(); 92 final List<Preference> prefs = getLocationServices(); 93 for (Preference pref : prefs) { 94 if (pref instanceof RestrictedAppPreference) { 95 ((RestrictedAppPreference) pref).checkRestrictionAndSetDisabled(); 96 } 97 } 98 LocationSettings.addPreferencesSorted(prefs, mCategoryLocationServices); 99 } 100 101 @Override 102 public void onLocationModeChanged(int mode, boolean restricted) { 103 // As a safety measure, also reloads on location mode change to ensure the settings are 104 // up-to-date even if an affected app doesn't send the setting changed broadcast. 105 mInjector.reloadStatusMessages(); 106 } 107 108 @Override 109 public void onResume() { 110 if (mInjectedSettingsReceiver == null) { 111 mInjectedSettingsReceiver = new BroadcastReceiver() { 112 @Override 113 public void onReceive(Context context, Intent intent) { 114 if (Log.isLoggable(TAG, Log.DEBUG)) { 115 Log.d(TAG, "Received settings change intent: " + intent); 116 } 117 mInjector.reloadStatusMessages(); 118 } 119 }; 120 } 121 mContext.registerReceiver( 122 mInjectedSettingsReceiver, INTENT_FILTER_INJECTED_SETTING_CHANGED); 123 } 124 125 @Override 126 public void onPause() { 127 mContext.unregisterReceiver(mInjectedSettingsReceiver); 128 } 129 130 private List<Preference> getLocationServices() { 131 // If location access is locked down by device policy then we only show injected settings 132 // for the primary profile. 133 return mInjector.getInjectedSettings(mFragment.getPreferenceManager().getContext(), 134 mLocationEnabler.isManagedProfileRestrictedByBase() 135 ? UserHandle.myUserId() : UserHandle.USER_CURRENT); 136 } 137 } 138