| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package androidx.window.extensions.layout; |
| 18 |
|
| 19 |
import static android.view.Display.DEFAULT_DISPLAY; |
| 20 |
|
| 21 |
import static androidx.window.common.CommonFoldingFeature.COMMON_STATE_FLAT; |
| 22 |
import static androidx.window.common.CommonFoldingFeature.COMMON_STATE_HALF_OPENED; |
| 23 |
import static androidx.window.util.ExtensionHelper.isZero; |
| 24 |
import static androidx.window.util.ExtensionHelper.rotateRectToDisplayRotation; |
| 25 |
import static androidx.window.util.ExtensionHelper.transformToWindowSpaceRect; |
| 26 |
|
| 27 |
import android.app.Activity; |
| 28 |
import android.app.ActivityClient; |
| 29 |
import android.app.Application; |
| 30 |
import android.app.WindowConfiguration; |
| 31 |
import android.content.ComponentCallbacks; |
| 32 |
import android.content.Context; |
| 33 |
import android.content.res.Configuration; |
| 34 |
import android.graphics.Rect; |
| 35 |
import android.os.Bundle; |
| 36 |
import android.os.IBinder; |
| 37 |
import android.util.ArrayMap; |
| 38 |
import android.view.WindowManager; |
| 39 |
import android.window.TaskFragmentOrganizer; |
| 40 |
|
| 41 |
import androidx.annotation.GuardedBy; |
| 42 |
import androidx.annotation.NonNull; |
| 43 |
import androidx.annotation.Nullable; |
| 44 |
import androidx.annotation.UiContext; |
| 45 |
import androidx.window.common.CommonFoldingFeature; |
| 46 |
import androidx.window.common.DeviceStateManagerFoldingFeatureProducer; |
| 47 |
import androidx.window.common.EmptyLifecycleCallbacksAdapter; |
| 48 |
import androidx.window.util.DataProducer; |
| 49 |
|
| 50 |
import java.util.ArrayList; |
| 51 |
import java.util.Collections; |
| 52 |
import java.util.List; |
| 53 |
import java.util.Map; |
| 54 |
import java.util.Objects; |
| 55 |
import java.util.Set; |
| 56 |
import java.util.function.Consumer; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public class WindowLayoutComponentImpl implements WindowLayoutComponent { |
| 67 |
private static final String TAG = "SampleExtension"; |
| 68 |
|
| 69 |
private final Object mLock = new Object(); |
| 70 |
|
| 71 |
@GuardedBy("mLock") |
| 72 |
private final Map<Context, Consumer<WindowLayoutInfo>> mWindowLayoutChangeListeners = |
| 73 |
new ArrayMap<>(); |
| 74 |
|
| 75 |
@GuardedBy("mLock") |
| 76 |
private final DataProducer<List<CommonFoldingFeature>> mFoldingFeatureProducer; |
| 77 |
|
| 78 |
@GuardedBy("mLock") |
| 79 |
private final List<CommonFoldingFeature> mLastReportedFoldingFeatures = new ArrayList<>(); |
| 80 |
|
| 81 |
@GuardedBy("mLock") |
| 82 |
private final Map<IBinder, ConfigurationChangeListener> mConfigurationChangeListeners = |
| 83 |
new ArrayMap<>(); |
| 84 |
|
| 85 |
private final TaskFragmentOrganizer mTaskFragmentOrganizer; |
| 86 |
|
| 87 |
public WindowLayoutComponentImpl(@NonNull Context context, |
| 88 |
@NonNull TaskFragmentOrganizer taskFragmentOrganizer, |
| 89 |
@NonNull DeviceStateManagerFoldingFeatureProducer foldingFeatureProducer) { |
| 90 |
((Application) context.getApplicationContext()) |
| 91 |
.registerActivityLifecycleCallbacks(new NotifyOnConfigurationChanged()); |
| 92 |
mFoldingFeatureProducer = foldingFeatureProducer; |
| 93 |
mFoldingFeatureProducer.addDataChangedCallback(this::onDisplayFeaturesChanged); |
| 94 |
mTaskFragmentOrganizer = taskFragmentOrganizer; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public void addFoldingStateChangedCallback(Consumer<List<CommonFoldingFeature>> consumer) { |
| 99 |
synchronized (mLock) { |
| 100 |
mFoldingFeatureProducer.addDataChangedCallback(consumer); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
@Override |
| 111 |
public void addWindowLayoutInfoListener(@NonNull Activity activity, |
| 112 |
@NonNull Consumer<WindowLayoutInfo> consumer) { |
| 113 |
addWindowLayoutInfoListener((Context) activity, consumer); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
@Override |
| 128 |
public void addWindowLayoutInfoListener(@NonNull @UiContext Context context, |
| 129 |
@NonNull Consumer<WindowLayoutInfo> consumer) { |
| 130 |
synchronized (mLock) { |
| 131 |
if (mWindowLayoutChangeListeners.containsKey(context) |
| 132 |
|
| 133 |
|
| 134 |
|| mWindowLayoutChangeListeners.containsValue(consumer)) { |
| 135 |
return; |
| 136 |
} |
| 137 |
if (!context.isUiContext()) { |
| 138 |
throw new IllegalArgumentException("Context must be a UI Context, which should be" |
| 139 |
+ " an Activity, WindowContext or InputMethodService"); |
| 140 |
} |
| 141 |
mFoldingFeatureProducer.getData((features) -> { |
| 142 |
WindowLayoutInfo newWindowLayout = getWindowLayoutInfo(context, features); |
| 143 |
consumer.accept(newWindowLayout); |
| 144 |
}); |
| 145 |
mWindowLayoutChangeListeners.put(context, consumer); |
| 146 |
|
| 147 |
final IBinder windowContextToken = context.getWindowContextToken(); |
| 148 |
if (windowContextToken != null) { |
| 149 |
|
| 150 |
|
| 151 |
final ConfigurationChangeListener listener = |
| 152 |
new ConfigurationChangeListener(windowContextToken); |
| 153 |
context.registerComponentCallbacks(listener); |
| 154 |
mConfigurationChangeListeners.put(windowContextToken, listener); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
@Override |
| 165 |
public void removeWindowLayoutInfoListener(@NonNull Consumer<WindowLayoutInfo> consumer) { |
| 166 |
synchronized (mLock) { |
| 167 |
for (Context context : mWindowLayoutChangeListeners.keySet()) { |
| 168 |
if (!mWindowLayoutChangeListeners.get(context).equals(consumer)) { |
| 169 |
continue; |
| 170 |
} |
| 171 |
final IBinder token = context.getWindowContextToken(); |
| 172 |
if (token != null) { |
| 173 |
context.unregisterComponentCallbacks(mConfigurationChangeListeners.get(token)); |
| 174 |
mConfigurationChangeListeners.remove(token); |
| 175 |
} |
| 176 |
break; |
| 177 |
} |
| 178 |
mWindowLayoutChangeListeners.values().remove(consumer); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
@GuardedBy("mLock") |
| 183 |
@NonNull |
| 184 |
private Set<Context> getContextsListeningForLayoutChanges() { |
| 185 |
return mWindowLayoutChangeListeners.keySet(); |
| 186 |
} |
| 187 |
|
| 188 |
@GuardedBy("mLock") |
| 189 |
private boolean isListeningForLayoutChanges(IBinder token) { |
| 190 |
for (Context context : getContextsListeningForLayoutChanges()) { |
| 191 |
if (token.equals(Context.getToken(context))) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
} |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@Nullable |
| 210 |
private Integer convertToExtensionState(int state) { |
| 211 |
if (state == COMMON_STATE_FLAT) { |
| 212 |
return FoldingFeature.STATE_FLAT; |
| 213 |
} else if (state == COMMON_STATE_HALF_OPENED) { |
| 214 |
return FoldingFeature.STATE_HALF_OPENED; |
| 215 |
} else { |
| 216 |
return null; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
private void onDisplayFeaturesChanged(List<CommonFoldingFeature> storedFeatures) { |
| 221 |
synchronized (mLock) { |
| 222 |
mLastReportedFoldingFeatures.clear(); |
| 223 |
mLastReportedFoldingFeatures.addAll(storedFeatures); |
| 224 |
for (Context context : getContextsListeningForLayoutChanges()) { |
| 225 |
|
| 226 |
|
| 227 |
Consumer<WindowLayoutInfo> layoutConsumer = mWindowLayoutChangeListeners.get( |
| 228 |
context); |
| 229 |
WindowLayoutInfo newWindowLayout = getWindowLayoutInfo(context, storedFeatures); |
| 230 |
layoutConsumer.accept(newWindowLayout); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private WindowLayoutInfo getWindowLayoutInfo(@NonNull @UiContext Context context, |
| 243 |
List<CommonFoldingFeature> storedFeatures) { |
| 244 |
List<DisplayFeature> displayFeatureList = getDisplayFeatures(context, storedFeatures); |
| 245 |
return new WindowLayoutInfo(displayFeatureList); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
@NonNull |
| 255 |
public WindowLayoutInfo getCurrentWindowLayoutInfo(int displayId, |
| 256 |
@NonNull WindowConfiguration windowConfiguration) { |
| 257 |
synchronized (mLock) { |
| 258 |
return getWindowLayoutInfo(displayId, windowConfiguration, |
| 259 |
mLastReportedFoldingFeatures); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
private WindowLayoutInfo getWindowLayoutInfo(int displayId, |
| 265 |
@NonNull WindowConfiguration windowConfiguration, |
| 266 |
List<CommonFoldingFeature> storedFeatures) { |
| 267 |
List<DisplayFeature> displayFeatureList = getDisplayFeatures(displayId, windowConfiguration, |
| 268 |
storedFeatures); |
| 269 |
return new WindowLayoutInfo(displayFeatureList); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private List<DisplayFeature> getDisplayFeatures( |
| 291 |
@NonNull @UiContext Context context, List<CommonFoldingFeature> storedFeatures) { |
| 292 |
if (!shouldReportDisplayFeatures(context)) { |
| 293 |
return Collections.emptyList(); |
| 294 |
} |
| 295 |
return getDisplayFeatures(context.getDisplayId(), |
| 296 |
context.getResources().getConfiguration().windowConfiguration, |
| 297 |
storedFeatures); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
private List<DisplayFeature> getDisplayFeatures(int displayId, |
| 302 |
@NonNull WindowConfiguration windowConfiguration, |
| 303 |
List<CommonFoldingFeature> storedFeatures) { |
| 304 |
List<DisplayFeature> features = new ArrayList<>(); |
| 305 |
if (displayId != DEFAULT_DISPLAY) { |
| 306 |
return features; |
| 307 |
} |
| 308 |
|
| 309 |
for (CommonFoldingFeature baseFeature : storedFeatures) { |
| 310 |
Integer state = convertToExtensionState(baseFeature.getState()); |
| 311 |
if (state == null) { |
| 312 |
continue; |
| 313 |
} |
| 314 |
Rect featureRect = baseFeature.getRect(); |
| 315 |
rotateRectToDisplayRotation(displayId, featureRect); |
| 316 |
transformToWindowSpaceRect(windowConfiguration, featureRect); |
| 317 |
|
| 318 |
if (!isZero(featureRect)) { |
| 319 |
|
| 320 |
features.add(new FoldingFeature(featureRect, baseFeature.getType(), state)); |
| 321 |
} |
| 322 |
} |
| 323 |
return features; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private boolean shouldReportDisplayFeatures(@NonNull @UiContext Context context) { |
| 334 |
int displayId = context.getDisplay().getDisplayId(); |
| 335 |
if (displayId != DEFAULT_DISPLAY) { |
| 336 |
|
| 337 |
return false; |
| 338 |
} |
| 339 |
final int windowingMode; |
| 340 |
IBinder activityToken = context.getActivityToken(); |
| 341 |
if (activityToken != null) { |
| 342 |
final Configuration taskConfig = ActivityClient.getInstance().getTaskConfiguration( |
| 343 |
activityToken); |
| 344 |
if (taskConfig == null) { |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
return false; |
| 349 |
} |
| 350 |
final Rect taskBounds = taskConfig.windowConfiguration.getBounds(); |
| 351 |
final WindowManager windowManager = Objects.requireNonNull( |
| 352 |
context.getSystemService(WindowManager.class)); |
| 353 |
final Rect currentBounds = windowManager.getCurrentWindowMetrics().getBounds(); |
| 354 |
final Rect maxBounds = windowManager.getMaximumWindowMetrics().getBounds(); |
| 355 |
boolean isTaskExpanded = maxBounds.equals(taskBounds); |
| 356 |
boolean isActivityExpanded = maxBounds.equals(currentBounds); |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
return isTaskExpanded && (isActivityExpanded |
| 370 |
|| mTaskFragmentOrganizer.isActivityEmbedded(activityToken)); |
| 371 |
} else { |
| 372 |
|
| 373 |
|
| 374 |
windowingMode = context.getResources().getConfiguration().windowConfiguration |
| 375 |
.getWindowingMode(); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
return !WindowConfiguration.inMultiWindowMode(windowingMode); |
| 380 |
} |
| 381 |
|
| 382 |
@GuardedBy("mLock") |
| 383 |
private void onDisplayFeaturesChangedIfListening(@NonNull IBinder token) { |
| 384 |
if (isListeningForLayoutChanges(token)) { |
| 385 |
mFoldingFeatureProducer.getData( |
| 386 |
WindowLayoutComponentImpl.this::onDisplayFeaturesChanged); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
private final class NotifyOnConfigurationChanged extends EmptyLifecycleCallbacksAdapter { |
| 391 |
@Override |
| 392 |
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { |
| 393 |
super.onActivityCreated(activity, savedInstanceState); |
| 394 |
synchronized (mLock) { |
| 395 |
onDisplayFeaturesChangedIfListening(activity.getActivityToken()); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
@Override |
| 400 |
public void onActivityConfigurationChanged(Activity activity) { |
| 401 |
super.onActivityConfigurationChanged(activity); |
| 402 |
synchronized (mLock) { |
| 403 |
onDisplayFeaturesChangedIfListening(activity.getActivityToken()); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
private final class ConfigurationChangeListener implements ComponentCallbacks { |
| 409 |
final IBinder mToken; |
| 410 |
|
| 411 |
ConfigurationChangeListener(IBinder token) { |
| 412 |
mToken = token; |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public void onConfigurationChanged(@NonNull Configuration newConfig) { |
| 417 |
synchronized (mLock) { |
| 418 |
onDisplayFeaturesChangedIfListening(mToken); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
public void onLowMemory() { |
| 424 |
} |
| 425 |
} |
| 426 |
} |