Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds immersive mode for Samsung Dex #3908

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -46,6 +46,7 @@
android:label="@string/application_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:resizeableActivity="true"
android:supportsRtl="false"
android:theme="@style/Theme.TermuxApp.DayNight.DarkActionBar"
tools:targetApi="m">
Expand Down Expand Up @@ -73,6 +74,14 @@
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

<meta-data
android:name="com.samsung.android.dex.transient_bar_delay"
android:value="2000" />

<meta-data android:name="WindowManagerPreference:SuppressWindowControlNavigationButton"
android:value="true" />

</activity>

<activity-alias
Expand All @@ -87,6 +96,14 @@
<category android:name="android.intent.category.IOT_LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<meta-data
android:name="com.samsung.android.dex.transient_bar_delay"
android:value="2000" />

<meta-data android:name="WindowManagerPreference:SuppressWindowControlNavigationButton"
android:value="true" />

</activity-alias>

<activity
Expand Down
72 changes: 72 additions & 0 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Expand Up @@ -22,6 +22,8 @@
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.autofill.AutofillManager;
// import android.view.WindowInsets;
// import android.view.WindowInsetsController;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
Expand Down Expand Up @@ -1021,4 +1023,74 @@ public static Intent newInstance(@NonNull final Context context) {
return intent;
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (mProperties.isUsingFullScreen()) {
enableImmersiveMode();
} else {
disableImmersiveMode();
}
}
}

@SuppressWarnings("deprecation")
private void enableImmersiveMode() {
// if (Build.VERSION.SDK_INT >= 30) {
// getWindow().setDecorFitsSystemWindows(false);
// WindowInsetsController insetsController = getWindow().getInsetsController();
// if (insetsController != null) {
// insetsController.hide(WindowInsets.Type.navigationBars() | WindowInsets.Type.statusBars());
// insetsController.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
// }
// } else {
View decorView = getWindow().getDecorView();
int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(flags);
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
// }
}

@SuppressWarnings("deprecation")
private void disableImmersiveMode() {
// if (Build.VERSION.SDK_INT >= 30) {
// getWindow().setDecorFitsSystemWindows(true);
// WindowInsetsController insetsController = getWindow().getInsetsController();
// if (insetsController != null) {
// insetsController.show(WindowInsets.Type.navigationBars() | WindowInsets.Type.statusBars());
// insetsController.setSystemBarsBehavior(
// Build.VERSION.SDK_INT >= 31
// ? WindowInsetsController.BEHAVIOR_DEFAULT
// : WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_SWIPE
// );
// }
// } else {
View decorView = getWindow().getDecorView();
int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
decorView.setSystemUiVisibility(flags);
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
// }
}
}
@@ -0,0 +1,187 @@
package com.termux.terminal;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;

import android.os.SystemClock;

/**
* A circular buffer of {@link TerminalRow}:s which keeps notes about what is visible on a logical screen and the scroll
* history.
* <p>
* See {@link #externalToInternalRow(int)} for how to map from logical screen rows to array indices.
*/
public class TerminalBitmap {
public Bitmap bitmap;
public int cellWidth;
public int cellHeight;
public int scrollLines;
public int[] cursorDelta;
private static final String LOG_TAG = "TerminalBitmap";


public TerminalBitmap(int num, WorkingTerminalBitmap sixel, int Y, int X, int cellW, int cellH, TerminalBuffer screen) {
Bitmap bm = sixel.bitmap;
bm = resizeBitmapConstraints(bm, sixel.width, sixel.height, cellW, cellH, screen.mColumns - X);
addBitmap(num, bm, Y, X, cellW, cellH, screen);
}

public TerminalBitmap(int num, byte[] image, int Y, int X, int cellW, int cellH, int width, int height, boolean aspect, TerminalBuffer screen) {
Bitmap bm = null;
int imageHeight;
int imageWidth;
int newWidth = width;
int newHeight = height;
if (height > 0 || width > 0) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeByteArray(image, 0, image.length, options);
} catch (Exception e) {
Logger.logWarn(null, LOG_TAG, "Cannot decode image");
}
imageHeight = options.outHeight;
imageWidth = options.outWidth;
if (aspect) {
double wFactor = 9999.0;
double hFactor = 9999.0;
if (width > 0) {
wFactor = (double)width / imageWidth;
}
if (height > 0) {
hFactor = (double)height / imageHeight;
}
double factor = Math.min(wFactor, hFactor);
newWidth = (int)(factor * imageWidth);
newHeight = (int)(factor * imageHeight);
} else {
if (height <= 0) {
newHeight = imageHeight;
}
if (width <= 0) {
newWidth = imageWidth;
}
}
int scaleFactor = 1;
while (imageHeight >= 2 * newHeight * scaleFactor && imageWidth >= 2 * newWidth * scaleFactor) {
scaleFactor = scaleFactor * 2;
}
BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
scaleOptions.inSampleSize = scaleFactor;
try {
bm = BitmapFactory.decodeByteArray(image, 0, image.length, scaleOptions);
} catch (Exception e) {
Logger.logWarn(null, LOG_TAG, "Out of memory, cannot decode image");
bitmap = null;
return;
}
if (bm == null) {
Logger.logWarn(null, LOG_TAG, "Could not decode image");
bitmap = null;
return;
}
int maxWidth = (screen.mColumns - X) * cellW;
if (newWidth > maxWidth) {
int cropWidth = bm.getWidth() * maxWidth / newWidth;
try {
bm = Bitmap.createBitmap(bm, 0, 0, cropWidth, bm.getHeight());
newWidth = maxWidth;
} catch(OutOfMemoryError e) {
// This is just a memory optimization. If it fails,
// continue (and probably fail later).
}
}
try {
bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
} catch(OutOfMemoryError e) {
Logger.logWarn(null, LOG_TAG, "Out of memory, cannot rescale image");
bm = null;
}
} else {
try {
bm = BitmapFactory.decodeByteArray(image, 0, image.length);
} catch (Exception e) {
Logger.logWarn(null, LOG_TAG, "Out of memory, cannot decode image");
}
}

if (bm == null) {
Logger.logWarn(null, LOG_TAG, "Cannot decode image");
bitmap = null;
return;
}

bm = resizeBitmapConstraints(bm, bm.getWidth(), bm.getHeight(), cellW, cellH, screen.mColumns - X);
addBitmap(num, bm, Y, X, cellW, cellH, screen);
cursorDelta = new int[] {scrollLines, (bitmap.getWidth() + cellW - 1) / cellW};
}

private void addBitmap(int num, Bitmap bm, int Y, int X, int cellW, int cellH, TerminalBuffer screen) {
if (bm == null) {
bitmap = null;
return;
}
int width = bm.getWidth();
int height = bm.getHeight();
cellWidth = cellW;
cellHeight = cellH;
int w = Math.min(screen.mColumns - X, (width + cellW - 1) / cellW);
int h = (height + cellH - 1) / cellH;
int s = 0;
for (int i=0; i<h; i++) {
if (Y+i-s == screen.mScreenRows) {
screen.scrollDownOneLine(0, screen.mScreenRows, TextStyle.NORMAL);
s++;
}
for (int j=0; j<w ; j++) {
screen.setChar(X+j, Y+i-s, '+', TextStyle.encodeBitmap(num, j, i));
}
}
if (w * cellW < width) {
try {
bm = Bitmap.createBitmap(bm, 0, 0, w * cellW, height);
} catch(OutOfMemoryError e) {
// Image cannot be cropped to only visible part due to out of memory.
// This causes memory waste.
}
}
bitmap = bm;
scrollLines = h - s;
}

static public Bitmap resizeBitmap(Bitmap bm, int w, int h) {
int[] pixels = new int[bm.getAllocationByteCount()];
bm.getPixels(pixels, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
Bitmap newbm;
try {
newbm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
} catch(OutOfMemoryError e) {
// Only a minor display glitch in this case
return bm;
}
int newWidth = Math.min(bm.getWidth(), w);
int newHeight = Math.min(bm.getHeight(), h);
newbm.setPixels(pixels, 0, bm.getWidth(), 0, 0, newWidth, newHeight);
return newbm;
}

static public Bitmap resizeBitmapConstraints(Bitmap bm, int w, int h, int cellW, int cellH, int Columns) {
// Width and height must be multiples of the cell width and height
// Bitmap should not extend beyonf screen width
if (w > cellW * Columns || (w % cellW) != 0 || (h % cellH) != 0) {
int newW = Math.min(cellW * Columns, ((w - 1) / cellW) * cellW + cellW);
int newH = ((h - 1) / cellH) * cellH + cellH;
try {
bm = resizeBitmap(bm, newW, newH);
} catch(OutOfMemoryError e) {
// Only a minor display glitch in this case
}
}
return bm;
}
}