summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/fuelgauge/anomaly/AnomalyDialogFragment.java
blob: 69d03b94c1bf341120874fe199556b71a9b0fd61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2017 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.fuelgauge.anomaly;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;

import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;

/**
 * Dialog Fragment to show action dialog for each anomaly
 */
public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
        DialogInterface.OnClickListener {

    private static final String ARG_ANOMALY = "anomaly";
    private static final String ARG_METRICS_KEY = "metrics_key";

    @VisibleForTesting
    Anomaly mAnomaly;
    @VisibleForTesting
    AnomalyUtils mAnomalyUtils;

    /**
     * Listener to give the control back to target fragment
     */
    public interface AnomalyDialogListener {
        /**
         * This method is invoked once anomaly is handled, then target fragment could do
         * extra work. One example is that fragment could remove the anomaly preference
         * since it has been handled
         *
         * @param anomaly that has been handled
         */
        void onAnomalyHandled(Anomaly anomaly);
    }

    public static AnomalyDialogFragment newInstance(Anomaly anomaly, int metricsKey) {
        AnomalyDialogFragment dialogFragment = new AnomalyDialogFragment();

        Bundle args = new Bundle(2);
        args.putParcelable(ARG_ANOMALY, anomaly);
        args.putInt(ARG_METRICS_KEY, metricsKey);
        dialogFragment.setArguments(args);

        return dialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initAnomalyUtils();
    }

    @VisibleForTesting
    void initAnomalyUtils() {
        mAnomalyUtils = AnomalyUtils.getInstance(getContext());
    }

    @Override
    public int getMetricsCategory() {
        // TODO(b/37681923): add anomaly metric id
        return 0;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        final AnomalyDialogListener lsn = (AnomalyDialogListener) getTargetFragment();
        if (lsn == null) {
            return;
        }

        final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
        final int metricsKey = getArguments().getInt(ARG_METRICS_KEY);

        anomalyAction.handlePositiveAction(mAnomaly, metricsKey);
        lsn.onAnomalyHandled(mAnomaly);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Bundle bundle = getArguments();
        mAnomaly = bundle.getParcelable(ARG_ANOMALY);

        final Context context = getContext();
        final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
        switch (anomalyAction.getActionType()) {
            case Anomaly.AnomalyActionType.FORCE_STOP:
                return new AlertDialog.Builder(context)
                        .setTitle(R.string.dialog_stop_title)
                        .setMessage(getString(mAnomaly.type == Anomaly.AnomalyType.WAKE_LOCK
                                ? R.string.dialog_stop_message
                                : R.string.dialog_stop_message_wakeup_alarm, mAnomaly.displayName))
                        .setPositiveButton(R.string.dialog_stop_ok, this)
                        .setNegativeButton(R.string.dlg_cancel, null)
                        .create();
            case Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK:
                return new AlertDialog.Builder(context)
                        .setTitle(R.string.dialog_background_check_title)
                        .setMessage(getString(R.string.dialog_background_check_message,
                                mAnomaly.displayName))
                        .setPositiveButton(R.string.dialog_background_check_ok, this)
                        .setNegativeButton(R.string.dlg_cancel, null)
                        .create();
            case Anomaly.AnomalyActionType.LOCATION_CHECK:
                return new AlertDialog.Builder(context)
                        .setTitle(R.string.dialog_location_title)
                        .setMessage(getString(R.string.dialog_location_message,
                                mAnomaly.displayName))
                        .setPositiveButton(R.string.dialog_location_ok, this)
                        .setNegativeButton(R.string.dlg_cancel, null)
                        .create();
            default:
                throw new IllegalArgumentException("unknown type " + mAnomaly.type);
        }
    }

}