Long-press the usage gauge key to open the usage page
This commit is contained in:
@@ -2,6 +2,7 @@ package juloo.keyboard2;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Insets;
|
||||
import android.graphics.Paint;
|
||||
@@ -11,7 +12,10 @@ import android.graphics.Rect;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.Shader;
|
||||
import android.inputmethodservice.InputMethodService;
|
||||
import android.net.Uri;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.MotionEvent;
|
||||
@@ -40,6 +44,11 @@ public class Keyboard2View extends View
|
||||
|
||||
private Pointers.Modifiers _mods;
|
||||
|
||||
/** Long-pressing the usage gauge key opens the usage page; a tap stays inert. */
|
||||
private final Handler _gaugeHandler = new Handler(Looper.getMainLooper());
|
||||
private Runnable _gaugeLongPress;
|
||||
private boolean _gaugePressed;
|
||||
|
||||
private static int _currentWhat = 0;
|
||||
|
||||
private Config _config;
|
||||
@@ -205,6 +214,7 @@ public class Keyboard2View extends View
|
||||
{
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_POINTER_UP:
|
||||
cancelGaugePress();
|
||||
_pointers.onTouchUp(event.getPointerId(event.getActionIndex()));
|
||||
break;
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
@@ -213,14 +223,23 @@ public class Keyboard2View extends View
|
||||
float tx = event.getX(p);
|
||||
float ty = event.getY(p);
|
||||
KeyboardData.Key key = getKeyAtPosition(tx, ty);
|
||||
if (key != null && !UsageGauge.isGaugeKey(key))
|
||||
if (key != null && UsageGauge.isGaugeKey(key))
|
||||
startGaugePress();
|
||||
else if (key != null)
|
||||
_pointers.onTouchDown(tx, ty, event.getPointerId(p), key);
|
||||
break;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
if (_gaugePressed)
|
||||
{
|
||||
KeyboardData.Key moved = getKeyAtPosition(event.getX(0), event.getY(0));
|
||||
if (moved == null || !UsageGauge.isGaugeKey(moved))
|
||||
cancelGaugePress(); // slid off the gauge: no longer a press on it
|
||||
}
|
||||
for (p = 0; p < event.getPointerCount(); p++)
|
||||
_pointers.onTouchMove(event.getX(p), event.getY(p), event.getPointerId(p));
|
||||
break;
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
cancelGaugePress();
|
||||
_pointers.onTouchCancel();
|
||||
break;
|
||||
default:
|
||||
@@ -387,6 +406,50 @@ public class Keyboard2View extends View
|
||||
}
|
||||
}
|
||||
|
||||
/** Holding the gauge key opens the usage page. Short taps do nothing: the key never types. */
|
||||
private void startGaugePress()
|
||||
{
|
||||
cancelGaugePress();
|
||||
_gaugePressed = true;
|
||||
_gaugeLongPress = new Runnable()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
if (!_gaugePressed)
|
||||
return;
|
||||
_gaugePressed = false;
|
||||
_gaugeLongPress = null;
|
||||
openUsagePage();
|
||||
}
|
||||
};
|
||||
// Same delay the keyboard uses for every other long press, so it feels like the rest of it.
|
||||
_gaugeHandler.postDelayed(_gaugeLongPress, _config != null ? _config.longPressTimeout : 600L);
|
||||
}
|
||||
|
||||
private void cancelGaugePress()
|
||||
{
|
||||
_gaugePressed = false;
|
||||
if (_gaugeLongPress != null)
|
||||
{
|
||||
_gaugeHandler.removeCallbacks(_gaugeLongPress);
|
||||
_gaugeLongPress = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void openUsagePage()
|
||||
{
|
||||
try
|
||||
{
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(UsageGauge.USAGE_PAGE_URL));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
getContext().startActivity(intent);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// No browser, or the launcher refused: the keyboard must never crash over a long press.
|
||||
}
|
||||
}
|
||||
|
||||
private void drawUsageGauge(Canvas canvas, float x, float y, float keyW,
|
||||
float keyH, Theme.Computed.Key tc)
|
||||
{
|
||||
|
||||
@@ -17,6 +17,8 @@ public final class UsageGauge
|
||||
// The layout stores this marker in 'indication', so it identifies the key
|
||||
// without adding a character to the generated compose table.
|
||||
public static final String KEY_MARKER = "usage_gauge";
|
||||
/** Long-pressing the gauge key opens this in the browser. */
|
||||
public static final String USAGE_PAGE_URL = "https://platform.deepseek.com/usage";
|
||||
public static final long REFRESH_AFTER_MS = 5 * 60 * 1000L;
|
||||
// The sampler publishes every 15 minutes, so one missed tick must not read as stale: three
|
||||
// do. A threshold equal to the interval makes the gauge blink to '--' on every cycle.
|
||||
|
||||
Reference in New Issue
Block a user