2019-09-20 11:48:34 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) Eltex ltd 2019 <eltex@eltex-co.ru>
|
|
|
|
* FocusAwareDrawerLayout.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.schabi.newpipe.views;
|
|
|
|
|
2019-09-23 08:50:51 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2019-09-20 11:48:34 +02:00
|
|
|
import android.content.Context;
|
2020-03-15 03:20:22 +01:00
|
|
|
import android.graphics.Rect;
|
2019-09-20 11:48:34 +02:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.drawerlayout.widget.DrawerLayout;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public final class FocusAwareDrawerLayout extends DrawerLayout {
|
2020-04-11 03:25:05 +02:00
|
|
|
public FocusAwareDrawerLayout(@NonNull final Context context) {
|
2019-09-20 11:48:34 +02:00
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
2020-04-11 04:03:22 +02:00
|
|
|
public FocusAwareDrawerLayout(@NonNull final Context context,
|
|
|
|
@Nullable final AttributeSet attrs) {
|
2019-09-20 11:48:34 +02:00
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
2020-04-11 04:03:22 +02:00
|
|
|
public FocusAwareDrawerLayout(@NonNull final Context context,
|
|
|
|
@Nullable final AttributeSet attrs,
|
|
|
|
final int defStyle) {
|
2019-09-20 11:48:34 +02:00
|
|
|
super(context, attrs, defStyle);
|
|
|
|
}
|
|
|
|
|
2020-03-15 03:20:22 +01:00
|
|
|
@Override
|
2020-04-11 04:03:22 +02:00
|
|
|
protected boolean onRequestFocusInDescendants(final int direction,
|
|
|
|
final Rect previouslyFocusedRect) {
|
|
|
|
// SDK implementation of this method picks whatever visible View takes the focus first
|
|
|
|
// without regard to addFocusables. If the open drawer is temporarily empty, the focus
|
|
|
|
// escapes outside of it, which can be confusing
|
2020-03-15 03:20:22 +01:00
|
|
|
|
|
|
|
boolean hasOpenPanels = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < getChildCount(); ++i) {
|
2020-08-16 10:24:58 +02:00
|
|
|
final View child = getChildAt(i);
|
2020-03-15 03:20:22 +01:00
|
|
|
|
2020-08-16 10:24:58 +02:00
|
|
|
final DrawerLayout.LayoutParams lp
|
|
|
|
= (DrawerLayout.LayoutParams) child.getLayoutParams();
|
2020-03-15 03:20:22 +01:00
|
|
|
|
|
|
|
if (lp.gravity != 0 && isDrawerVisible(child)) {
|
|
|
|
hasOpenPanels = true;
|
|
|
|
|
|
|
|
if (child.requestFocus(direction, previouslyFocusedRect)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasOpenPanels) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:48:34 +02:00
|
|
|
@Override
|
2020-04-11 04:03:22 +02:00
|
|
|
public void addFocusables(final ArrayList<View> views, final int direction,
|
|
|
|
final int focusableMode) {
|
2019-09-20 11:48:34 +02:00
|
|
|
boolean hasOpenPanels = false;
|
|
|
|
View content = null;
|
|
|
|
|
|
|
|
for (int i = 0; i < getChildCount(); ++i) {
|
2020-08-16 10:24:58 +02:00
|
|
|
final View child = getChildAt(i);
|
2019-09-20 11:48:34 +02:00
|
|
|
|
2020-08-16 10:24:58 +02:00
|
|
|
final DrawerLayout.LayoutParams lp
|
|
|
|
= (DrawerLayout.LayoutParams) child.getLayoutParams();
|
2019-09-20 11:48:34 +02:00
|
|
|
|
|
|
|
if (lp.gravity == 0) {
|
|
|
|
content = child;
|
|
|
|
} else {
|
|
|
|
if (isDrawerVisible(child)) {
|
|
|
|
hasOpenPanels = true;
|
|
|
|
child.addFocusables(views, direction, focusableMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content != null && !hasOpenPanels) {
|
|
|
|
content.addFocusables(views, direction, focusableMode);
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 08:50:51 +02:00
|
|
|
|
2020-04-11 03:25:05 +02:00
|
|
|
// this override isn't strictly necessary, but it is helpful when DrawerLayout isn't
|
|
|
|
// the topmost view in hierarchy (such as when system or builtin appcompat ActionBar is used)
|
2019-09-23 08:50:51 +02:00
|
|
|
@Override
|
|
|
|
@SuppressLint("RtlHardcoded")
|
2020-04-11 03:25:05 +02:00
|
|
|
public void openDrawer(@NonNull final View drawerView, final boolean animate) {
|
2019-09-23 08:50:51 +02:00
|
|
|
super.openDrawer(drawerView, animate);
|
|
|
|
|
2020-04-12 04:39:16 +02:00
|
|
|
drawerView.requestFocus(FOCUS_FORWARD);
|
2019-09-23 08:50:51 +02:00
|
|
|
}
|
2019-09-20 11:48:34 +02:00
|
|
|
}
|