| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package androidx.window.sidecar; |
| 18 |
|
| 19 |
import static android.view.Display.DEFAULT_DISPLAY; |
| 20 |
|
| 21 |
import static androidx.window.util.ExtensionHelper.rotateRectToDisplayRotation; |
| 22 |
import static androidx.window.util.ExtensionHelper.transformToWindowSpaceRect; |
| 23 |
|
| 24 |
import android.app.Activity; |
| 25 |
import android.app.ActivityThread; |
| 26 |
import android.app.Application; |
| 27 |
import android.content.Context; |
| 28 |
import android.graphics.Rect; |
| 29 |
import android.os.Bundle; |
| 30 |
import android.os.IBinder; |
| 31 |
|
| 32 |
import androidx.annotation.NonNull; |
| 33 |
import androidx.window.common.CommonFoldingFeature; |
| 34 |
import androidx.window.common.DeviceStateManagerFoldingFeatureProducer; |
| 35 |
import androidx.window.common.EmptyLifecycleCallbacksAdapter; |
| 36 |
import androidx.window.common.RawFoldingFeatureProducer; |
| 37 |
import androidx.window.util.BaseDataProducer; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.Collections; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class SampleSidecarImpl extends StubSidecar { |
| 48 |
private List<CommonFoldingFeature> mStoredFeatures = new ArrayList<>(); |
| 49 |
|
| 50 |
SampleSidecarImpl(Context context) { |
| 51 |
((Application) context.getApplicationContext()) |
| 52 |
.registerActivityLifecycleCallbacks(new NotifyOnConfigurationChanged()); |
| 53 |
BaseDataProducer<String> settingsFeatureProducer = new RawFoldingFeatureProducer(context); |
| 54 |
BaseDataProducer<List<CommonFoldingFeature>> foldingFeatureProducer = |
| 55 |
new DeviceStateManagerFoldingFeatureProducer(context, |
| 56 |
settingsFeatureProducer); |
| 57 |
|
| 58 |
foldingFeatureProducer.addDataChangedCallback(this::onDisplayFeaturesChanged); |
| 59 |
} |
| 60 |
|
| 61 |
private void setStoredFeatures(List<CommonFoldingFeature> storedFeatures) { |
| 62 |
mStoredFeatures = storedFeatures; |
| 63 |
} |
| 64 |
|
| 65 |
private void onDisplayFeaturesChanged(List<CommonFoldingFeature> storedFeatures) { |
| 66 |
setStoredFeatures(storedFeatures); |
| 67 |
updateDeviceState(getDeviceState()); |
| 68 |
for (IBinder windowToken : getWindowsListeningForLayoutChanges()) { |
| 69 |
SidecarWindowLayoutInfo newLayout = getWindowLayoutInfo(windowToken); |
| 70 |
updateWindowLayout(windowToken, newLayout); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@NonNull |
| 75 |
@Override |
| 76 |
public SidecarDeviceState getDeviceState() { |
| 77 |
SidecarDeviceState deviceState = new SidecarDeviceState(); |
| 78 |
deviceState.posture = deviceStateFromFeature(); |
| 79 |
return deviceState; |
| 80 |
} |
| 81 |
|
| 82 |
private int deviceStateFromFeature() { |
| 83 |
for (int i = 0; i < mStoredFeatures.size(); i++) { |
| 84 |
CommonFoldingFeature feature = mStoredFeatures.get(i); |
| 85 |
final int state = feature.getState(); |
| 86 |
switch (state) { |
| 87 |
case CommonFoldingFeature.COMMON_STATE_FLAT: |
| 88 |
return SidecarDeviceState.POSTURE_OPENED; |
| 89 |
case CommonFoldingFeature.COMMON_STATE_HALF_OPENED: |
| 90 |
return SidecarDeviceState.POSTURE_HALF_OPENED; |
| 91 |
case CommonFoldingFeature.COMMON_STATE_UNKNOWN: |
| 92 |
return SidecarDeviceState.POSTURE_UNKNOWN; |
| 93 |
} |
| 94 |
} |
| 95 |
return SidecarDeviceState.POSTURE_UNKNOWN; |
| 96 |
} |
| 97 |
|
| 98 |
@NonNull |
| 99 |
@Override |
| 100 |
public SidecarWindowLayoutInfo getWindowLayoutInfo(@NonNull IBinder windowToken) { |
| 101 |
Activity activity = ActivityThread.currentActivityThread().getActivity(windowToken); |
| 102 |
SidecarWindowLayoutInfo windowLayoutInfo = new SidecarWindowLayoutInfo(); |
| 103 |
if (activity == null) { |
| 104 |
return windowLayoutInfo; |
| 105 |
} |
| 106 |
windowLayoutInfo.displayFeatures = getDisplayFeatures(activity); |
| 107 |
return windowLayoutInfo; |
| 108 |
} |
| 109 |
|
| 110 |
private List<SidecarDisplayFeature> getDisplayFeatures(@NonNull Activity activity) { |
| 111 |
int displayId = activity.getDisplay().getDisplayId(); |
| 112 |
if (displayId != DEFAULT_DISPLAY) { |
| 113 |
return Collections.emptyList(); |
| 114 |
} |
| 115 |
|
| 116 |
if (activity.isInMultiWindowMode()) { |
| 117 |
|
| 118 |
|
| 119 |
return Collections.emptyList(); |
| 120 |
} |
| 121 |
|
| 122 |
List<SidecarDisplayFeature> features = new ArrayList<>(); |
| 123 |
for (CommonFoldingFeature baseFeature : mStoredFeatures) { |
| 124 |
SidecarDisplayFeature feature = new SidecarDisplayFeature(); |
| 125 |
Rect featureRect = baseFeature.getRect(); |
| 126 |
rotateRectToDisplayRotation(displayId, featureRect); |
| 127 |
transformToWindowSpaceRect(activity, featureRect); |
| 128 |
feature.setRect(featureRect); |
| 129 |
feature.setType(baseFeature.getType()); |
| 130 |
features.add(feature); |
| 131 |
} |
| 132 |
return Collections.unmodifiableList(features); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
protected void onListenersChanged() { |
| 137 |
if (hasListeners()) { |
| 138 |
onDisplayFeaturesChanged(mStoredFeatures); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
private final class NotifyOnConfigurationChanged extends EmptyLifecycleCallbacksAdapter { |
| 143 |
@Override |
| 144 |
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { |
| 145 |
super.onActivityCreated(activity, savedInstanceState); |
| 146 |
onDisplayFeaturesChangedForActivity(activity); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onActivityConfigurationChanged(Activity activity) { |
| 151 |
super.onActivityConfigurationChanged(activity); |
| 152 |
onDisplayFeaturesChangedForActivity(activity); |
| 153 |
} |
| 154 |
|
| 155 |
private void onDisplayFeaturesChangedForActivity(@NonNull Activity activity) { |
| 156 |
IBinder token = activity.getWindow().getAttributes().token; |
| 157 |
if (token == null || mWindowLayoutChangeListenerTokens.contains(token)) { |
| 158 |
onDisplayFeaturesChanged(mStoredFeatures); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |