/
CustomNavigationDrawerDemoActivity.java
195 lines (169 loc) · 6.84 KB
/
CustomNavigationDrawerDemoActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.material.catalog.navigationdrawer;
import io.material.catalog.R;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.window.BackEvent;
import android.window.OnBackAnimationCallback;
import android.window.OnBackInvokedDispatcher;
import androidx.activity.BackEventCompat;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.drawerlayout.widget.DrawerLayout.LayoutParams;
import androidx.drawerlayout.widget.DrawerLayout.SimpleDrawerListener;
import com.google.android.material.motion.MaterialSideContainerBackHelper;
import com.google.android.material.navigation.DrawerLayoutUtils;
import io.material.catalog.feature.DemoActivity;
/** A fragment that displays a custom Navigation Drawer demo for the Catalog app. */
@SuppressWarnings("RestrictTo")
public class CustomNavigationDrawerDemoActivity extends DemoActivity {
private final OnBackPressedCallback drawerOnBackPressedCallback =
new OnBackPressedCallback(/* enabled= */ true) {
@Override
public void handleOnBackPressed() {
drawerLayout.closeDrawers();
}
};
@Nullable private OnBackAnimationCallback drawerOnBackAnimationCallback;
private DrawerLayout drawerLayout;
private View currentDrawerView;
private MaterialSideContainerBackHelper sideContainerBackHelper;
@NonNull
@Override
public View onCreateDemoView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {
View view =
layoutInflater.inflate(
R.layout.cat_navigationdrawer_custom, viewGroup, false /* attachToRoot */);
Toolbar toolbar = view.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = view.findViewById(R.id.drawer);
drawerLayout.addDrawerListener(
new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.cat_navigationdrawer_button_show_content_description,
R.string.cat_navigationdrawer_button_hide_content_description));
drawerLayout.addDrawerListener(
new SimpleDrawerListener() {
@Override
public void onDrawerOpened(@NonNull View drawerView) {
registerBackCallback(drawerView);
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
unregisterBackCallback();
}
});
View endDrawer = view.findViewById(R.id.custom_drawer_end);
view.findViewById(R.id.show_end_drawer_gravity)
.setOnClickListener(v -> drawerLayout.openDrawer(endDrawer));
drawerLayout.post(
() -> {
View startDrawer = view.findViewById(R.id.custom_drawer_start);
if (drawerLayout.isDrawerOpen(startDrawer)) {
registerBackCallback(startDrawer);
} else if (drawerLayout.isDrawerOpen(endDrawer)) {
registerBackCallback(endDrawer);
}
});
return view;
}
private void registerBackCallback(@NonNull View drawerView) {
currentDrawerView = drawerView;
sideContainerBackHelper = new MaterialSideContainerBackHelper(drawerView);
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (drawerOnBackAnimationCallback == null) {
drawerOnBackAnimationCallback = createOnBackAnimationCallback();
}
drawerLayout.post(
() ->
getOnBackInvokedDispatcher()
.registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_OVERLAY, drawerOnBackAnimationCallback));
} else {
getOnBackPressedDispatcher().addCallback(this, drawerOnBackPressedCallback);
}
}
private void unregisterBackCallback() {
currentDrawerView = null;
sideContainerBackHelper = null;
if (VERSION.SDK_INT >= VERSION_CODES.UPSIDE_DOWN_CAKE) {
if (drawerOnBackAnimationCallback != null) {
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(drawerOnBackAnimationCallback);
drawerOnBackAnimationCallback = null;
}
} else {
drawerOnBackPressedCallback.remove();
}
}
@Override
protected boolean shouldShowDefaultDemoActionBar() {
return false;
}
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
private OnBackAnimationCallback createOnBackAnimationCallback() {
return new OnBackAnimationCallback() {
@Override
public void onBackStarted(@NonNull BackEvent backEvent) {
sideContainerBackHelper.startBackProgress(new BackEventCompat(backEvent));
}
@Override
public void onBackProgressed(@NonNull BackEvent backEvent) {
DrawerLayout.LayoutParams drawerLayoutParams =
(LayoutParams) currentDrawerView.getLayoutParams();
sideContainerBackHelper.updateBackProgress(
new BackEventCompat(backEvent), drawerLayoutParams.gravity);
}
@Override
public void onBackInvoked() {
BackEventCompat backEvent = sideContainerBackHelper.onHandleBackInvoked();
if (backEvent == null) {
drawerLayout.closeDrawers();
return;
}
DrawerLayout.LayoutParams drawerLayoutParams =
(LayoutParams) currentDrawerView.getLayoutParams();
int gravity = drawerLayoutParams.gravity;
AnimatorListener scrimCloseAnimatorListener =
DrawerLayoutUtils.getScrimCloseAnimatorListener(drawerLayout, currentDrawerView);
AnimatorUpdateListener scrimCloseAnimatorUpdateListener =
DrawerLayoutUtils.getScrimCloseAnimatorUpdateListener(drawerLayout);
sideContainerBackHelper.finishBackProgress(
backEvent, gravity, scrimCloseAnimatorListener, scrimCloseAnimatorUpdateListener);
}
@Override
public void onBackCancelled() {
sideContainerBackHelper.cancelBackProgress();
}
};
}
}