feat(android): Add menu to customize longpress delay time

This commit is contained in:
Darcy Wong 2024-08-09 07:13:38 +07:00
parent 82db2389a8
commit fe289e4133
6 changed files with 178 additions and 1 deletions

View file

@ -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 -->

View file

@ -0,0 +1,150 @@
/**
* Copyright (C) SIL International. All rights reserved.
*/
package com.tavultesoft.kmapro;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
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.
*/
public class AdjustLongpressDelayActivity extends BaseActivity {
private static final String TAG = "AdjustLongpressDelay";
public static final String adjustLongpressDelayKey = "AdjustLongpressDelay";
private static Button resetButton = null;
private static ImageView sampleKeyboard = null;
// Keeps track of the adjusted keyboard height for saving
private static SharedPreferences.Editor editor = null;
private static int currentHeight = 0;
private static ViewGroup.LayoutParams layoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = this;
setContentView(R.layout.activity_adjust_keyboard_height);
Toolbar toolbar = (Toolbar) findViewById(R.id.titlebar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(null);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.keyman_blue)));
}
TextView adjustKeyboardHeightActivityTitle = (TextView) findViewById(R.id.bar_title);
adjustKeyboardHeightActivityTitle.setWidth((int) getResources().getDimension(R.dimen.package_label_width));
String titleStr = getString(R.string.adjust_keyboard_height);
adjustKeyboardHeightActivityTitle.setTextColor(ContextCompat.getColor(this, R.color.ms_white));
adjustKeyboardHeightActivityTitle.setText(titleStr);
SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.kma_prefs_name), Context.MODE_PRIVATE);
editor = prefs.edit();
sampleKeyboard = (ImageView) findViewById(R.id.sample_keyboard);
layoutParams = sampleKeyboard.getLayoutParams();
currentHeight = KMManager.getKeyboardHeight(context);
refreshSampleKeyboard(context);
resetButton = (Button) findViewById(R.id.reset_to_defaults);
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Clear the keyboard height preferences to reset
editor.remove(KMManager.KMKey_KeyboardHeightPortrait);
editor.remove(KMManager.KMKey_KeyboardHeightLandscape);
editor.commit();
// Restore default height
currentHeight = KMManager.getKeyboardHeight(context);
refreshSampleKeyboard(context);
}
});
sampleKeyboard.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
int y = (int)event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
// Update currentHeight as the user drags (moves)
// Increasing the keyboard height is a negative y
currentHeight -= y;
// Apply lower and upper bounds on currentHeight
int defaultHeight = (int) context.getResources().getDimension(R.dimen.keyboard_height);
currentHeight = Math.max(defaultHeight/2, currentHeight);
currentHeight = Math.min(defaultHeight*2, currentHeight);
refreshSampleKeyboard(context);
break;
case MotionEvent.ACTION_UP:
// Save the currentHeight when the user releases
int orientation = KMManager.getOrientation(context);
String keyboardHeightKey = (orientation == Configuration.ORIENTATION_LANDSCAPE) ?
KMManager.KMKey_KeyboardHeightLandscape : KMManager.KMKey_KeyboardHeightPortrait;
editor.putInt(keyboardHeightKey, currentHeight);
editor.commit();
break;
}
return true;
}
});
}
/**
* Refresh the layout for the sample keyboard
* @param context
*/
private void refreshSampleKeyboard(Context context) {
layoutParams.height = currentHeight;
sampleKeyboard.setLayoutParams(layoutParams);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
layoutParams = sampleKeyboard.getLayoutParams();
// When the user rotates the device, restore currentHeight
currentHeight = KMManager.getKeyboardHeight(this);
refreshSampleKeyboard(this);
}
@Override
public void onBackPressed() {
// Apply the adjusted height on exit
KMManager.applyKeyboardHeight(this, currentHeight);
super.onBackPressed();
}
}

View file

@ -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;
@ -99,6 +99,7 @@ public class KeymanSettingsFragment extends PreferenceFragmentCompat {
});
setDefaultKeyboardPreference.setOnPreferenceChangeListener(checkBlocker);
adjustKeyboardHeight = new Preference(context);
adjustKeyboardHeight.setKey(AdjustKeyboardHeightActivity.adjustKeyboardHeightKey);
adjustKeyboardHeight.setTitle(getString(R.string.adjust_keyboard_height));
@ -106,6 +107,13 @@ public class KeymanSettingsFragment extends PreferenceFragmentCompat {
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);
@ -197,6 +205,7 @@ public class KeymanSettingsFragment extends PreferenceFragmentCompat {
screen.addPreference(setDefaultKeyboardPreference);
screen.addPreference(adjustKeyboardHeight);
screen.addPreference(adjustLongpressDelay);
screen.addPreference(spacebarTextPreference);
screen.addPreference(hapticFeedbackPreference);

View file

@ -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>

View file

@ -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" />

View file

@ -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>