1 /*
2  * Copyright (C) 2014 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.cts.verifier.sensors.helpers;
18 
19 import com.android.cts.verifier.R;
20 import com.android.cts.verifier.sensors.base.BaseSensorTestActivity;
21 import com.android.cts.verifier.sensors.base.ISensorTestStateContainer;
22 
23 /**
24  * A helper class for {@link SensorFeaturesDeactivator}. It abstracts the responsibility of handling
25  * device settings that affect sensors.
26  *
27  * This class is meant to be used only by {@link SensorFeaturesDeactivator}.
28  * To keep things simple, this class synchronizes access to its internal state on public methods.
29  * This approach is fine, because there is no need for concurrent access.
30  */
31 abstract class SensorSettingContainer {
32     private static final int DEFAULT_SETTING_VALUE = -1;
33 
34     private final String mAction;
35     private final int mSettingNameResId;
36 
37     private boolean mInitialized;
38     private boolean mSettingAvailable;
39     private boolean mCapturedModeOn;
40 
SensorSettingContainer(String action, int settingNameResId)41     public SensorSettingContainer(String action, int settingNameResId) {
42         mAction = action;
43         mSettingNameResId = settingNameResId;
44     }
45 
captureInitialState()46     public synchronized void captureInitialState() {
47         if (mInitialized) {
48             return;
49         }
50         mSettingAvailable = getSettingMode(DEFAULT_SETTING_VALUE) != DEFAULT_SETTING_VALUE;
51         mCapturedModeOn = getCurrentSettingMode();
52         mInitialized = true;
53     }
54 
requestToSetMode( ISensorTestStateContainer stateContainer, boolean modeOn)55     public synchronized void requestToSetMode(
56             ISensorTestStateContainer stateContainer,
57             boolean modeOn) throws InterruptedException {
58         if (!isSettingAvailable() || !isSettingUiAvailable(stateContainer)) {
59             return;
60         }
61         trySetMode(stateContainer, modeOn);
62         if (getCurrentSettingMode() != modeOn) {
63             String message = stateContainer.getString(
64                     R.string.snsr_setting_mode_not_set,
65                     getSettingName(stateContainer),
66                     modeOn);
67             throw new IllegalStateException(message);
68         }
69     }
70 
requestToResetMode(ISensorTestStateContainer stateContainer)71     public synchronized void requestToResetMode(ISensorTestStateContainer stateContainer)
72             throws InterruptedException {
73         if (!isSettingAvailable() || !isSettingUiAvailable(stateContainer)) {
74             return;
75         }
76         trySetMode(stateContainer, mCapturedModeOn);
77     }
78 
trySetMode(ISensorTestStateContainer stateContainer, boolean modeOn)79     private void trySetMode(ISensorTestStateContainer stateContainer, boolean modeOn)
80             throws InterruptedException {
81         BaseSensorTestActivity.SensorTestLogger logger = stateContainer.getTestLogger();
82         String settingName = getSettingName(stateContainer);
83         if (getCurrentSettingMode() == modeOn) {
84             logger.logMessage(R.string.snsr_setting_mode_set, settingName, modeOn);
85             return;
86         }
87 
88         logger.logInstructions(R.string.snsr_setting_mode_request, settingName, modeOn);
89         logger.logInstructions(R.string.snsr_on_complete_return);
90         stateContainer.waitForUserToContinue();
91         stateContainer.executeActivity(mAction);
92     }
93 
getCurrentSettingMode()94     private boolean getCurrentSettingMode() {
95         return getSettingMode(DEFAULT_SETTING_VALUE) != 0;
96     }
97 
getSettingName(ISensorTestStateContainer stateContainer)98     private String getSettingName(ISensorTestStateContainer stateContainer) {
99         return stateContainer.getString(mSettingNameResId);
100     }
101 
isSettingAvailable()102     protected boolean isSettingAvailable() {
103         if (!mInitialized) {
104             throw new IllegalStateException(
105                     "Object must be initialized first by invoking #captureInitialState.");
106         }
107         return mSettingAvailable;
108     }
109 
isSettingUiAvailable(ISensorTestStateContainer stateContainer)110     protected boolean isSettingUiAvailable(ISensorTestStateContainer stateContainer) {
111         return stateContainer.hasActivity(mAction);
112     }
113 
getSettingMode(int defaultValue)114     protected abstract int getSettingMode(int defaultValue);
115 }
116