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

Commit 92f16dc6 authored by Bruno Martins's avatar Bruno Martins
Browse files

LocationSettingsActivity: Account for deprecated location modes

LOCATION_MODE_HIGH_ACCURACY, LOCATION_MODE_SENSORS_ONLY and
LOCATION_MODE_BATTERY_SAVING are deprecated.

Take one further step and refactor the entire logic to call
LocationManager#setLocationEnabledForUser(boolean, int) instead
of setting location mode and LocationManager#isLocationEnabled()
to check if location is enabled.

Change-Id: Ibe1930902f6bfff99eb2c2d852ab600620e25f08
parent 011b5e95
Loading
Loading
Loading
Loading
+1 −71
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2013 The CyanogenMod Project
     Copyright (C) 2017 The LineageOS Project
     Copyright (C) 2017-2020 The LineageOS Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -83,76 +83,6 @@

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/battery_saving"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/location_margin_left"
                    android:paddingRight="@dimen/content_margin_right"
                    android:background="?android:attr/selectableItemBackground"
                    android:clickable="true">

                    <CheckBox
                        android:id="@+id/battery_saving_checkbox"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="top"
                        android:layout_marginTop="5dp"
                        android:duplicateParentState="true"
                        android:clickable="false" />


                    <TextView
                        android:id="@+id/battery_saving_summary"
                        android:layout_width="0px"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:textSize="15sp"
                        android:lineSpacingExtra="@dimen/setup_line_spacing"
                        android:gravity="top"
                        android:layout_marginLeft="@dimen/location_text_margin_left"
                        android:layout_marginRight="@dimen/location_text_margin_right"
                        android:paddingBottom="@dimen/content_margin_bottom"
                        android:text="@string/location_battery_saving"
                        android:maxLines="5" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/network"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="@dimen/location_margin_left"
                    android:paddingRight="@dimen/content_margin_right"
                    android:background="?android:attr/selectableItemBackground"
                    android:clickable="true">

                    <CheckBox
                        android:id="@+id/network_checkbox"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="top"
                        android:layout_marginTop="5dp"
                        android:duplicateParentState="true"
                        android:clickable="false" />

                    <TextView
                        android:id="@+id/network_summary"
                        android:layout_width="0px"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:textSize="15sp"
                        android:lineSpacingExtra="@dimen/setup_line_spacing"
                        android:gravity="top"
                        android:layout_marginLeft="@dimen/location_text_margin_left"
                        android:layout_marginRight="@dimen/location_text_margin_right"
                        android:paddingBottom="@dimen/content_margin_bottom"
                        android:text="@string/location_network"
                        android:maxLines="5" />

                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </FrameLayout>
+1 −5
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2013-2015 The CyanogenMod Project
     Copyright (C) 2017,2019 The LineageOS Project
     Copyright (C) 2017-2020 The LineageOS Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -55,10 +55,6 @@
    <string name="other_services_summary">These services put Google to work for you, and you can turn them on or off at any time. Data will be used in accordance with Google\'s <xliff:g id="name" example="Privacy Policy">%s</xliff:g>.</string>
    <string name="location_services_summary">Location services allows system and third party apps to gather and use data such as your approximate location. For example, an app may use your approximate location to locate nearby coffee shops.</string>
    <string name="location_access_summary"><b>Allow apps that have asked your permission</b> to use your location information. This may include your current location and past locations.</string>
    <string name="location_battery_saving"><b>Reduce battery consumption</b> by restricting the number of GPS updates per hour.</string>
    <string name="location_network"><b>Use Wi-Fi</b> to help apps determine your location.</string>
    <string name="location_network_telephony"><b>Use Wi-Fi and cellular networks</b> to help apps determine your location.</string>
    <string name="location_network_gms"><b>Use Google\'s location service</b> to help apps determine your location. This means sending anonymous location data to Google, even when no apps are running.</string>

    <string name="setup_mobile_data">Turn on cellular data</string>
    <string name="setup_mobile_data_no_service">No service</string>
+12 −151
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The CyanogenMod Project
 * Copyright (C) 2017-2018 The LineageOS Project
 * Copyright (C) 2017-2020 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -17,12 +17,9 @@

package org.lineageos.setupwizard;

import android.content.ContentResolver;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
@@ -34,75 +31,32 @@ public class LocationSettingsActivity extends BaseSetupWizardActivity {
    public static final String TAG =
            LocationSettingsActivity.class.getSimpleName().substring(0, 22);

    private View mLocationRow;
    private View mBatteryRow;
    private View mNetworkRow;
    private CheckBox mNetwork;
    private CheckBox mBattery;
    private CheckBox mLocationAccess;

    private ContentResolver mContentResolver;


    /** Broadcast intent action when the location mode is about to change. */
    private static final String MODE_CHANGING_ACTION =
            "com.android.settings.location.MODE_CHANGING";
    private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
    private static final String NEW_MODE_KEY = "NEW_MODE";

    private int mCurrentMode = Settings.Secure.LOCATION_MODE_OFF;

    private View.OnClickListener mLocationClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onToggleLocationAccess(!mLocationAccess.isChecked());
        }
    };

    private View.OnClickListener mBatteryClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onToggleBatterySaving(!mBattery.isChecked());
        }
    };

    private View.OnClickListener mNetworkClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onToggleNetwork(!mNetwork.isChecked());
        }
    };
    private LocationManager mLocationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setNextText(R.string.next);

        mContentResolver = getContentResolver();
        TextView summaryView = (TextView) findViewById(android.R.id.summary);
        summaryView.setText(R.string.location_services_summary);
        mLocationRow = findViewById(R.id.location);
        mLocationRow.setOnClickListener(mLocationClickListener);

        mLocationAccess = (CheckBox) findViewById(R.id.location_checkbox);
        mBatteryRow = findViewById(R.id.battery_saving);
        mBatteryRow.setOnClickListener(mBatteryClickListener);
        mBattery = (CheckBox) findViewById(R.id.battery_saving_checkbox);
        mNetworkRow = findViewById(R.id.network);
        mNetworkRow.setOnClickListener(mNetworkClickListener);
        mNetwork = (CheckBox) findViewById(R.id.network_checkbox);
        TextView networkSummary = (TextView) findViewById(R.id.network_summary);
        final boolean hasTelephony = SetupWizardUtils.hasTelephony(this);
        if (hasTelephony) {
            networkSummary.setText(R.string.location_network_telephony);
        } else {
            networkSummary.setText(R.string.location_network);
        }
        mLocationManager = getSystemService(LocationManager.class);
        View locationAccessView = findViewById(R.id.location);
        locationAccessView.setOnClickListener(v -> {
            mLocationManager.setLocationEnabledForUser(!mLocationAccess.isChecked(),
                        new UserHandle(UserHandle.USER_CURRENT));
            mLocationAccess.setChecked(!mLocationAccess.isChecked());
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshLocationMode();
        mLocationAccess.setChecked(mLocationManager.isLocationEnabled());
    }

    @Override
@@ -125,97 +79,4 @@ public class LocationSettingsActivity extends BaseSetupWizardActivity {
        return R.drawable.ic_location;
    }

    private void setLocationMode(int mode) {
        Intent intent = new Intent(MODE_CHANGING_ACTION);
        intent.putExtra(CURRENT_MODE_KEY, mCurrentMode);
        intent.putExtra(NEW_MODE_KEY, mode);
        sendBroadcastAsUser(intent, new UserHandle(UserHandle.USER_CURRENT),
                android.Manifest.permission.WRITE_SECURE_SETTINGS);
        Settings.Secure.putInt(mContentResolver, Settings.Secure.LOCATION_MODE, mode);
        refreshLocationMode();
    }

    private void refreshLocationMode() {
        int mode = Settings.Secure.getInt(mContentResolver, Settings.Secure.LOCATION_MODE,
                Settings.Secure.LOCATION_MODE_OFF);

        if (mCurrentMode != mode) {
            mCurrentMode = mode;
            if (Log.isLoggable(TAG, Log.INFO)) {
                Log.i(TAG, "Location mode has been changed");
            }
            updateLocationToggles(mode);
        }
    }

    private void updateLocationToggles(int mode) {
        switch (mode) {
            case Settings.Secure.LOCATION_MODE_OFF:
                mLocationAccess.setChecked(false);
                mBattery.setChecked(false);
                mBattery.setEnabled(false);
                mBatteryRow.setEnabled(false);
                mNetwork.setChecked(false);
                mNetwork.setEnabled(false);
                mNetworkRow.setEnabled(false);
                break;
            case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
                mLocationAccess.setChecked(true);
                mBattery.setChecked(false);
                mBattery.setEnabled(true);
                mBatteryRow.setEnabled(true);
                mNetwork.setChecked(false);
                mNetwork.setEnabled(true);
                mNetworkRow.setEnabled(true);
                break;
            case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
                mLocationAccess.setChecked(true);
                mBattery.setChecked(true);
                mNetwork.setChecked(false);
                mNetwork.setEnabled(false);
                mNetworkRow.setEnabled(false);
                break;
            case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
                mLocationAccess.setChecked(true);
                mNetwork.setChecked(true);
                mBattery.setChecked(false);
                mBattery.setEnabled(false);
                mBatteryRow.setEnabled(false);
                break;
            default:
                mLocationAccess.setChecked(false);
                mBattery.setChecked(false);
                mBattery.setEnabled(false);
                mBatteryRow.setEnabled(false);
                mNetwork.setChecked(false);
                mNetwork.setEnabled(false);
                mNetworkRow.setEnabled(false);
                break;
        }
    }

    private void onToggleLocationAccess(boolean checked) {
        if (checked) {
            setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
        } else {
            setLocationMode(Settings.Secure.LOCATION_MODE_OFF);
        }
    }

    private void onToggleBatterySaving(boolean checked) {
        if (checked) {
            setLocationMode(Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
        } else {
            setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
        }
    }

    private void onToggleNetwork(boolean checked) {
        if (checked) {
            setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
        } else {
            setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
        }
    }

}