mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 10:45:23 +08:00
Changed: Add general compatibility fixes for minSdkVerion
21
This commit is contained in:
@@ -1245,6 +1245,7 @@ public final class TerminalView extends View {
|
||||
* Define functions required for long hold toolbar.
|
||||
*/
|
||||
private final Runnable mShowFloatingToolbar = new Runnable() {
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
@Override
|
||||
public void run() {
|
||||
if (getTextSelectionActionMode() != null) {
|
||||
@@ -1253,6 +1254,7 @@ public final class TerminalView extends View {
|
||||
}
|
||||
};
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
private void showFloatingToolbar() {
|
||||
if (getTextSelectionActionMode() != null) {
|
||||
int delay = ViewConfiguration.getDoubleTapTimeout();
|
||||
@@ -1260,6 +1262,7 @@ public final class TerminalView extends View {
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
void hideFloatingToolbar() {
|
||||
if (getTextSelectionActionMode() != null) {
|
||||
removeCallbacks(mShowFloatingToolbar);
|
||||
@@ -1268,7 +1271,7 @@ public final class TerminalView extends View {
|
||||
}
|
||||
|
||||
public void updateFloatingToolbarVisibility(MotionEvent event) {
|
||||
if (getTextSelectionActionMode() != null) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && getTextSelectionActionMode() != null) {
|
||||
switch (event.getActionMasked()) {
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
hideFloatingToolbar();
|
||||
|
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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
|
||||
*
|
||||
* http://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 com.termux.view.support;
|
||||
|
||||
import android.util.Log;
|
||||
import android.widget.PopupWindow;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Implementation of PopupWindow compatibility that can call Gingerbread APIs.
|
||||
* https://chromium.googlesource.com/android_tools/+/HEAD/sdk/extras/android/support/v4/src/gingerbread/android/support/v4/widget/PopupWindowCompatGingerbread.java
|
||||
*/
|
||||
public class PopupWindowCompatGingerbread {
|
||||
|
||||
private static Method sSetWindowLayoutTypeMethod;
|
||||
private static boolean sSetWindowLayoutTypeMethodAttempted;
|
||||
private static Method sGetWindowLayoutTypeMethod;
|
||||
private static boolean sGetWindowLayoutTypeMethodAttempted;
|
||||
|
||||
public static void setWindowLayoutType(PopupWindow popupWindow, int layoutType) {
|
||||
if (!sSetWindowLayoutTypeMethodAttempted) {
|
||||
try {
|
||||
sSetWindowLayoutTypeMethod = PopupWindow.class.getDeclaredMethod(
|
||||
"setWindowLayoutType", int.class);
|
||||
sSetWindowLayoutTypeMethod.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
// Reflection method fetch failed. Oh well.
|
||||
}
|
||||
sSetWindowLayoutTypeMethodAttempted = true;
|
||||
}
|
||||
if (sSetWindowLayoutTypeMethod != null) {
|
||||
try {
|
||||
sSetWindowLayoutTypeMethod.invoke(popupWindow, layoutType);
|
||||
} catch (Exception e) {
|
||||
// Reflection call failed. Oh well.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int getWindowLayoutType(PopupWindow popupWindow) {
|
||||
if (!sGetWindowLayoutTypeMethodAttempted) {
|
||||
try {
|
||||
sGetWindowLayoutTypeMethod = PopupWindow.class.getDeclaredMethod(
|
||||
"getWindowLayoutType");
|
||||
sGetWindowLayoutTypeMethod.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
// Reflection method fetch failed. Oh well.
|
||||
}
|
||||
sGetWindowLayoutTypeMethodAttempted = true;
|
||||
}
|
||||
if (sGetWindowLayoutTypeMethod != null) {
|
||||
try {
|
||||
return (Integer) sGetWindowLayoutTypeMethod.invoke(popupWindow);
|
||||
} catch (Exception e) {
|
||||
// Reflection call failed. Oh well.
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
@@ -3,6 +3,7 @@ package com.termux.view.textselection;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.view.ActionMode;
|
||||
import android.view.Menu;
|
||||
@@ -153,6 +154,12 @@ public class TextSelectionCursorController implements CursorController {
|
||||
|
||||
};
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||
mActionMode = terminalView.startActionMode(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
//noinspection NewApi
|
||||
mActionMode = terminalView.startActionMode(new ActionMode.Callback2() {
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
|
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.os.SystemClock;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
@@ -14,6 +15,7 @@ import android.widget.PopupWindow;
|
||||
|
||||
import com.termux.view.R;
|
||||
import com.termux.view.TerminalView;
|
||||
import com.termux.view.support.PopupWindowCompatGingerbread;
|
||||
|
||||
@SuppressLint("ViewConstructor")
|
||||
public class TextSelectionHandleView extends View {
|
||||
@@ -68,13 +70,18 @@ public class TextSelectionHandleView extends View {
|
||||
android.R.attr.textSelectHandleWindowStyle);
|
||||
mHandle.setSplitTouchEnabled(true);
|
||||
mHandle.setClippingEnabled(false);
|
||||
mHandle.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
|
||||
mHandle.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
mHandle.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
mHandle.setBackgroundDrawable(null);
|
||||
mHandle.setAnimationStyle(0);
|
||||
mHandle.setEnterTransition(null);
|
||||
mHandle.setExitTransition(null);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
mHandle.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
|
||||
mHandle.setEnterTransition(null);
|
||||
mHandle.setExitTransition(null);
|
||||
} else {
|
||||
PopupWindowCompatGingerbread.setWindowLayoutType(mHandle, WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
|
||||
}
|
||||
mHandle.setContentView(this);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user