FairEmail/app/src/main/java/eu/faircode/email/DrawerLayoutEx.java

115 lines
4.1 KiB
Java
Raw Normal View History

2020-01-02 09:37:08 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail 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.
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 by Marcel Bokhorst (M66B)
2020-01-02 09:37:08 +00:00
*/
import android.content.Context;
2020-01-03 16:33:17 +00:00
import android.content.SharedPreferences;
2020-01-02 09:37:08 +00:00
import android.content.res.Configuration;
2020-01-03 16:33:17 +00:00
import android.graphics.Color;
2020-01-03 17:17:57 +00:00
import android.graphics.Rect;
2020-01-02 09:37:08 +00:00
import android.util.AttributeSet;
2020-01-02 12:55:37 +00:00
import android.view.Gravity;
2020-01-02 09:37:08 +00:00
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2020-01-03 16:33:17 +00:00
import androidx.appcompat.app.ActionBarDrawerToggle;
2020-01-02 09:37:08 +00:00
import androidx.drawerlayout.widget.DrawerLayout;
2020-01-03 16:33:17 +00:00
import androidx.preference.PreferenceManager;
2020-01-02 09:37:08 +00:00
public class DrawerLayoutEx extends DrawerLayout {
public DrawerLayoutEx(@NonNull Context context) {
super(context);
}
public DrawerLayoutEx(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public DrawerLayoutEx(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
2020-01-03 16:33:17 +00:00
void setup(Configuration config, View drawerContainer, ActionBarDrawerToggle drawerToggle) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean portrait3 = prefs.getBoolean("portrait3", false);
2021-02-13 06:54:52 +00:00
boolean landscape3 = prefs.getBoolean("landscape3", true);
2020-01-03 16:33:17 +00:00
2021-08-10 07:23:05 +00:00
if (((portrait3 && config.orientation == Configuration.ORIENTATION_PORTRAIT) ||
(landscape3 && config.orientation == Configuration.ORIENTATION_LANDSCAPE))) {
2020-01-03 16:33:17 +00:00
setScrimColor(Color.TRANSPARENT);
openDrawer(drawerContainer, false);
drawerToggle.onDrawerOpened(drawerContainer);
} else {
setScrimColor(Helper.resolveColor(getContext(), R.attr.colorDrawerScrim));
2020-01-03 10:32:50 +00:00
closeDrawer(drawerContainer, false);
2020-01-03 16:33:17 +00:00
drawerToggle.onDrawerClosed(drawerContainer);
2020-01-02 09:37:08 +00:00
}
2020-01-02 12:55:37 +00:00
}
2020-01-03 10:32:50 +00:00
public boolean isLocked(View view) {
return (getDrawerLockMode(view) != LOCK_MODE_UNLOCKED);
2020-01-02 21:07:49 +00:00
}
public boolean isLocked() {
return (getDrawerLockMode(Gravity.LEFT) == LOCK_MODE_LOCKED_OPEN ||
2020-01-02 12:55:37 +00:00
getDrawerLockMode(Gravity.RIGHT) == LOCK_MODE_LOCKED_OPEN);
2020-01-02 09:37:08 +00:00
}
@Override
public boolean onInterceptTouchEvent(final MotionEvent ev) {
2020-01-03 17:17:57 +00:00
if (isLocked()) {
Rect rect = new Rect();
getChildAt(1).getHitRect(rect);
if (!rect.contains((int) ev.getX(), (int) ev.getY()))
return false;
}
2020-01-04 15:46:06 +00:00
try {
return super.onInterceptTouchEvent(ev);
} catch (Throwable ex) {
Log.w(ex);
/*
java.lang.NullPointerException: Attempt to get length of null array
java.lang.NullPointerException: Attempt to get length of null array
at androidx.customview.widget.ViewDragHelper.checkTouchSlop(SourceFile:1334)
at androidx.drawerlayout.widget.DrawerLayout.onInterceptTouchEvent(SourceFile:1512)
*/
return false;
}
2020-01-02 09:37:08 +00:00
}
2020-01-03 18:23:17 +00:00
@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
if (isLocked()) {
View content = getChildAt(0);
Rect rect = new Rect();
content.getHitRect(rect);
rect.left += content.getPaddingLeft();
rect.right -= content.getPaddingRight();
if (rect.contains((int) ev.getX(), (int) ev.getY()))
return content.dispatchGenericMotionEvent(ev);
}
2020-01-04 15:46:06 +00:00
2020-01-03 18:23:17 +00:00
return super.dispatchGenericMotionEvent(ev);
}
2020-01-02 09:37:08 +00:00
}