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