|
| 1 | +/* |
| 2 | + * Copyright 2021 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 | + * https://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 io.material.catalog.adaptive; |
| 18 | + |
| 19 | +import io.material.catalog.R; |
| 20 | + |
| 21 | +import android.content.res.Configuration; |
| 22 | +import android.os.Bundle; |
| 23 | +import android.os.Handler; |
| 24 | +import android.os.Looper; |
| 25 | +import androidx.core.util.Consumer; |
| 26 | +import android.view.LayoutInflater; |
| 27 | +import android.view.View; |
| 28 | +import android.view.ViewGroup; |
| 29 | +import androidx.annotation.NonNull; |
| 30 | +import androidx.annotation.Nullable; |
| 31 | +import androidx.drawerlayout.widget.DrawerLayout; |
| 32 | +import androidx.window.java.layout.WindowInfoRepositoryCallbackAdapter; |
| 33 | +import androidx.window.layout.DisplayFeature; |
| 34 | +import androidx.window.layout.FoldingFeature; |
| 35 | +import androidx.window.layout.WindowInfoRepository; |
| 36 | +import androidx.window.layout.WindowLayoutInfo; |
| 37 | +import com.google.android.material.bottomnavigation.BottomNavigationView; |
| 38 | +import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton; |
| 39 | +import com.google.android.material.navigation.NavigationView; |
| 40 | +import com.google.android.material.navigationrail.NavigationRailView; |
| 41 | +import io.material.catalog.feature.DemoActivity; |
| 42 | +import java.util.List; |
| 43 | +import java.util.concurrent.Executor; |
| 44 | + |
| 45 | +/** An Activity which hosts the Adaptive feed demo flow. */ |
| 46 | +public class AdaptiveFeedDemoActivity extends DemoActivity { |
| 47 | + |
| 48 | + private View container; |
| 49 | + private DrawerLayout drawerLayout; |
| 50 | + private NavigationView modalNavDrawer; |
| 51 | + private BottomNavigationView bottomNav; |
| 52 | + private NavigationRailView navRail; |
| 53 | + private NavigationView navDrawer; |
| 54 | + private ExtendedFloatingActionButton navFab; |
| 55 | + private AdaptiveFeedDemoFragment feedFragment; |
| 56 | + |
| 57 | + @Nullable private WindowInfoRepositoryCallbackAdapter windowInfoRepo; |
| 58 | + private final Consumer<WindowLayoutInfo> stateContainer = new StateContainer(); |
| 59 | + private final Handler handler = new Handler(Looper.getMainLooper()); |
| 60 | + private final Executor executor = command -> handler.post(() -> handler.post(command)); |
| 61 | + private Configuration configuration; |
| 62 | + |
| 63 | + @Nullable |
| 64 | + @Override |
| 65 | + public View onCreateDemoView( |
| 66 | + @NonNull LayoutInflater layoutInflater, |
| 67 | + @Nullable ViewGroup viewGroup, |
| 68 | + @Nullable Bundle bundle) { |
| 69 | + View view = layoutInflater.inflate(R.layout.cat_adaptive_feed_activity, viewGroup, false); |
| 70 | + container = view.findViewById(R.id.feed_activity_container); |
| 71 | + drawerLayout = view.findViewById(R.id.drawer_layout); |
| 72 | + modalNavDrawer = view.findViewById(R.id.modal_nav_drawer); |
| 73 | + windowInfoRepo = |
| 74 | + new WindowInfoRepositoryCallbackAdapter(WindowInfoRepository.getOrCreate(this)); |
| 75 | + configuration = getResources().getConfiguration(); |
| 76 | + bottomNav = view.findViewById(R.id.bottom_nav); |
| 77 | + navRail = view.findViewById(R.id.nav_rail); |
| 78 | + navDrawer = view.findViewById(R.id.nav_drawer); |
| 79 | + navFab = view.findViewById(R.id.nav_fab); |
| 80 | + return view; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void onCreate(@Nullable Bundle bundle) { |
| 85 | + super.onCreate(bundle); |
| 86 | + feedFragment = new AdaptiveFeedDemoFragment(); |
| 87 | + |
| 88 | + // Update navigation views according to screen width size. |
| 89 | + int screenWidth = configuration.screenWidthDp; |
| 90 | + AdaptiveUtils.updateNavigationViewLayout( |
| 91 | + screenWidth, |
| 92 | + drawerLayout, |
| 93 | + modalNavDrawer, |
| 94 | + /* fab= */ null, |
| 95 | + bottomNav, |
| 96 | + navRail, |
| 97 | + navDrawer, |
| 98 | + navFab); |
| 99 | + |
| 100 | + getSupportFragmentManager() |
| 101 | + .beginTransaction() |
| 102 | + .replace(R.id.fragment_container, feedFragment) |
| 103 | + .commit(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onStart() { |
| 108 | + super.onStart(); |
| 109 | + if (windowInfoRepo != null) { |
| 110 | + windowInfoRepo.addWindowLayoutInfoListener(executor, stateContainer); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void onStop() { |
| 116 | + super.onStop(); |
| 117 | + if (windowInfoRepo != null) { |
| 118 | + windowInfoRepo.removeWindowLayoutInfoListener(stateContainer); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private class StateContainer implements Consumer<WindowLayoutInfo> { |
| 123 | + |
| 124 | + public StateContainer() {} |
| 125 | + |
| 126 | + @Override |
| 127 | + public void accept(WindowLayoutInfo windowLayoutInfo) { |
| 128 | + if (feedFragment == null) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (configuration.screenWidthDp < AdaptiveUtils.MEDIUM_SCREEN_WIDTH_SIZE) { |
| 133 | + feedFragment.updateFoldPosition(0); |
| 134 | + feedFragment.setClosedLayout(); |
| 135 | + } else { |
| 136 | + List<DisplayFeature> displayFeatures = windowLayoutInfo.getDisplayFeatures(); |
| 137 | + boolean isClosed = true; |
| 138 | + |
| 139 | + for (DisplayFeature displayFeature : displayFeatures) { |
| 140 | + if (displayFeature instanceof FoldingFeature) { |
| 141 | + FoldingFeature foldingFeature = (FoldingFeature) displayFeature; |
| 142 | + if (foldingFeature.getState().equals(FoldingFeature.State.HALF_OPENED) |
| 143 | + || foldingFeature.getState().equals(FoldingFeature.State.FLAT)) { |
| 144 | + feedFragment.setOpenLayout(); |
| 145 | + if (foldingFeature.getOrientation().equals(FoldingFeature.Orientation.VERTICAL)) { |
| 146 | + // Device is open and fold is vertical. |
| 147 | + feedFragment.updateFoldPosition( |
| 148 | + AdaptiveUtils.getFoldPosition(container, foldingFeature)); |
| 149 | + } else { |
| 150 | + // Device is open and fold is horizontal. |
| 151 | + feedFragment.updateFoldPosition(container.getWidth() / 2); |
| 152 | + } |
| 153 | + isClosed = false; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + if (isClosed) { |
| 158 | + if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) { |
| 159 | + // Device is closed or not foldable and in portrait. |
| 160 | + feedFragment.updateFoldPosition(0); |
| 161 | + feedFragment.setClosedLayout(); |
| 162 | + } else { |
| 163 | + // Device is closed or not foldable and in landscape. |
| 164 | + feedFragment.setOpenLayout(); |
| 165 | + feedFragment.updateFoldPosition(container.getWidth() / 2); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + protected boolean shouldShowDefaultDemoActionBar() { |
| 174 | + return false; |
| 175 | + } |
| 176 | +} |
0 commit comments