Home | History | Annotate | Download | only in anomaly
      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 
     17 package com.android.settings.fuelgauge.anomaly;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.os.Bundle;
     24 import android.support.annotation.VisibleForTesting;
     25 
     26 import com.android.internal.logging.nano.MetricsProto;
     27 import com.android.settings.R;
     28 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
     29 import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
     30 
     31 /**
     32  * Dialog Fragment to show action dialog for each anomaly
     33  */
     34 public class AnomalyDialogFragment extends InstrumentedDialogFragment implements
     35         DialogInterface.OnClickListener {
     36 
     37     private static final String ARG_ANOMALY = "anomaly";
     38     private static final String ARG_METRICS_KEY = "metrics_key";
     39 
     40     @VisibleForTesting
     41     Anomaly mAnomaly;
     42     @VisibleForTesting
     43     AnomalyUtils mAnomalyUtils;
     44 
     45     /**
     46      * Listener to give the control back to target fragment
     47      */
     48     public interface AnomalyDialogListener {
     49         /**
     50          * This method is invoked once anomaly is handled, then target fragment could do
     51          * extra work. One example is that fragment could remove the anomaly preference
     52          * since it has been handled
     53          *
     54          * @param anomaly that has been handled
     55          */
     56         void onAnomalyHandled(Anomaly anomaly);
     57     }
     58 
     59     public static AnomalyDialogFragment newInstance(Anomaly anomaly, int metricsKey) {
     60         AnomalyDialogFragment dialogFragment = new AnomalyDialogFragment();
     61 
     62         Bundle args = new Bundle(2);
     63         args.putParcelable(ARG_ANOMALY, anomaly);
     64         args.putInt(ARG_METRICS_KEY, metricsKey);
     65         dialogFragment.setArguments(args);
     66 
     67         return dialogFragment;
     68     }
     69 
     70     @Override
     71     public void onCreate(Bundle savedInstanceState) {
     72         super.onCreate(savedInstanceState);
     73         initAnomalyUtils();
     74     }
     75 
     76     @VisibleForTesting
     77     void initAnomalyUtils() {
     78         mAnomalyUtils = AnomalyUtils.getInstance(getContext());
     79     }
     80 
     81     @Override
     82     public int getMetricsCategory() {
     83         return MetricsProto.MetricsEvent.DIALOG_HANDLE_ANOMALY;
     84     }
     85 
     86     @Override
     87     public void onClick(DialogInterface dialog, int which) {
     88         final AnomalyDialogListener lsn = (AnomalyDialogListener) getTargetFragment();
     89         if (lsn == null) {
     90             return;
     91         }
     92 
     93         final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
     94         final int metricsKey = getArguments().getInt(ARG_METRICS_KEY);
     95 
     96         anomalyAction.handlePositiveAction(mAnomaly, metricsKey);
     97         lsn.onAnomalyHandled(mAnomaly);
     98     }
     99 
    100     @Override
    101     public Dialog onCreateDialog(Bundle savedInstanceState) {
    102         final Bundle bundle = getArguments();
    103         final Context context = getContext();
    104         final AnomalyUtils anomalyUtils = AnomalyUtils.getInstance(context);
    105 
    106         mAnomaly = bundle.getParcelable(ARG_ANOMALY);
    107         anomalyUtils.logAnomaly(mMetricsFeatureProvider, mAnomaly,
    108                 MetricsProto.MetricsEvent.DIALOG_HANDLE_ANOMALY);
    109 
    110         final AnomalyAction anomalyAction = mAnomalyUtils.getAnomalyAction(mAnomaly);
    111         switch (anomalyAction.getActionType()) {
    112             case Anomaly.AnomalyActionType.FORCE_STOP:
    113                 return new AlertDialog.Builder(context)
    114                         .setTitle(R.string.dialog_stop_title)
    115                         .setMessage(getString(mAnomaly.type == Anomaly.AnomalyType.WAKE_LOCK
    116                                 ? R.string.dialog_stop_message
    117                                 : R.string.dialog_stop_message_wakeup_alarm, mAnomaly.displayName))
    118                         .setPositiveButton(R.string.dialog_stop_ok, this)
    119                         .setNegativeButton(R.string.dlg_cancel, null)
    120                         .create();
    121             case Anomaly.AnomalyActionType.STOP_AND_BACKGROUND_CHECK:
    122                 return new AlertDialog.Builder(context)
    123                         .setTitle(R.string.dialog_background_check_title)
    124                         .setMessage(getString(R.string.dialog_background_check_message,
    125                                 mAnomaly.displayName))
    126                         .setPositiveButton(R.string.dialog_background_check_ok, this)
    127                         .setNegativeButton(R.string.dlg_cancel, null)
    128                         .create();
    129             case Anomaly.AnomalyActionType.LOCATION_CHECK:
    130                 return new AlertDialog.Builder(context)
    131                         .setTitle(R.string.dialog_location_title)
    132                         .setMessage(getString(R.string.dialog_location_message,
    133                                 mAnomaly.displayName))
    134                         .setPositiveButton(R.string.dialog_location_ok, this)
    135                         .setNegativeButton(R.string.dlg_cancel, null)
    136                         .create();
    137             default:
    138                 throw new IllegalArgumentException("unknown type " + mAnomaly.type);
    139         }
    140     }
    141 
    142 }
    143