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 */
16package com.android.server.power.batterysaver;
17
18import android.content.Context;
19import android.os.PowerManager;
20import android.provider.Settings;
21import android.provider.Settings.Global;
22import android.util.Slog;
23
24import com.android.server.power.batterysaver.BatterySaverController.Plugin;
25
26public class BatterySaverLocationPlugin implements Plugin {
27    private static final String TAG = "BatterySaverLocationPlugin";
28
29    private static final boolean DEBUG = BatterySaverController.DEBUG;
30
31    private final Context mContext;
32
33    public BatterySaverLocationPlugin(Context context) {
34        mContext = context;
35    }
36
37    @Override
38    public void onBatterySaverChanged(BatterySaverController caller) {
39        if (DEBUG) {
40            Slog.d(TAG, "onBatterySaverChanged");
41        }
42        updateLocationState(caller);
43    }
44
45    @Override
46    public void onSystemReady(BatterySaverController caller) {
47        if (DEBUG) {
48            Slog.d(TAG, "onSystemReady");
49        }
50        updateLocationState(caller);
51    }
52
53    private void updateLocationState(BatterySaverController caller) {
54        final boolean kill =
55                (caller.getBatterySaverPolicy().getGpsMode()
56                        == PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF) &&
57                caller.isEnabled() && !caller.isInteractive();
58
59        if (DEBUG) {
60            Slog.d(TAG, "Battery saver " + (kill ? "stopping" : "restoring") + " location.");
61        }
62        Settings.Global.putInt(mContext.getContentResolver(),
63                Global.LOCATION_GLOBAL_KILL_SWITCH, kill ? 1 : 0);
64    }
65}
66