From fe289e4133ab211b2bc14868fdf262e9f0dc3f8f Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Fri, 9 Aug 2024 07:13:38 +0700 Subject: [PATCH] feat(android): Add menu to customize longpress delay time --- .../kMAPro/src/main/AndroidManifest.xml | 5 + .../kmapro/AdjustLongpressDelayActivity.java | 150 ++++++++++++++++++ .../kmapro/KeymanSettingsFragment.java | 11 +- .../main/res/drawable/ic_action_timelapse.xml | 5 + .../preference_duration_icon_layout.xml | 5 + .../kMAPro/src/main/res/values/strings.xml | 3 + 6 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java create mode 100644 android/KMAPro/kMAPro/src/main/res/drawable/ic_action_timelapse.xml create mode 100644 android/KMAPro/kMAPro/src/main/res/layout/preference_duration_icon_layout.xml diff --git a/android/KMAPro/kMAPro/src/main/AndroidManifest.xml b/android/KMAPro/kMAPro/src/main/AndroidManifest.xml index 7196abdb22..3ba7eaf9fc 100644 --- a/android/KMAPro/kMAPro/src/main/AndroidManifest.xml +++ b/android/KMAPro/kMAPro/src/main/AndroidManifest.xml @@ -297,6 +297,11 @@ android:configChanges="orientation" android:label="@string/app_name" android:theme="@style/AppTheme.Base" /> + diff --git a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java new file mode 100644 index 0000000000..e18912abe9 --- /dev/null +++ b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/AdjustLongpressDelayActivity.java @@ -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(); + } + +} \ No newline at end of file diff --git a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/KeymanSettingsFragment.java b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/KeymanSettingsFragment.java index 72c54aa0d0..f825909bd2 100644 --- a/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/KeymanSettingsFragment.java +++ b/android/KMAPro/kMAPro/src/main/java/com/tavultesoft/kmapro/KeymanSettingsFragment.java @@ -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); diff --git a/android/KMAPro/kMAPro/src/main/res/drawable/ic_action_timelapse.xml b/android/KMAPro/kMAPro/src/main/res/drawable/ic_action_timelapse.xml new file mode 100644 index 0000000000..80dc29e33f --- /dev/null +++ b/android/KMAPro/kMAPro/src/main/res/drawable/ic_action_timelapse.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/android/KMAPro/kMAPro/src/main/res/layout/preference_duration_icon_layout.xml b/android/KMAPro/kMAPro/src/main/res/layout/preference_duration_icon_layout.xml new file mode 100644 index 0000000000..c8fd3a48f9 --- /dev/null +++ b/android/KMAPro/kMAPro/src/main/res/layout/preference_duration_icon_layout.xml @@ -0,0 +1,5 @@ + + diff --git a/android/KMAPro/kMAPro/src/main/res/values/strings.xml b/android/KMAPro/kMAPro/src/main/res/values/strings.xml index 3cec4f2558..9f3db9af11 100644 --- a/android/KMAPro/kMAPro/src/main/res/values/strings.xml +++ b/android/KMAPro/kMAPro/src/main/res/values/strings.xml @@ -113,6 +113,9 @@ Adjust keyboard height + + Adjust longpress delay + Spacebar caption