1 /*
2  * Copyright (C) 2021 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 android.car.cts.powerpolicy;
18 
19 import java.util.Objects;
20 
21 public final class SilentModeInfo {
22     private static final String[] ATTR_HEADERS = {"Monitoring HW state signal",
23             "Silent mode by HW state signal", "Forced silent mode"};
24     private static final int NUMBER_OF_ATTRS = 3;
25 
26     public static final String COMMAND = "cmd car_service silent-mode query";
27     public static final SilentModeInfo NO_SILENT = new SilentModeInfo(true, false, false);
28     public static final SilentModeInfo FORCED_SILENT = new SilentModeInfo(false, true, true);
29 
30     private final boolean mForcedSilentMode;
31     private final boolean mMonitoringHWStateSignal;
32     private final boolean mSilentModeByHWStateSignal;
33 
SilentModeInfo(boolean monitoring, boolean byHW, boolean forced)34     private SilentModeInfo(boolean monitoring, boolean byHW, boolean forced) {
35         mMonitoringHWStateSignal = monitoring;
36         mSilentModeByHWStateSignal = byHW;
37         mForcedSilentMode = forced;
38     }
39 
getForcedSilentMode()40     public boolean getForcedSilentMode() {
41         return mForcedSilentMode;
42     }
43 
getMonitoringHWStateSignal()44     public boolean getMonitoringHWStateSignal() {
45         return mMonitoringHWStateSignal;
46     }
47 
48     @Override
equals(Object o)49     public boolean equals(Object o) {
50         if (this == o) return true;
51         if (o == null || getClass() != o.getClass()) return false;
52         SilentModeInfo that = (SilentModeInfo) o;
53         return mMonitoringHWStateSignal == that.mMonitoringHWStateSignal
54                 && mSilentModeByHWStateSignal == that.mSilentModeByHWStateSignal
55                 && mForcedSilentMode == that.mForcedSilentMode;
56     }
57 
58     @Override
hashCode()59     public int hashCode() {
60         return Objects.hash(mMonitoringHWStateSignal,
61                 mSilentModeByHWStateSignal, mForcedSilentMode);
62     }
63 
64     @Override
toString()65     public String toString() {
66         return String.format("SilentModeInfo: %b, %b, %b", mMonitoringHWStateSignal,
67                 mSilentModeByHWStateSignal, mForcedSilentMode);
68     }
69 
parse(String cmdOutput)70     public static SilentModeInfo parse(String cmdOutput) throws Exception {
71         boolean[] attrs = new boolean[ATTR_HEADERS.length];
72         String[] lines = cmdOutput.split("\n");
73 
74         if (lines.length != SilentModeInfo.ATTR_HEADERS.length) {
75             throw new IllegalArgumentException(
76                     "SilentModeQueryResult.parse(): malformatted cmd output: " + cmdOutput);
77         }
78         for (int idx = 0; idx < ATTR_HEADERS.length; idx++) {
79             String[] tokens = lines[idx].trim().split(":");
80             if (tokens.length != 2) {
81                 throw new IllegalArgumentException(
82                         "SilentModeQueryResult.parse(): malformatted attr line: " + lines[idx]);
83             }
84             int hdrIdx;
85             String hdr = tokens[0];
86             String val = tokens[1];
87             for (hdrIdx = 0; hdrIdx < ATTR_HEADERS.length; hdrIdx++) {
88                 if (hdr.contains(ATTR_HEADERS[hdrIdx])) {
89                     break;
90                 }
91             }
92             if (hdrIdx == ATTR_HEADERS.length) {
93                 throw new IllegalArgumentException(
94                         "SilentModeQueryResult.parse(): unknown header: " + hdr);
95             }
96             attrs[hdrIdx] = Boolean.parseBoolean(val.trim());
97         }
98 
99         return new SilentModeInfo(attrs[0], attrs[1], attrs[2]);
100     }
101 }
102