Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8442db24 authored by Amith Yamasani's avatar Amith Yamasani
Browse files

Move Google-apps specific location settings to a Google package

Bug: 6882764

This removes the Google specific checkbox and adds a master toggle
for location providers which turns both providers on/off when
toggled.

Change-Id: I2a7af0953902bfd6f4b221725dc2d838bb3d0ce4
parent 4635ec6b
Loading
Loading
Loading
Loading
+19 −5
Original line number Diff line number Diff line
@@ -17,15 +17,29 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="@string/location_settings_title">

        <CheckBoxPreference
            android:key="location_network"
            android:title="@string/location_network_based"
            android:summary="@string/location_neighborhood_level"/>
        <com.android.settings.WrappingSwitchPreference
            android:key="location_toggle"
            android:title="@string/location_access_title"
            android:summary="@string/location_access_summary"
            android:persistent="true"/>

        <PreferenceCategory
            android:key="location_sources"
            android:title="@string/location_sources_heading" />

        <CheckBoxPreference
            android:key="location_gps"
            android:title="@string/location_gps"
            android:summary="@string/location_street_level"/>
            android:summary="@string/location_street_level"
            android:dependency="location_toggle"
            android:persistent="false" />

        <com.android.settings.WrappingCheckBoxPreference
            android:key="location_network"
            android:title="@string/location_network_based"
            android:summary="@string/location_neighborhood_level"
            android:dependency="location_toggle"
            android:persistent="false" />

<!-- Disabled to avoid confusion on devices with no AGPS
     For Google experience devices we want AGPS on by default (if supported) so we don't really need this.
+0 −129
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings;

import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * Helper class to read and write the 'Use My Location' setting used by Google Apps (e.g. GoogleQSB,
 * VoiceSearch).
 *
 * This class duplicates a small amount of functionality from GSF (Google Services Framework) to
 * allow the open source Settings app to interface to the 'Use My Location' setting owned by GSF.
 */
public class GoogleLocationSettingHelper {

    private static final String TAG = "GoogleLocationSettingHelper";

    /**
     * User has disagreed to use location for Google services.
     */
    public static final int USE_LOCATION_FOR_SERVICES_OFF = 0;

    /**
     * User has agreed to use location for Google services.
     */
    public static final int USE_LOCATION_FOR_SERVICES_ON = 1;

    /**
     * The user has neither agreed nor disagreed to use location for Google services yet.
     */
    public static final int USE_LOCATION_FOR_SERVICES_NOT_SET = 2;

    private static final String GOOGLE_SETTINGS_AUTHORITY = "com.google.settings";
    private static final Uri GOOGLE_SETTINGS_CONTENT_URI =
        Uri.parse("content://" + GOOGLE_SETTINGS_AUTHORITY + "/partner");
    private static final String NAME = "name";
    private static final String VALUE = "value";
    private static final String USE_LOCATION_FOR_SERVICES = "use_location_for_services";

    private static final String ACTION_SET_USE_LOCATION_FOR_SERVICES =
        "com.google.android.gsf.action.SET_USE_LOCATION_FOR_SERVICES";
    public static final String EXTRA_DISABLE_USE_LOCATION_FOR_SERVICES = "disable";

    /**
     * Determine if the 'Use My Location' setting is applicable on this device, i.e. if the
     * activity used to enabled/disable it is present.
     */
    public static boolean isAvailable(Context context) {
        ResolveInfo ri = context.getPackageManager().resolveActivity(getSetUseLocationIntent(),
                PackageManager.MATCH_DEFAULT_ONLY);
        return ri != null;
    }

    private static Intent getSetUseLocationIntent() {
        Intent i = new Intent(ACTION_SET_USE_LOCATION_FOR_SERVICES);
        return i;
    }

    /**
     * Get the current value for the 'Use value for location' setting.
     * @return One of {@link #USE_LOCATION_FOR_SERVICES_NOT_SET},
     *      {@link #USE_LOCATION_FOR_SERVICES_OFF} or {@link #USE_LOCATION_FOR_SERVICES_ON}.
     */
    public static int getUseLocationForServices(Context context) {
        ContentResolver resolver = context.getContentResolver();
        Cursor c = null;
        String stringValue = null;
        try {
            c = resolver.query(GOOGLE_SETTINGS_CONTENT_URI, new String[] { VALUE }, NAME + "=?",
                    new String[] { USE_LOCATION_FOR_SERVICES }, null);
            if (c != null && c.moveToNext()) {
                stringValue = c.getString(0);
            }
        } catch (RuntimeException e) {
            Log.w(TAG, "Failed to get 'Use My Location' setting", e);
        } finally {
            if (c != null) {
                c.close();
            }
        }
        if (stringValue == null) {
            return USE_LOCATION_FOR_SERVICES_NOT_SET;
        }
        int value;
        try {
            value = Integer.parseInt(stringValue);
        } catch (NumberFormatException nfe) {
            value = USE_LOCATION_FOR_SERVICES_NOT_SET;
        }
        return value;
    }

    /**
     * Change the value of the 'Use My Location' setting. This launches a GSF activity which has
     * the permissions to actually make the change, prompting the user if necessary.
     */
    public static void setUseLocationForServices(Context context, boolean use) {
        Intent i = getSetUseLocationIntent();
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra(EXTRA_DISABLE_USE_LOCATION_FOR_SERVICES, !use);
        try {
            context.startActivity(i);
        } catch (ActivityNotFoundException e) {
            Log.e("GoogleLocationSettingHelper", "Problem while starting GSF location activity");
        }
    }

}
+77 −33
Original line number Diff line number Diff line
@@ -19,14 +19,18 @@ package com.android.settings;

import android.content.ContentQueryMap;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.LocationManager;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import java.util.Observable;
import java.util.Observer;
@@ -35,18 +39,18 @@ import java.util.Observer;
 * Gesture lock pattern settings.
 */
public class LocationSettings extends SettingsPreferenceFragment
        implements OnPreferenceChangeListener {
        implements Preference.OnPreferenceChangeListener {

    // Location Settings
    private static final String KEY_LOCATION_TOGGLE = "location_toggle";
    private static final String KEY_LOCATION_NETWORK = "location_network";
    private static final String KEY_LOCATION_GPS = "location_gps";
    private static final String KEY_ASSISTED_GPS = "assisted_gps";
    private static final String KEY_USE_LOCATION = "location_use_for_services";

    private CheckBoxPreference mNetwork;
    private CheckBoxPreference mGps;
    private CheckBoxPreference mAssistedGps;
    private CheckBoxPreference mUseLocation;
    private SwitchPreference mLocationAccess;

    // These provide support for receiving notification when Location Manager settings change.
    // This is necessary because the Network Location Provider can change settings
@@ -82,24 +86,12 @@ public class LocationSettings extends SettingsPreferenceFragment
        addPreferencesFromResource(R.xml.location_settings);
        root = getPreferenceScreen();

        mLocationAccess = (SwitchPreference) root.findPreference(KEY_LOCATION_TOGGLE);
        mNetwork = (CheckBoxPreference) root.findPreference(KEY_LOCATION_NETWORK);
        mGps = (CheckBoxPreference) root.findPreference(KEY_LOCATION_GPS);
        mAssistedGps = (CheckBoxPreference) root.findPreference(KEY_ASSISTED_GPS);
        if (GoogleLocationSettingHelper.isAvailable(getActivity())) {
            // GSF present, Add setting for 'Use My Location'
            CheckBoxPreference useLocation = new CheckBoxPreference(getActivity());
            useLocation.setKey(KEY_USE_LOCATION);
            useLocation.setTitle(R.string.use_location_title);
            useLocation.setSummary(R.string.use_location_summary);
            useLocation.setChecked(
                    GoogleLocationSettingHelper.getUseLocationForServices(getActivity())
                    == GoogleLocationSettingHelper.USE_LOCATION_FOR_SERVICES_ON);
            useLocation.setPersistent(false);
            useLocation.setOnPreferenceChangeListener(this);
            getPreferenceScreen().addPreference(useLocation);
            mUseLocation = useLocation;
        }

        mLocationAccess.setOnPreferenceChangeListener(this);
        return root;
    }

@@ -125,19 +117,19 @@ public class LocationSettings extends SettingsPreferenceFragment

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

        final ContentResolver cr = getContentResolver();
        if (preference == mNetwork) {
            Settings.Secure.setLocationProviderEnabled(getContentResolver(),
            Settings.Secure.setLocationProviderEnabled(cr,
                    LocationManager.NETWORK_PROVIDER, mNetwork.isChecked());
        } else if (preference == mGps) {
            boolean enabled = mGps.isChecked();
            Settings.Secure.setLocationProviderEnabled(getContentResolver(),
            Settings.Secure.setLocationProviderEnabled(cr,
                    LocationManager.GPS_PROVIDER, enabled);
            if (mAssistedGps != null) {
                mAssistedGps.setEnabled(enabled);
            }
        } else if (preference == mAssistedGps) {
            Settings.Secure.putInt(getContentResolver(), Settings.Secure.ASSISTED_GPS_ENABLED,
            Settings.Secure.putInt(cr, Settings.Secure.ASSISTED_GPS_ENABLED,
                    mAssistedGps.isChecked() ? 1 : 0);
        } else {
            // If we didn't handle it, let preferences handle it.
@@ -154,9 +146,11 @@ public class LocationSettings extends SettingsPreferenceFragment
        ContentResolver res = getContentResolver();
        boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
                res, LocationManager.GPS_PROVIDER);
        mNetwork.setChecked(Settings.Secure.isLocationProviderEnabled(
                res, LocationManager.NETWORK_PROVIDER));
        boolean networkEnabled = Settings.Secure.isLocationProviderEnabled(
                res, LocationManager.NETWORK_PROVIDER);
        mGps.setChecked(gpsEnabled);
        mNetwork.setChecked(networkEnabled);
        mLocationAccess.setChecked(gpsEnabled || networkEnabled);
        if (mAssistedGps != null) {
            mAssistedGps.setChecked(Settings.Secure.getInt(res,
                    Settings.Secure.ASSISTED_GPS_ENABLED, 2) == 1);
@@ -173,16 +167,66 @@ public class LocationSettings extends SettingsPreferenceFragment
        createPreferenceHierarchy();
    }

    public boolean onPreferenceChange(Preference preference, Object value) {
        if (preference == mUseLocation) {
            boolean newValue = (value == null ? false : (Boolean) value);
            GoogleLocationSettingHelper.setUseLocationForServices(getActivity(), newValue);
            // We don't want to change the value immediately here, since the user may click
            // disagree in the dialog that pops up. When the activity we just launched exits, this
            // activity will be restated and the new value re-read, so the checkbox will get its
            // new value then.
            return false;
    /** Enable or disable all providers when the master toggle is changed. */
    private void onToggleLocationAccess(boolean checked) {
        final ContentResolver cr = getContentResolver();
        Settings.Secure.setLocationProviderEnabled(cr,
                LocationManager.GPS_PROVIDER, checked);
        Settings.Secure.setLocationProviderEnabled(cr,
                LocationManager.NETWORK_PROVIDER, checked);
        updateLocationToggles();
    }

    @Override
    public boolean onPreferenceChange(Preference pref, Object newValue) {
        if (pref.getKey().equals(KEY_LOCATION_TOGGLE)) {
            onToggleLocationAccess((Boolean) newValue);
        }
        return true;
    }

}

class WrappingSwitchPreference extends SwitchPreference {

    public WrappingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public WrappingSwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        TextView title = (TextView) view.findViewById(android.R.id.title);
        if (title != null) {
            title.setSingleLine(false);
            title.setMaxLines(3);
        }
    }
}

class WrappingCheckBoxPreference extends CheckBoxPreference {

    public WrappingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public WrappingCheckBoxPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        TextView title = (TextView) view.findViewById(android.R.id.title);
        if (title != null) {
            title.setSingleLine(false);
            title.setMaxLines(3);
        }
    }
}