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.app.ActivityManager;
20import android.content.Context;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageManager;
23import android.util.Log;
24
25import com.android.internal.logging.nano.MetricsProto;
26import com.android.settings.fuelgauge.anomaly.Anomaly;
27
28/**
29 * Force stop action for anomaly app, which means to stop the app which causes anomaly
30 */
31public class ForceStopAction extends AnomalyAction {
32    private static final String TAG = "ForceStopAction";
33
34    private ActivityManager mActivityManager;
35    private PackageManager mPackageManager;
36
37    public ForceStopAction(Context context) {
38        super(context);
39        mActivityManager = (ActivityManager) context.getSystemService(
40                Context.ACTIVITY_SERVICE);
41        mPackageManager = context.getPackageManager();
42        mActionMetricKey = MetricsProto.MetricsEvent.ACTION_APP_FORCE_STOP;
43    }
44
45    @Override
46    public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
47        super.handlePositiveAction(anomaly, contextMetricsKey);
48
49        mActivityManager.forceStopPackage(anomaly.packageName);
50    }
51
52    @Override
53    public boolean isActionActive(Anomaly anomaly) {
54        try {
55            ApplicationInfo info = mPackageManager.getApplicationInfo(anomaly.packageName,
56                    PackageManager.GET_META_DATA);
57            return (info.flags & ApplicationInfo.FLAG_STOPPED) == 0;
58        } catch (PackageManager.NameNotFoundException e) {
59            Log.e(TAG, "Cannot find info for app: " + anomaly.packageName);
60        }
61        return false;
62    }
63
64    @Override
65    public int getActionType() {
66        return Anomaly.AnomalyActionType.FORCE_STOP;
67    }
68}
69