1 /* 2 * Copyright (C) 2023 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.car.carlauncher.pagination; 18 19 import android.view.View; 20 import android.view.ViewTreeObserver.OnGlobalLayoutListener; 21 22 import com.android.car.carlauncher.pagination.PageMeasurementHelper.GridDimensions; 23 import com.android.car.carlauncher.pagination.PageMeasurementHelper.PageDimensions; 24 25 import java.util.HashSet; 26 import java.util.Set; 27 28 /** 29 * Controller class that handling all pagination related logic. 30 */ 31 public class PaginationController { 32 private final PageMeasurementHelper mPageMeasurementHelper; 33 private final DimensionUpdateCallback mCallback; 34 PaginationController(View windowBackground, DimensionUpdateCallback callback)35 public PaginationController(View windowBackground, DimensionUpdateCallback callback) { 36 mCallback = callback; 37 mPageMeasurementHelper = new PageMeasurementHelper(windowBackground); 38 windowBackground.getViewTreeObserver().addOnGlobalLayoutListener( 39 new OnGlobalLayoutListener() { 40 @Override 41 public void onGlobalLayout() { 42 int windowHeight = windowBackground.getMeasuredHeight(); 43 int windowWidth = windowBackground.getMeasuredWidth(); 44 maybeHandleWindowResize(windowWidth, windowHeight); 45 } 46 }); 47 } 48 maybeHandleWindowResize(int windowWidth, int windowHeight)49 private void maybeHandleWindowResize(int windowWidth, int windowHeight) { 50 boolean consumed = mPageMeasurementHelper.handleWindowSizeChange(windowWidth, windowHeight); 51 if (consumed) { 52 mCallback.notifyDimensionsUpdated(mPageMeasurementHelper.getPageDimensions(), 53 mPageMeasurementHelper.getGridDimensions()); 54 } 55 } 56 57 /** 58 * Callback contract between this controller and its {@link DimensionUpdateListener} classes. 59 * 60 * When {@link PageMeasurementHelper#handleWindowSizeChange} returns {@code true}, the callback 61 * will notify its listeners that the window size has changed. 62 */ 63 public static class DimensionUpdateCallback { 64 Set<DimensionUpdateListener> mListeners = new HashSet<>(); 65 66 /** 67 * Adds an {@link DimensionUpdateListener} to 68 */ addListener(DimensionUpdateListener listener)69 public void addListener(DimensionUpdateListener listener) { 70 mListeners.add(listener); 71 } 72 73 /** 74 * Updates all listeners with the new measured dimensions. 75 */ notifyDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens)76 public void notifyDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens) { 77 for (DimensionUpdateListener listener : mListeners) { 78 listener.onDimensionsUpdated(pageDimens, gridDimens); 79 } 80 } 81 } 82 83 /** 84 * Listener interface for {@link DimensionUpdateCallback}. 85 * 86 * Classes that implement the listener should use the measured dimensions from 87 * {@link DimensionUpdateListener#onDimensionsUpdated} to update relevant layout params. 88 */ 89 public interface DimensionUpdateListener { 90 /** 91 * Updates layout params from the updated dimensions measurements in {@link PageDimensions} 92 * and {@link GridDimensions}*/ onDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens)93 void onDimensionsUpdated(PageDimensions pageDimens, GridDimensions gridDimens); 94 } 95 } 96