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

Added: Add support for using image as background #3079

Open
wants to merge 11 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
51 changes: 47 additions & 4 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Expand Up @@ -9,6 +9,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -18,6 +19,7 @@
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
Expand All @@ -30,6 +32,7 @@

import com.termux.R;
import com.termux.app.api.file.FileReceiverActivity;
import com.termux.app.style.TermuxBackgroundManager;
import com.termux.app.terminal.TermuxActivityRootView;
import com.termux.app.terminal.TermuxTerminalSessionActivityClient;
import com.termux.app.terminal.io.TermuxTerminalExtraKeys;
Expand Down Expand Up @@ -140,6 +143,11 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
*/
TermuxSessionsListViewController mTermuxSessionListViewController;

/**
* The termux background manager for updating background.
*/
TermuxBackgroundManager mTermuxBackgroundManager;

/**
* The {@link TermuxActivity} broadcast receiver for various things like terminal style configuration changes.
*/
Expand Down Expand Up @@ -185,6 +193,9 @@ public final class TermuxActivity extends AppCompatActivity implements ServiceCo
private static final int CONTEXT_MENU_RESET_TERMINAL_ID = 3;
private static final int CONTEXT_MENU_KILL_PROCESS_ID = 4;
private static final int CONTEXT_MENU_STYLING_ID = 5;
private static final int CONTEXT_SUBMENU_FONT_AND_COLOR_ID = 11;
private static final int CONTEXT_SUBMENU_SET_BACKROUND_IMAGE_ID = 12;
private static final int CONTEXT_SUBMENU_REMOVE_BACKGROUND_IMAGE_ID = 13;
private static final int CONTEXT_MENU_TOGGLE_KEEP_SCREEN_ON = 6;
private static final int CONTEXT_MENU_HELP_ID = 7;
private static final int CONTEXT_MENU_SETTINGS_ID = 8;
Expand Down Expand Up @@ -242,6 +253,10 @@ public void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

// Must be done every time activity is created in order to registerForActivityResult,
// Even if the logic of launching is based on user input.
setBackgroundManager();

setTermuxTerminalViewAndClients();

setTerminalToolbarView(savedInstanceState);
Expand Down Expand Up @@ -375,6 +390,14 @@ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
savedInstanceState.putBoolean(ARG_ACTIVITY_RECREATED, true);
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
Logger.logVerbose(LOG_TAG, "onConfigurationChanged");

super.onConfigurationChanged(newConfig);
mTermuxTerminalSessionActivityClient.onConfigurationChanged(newConfig);
}




Expand Down Expand Up @@ -595,6 +618,10 @@ private void setToggleKeyboardView() {
});
}

private void setBackgroundManager() {
this.mTermuxBackgroundManager = new TermuxBackgroundManager(TermuxActivity.this);
}




Expand Down Expand Up @@ -648,7 +675,13 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuIn
menu.add(Menu.NONE, CONTEXT_MENU_AUTOFILL_ID, Menu.NONE, R.string.action_autofill_password);
menu.add(Menu.NONE, CONTEXT_MENU_RESET_TERMINAL_ID, Menu.NONE, R.string.action_reset_terminal);
menu.add(Menu.NONE, CONTEXT_MENU_KILL_PROCESS_ID, Menu.NONE, getResources().getString(R.string.action_kill_process, getCurrentSession().getPid())).setEnabled(currentSession.isRunning());
menu.add(Menu.NONE, CONTEXT_MENU_STYLING_ID, Menu.NONE, R.string.action_style_terminal);

SubMenu subMenu = menu.addSubMenu(Menu.NONE, CONTEXT_MENU_STYLING_ID, Menu.NONE, R.string.action_style_terminal);
subMenu.clearHeader();
subMenu.add(SubMenu.NONE, CONTEXT_SUBMENU_FONT_AND_COLOR_ID, SubMenu.NONE, R.string.action_font_and_color);
subMenu.add(SubMenu.NONE, CONTEXT_SUBMENU_SET_BACKROUND_IMAGE_ID, SubMenu.NONE, R.string.action_set_background_image);
subMenu.add(SubMenu.NONE, CONTEXT_SUBMENU_REMOVE_BACKGROUND_IMAGE_ID, SubMenu.NONE, R.string.action_remove_background_image);

menu.add(Menu.NONE, CONTEXT_MENU_TOGGLE_KEEP_SCREEN_ON, Menu.NONE, R.string.action_toggle_keep_screen_on).setCheckable(true).setChecked(mPreferences.shouldKeepScreenOn());
menu.add(Menu.NONE, CONTEXT_MENU_HELP_ID, Menu.NONE, R.string.action_open_help);
menu.add(Menu.NONE, CONTEXT_MENU_SETTINGS_ID, Menu.NONE, R.string.action_open_settings);
Expand Down Expand Up @@ -685,8 +718,14 @@ public boolean onContextItemSelected(MenuItem item) {
case CONTEXT_MENU_KILL_PROCESS_ID:
showKillSessionDialog(session);
return true;
case CONTEXT_MENU_STYLING_ID:
showStylingDialog();
case CONTEXT_SUBMENU_FONT_AND_COLOR_ID:
showFontAndColorDialog();
return true;
case CONTEXT_SUBMENU_SET_BACKROUND_IMAGE_ID:
mTermuxBackgroundManager.setBackgroundImage();
return true;
case CONTEXT_SUBMENU_REMOVE_BACKGROUND_IMAGE_ID:
mTermuxBackgroundManager.removeBackgroundImage(true);
return true;
case CONTEXT_MENU_TOGGLE_KEEP_SCREEN_ON:
toggleKeepScreenOn();
Expand Down Expand Up @@ -736,7 +775,7 @@ private void onResetTerminalSession(TerminalSession session) {
}
}

private void showStylingDialog() {
private void showFontAndColorDialog() {
Intent stylingIntent = new Intent();
stylingIntent.setClassName(TermuxConstants.TERMUX_STYLING_PACKAGE_NAME, TermuxConstants.TERMUX_STYLING.TERMUX_STYLING_ACTIVITY_NAME);
try {
Expand Down Expand Up @@ -916,6 +955,10 @@ public TermuxAppSharedProperties getProperties() {
return mProperties;
}

public TermuxBackgroundManager getmTermuxBackgroundManager() {
return mTermuxBackgroundManager;
}




Expand Down
@@ -0,0 +1,104 @@
package com.termux.app.fragments.settings.termux;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceDataStore;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.SwitchPreferenceCompat;

import com.termux.R;
import com.termux.app.style.TermuxBackgroundManager;
import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences;

@Keep
public class TermuxStylePreferencesFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Context context = getContext();
if (context == null) return;

PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setPreferenceDataStore(TermuxStylePreferencesDataStore.getInstance(context));

setPreferencesFromResource(R.xml.termux_style_preferences, rootKey);

configureBackgroundPreferences(context);
}

/**
* Configure background preferences and make appropriate changes in the state of components.
*
* @param context The context for operations.
*/
private void configureBackgroundPreferences(@NonNull Context context) {
SwitchPreferenceCompat backgroundImagePreference = findPreference("background_image_enabled");

if (backgroundImagePreference != null) {
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(context, true);

if (preferences == null) return;

// If background image preference is disabled and background images are
// missing, then don't allow user to enable it from setting.
if (!preferences.isBackgroundImageEnabled() && !TermuxBackgroundManager.isImageFilesExist(context, false)) {
backgroundImagePreference.setEnabled(false);
}
}
}

}

class TermuxStylePreferencesDataStore extends PreferenceDataStore {

private final Context mContext;

private final TermuxAppSharedPreferences mPreferences;

private static TermuxStylePreferencesDataStore mInstance;

private TermuxStylePreferencesDataStore(Context context) {
mContext = context;
mPreferences = TermuxAppSharedPreferences.build(context, true);
}

public static synchronized TermuxStylePreferencesDataStore getInstance(Context context) {
if (mInstance == null) {
mInstance = new TermuxStylePreferencesDataStore(context);
}

return mInstance;
}



@Override
public void putBoolean(String key, boolean value) {
if (mPreferences == null) return;
if (key == null) return;

switch (key) {
case "background_image_enabled":
mPreferences.setBackgroundImageEnabled(value);
default:
break;
}
}

@Override
public boolean getBoolean(String key, boolean defValue) {
if (mPreferences == null) return false;

switch (key) {
case "background_image_enabled":
return mPreferences.isBackgroundImageEnabled();
default:
return false;
}
}

}