Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -19,7 +19,9 @@
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;

@Kroll.module(parentModule = AppModule.class)
public class AndroidModule extends KrollModule
Expand Down Expand Up @@ -70,6 +72,17 @@ public int getAppVersionCode()
return appVersionCode;
}

@Kroll.getProperty
public boolean getIsFoldable()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return TiApplication.getInstance().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_SENSOR_HINGE_ANGLE);
} else {
return false;
}
}

@Kroll.getProperty
public IntentProxy getLaunchIntent()
{
Expand Down
1 change: 1 addition & 0 deletions android/titanium/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ dependencies {
implementation 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
implementation 'androidx.viewpager:viewpager:1.0.0'
implementation 'androidx.annotation:annotation:1.7.1'
implementation 'androidx.window:window-java:1.3.0'

// Google's "Material Components" themed UI library.
implementation "com.google.android.material:material:${project.ext.tiMaterialLibVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.appcelerator.kroll.KrollDict;
Expand Down Expand Up @@ -48,6 +49,7 @@

import android.app.Activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
Expand All @@ -69,6 +71,12 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.core.util.Consumer;
import androidx.window.java.layout.WindowInfoTrackerCallbackAdapter;
import androidx.window.layout.DisplayFeature;
import androidx.window.layout.FoldingFeature;
import androidx.window.layout.WindowInfoTracker;
import androidx.window.layout.WindowLayoutInfo;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
Expand Down Expand Up @@ -107,6 +115,10 @@ public abstract class TiBaseActivity extends AppCompatActivity implements TiActi
private TiActivitySafeAreaMonitor safeAreaMonitor;
private Context baseContext;
public boolean keyboardVisible = false;
private static boolean hasWindowLayoutInfoListener = false;
private WindowInfoTrackerCallbackAdapter windowInfoTracker;
private final LayoutStateChangeCallback layoutStateChangeCallback =
new LayoutStateChangeCallback();
/**
* 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 @@ -867,6 +879,8 @@ public void onKeyboardChanged(boolean isVisible, int width, int height, Insets k
}
}
setCustomActionBar();

windowInfoTracker = new WindowInfoTrackerCallbackAdapter(WindowInfoTracker.getOrCreate(this));
}

private void setCustomActionBar()
Expand Down Expand Up @@ -1561,6 +1575,11 @@ protected void onStart()
}

applyNightMode();
if (!hasWindowLayoutInfoListener) {
windowInfoTracker.addWindowLayoutInfoListener(
TiApplication.getAppRootOrCurrentActivity(), Runnable::run, layoutStateChangeCallback);
hasWindowLayoutInfoListener = true;
}
}

@Override
Expand All @@ -1587,6 +1606,11 @@ protected void onStop()
}
}
}

if (hasWindowLayoutInfoListener) {
windowInfoTracker.removeWindowLayoutInfoListener(layoutStateChangeCallback);
hasWindowLayoutInfoListener = false;
}
}

@Override
Expand Down Expand Up @@ -1957,4 +1981,39 @@ public Context getInitialBaseContext()
{
return baseContext;
}

@RequiresApi(api = Build.VERSION_CODES.N)
class LayoutStateChangeCallback implements Consumer<WindowLayoutInfo>
{
@Override
public void accept(WindowLayoutInfo newLayoutInfo)
{
TiBaseActivity.this.runOnUiThread(() -> {
List<DisplayFeature> displayFeatures = newLayoutInfo.getDisplayFeatures();
if (displayFeatures.toArray().length == 0) {
// closed
KrollDict kd = new KrollDict();
String orientation = TiApplication.getInstance().getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE ? "HORIZONTAL" : "VERTICAL";
kd.put("state", "CLOSED");
kd.put("occlusionType", "NONE");
kd.put("orientation", orientation);
kd.put("isSeparating", false);
TiApplication.getInstance().fireAppEvent("windowState", kd);
} else {
// other states
for (DisplayFeature feature : displayFeatures) {
if (feature instanceof FoldingFeature foldingFeature) {
KrollDict kd = new KrollDict();
kd.put("state", foldingFeature.getState().toString());
kd.put("occlusionType", foldingFeature.getOcclusionType().toString());
kd.put("orientation", foldingFeature.getOrientation().toString());
kd.put("isSeparating", foldingFeature.isSeparating());
TiApplication.getInstance().fireAppEvent("windowState", kd);
}
}
}
});
}
}
}
Loading