Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f31e0b7
feat(android): add keyboard show/hide event
m1ga Jan 21, 2023
64b4ece
docs
m1ga Jan 21, 2023
a2ee315
use Android version
m1ga Jan 21, 2023
0a9c121
move to app event
m1ga Jan 23, 2023
21dab2a
move to app event
m1ga Jan 23, 2023
89fe70f
size return value
m1ga Jan 23, 2023
88d11b9
check for listener
m1ga Jan 24, 2023
c161d9f
remove extra check, simplify if statement
m1ga Jan 24, 2023
dc213e3
some more null checks for safety
m1ga Jan 24, 2023
ea52729
lint
m1ga Jan 24, 2023
fec7cc7
hasListener check
m1ga Jan 24, 2023
8e75df1
check app listener in safearea
m1ga Feb 6, 2023
e3987fd
Merge branch 'master' into 230121_keyboard_hide_event
m1ga Dec 25, 2023
1c19345
Update apidoc/Titanium/App/App.yml
m1ga Dec 25, 2023
a816eed
Update apidoc/Titanium/App/App.yml
m1ga Dec 25, 2023
3a344c3
Merge branch 'master' into 230121_keyboard_hide_event
m1ga Jun 10, 2024
228a6b6
Merge branch 'master' into 230121_keyboard_hide_event
m1ga Sep 14, 2024
f02e54e
Merge branch 'master' of https://github.com/tidev/titanium-sdk into 2…
m1ga Sep 14, 2024
52a3bb2
Merge branch '230121_keyboard_hide_event' of https://github.com/tidev…
m1ga Sep 14, 2024
2e58044
update docs
m1ga Sep 14, 2024
ada538a
Merge branch 'master' into 230121_keyboard_hide_event
m1ga Oct 24, 2024
b328c80
Merge branch 'master' into 230121_keyboard_hide_event
m1ga Apr 6, 2025
5bca43b
Update apidoc/Titanium/App/App.yml
m1ga Apr 29, 2025
1366587
Merge branch 'master' into 230121_keyboard_hide_event
m1ga Apr 29, 2025
979027b
fixes
m1ga Apr 29, 2025
ac0254d
Update apidoc/Titanium/App/App.yml
m1ga Apr 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.ITiAppInfo;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiSensorHelper;
Expand Down Expand Up @@ -155,6 +156,16 @@ public boolean getAccessibilityEnabled()
return TiApplication.getInstance().getAccessibilityManager().isEnabled();
}

@Kroll.getProperty
public boolean getKeyboardVisible()
{
TiBaseActivity activity = (TiBaseActivity) TiApplication.getAppCurrentActivity();
if (activity == null) {
return false;
}
return TiConvert.toBoolean(activity.keyboardVisible, false);
}

@Kroll.method(name = "_restart")
public void restart()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,21 @@ public void removeAppEventProxy(KrollProxy appEventProxy)
appEventProxies.remove(appEventProxy);
}

public boolean hasListener(String eventName)
{
for (WeakReference<KrollProxy> weakProxy : appEventProxies) {
KrollProxy appEventProxy = weakProxy.get();
if (appEventProxy == null) {
continue;
}
if (appEventProxy.hasListeners(eventName)) {
return true;
}

}
return false;
}

public boolean fireAppEvent(String eventName, KrollDict data)
{
boolean handled = false;
Expand All @@ -696,7 +711,6 @@ public boolean fireAppEvent(String eventName, KrollDict data)
if (appEventProxy == null) {
continue;
}

boolean proxyHandled = appEventProxy.fireEvent(eventName, data);
handled = handled || proxyHandled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Insets;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Build;
Expand Down Expand Up @@ -105,6 +106,7 @@ public abstract class TiBaseActivity extends AppCompatActivity implements TiActi
private TiActionBarStyleHandler actionBarStyleHandler;
private TiActivitySafeAreaMonitor safeAreaMonitor;
private Context baseContext;
public boolean keyboardVisible = false;
/**
* Callback to be invoked when the TiBaseActivity.onRequestPermissionsResult() has been called,
* providing the results of a requestPermissions() call. Instances of this interface are to
Expand Down Expand Up @@ -671,7 +673,11 @@ protected void onCreate(Bundle savedInstanceState)
Log.d(TAG, "Activity " + this + " onCreate", Log.DEBUG_MODE);
this.inForeground = true;
this.launchIntent = getIntent();
this.safeAreaMonitor = new TiActivitySafeAreaMonitor(this);

TiApplication tiApp = getTiApp();
TiApplication.addToActivityStack(this);

this.safeAreaMonitor = new TiActivitySafeAreaMonitor(this, tiApp);

// Fetch the current UI mode flags. Used to determine light/dark theme being used.
Configuration config = getResources().getConfiguration();
Expand All @@ -692,9 +698,6 @@ protected void onCreate(Bundle savedInstanceState)
}
}

TiApplication tiApp = getTiApp();
TiApplication.addToActivityStack(this);

// Increment the Titanium activity reference count. To be decremented in onDestroy() method.
// Titanium's JavaScript runtime is created when we have at least 1 activity and destroyed when we have 0.
KrollRuntime.incrementActivityRefCount();
Expand Down Expand Up @@ -784,6 +787,35 @@ public void onChanged(TiActivitySafeAreaMonitor monitor)
windowProxy.fireSafeAreaChangedEvent();
}
}

@Override
public void onKeyboardChanged(boolean isVisible, int width, int height, Insets keyboardSize)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || tiApp == null) return;

if (isVisible != keyboardVisible && tiApp.hasListener(TiC.EVENT_KEYBOARD_FRAME_CHANGED)) {
KrollDict kdX = new KrollDict();
kdX.put("left", keyboardSize.left);
kdX.put("right", keyboardSize.right);

KrollDict kdY = new KrollDict();
kdX.put("top", keyboardSize.top);
kdX.put("bottom", keyboardSize.bottom);

KrollDict kdFrame = new KrollDict();
kdFrame.put("x", kdX);
kdFrame.put("y", kdY);
kdFrame.put("height", keyboardSize.bottom);
kdFrame.put("width", width - keyboardSize.left - keyboardSize.right);

KrollDict kdAll = new KrollDict();
kdAll.put("keyboardFrame", kdFrame);
kdAll.put("animationDuration", 0);

tiApp.fireAppEvent(TiC.EVENT_KEYBOARD_FRAME_CHANGED, kdAll);
keyboardVisible = isVisible;
}
}
});
this.safeAreaMonitor.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class TiC
public static final String EVENT_SELECTED = "selected";
public static final String EVENT_UNSELECTED = "unselected";
public static final String EVENT_KEY_PRESSED = "keypressed";
public static final String EVENT_KEYBOARD_FRAME_CHANGED = "keyboardframechanged";
public static final String EVENT_HEADING = "heading";
public static final String EVENT_ITEM_CLICK = "itemclick";
public static final String EVENT_ITEMS_SELECTED = "itemsselected";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

package org.appcelerator.titanium.view;

import android.graphics.Insets;
import android.graphics.Rect;
import android.os.Build;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.view.Window;
import android.view.WindowInsets;

import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;

import java.util.ArrayList;

/** Tracks safe-area inset changes for a given activity. */
Expand All @@ -27,6 +33,7 @@ public class TiActivitySafeAreaMonitor
*/
public interface OnChangedListener {
void onChanged(TiActivitySafeAreaMonitor monitor);
void onKeyboardChanged(boolean keyboardVisible, int width, int height, Insets keyboardSize);
}

/** The activity to be monitored. */
Expand Down Expand Up @@ -72,7 +79,7 @@ public interface OnChangedListener {
* Creates an object used to track safe-area region changes for the given activity.
* @param activity The activity to be monitored. Cannot be null.
*/
public TiActivitySafeAreaMonitor(AppCompatActivity activity)
public TiActivitySafeAreaMonitor(AppCompatActivity activity, TiApplication app)
{
// Validate.
if (activity == null) {
Expand Down Expand Up @@ -105,6 +112,19 @@ public void onLayoutChange(View view, int left, int top, int right, int bottom,
@Override
public WindowInsets onApplyWindowInsets(View view, WindowInsets insets)
{
TiApplication tiApp = TiApplication.getInstance();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
&& tiApp != null
&& tiApp.hasListener(TiC.EVENT_KEYBOARD_FRAME_CHANGED)
) {
boolean keyboardVisible = insets.isVisible(WindowInsets.Type.ime());
Insets keyboardSize = insets.getInsets(WindowInsets.Type.ime());
if (changeListener != null && view != null) {
changeListener.onKeyboardChanged(keyboardVisible, view.getWidth(),
view.getHeight(), keyboardSize);
}
}
// Validate.
if (view == null) {
return insets;
Expand Down
14 changes: 10 additions & 4 deletions apidoc/Titanium/App/App.yml
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,17 @@ events:
summary: Fired when the soft keyboard is presented, on and off the screen.
description: |
This event fires when the application presents the soft keyboard on/off the screen . The
event returns the dictionary `keyboardFrame` containing `x`, `y`, `height` and `width` keys,
event on iOS returns the dictionary `keyboardFrame` containing `x`, `y`, `height` and `width` keys,
corresponding to the frame of the keyboard with respect to the screen coordinates.

On Titanium SDK 4.0.0 and later the event also contains a second parameter `animationDuration` representing
the duration of the animation for the presentation and dismissal of the soft keyboard.

Note that the keyboard `height` and `width` properties will not be accurate when the keyboard
is being dissmissed.

For Android it will only work using Android 11 and up. The return values are empty but you can use
`keyboardVisible` to check if the keyboard is visible or not.
properties:
- name: keyboardFrame
summary: A dictionary with keys x, y, width and height representing the frame of keyboard on screen.
Expand All @@ -394,8 +397,8 @@ events:
- name: animationDuration
summary: The duration of the keyboard animation. This parameter is only available on Titanium SDK 4.0.0 and later.
type: Number
platforms: [iphone, ipad, macos]
since: {iphone: "3.0.0", ipad: "3.0.0", macos: "9.2.0"}
platforms: [android, iphone, ipad, macos]
since: {android: "12.7.0", iphone: "3.0.0", ipad: "3.0.0", macos: "9.2.0"}

- name: significanttimechange
summary: Fired when there is a significant change in the time.
Expand Down Expand Up @@ -609,9 +612,12 @@ properties:

- name: keyboardVisible
summary: Indicates whether or not the soft keyboard is visible.
description: |
On Android it will only work using Android 11 and up. Otherwise it will always be false.
type: Boolean
permission: read-only
platforms: [iphone, ipad, macos]
since: {android: "12.7.0", iphone: "7.5.0", ipad: "7.5.0", macos: "9.2.0"}
platforms: [android, iphone, ipad, macos]

- name: trackUserInteraction
summary: Indicates whether or not the user interaction shoud be tracked.
Expand Down
Loading