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
3 changes: 3 additions & 0 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2025-05-15 - Interactive Technical Samples
**Learning:** In technical sample apps, users benefit from immediate feedback on native operations and easy access to terminal commands.
**Action:** Implement "click-to-copy" for terminal commands with `selectableItemBackground` and `Toast` feedback. For library loading, disable the button and update text to reflect the new state (e.g., "lib... loaded") to prevent redundant operations and clarify app state.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@

package com.bytedance.shadowhook.sample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.bytedance.shadowhook.ShadowHook;
import com.bytedance.shadowhook.systest.SysTest;
Expand All @@ -46,41 +53,82 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.adbCommand).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("adb_command", ((TextView) v).getText());
if (clipboard != null) {
clipboard.setPrimaryClip(clip);
Toast.makeText(MainActivity.this, R.string.copy_adb_success, Toast.LENGTH_SHORT).show();
}
}
});
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("hookee2Loaded", hookee2Loaded);
}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
hookee2Loaded = savedInstanceState.getBoolean("hookee2Loaded");
if (hookee2Loaded) {
updateLoadButton();
}
}

private void updateLoadButton() {
Button btn = findViewById(R.id.unitTestLoad);
btn.setEnabled(false);
btn.setText(R.string.lib_loaded);
}

public void onUnitTestHookSymAddrClick(View view) {
NativeHandler.nativeHookSymAddr(Build.VERSION.SDK_INT);
Toast.makeText(this, R.string.hook_success, Toast.LENGTH_SHORT).show();
}

public void onUnitTestHookSymNameClick(View view) {
NativeHandler.nativeHookSymName(Build.VERSION.SDK_INT);
Toast.makeText(this, R.string.hook_success, Toast.LENGTH_SHORT).show();
}

public void onUnitTestUnhookClick(View view) {
NativeHandler.nativeUnhook();
Toast.makeText(this, R.string.unhook_success, Toast.LENGTH_SHORT).show();
}

public void onUnitTestLoadClick(View view) {
if(!hookee2Loaded) {
hookee2Loaded = true;
System.loadLibrary("hookee2");
updateLoadButton();
}
}

public void onUnitTestRunClick(View view) {
NativeHandler.nativeRun(hookee2Loaded);
Toast.makeText(this, R.string.run_success, Toast.LENGTH_SHORT).show();
}

public void onSystemTestHookClick(View view) {
SysTest.hook();
Toast.makeText(this, R.string.hook_success, Toast.LENGTH_SHORT).show();
}

public void onSystemTestUnhookClick(View view) {
SysTest.unhook();
Toast.makeText(this, R.string.unhook_success, Toast.LENGTH_SHORT).show();
}

public void onSystemTestRunClick(View view) {
SysTest.run();
Toast.makeText(this, R.string.run_success, Toast.LENGTH_SHORT).show();
}

public void onGetRecordsClick(View view) {
Expand All @@ -90,6 +138,7 @@ public void onGetRecordsClick(View view) {
for (String line : records.split("\n")) {
Log.i(tag, line);
}
Toast.makeText(this, R.string.records_success, Toast.LENGTH_SHORT).show();
}
}

Expand All @@ -112,6 +161,7 @@ public void onDumpRecordsClick(View view) {
} catch (Exception ignored) {
}
}
Toast.makeText(this, R.string.records_success, Toast.LENGTH_SHORT).show();
}
}
}
9 changes: 7 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
android:orientation="vertical">

<TextView style="@style/Theme.TextView.Top"
android:text="adb logcat -b main -s shadowhook_tag" />
android:id="@+id/adbCommand"
android:text="@string/adb_log_command"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:contentDescription="@string/copy_adb_desc" />

<TextView style="@style/Theme.TextView.Title"
android:text="1. unit test" />
Expand All @@ -39,7 +44,7 @@
<Button style="@style/Theme.Button.Red"
android:id="@+id/unitTestLoad"
android:onClick="onUnitTestLoadClick"
android:text="load libhookee2.so (automatically hook)" />
android:text="@string/lib_load_initial" />

<Button style="@style/Theme.Button.Red"
android:id="@+id/unitTestRun"
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<resources>
<string name="app_name">shadowhook</string>
</resources>
<string name="adb_log_command">adb logcat -b main -s shadowhook_tag</string>
<string name="copy_adb_desc">Copy ADB logcat command</string>
<string name="copy_adb_success">ADB command copied to clipboard</string>
<string name="lib_load_initial">load libhookee2.so (automatically hook)</string>
<string name="lib_loaded">libhookee2.so loaded</string>
<string name="hook_success">Hooking completed</string>
<string name="unhook_success">Unhooking completed</string>
<string name="run_success">Running completed</string>
<string name="records_success">Records retrieved to logcat</string>
</resources>
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.2.2' apply false
}

task clean(type: Delete) {
delete rootProject.buildDir
delete rootProject.layout.buildDirectory
}

ext {
minSdkVersion = 16
compileSdkVersion = 34
targetSdkVersion = 34
buildToolsVersion = '34.0.0'
javaVersion = JavaVersion.VERSION_1_7
javaVersion = JavaVersion.VERSION_1_8
ndkVersion = "23.2.8568313"
cmakeVersion = "3.22.1"
abiFilters = "armeabi-v7a,arm64-v8a"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ android.useAndroidX=true
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
#android.prefabVersion=2.0.0
android.disableAutomaticComponentCreation=true
#android.disableAutomaticComponentCreation=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Dec 30 21:41:31 CST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME