Skip to content

Commit

Permalink
Merge pull request #12170 from keymanapp/feat/android/longpress-delay…
Browse files Browse the repository at this point in the history
…-menu

feat(android): Add menu to specify long-press delay
  • Loading branch information
darcywong00 authored Aug 20, 2024
2 parents 5bf42c5 + 36b2a98 commit a31aded
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 3 deletions.
5 changes: 5 additions & 0 deletions android/KMAPro/kMAPro/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@
android:configChanges="orientation"
android:label="@string/app_name"
android:theme="@style/AppTheme.Base" />
<activity
android:name=".AdjustLongpressDelayActivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:theme="@style/AppTheme.Base" />

<!-- Put other WebViewActivities in a separate process so the Keyboard WebView doesn't lag.
Ref https://stackoverflow.com/questions/40650643/timed-out-waiting-on-iinputcontextcallback-with-custom-keyboard-on-android -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Keyman is copyright (C) SIL International. MIT License.
*
* (Optional description of this file)
*/
package com.tavultesoft.kmapro;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import com.keyman.engine.BaseActivity;
import com.keyman.engine.KMManager;

/**
* Settings menu for adjusting the longpress delay time. The value for the current longpress delay time
* is saved in shared preferences as an integer (milliseconds).
*/
public class AdjustLongpressDelayActivity extends BaseActivity {
private static final String TAG = "AdjustLongpressDelay";
public static final String adjustLongpressDelayKey = "AdjustLongpressDelay";
private static SharedPreferences.Editor editor = null;

// Keeps track of the adjusted longpress delay time for saving.
// Internally use milliseconds, but GUI displays seconds
private static int currentDelayTime = 500; // ms
private static int minLongpressTime = 300; // ms
private static int maxLongpressTime = 1500; // ms
private static int delayTimeIncrement = 200; // ms

/**
* Convert currentDelayTime to progress
* @return int
*/
private int delayTimeToProgress() {
return (currentDelayTime / delayTimeIncrement) - 1;
}

/**
* Convert progress to currentDelayTime
* @param progress
* @return int (milliseconds)
*/
private int progressToDelayTime(int progress) {
return (progress + 1) * delayTimeIncrement + 100;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final Context context = this;

setContentView(R.layout.activity_adjust_longpress_delay);
Toolbar toolbar = (Toolbar) findViewById(R.id.titlebar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(null);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.keyman_blue)));
}

TextView adjustLongpressDelayActivityTitle = (TextView) findViewById(R.id.bar_title);

String titleStr = getString(R.string.adjust_longpress_delay);
adjustLongpressDelayActivityTitle.setTextColor(ContextCompat.getColor(this, R.color.ms_white));
adjustLongpressDelayActivityTitle.setText(titleStr);

SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE);
editor = prefs.edit();
currentDelayTime = KMManager.getLongpressDelay(this);

TextView adjustLongpressDelayText = (TextView) findViewById(R.id.delayTimeText);
String longpressDelayText = String.format(getString(R.string.longpress_delay_time), (float)(currentDelayTime/1000.0));
adjustLongpressDelayText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
adjustLongpressDelayText.setText(longpressDelayText);

final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setProgress(delayTimeToProgress());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Do nothing
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do nothing
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
currentDelayTime = progressToDelayTime(progress);
String longpressDelayText = String.format(getString(R.string.longpress_delay_time), (float)(currentDelayTime/1000.0));
adjustLongpressDelayText.setText(longpressDelayText);

editor.putInt(KMManager.KMKey_LongpressDelay, currentDelayTime);
editor.commit();
}
});

findViewById(R.id.delayTimeDownButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentDelayTime > minLongpressTime) {
currentDelayTime -= delayTimeIncrement;
seekBar.setProgress(delayTimeToProgress());
}
}
});

findViewById(R.id.delayTimeUpButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentDelayTime < maxLongpressTime) {
currentDelayTime += delayTimeIncrement;
seekBar.setProgress(delayTimeToProgress());
}
}
});
}

@Override
public void onBackPressed() {
// Apply the adjusted longpress delay on exit
KMManager.setLongpressDelay(currentDelayTime);

super.onBackPressed();
}

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class KeymanSettingsFragment extends PreferenceFragmentCompat {
private static Context context;

private Preference languagesPreference, installKeyboardOrDictionary, displayLanguagePreference,
adjustKeyboardHeight;
adjustKeyboardHeight, adjustLongpressDelay;
private ListPreference spacebarTextPreference;
private CheckBoxPreference setSystemKeyboardPreference;
private CheckBoxPreference setDefaultKeyboardPreference;
Expand Down Expand Up @@ -99,13 +99,21 @@ public boolean onPreferenceClick(Preference preference) {
});

setDefaultKeyboardPreference.setOnPreferenceChangeListener(checkBlocker);

adjustKeyboardHeight = new Preference(context);
adjustKeyboardHeight.setKey(AdjustKeyboardHeightActivity.adjustKeyboardHeightKey);
adjustKeyboardHeight.setTitle(getString(R.string.adjust_keyboard_height));
adjustKeyboardHeight.setWidgetLayoutResource(R.layout.preference_height_icon_layout);
Intent adjustKeyboardHeightIntent = new Intent(context, AdjustKeyboardHeightActivity.class);
adjustKeyboardHeight.setIntent(adjustKeyboardHeightIntent);

adjustLongpressDelay = new Preference(context);
adjustLongpressDelay.setKey(AdjustLongpressDelayActivity.adjustLongpressDelayKey);
adjustLongpressDelay.setTitle(getString(R.string.adjust_longpress_delay));
adjustLongpressDelay.setWidgetLayoutResource(R.layout.preference_duration_icon_layout);
Intent adjustLongpressDelayIntent = new Intent(context, AdjustLongpressDelayActivity.class);
adjustLongpressDelay.setIntent(adjustLongpressDelayIntent);

/* Spacebar Caption Preference */

spacebarTextPreference = new ListPreference(context);
Expand Down Expand Up @@ -188,15 +196,14 @@ as part of the default onClick() used by SwitchPreference.
sendCrashReportPreference.setSummaryOff(getString(R.string.show_send_crash_report_off));
sendCrashReportPreference.setDefaultValue(true);



screen.addPreference(languagesPreference);
screen.addPreference(installKeyboardOrDictionary);
screen.addPreference(displayLanguagePreference);
screen.addPreference(setSystemKeyboardPreference);
screen.addPreference(setDefaultKeyboardPreference);

screen.addPreference(adjustKeyboardHeight);
screen.addPreference(adjustLongpressDelay);
screen.addPreference(spacebarTextPreference);

screen.addPreference(hapticFeedbackPreference);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M16.24,7.76C15.07,6.59 13.54,6 12,6v6l-4.24,4.24c2.34,2.34 6.14,2.34 8.49,0 2.34,-2.34 2.34,-6.14 -0.01,-8.48zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>

</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/titlebar" />

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="25dp"
android:paddingTop="60dp" >

<ImageButton
android:id="@+id/delayTimeDownButton"
style="?android:attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:contentDescription="@string/ic_delay_time_down"
android:src="@drawable/ic_light_dialog_textsize_down" />

<!-- Seekbar range 0 to 6 corresponds to longpress duration 300 ms to 1500 ms -->
<SeekBar
android:id="@+id/seekBar"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:contentDescription="@string/ic_longpress_delay_slider"
android:progress="1"
android:max="6"
android:layout_gravity="center" />

<ImageButton
android:id="@+id/delayTimeUpButton"
style="?android:attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:contentDescription="@string/ic_delay_time_up"
android:src="@drawable/ic_light_dialog_textsize_up" />

</LinearLayout>

<TextView
android:id="@+id/delayTimeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearLayout"
android:gravity="center_horizontal"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:text="@string/longpress_delay_time"
android:textSize="@dimen/longpress_delay_label_textsize"
/>

</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/preference_icon"
android:layout_height="@dimen/preference_icon"
android:src="@drawable/ic_action_timelapse" />
1 change: 1 addition & 0 deletions android/KMAPro/kMAPro/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
<dimen name="preference_icon">32dp</dimen>
<dimen name="update_indicator_size">16dp</dimen>
<dimen name="update_indicator_bg_margin">0dp</dimen>
<dimen name="longpress_delay_label_textsize">24sp</dimen>
</resources>
14 changes: 14 additions & 0 deletions android/KMAPro/kMAPro/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<!-- Context: Keyman Settings menu -->
<string name="adjust_keyboard_height" comment="Menu action to adjust keyboard height">Adjust keyboard height</string>

<!-- Context: Keyman Settings menu -->
<string name="adjust_longpress_delay" comment="Menu action to adjust longpress delay duration">Adjust longpress delay</string>

<!-- Context: Keyman Settings menu -->
<string name="spacebar_caption" comment="Menu action to set the spacebar caption">Spacebar caption</string>

Expand Down Expand Up @@ -198,6 +201,17 @@
<!-- Context: Adjust Keyboard Height menu -->
<string name="reset_to_defaults" comment="Button to reset to default heights">Reset to Defaults</string>

<!-- Context: Adjust Longpress Delay Time menu -->
<string name="longpress_delay_time" comment="Current longpress delay time (seconds)">Delay Time: %1$.1f seconds</string>

<!-- Context: Adjust Longpress Delay Time menu -->
<string name="ic_delay_time_up" comment="Make longpress delay longer">Delay time longer</string>

<!-- Context: Adjust Longpress Delay Time menu -->
<string name="ic_delay_time_down" comment="Make longpress delay shorter">Delay time shorter</string>

<!-- Context: Adjust Longpress Delay Time menu -->
<string name="ic_longpress_delay_slider" comment="Adjust longpress delay time">Longpress delay time slider</string>

<!-- Context: Keyman Web Browser -->
<string name="hint_text" comment="Prompt to type in the browser search bar">Search or type URL</string>
Expand Down
5 changes: 5 additions & 0 deletions android/KMEA/app/src/main/assets/android-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function notifyHost(event, params) {
}, 10);
}

// Update the KeymanWeb longpress delay
// delay is in milliseconds
function setLongpressDelay(delay) {
}

// Update the KMW banner height
// h is in dpi (different from iOS)
function setBannerHeight(h) {
Expand Down
28 changes: 28 additions & 0 deletions android/KMEA/app/src/main/java/com/keyman/engine/KMManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ public enum EnterModeType {
public static final String KMKey_KeyboardHeightPortrait = "keyboardHeightPortrait";
public static final String KMKey_KeyboardHeightLandscape = "keyboardHeightLandscape";

public static final String KMKey_LongpressDelay = "longpressDelay";

public static final String KMKey_CustomHelpLink = "CustomHelpLink";
public static final String KMKey_KMPLink = "kmp";
public static final String KMKey_UserKeyboardIndex = "UserKeyboardIndex";
Expand Down Expand Up @@ -2080,6 +2082,32 @@ public static int getOrientation(Context context) {
return Configuration.ORIENTATION_UNDEFINED;
}

/**
* Get the long-press delay (in milliseconds)
* @param context
* @return int - long-press delay in milliseconds
*/
public static int getLongpressDelay(Context context) {
int defaultDelay = 500; // default longpress delay in KeymanWeb (ms)
SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE);

return prefs.getInt(KMManager.KMKey_LongpressDelay, defaultDelay);
}

/**
* Update KeymanWeb with the long-press delay (in milliseconds)
* @param longpressDelay - int long-press delay in milliseconds
*/
public static void setLongpressDelay(int longpressDelay) {
if (isKeyboardLoaded(KeyboardType.KEYBOARD_TYPE_INAPP)) {
InAppKeyboard.loadJavascript(KMString.format("setLongpressDelay(%d)", longpressDelay));
}

if (SystemKeyboard != null) {
SystemKeyboard.loadJavascript(KMString.format("setLongpressDelay(%d)", longpressDelay));
}
}

public static int getBannerHeight(Context context) {
int bannerHeight = 0;
if (InAppKeyboard != null && InAppKeyboard.getBanner() != BannerType.BLANK) {
Expand Down

0 comments on commit a31aded

Please sign in to comment.