0% found this document useful (0 votes)
14 views3 pages

Android Toggle Switch Commands

The document is a Java class for an Android application that manages toggle switches to execute various ADB commands using the Shizuku library. It initializes multiple toggle switches from a layout, sets commands for toggles, and provides alerts for toggles without commands. The application also handles full-screen display and permission checks for Shizuku before executing commands in a separate thread, displaying success or error messages accordingly.

Uploaded by

tiar.betet25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Android Toggle Switch Commands

The document is a Java class for an Android application that manages toggle switches to execute various ADB commands using the Shizuku library. It initializes multiple toggle switches from a layout, sets commands for toggles, and provides alerts for toggles without commands. The application also handles full-screen display and permission checks for Shizuku before executing commands in a separate thread, displaying success or error messages accordingly.

Uploaded by

tiar.betet25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package [Link].

example;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class TrashActivity extends AppCompatActivity {

private Switch toggle1, toggle2, toggle3, toggle4, toggle5, toggle6, toggle7,


toggle8, toggle9, toggle10;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_trash); // Menggunakan layout
activity_trash.xml

// Inisialisasi Toggle dari layout activity_trash.xml


toggle1 = findViewById([Link].switch_toggle1);
toggle2 = findViewById([Link].switch_toggle2);
toggle3 = findViewById([Link].switch_toggle3);
toggle4 = findViewById([Link].switch_toggle4);
toggle5 = findViewById([Link].switch_toggle5);
toggle6 = findViewById([Link].switch_toggle6);
toggle7 = findViewById([Link].switch_toggle7);
toggle8 = findViewById([Link].switch_toggle8);
toggle9 = findViewById([Link].switch_toggle9);
toggle10 = findViewById([Link].switch_toggle10);

// Mengatur tampilan menjadi full-screen


hideSystemUI();

// Set Listener untuk setiap toggle


setToggleCommand(toggle1, "setprop [Link].perf_mode true; settings put
secure location_providers_allowed -gps", "setprop [Link].perf_mode false");
setToggleCommand(toggle2, "wm size 1320x2868; settings put system
pointer_speed 1; settings put global window_animation_scale 0; settings put global
transition_animation_scale 0; settings put global animator_duration_scale 0;
settings put system long_press_timeout 150", "wm size reset; settings put system
pointer_speed 0; settings put global window_animation_scale 1; settings put global
transition_animation_scale 1; settings put global animator_duration_scale 1;
settings put system long_press_timeout 300");
setToggleCommand(toggle3, "settings put system touch_sensitivity 0.5",
"settings delete system touch_sensitivity");

// Menambahkan alert untuk toggle yang tidak memiliki command


setToggleAlert(toggle4);
setToggleAlert(toggle5);
setToggleAlert(toggle6);
setToggleAlert(toggle7);
setToggleAlert(toggle8);
setToggleAlert(toggle9);
setToggleAlert(toggle10);
}

private void hideSystemUI() {


View decorView = getWindow().getDecorView();
[Link](
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR // Jika ingin status
bar gelap
);
getWindow().setStatusBarColor([Link]);
}

private void setToggleCommand(Switch toggle, String commandOn, String


commandOff) {
[Link]((buttonView, isChecked) -> {
String command = isChecked ? commandOn : commandOff;
executeAdbCommandWithShizuku(command, [Link]().toString());
});
}

private void setToggleAlert(Switch toggle) {


[Link]((buttonView, isChecked) -> {
if (isChecked) {
[Link]([Link], "COMING SOON FEATURE!",
Toast.LENGTH_SHORT).show();
[Link](false); // Mengembalikan toggle ke posisi off
}
});
}

private void executeAdbCommandWithShizuku(String command, String toggleName) {


if ([Link]() != PackageManager.PERMISSION_GRANTED) {
[Link](this, "Shizuku permission not granted",
Toast.LENGTH_SHORT).show();
return;
}

new Thread(() -> {


try {
String[] commands = [Link](";");
boolean isSuccess = false; // Untuk mengecek keberhasilan
for (String cmd : commands) {
String trimmedCmd = [Link]();
Process process = [Link](new String[]{"sh", "-c",
trimmedCmd}, null, null);
int exitCode = [Link]();
if (exitCode == 0) {
isSuccess = true; // Set berhasil jika ada perintah yang
berhasil
} else {
isSuccess = false; // Jika ada yang gagal
}
}

// Menampilkan toast berdasarkan keberhasilan


String toastMessage = isSuccess ? "Berhasil Menjalankan: " +
toggleName : "Gagal Menjalankan: " + toggleName;
runOnUiThread(() -> [Link]([Link],
toastMessage, Toast.LENGTH_SHORT).show());

} catch (Exception e) {
[Link]();
runOnUiThread(() -> [Link]([Link], "Error: " +
[Link](), Toast.LENGTH_SHORT).show());
}
}).start();
}
}

Common questions

Powered by AI

TrashActivity implements error handling mechanisms to address exceptions during command execution using try-catch blocks within the executeAdbCommandWithShizuku method. If exceptions are encountered, such as issues in launching commands or permission errors, the catch block captures these exceptions and outputs an error message through a Toast notification. This mechanism ensures that any failure during command execution due to unforeseen circumstances is communicated promptly to the user, maintaining transparency and aiding in debugging processs .

The TrashActivity class manipulates UI settings programmatically using toggle switches by defining specific shell commands that correspond to each toggle's on/off state using the Shizuku library. Each toggle is linked to a set of commands that adjust system properties affecting UI settings such as screen resolution, animation speed, and touch sensitivity. Functions like setToggleCommand specify commands to execute based on the toggle state, and setToggleAlert handles unimplemented toggles. The impact of toggling is immediate, relying on the successful execution of system commands to adjust UI parameters such as animation scales and pointer speed .

Setting up toggle switches controlling complex system settings can have significant implications for user experience. On one hand, it allows for advanced customization and convenience for tech-savvy users who understand the implications of each setting. On the other hand, it may pose risks, such as undesired behavior if incorrect settings are applied, or confusion for less experienced users due to the technical nature of changes like pointer speed and window animation scales. Ensuring users are fully informed about each switch's function through clear labeling, descriptions, and safeguards like alerts for unsupported toggles can enhance usability while mitigating potential user experience issues .

TrashActivity provides feedback to users about the success or failure of executing a command primarily through Toast notifications. After attempting to execute a sequence of commands in the executeAdbCommandWithShizuku method, it evaluates each command's execution success by checking their exit status. Depending on whether at least one command is successful, or if all commands fail, it displays a toast message indicating "Berhasil Menjalankan" or "Gagal Menjalankan" along with the toggle name, respectively. This user feedback mechanism is essential for informing users of action results in a concise and immediate manner .

The executeAdbCommandWithShizuku method is crucial for handling asynchronous tasks within TrashActivity by offloading command execution from the main UI thread. This method initiates a new thread for executing each command sequence, ensuring the main UI remains responsive during the execution of potentially blocking commands. It evaluates command execution results by checking exit codes, thus facilitating asynchronous management of commands that potentially affect system performance or settings. Moreover, it ensures real-time feedback by utilizing runOnUiThread to update the UI with success or failure messages through Toast notifications, integrating asynchronous behavior into user interactions .

TrashActivity ensures visual consistency by programmatically manipulating UI aspects like hiding system UI elements through the hideSystemUI method. It adjusts the system UI flags to layout full screen and hides the status bar using SYSTEM_UI_FLAG_FULLSCREEN while maintaining layout stability with SYSTEM_UI_FLAG_LAYOUT_STABLE. Additionally, by setting the status bar color to transparent, the application ensures any part of the content under the status bar maintains its visual integrity, supporting a seamless user experience .

In TrashActivity, when a toggle is switched on, commands are executed using the ADB commands in the setToggleCommand method. This approach uses the Shizuku library to execute shell commands with the necessary permissions. The commands are set according to whether the toggle is checked or not, and are executed inside a new thread to prevent blocking the main UI thread. The method applies permissions by checking if Shizuku permission is granted, and it loops through the individual commands, executing each one in the shell and checking the success by evaluating their exit codes .

Using the Shizuku library for executing system commands entails critical security considerations. Since it allows apps to execute commands with elevated privileges, there is potential for misuse if not properly controlled. Security considerations include ensuring permissions are explicitly requested and checked before executing commands, as seen with the Shizuku.checkSelfPermission method. Additionally, there should be thorough validation and sanitation of commands to prevent injecting malicious scripts. Proper user consent and clear explanations of why such permissions are required must be communicated to ensure trustworthiness .

The hideSystemUI function in the TrashActivity class serves to hide system UI elements such as the status bar, contributing to an immersive and distraction-free user experience. It sets the system UI flags to ensure that the app can utilize the full screen real estate, creating a visually large interaction space. By removing visual clutter from system interfaces, it aligns the interaction design with a focus on the core functionalities and features offered by the app without unnecessary distractions .

The TrashActivity class handles unsupported toggle functionalities by displaying a toast message "COMING SOON FEATURE!" when a user attempts to enable an unsupported feature, and immediately sets the toggle back to the 'off' position. It achieves this by calling the setToggleAlert method, which uses the setOnCheckedChangeListener function to monitor the change in state of each toggle and respond accordingly .

You might also like