| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package androidx.window.common; |
| 18 |
|
| 19 |
import static android.hardware.devicestate.DeviceStateManager.INVALID_DEVICE_STATE; |
| 20 |
|
| 21 |
import static androidx.window.common.CommonFoldingFeature.COMMON_STATE_UNKNOWN; |
| 22 |
import static androidx.window.common.CommonFoldingFeature.parseListFromString; |
| 23 |
|
| 24 |
import android.annotation.NonNull; |
| 25 |
import android.content.Context; |
| 26 |
import android.hardware.devicestate.DeviceStateManager; |
| 27 |
import android.hardware.devicestate.DeviceStateManager.DeviceStateCallback; |
| 28 |
import android.text.TextUtils; |
| 29 |
import android.util.Log; |
| 30 |
import android.util.SparseIntArray; |
| 31 |
|
| 32 |
import androidx.window.util.AcceptOnceConsumer; |
| 33 |
import androidx.window.util.BaseDataProducer; |
| 34 |
|
| 35 |
import com.android.internal.R; |
| 36 |
|
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.List; |
| 39 |
import java.util.Objects; |
| 40 |
import java.util.Optional; |
| 41 |
import java.util.Set; |
| 42 |
import java.util.function.Consumer; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public final class DeviceStateManagerFoldingFeatureProducer |
| 50 |
extends BaseDataProducer<List<CommonFoldingFeature>> { |
| 51 |
private static final String TAG = |
| 52 |
DeviceStateManagerFoldingFeatureProducer.class.getSimpleName(); |
| 53 |
private static final boolean DEBUG = false; |
| 54 |
|
| 55 |
private final SparseIntArray mDeviceStateToPostureMap = new SparseIntArray(); |
| 56 |
|
| 57 |
private int mCurrentDeviceState = INVALID_DEVICE_STATE; |
| 58 |
|
| 59 |
@NonNull |
| 60 |
private final BaseDataProducer<String> mRawFoldSupplier; |
| 61 |
|
| 62 |
public DeviceStateManagerFoldingFeatureProducer(@NonNull Context context, |
| 63 |
@NonNull BaseDataProducer<String> rawFoldSupplier) { |
| 64 |
mRawFoldSupplier = rawFoldSupplier; |
| 65 |
String[] deviceStatePosturePairs = context.getResources() |
| 66 |
.getStringArray(R.array.config_device_state_postures); |
| 67 |
for (String deviceStatePosturePair : deviceStatePosturePairs) { |
| 68 |
String[] deviceStatePostureMapping = deviceStatePosturePair.split(":"); |
| 69 |
if (deviceStatePostureMapping.length != 2) { |
| 70 |
if (DEBUG) { |
| 71 |
Log.e(TAG, "Malformed device state posture pair: " |
| 72 |
+ deviceStatePosturePair); |
| 73 |
} |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
int deviceState; |
| 78 |
int posture; |
| 79 |
try { |
| 80 |
deviceState = Integer.parseInt(deviceStatePostureMapping[0]); |
| 81 |
posture = Integer.parseInt(deviceStatePostureMapping[1]); |
| 82 |
} catch (NumberFormatException e) { |
| 83 |
if (DEBUG) { |
| 84 |
Log.e(TAG, "Failed to parse device state or posture: " |
| 85 |
+ deviceStatePosturePair, |
| 86 |
e); |
| 87 |
} |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
mDeviceStateToPostureMap.put(deviceState, posture); |
| 92 |
} |
| 93 |
|
| 94 |
if (mDeviceStateToPostureMap.size() > 0) { |
| 95 |
DeviceStateCallback deviceStateCallback = (state) -> { |
| 96 |
mCurrentDeviceState = state; |
| 97 |
mRawFoldSupplier.getData(this::notifyFoldingFeatureChange); |
| 98 |
}; |
| 99 |
Objects.requireNonNull(context.getSystemService(DeviceStateManager.class)) |
| 100 |
.registerCallback(context.getMainExecutor(), deviceStateCallback); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private void runCallbackWhenValidState(@NonNull Consumer<List<CommonFoldingFeature>> callback, |
| 109 |
String displayFeaturesString) { |
| 110 |
if (isCurrentStateValid()) { |
| 111 |
callback.accept(calculateFoldingFeature(displayFeaturesString)); |
| 112 |
} else { |
| 113 |
|
| 114 |
AcceptOnceConsumer<List<CommonFoldingFeature>> singleRunCallback = |
| 115 |
new AcceptOnceConsumer<>(this, callback); |
| 116 |
addDataChangedCallback(singleRunCallback); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private boolean isCurrentStateValid() { |
| 127 |
|
| 128 |
return mDeviceStateToPostureMap.indexOfKey(mCurrentDeviceState) >= 0; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
protected void onListenersChanged( |
| 133 |
@NonNull Set<Consumer<List<CommonFoldingFeature>>> callbacks) { |
| 134 |
super.onListenersChanged(callbacks); |
| 135 |
if (callbacks.isEmpty()) { |
| 136 |
mCurrentDeviceState = INVALID_DEVICE_STATE; |
| 137 |
mRawFoldSupplier.removeDataChangedCallback(this::notifyFoldingFeatureChange); |
| 138 |
} else { |
| 139 |
mRawFoldSupplier.addDataChangedCallback(this::notifyFoldingFeatureChange); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
@NonNull |
| 144 |
@Override |
| 145 |
public Optional<List<CommonFoldingFeature>> getCurrentData() { |
| 146 |
Optional<String> displayFeaturesString = mRawFoldSupplier.getCurrentData(); |
| 147 |
if (!isCurrentStateValid()) { |
| 148 |
return Optional.empty(); |
| 149 |
} else { |
| 150 |
return displayFeaturesString.map(this::calculateFoldingFeature); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@Override |
| 159 |
public void getData(Consumer<List<CommonFoldingFeature>> storeFeaturesConsumer) { |
| 160 |
mRawFoldSupplier.getData((String displayFeaturesString) -> { |
| 161 |
if (TextUtils.isEmpty(displayFeaturesString)) { |
| 162 |
storeFeaturesConsumer.accept(new ArrayList<>()); |
| 163 |
} else { |
| 164 |
runCallbackWhenValidState(storeFeaturesConsumer, displayFeaturesString); |
| 165 |
} |
| 166 |
}); |
| 167 |
} |
| 168 |
|
| 169 |
private void notifyFoldingFeatureChange(String displayFeaturesString) { |
| 170 |
if (!isCurrentStateValid()) { |
| 171 |
return; |
| 172 |
} |
| 173 |
if (TextUtils.isEmpty(displayFeaturesString)) { |
| 174 |
notifyDataChanged(new ArrayList<>()); |
| 175 |
} else { |
| 176 |
notifyDataChanged(calculateFoldingFeature(displayFeaturesString)); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
private List<CommonFoldingFeature> calculateFoldingFeature(String displayFeaturesString) { |
| 181 |
final int globalHingeState = globalHingeState(); |
| 182 |
return parseListFromString(displayFeaturesString, globalHingeState); |
| 183 |
} |
| 184 |
|
| 185 |
private int globalHingeState() { |
| 186 |
return mDeviceStateToPostureMap.get(mCurrentDeviceState, COMMON_STATE_UNKNOWN); |
| 187 |
} |
| 188 |
} |