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
17package com.android.settings.fuelgauge.anomaly.action;
18
19import android.Manifest;
20import android.content.Context;
21import android.content.pm.permission.RuntimePermissionPresenter;
22import android.support.annotation.VisibleForTesting;
23import android.support.v4.content.PermissionChecker;
24
25import com.android.internal.logging.nano.MetricsProto;
26import com.android.settings.fuelgauge.anomaly.Anomaly;
27
28/**
29 * Location action for anomaly app, which means to turn off location permission for this app
30 */
31public class LocationCheckAction extends AnomalyAction {
32
33    private static final String TAG = "LocationCheckAction";
34
35    private final RuntimePermissionPresenter mRuntimePermissionPresenter;
36
37    public LocationCheckAction(Context context) {
38        this(context, RuntimePermissionPresenter.getInstance(context));
39    }
40
41    @VisibleForTesting
42    LocationCheckAction(Context context, RuntimePermissionPresenter runtimePermissionPresenter) {
43        super(context);
44        mRuntimePermissionPresenter = runtimePermissionPresenter;
45        mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_LOCATION_CHECK;
46    }
47
48    @Override
49    public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
50        super.handlePositiveAction(anomaly, contextMetricsKey);
51        mRuntimePermissionPresenter.revokeRuntimePermission(anomaly.packageName,
52                Manifest.permission.ACCESS_COARSE_LOCATION);
53        mRuntimePermissionPresenter.revokeRuntimePermission(anomaly.packageName,
54                Manifest.permission.ACCESS_FINE_LOCATION);
55    }
56
57    @Override
58    public boolean isActionActive(Anomaly anomaly) {
59        return isPermissionGranted(anomaly, Manifest.permission.ACCESS_COARSE_LOCATION)
60                || isPermissionGranted(anomaly, Manifest.permission.ACCESS_FINE_LOCATION);
61    }
62
63    @Override
64    public int getActionType() {
65        return Anomaly.AnomalyActionType.LOCATION_CHECK;
66    }
67
68    private boolean isPermissionGranted(Anomaly anomaly, String permission) {
69        return PermissionChecker.checkPermission(mContext, permission, -1, anomaly.uid,
70                anomaly.packageName) == PermissionChecker.PERMISSION_GRANTED;
71    }
72}
73