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

Set Background image for terminal window #3495

Open
wants to merge 2 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
148 changes: 148 additions & 0 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Expand Up @@ -9,10 +9,15 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
Expand All @@ -24,15 +29,18 @@
import android.view.autofill.AutofillManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.termux.R;
import com.termux.app.api.file.FileReceiverActivity;
import com.termux.app.terminal.BackgroundOptions_dialog;
import com.termux.app.terminal.TermuxActivityRootView;
import com.termux.app.terminal.TermuxTerminalSessionActivityClient;
import com.termux.app.terminal.io.TermuxTerminalExtraKeys;
import com.termux.filepicker.FilePathFromDevice;
import com.termux.shared.activities.ReportActivity;
import com.termux.shared.activity.ActivityUtils;
import com.termux.shared.activity.media.AppCompatActivityUtils;
Expand Down Expand Up @@ -65,8 +73,15 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.preference.PreferenceManager;
import androidx.viewpager.widget.ViewPager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

/**
Expand Down Expand Up @@ -195,6 +210,11 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo

private static final String LOG_TAG = "TermuxActivity";

private ImageView background;
private View overlay;
private SharedPreferences prefs;
private final String BACKGROUND_FILE_NAME = "terminal_background.jpg";

@Override
public void onCreate(Bundle savedInstanceState) {
Logger.logDebug(LOG_TAG, "onCreate");
Expand Down Expand Up @@ -242,6 +262,17 @@ public void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}


prefs = PreferenceManager.getDefaultSharedPreferences(TermuxActivity.this);
background = findViewById(R.id.bgImage);
overlay = findViewById(R.id.overlay);
// Load background on startup also
final File back_file = new File(getFilesDir(), BACKGROUND_FILE_NAME);
if(back_file.exists()){
setImage(back_file.getAbsolutePath());
}
setOverlay();

setTermuxTerminalViewAndClients();

setTerminalToolbarView(savedInstanceState);
Expand Down Expand Up @@ -377,6 +408,94 @@ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {



/**
* Choose Terminal background image from Gallery
*/
public void choose_background(View view) {

new BackgroundOptions_dialog(TermuxActivity.this, new BackgroundOptions_dialog.Callback() {
@Override
public void onReset() {
final File back_file = new File(getFilesDir(), "terminal_background.jpg");
if(back_file.exists()){
boolean deleted = back_file.delete();
if(deleted){
background.setVisibility(View.GONE);
}
}
prefs.edit()
.putString("overlay", "#00000000")
.apply();
overlay.setVisibility(View.GONE);
}

@Override
public void onPick() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Terminal Background"), 47);
}

@Override
public void onSave(String hex_overlay_color) {
if(hex_overlay_color==null || hex_overlay_color.length()<6){
Toast.makeText(TermuxActivity.this, "Not a valid color code", Toast.LENGTH_SHORT).show();
return;
}
if(!hex_overlay_color.startsWith("#")){
hex_overlay_color = "#"+hex_overlay_color;
}
try{
overlay.setBackgroundColor(Color.parseColor(hex_overlay_color));
overlay.setVisibility(View.VISIBLE);
prefs.edit()
.putString("overlay", hex_overlay_color)
.apply();
}catch (Exception ex){
ex.printStackTrace();
Toast.makeText(TermuxActivity.this, "Oops !"+ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}).show();
}


private void setImage(String selected){
try{
Bitmap bmp = BitmapFactory.decodeFile(selected);
background.setImageBitmap(bmp);
background.setVisibility(View.VISIBLE);
}catch (Exception ex){
ex.printStackTrace();
}
}

private void setOverlay(){
try{
String overlay_color = prefs.getString("overlay", "#00000000");
if(overlay_color!=null){
overlay.setBackgroundColor(Color.parseColor(overlay_color));
}
}catch (Exception ex){
ex.printStackTrace();
}
}

public void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.flush();
out.close();
}



/**
Expand Down Expand Up @@ -806,6 +925,35 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Logger.logVerbose(LOG_TAG, "onActivityResult: requestCode: " + requestCode + ", resultCode: " + resultCode + ", data: " + IntentUtils.getIntentString(data));
if (requestCode == PermissionUtils.REQUEST_GRANT_STORAGE_PERMISSION) {
requestStoragePermission(true);
} else if (requestCode == 47) {
try {
if (resultCode == RESULT_OK) {

if (null == data) return;
Uri mImageCaptureUri = data.getData();
background.invalidate();
final File dir = getFilesDir();
if (!dir.exists()) {
dir.mkdirs();
}
final File back_file = new File(dir, BACKGROUND_FILE_NAME);
String selected;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
selected = FilePathFromDevice.loadFromGallery(TermuxActivity.this, mImageCaptureUri, back_file);
setImage(selected);
} else {
selected = FilePathFromDevice.getPath(TermuxActivity.this, mImageCaptureUri);
final File gallery_file = new File(selected);
if (!back_file.exists()) {
boolean created = back_file.createNewFile();
}
copy(gallery_file, back_file);
}
setImage(selected);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}

Expand Down
@@ -0,0 +1,91 @@
package com.termux.app.terminal;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.preference.PreferenceManager;

import com.termux.R;
import com.termux.app.TermuxActivity;

/**
* -- by srb.bans on 3/7/2023.
*/
public class BackgroundOptions_dialog {
private final Context context;
private final Callback callback;

public BackgroundOptions_dialog(Context ctx, Callback call) {
context = ctx;
callback = call;
}

public void show() {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(context);
View mView = layoutInflaterAndroid.inflate(R.layout.background_options_dialog, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context, R.style.TermuxAlertDialogStyle);
alertDialogBuilderUserInput.setView(mView);

final AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();

final EditText hex_code = mView.findViewById(R.id.hex_code);
final Button bt_reset = mView.findViewById(R.id.reset_background);
final Button bt_save = mView.findViewById(R.id.save);
final Button bt_pick = mView.findViewById(R.id.choose);
final View actionClose = mView.findViewById(R.id.action_close);

bt_save.setOnClickListener(view -> {
alertDialogAndroid.dismiss();
if (callback != null) {
String hex = "#00000000";
if (hex_code.length() > 0) {
hex = hex_code.getText().toString();
}
callback.onSave(hex);
}
});
bt_pick.setOnClickListener(view -> {
alertDialogAndroid.dismiss();
if (callback != null) callback.onPick();
});
bt_reset.setOnClickListener(view -> {
alertDialogAndroid.dismiss();
if (callback != null) callback.onReset();
});
actionClose.setOnClickListener(view -> {
alertDialogAndroid.dismiss();
});


SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String overlay_color = prefs.getString("overlay", "#00000000");
if(overlay_color!=null){
hex_code.setText(overlay_color);
}

if (alertDialogAndroid.getWindow() != null)
alertDialogAndroid.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Add animation to view
//BounceView.addAnimTo(alertDialogAndroid);

alertDialogAndroid.show();
}


public interface Callback {

void onReset();

void onPick();

void onSave(String hex_overlay_color);
}

}