Skip to content

Commit

Permalink
Add button to copy request/response payload
Browse files Browse the repository at this point in the history
The button copies the full request/response to the clipboard. The content
type label has been removed to make space for this button.

See #362
  • Loading branch information
emanuele-f committed Dec 25, 2023
1 parent 852c7ca commit ef09ebc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -40,6 +41,7 @@
import com.emanuelef.remote_capture.model.PayloadChunk;
import com.emanuelef.remote_capture.model.PayloadChunk.ChunkType;
import com.emanuelef.remote_capture.model.Prefs;
import com.google.android.material.button.MaterialButton;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -111,18 +113,24 @@ PayloadChunk getPayloadChunk() {
return mChunk;
}

private void makeText() {
int dump_len = mIsExpanded ? mChunk.payload.length : Math.min(mChunk.payload.length, COLLAPSE_CHUNK_SIZE);
@CheckResult
private String makeText(boolean expanded) {
int dump_len = expanded ? mChunk.payload.length : Math.min(mChunk.payload.length, COLLAPSE_CHUNK_SIZE);

if(!mShowAsPrintable)
mTheText = Utils.hexdump(mChunk.payload, 0, dump_len);
return Utils.hexdump(mChunk.payload, 0, dump_len);
else
mTheText = new String(mChunk.payload, 0, dump_len, StandardCharsets.UTF_8);
return new String(mChunk.payload, 0, dump_len, StandardCharsets.UTF_8);
}

@CheckResult
private String makeText() {
return makeText(mIsExpanded);
}

void expand() {
mIsExpanded = true;
makeText();
mTheText = makeText();

// round up div
mNumPages = (mTheText.length() + VISUAL_PAGE_SIZE - 1) / VISUAL_PAGE_SIZE;
Expand All @@ -138,7 +146,7 @@ void collapse() {

String getText(int start, int end) {
if(mTheText == null)
makeText();
mTheText = makeText();

if((start == 0) && (end >= mTheText.length() - 1)) {
return mTheText;
Expand All @@ -147,11 +155,15 @@ String getText(int start, int end) {
return mTheText.substring(start, end);
}

String getExpandedText() {
return makeText(true);
}

Page getPage(int pageIdx) {
assert(pageIdx < mNumPages);

if(mTheText == null)
makeText();
mTheText = makeText();

if(!mIsExpanded)
return new Page(this, 0, mTheText.length() - 1, true);
Expand Down Expand Up @@ -189,8 +201,8 @@ protected static class PayloadViewHolder extends RecyclerView.ViewHolder {
View dumpBox;
TextView header;
TextView dump;
TextView contentType;
ImageView expandButton;
MaterialButton copybutton;

public PayloadViewHolder(View view) {
super(view);
Expand All @@ -200,7 +212,7 @@ public PayloadViewHolder(View view) {
dump = view.findViewById(R.id.dump);
dumpBox = view.findViewById(R.id.dump_box);
expandButton = view.findViewById(R.id.expand_button);
contentType = view.findViewById(R.id.content_type);
copybutton = view.findViewById(R.id.copy_button);
}
}

Expand All @@ -227,6 +239,13 @@ public PayloadViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewT
}
});

holder.copybutton.setOnClickListener(v -> {
int pos = holder.getAbsoluteAdapterPosition();
String payload = getItem(pos).adaptChunk.getExpandedText();

Utils.copyToClipboard(mContext, payload);
});

return holder;
}

Expand All @@ -251,8 +270,6 @@ public void onBindViewHolder(@NonNull PayloadViewHolder holder, int position) {
getHeaderTag(chunk),
(new SimpleDateFormat("HH:mm:ss.SSS", locale)).format(new Date(chunk.timestamp)),
Utils.formatBytes(chunk.payload.length)));

holder.contentType.setText((chunk.contentType != null) ? chunk.contentType : "");
} else
holder.headerLine.setVisibility(View.GONE);

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_content_copy_small.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="18dp" android:tint="?attr/colorControlNormal"
android:viewportHeight="24" android:viewportWidth="24"
android:width="18dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M16,1L4,1c-1.1,0 -2,0.9 -2,2v14h2L4,3h12L16,1zM19,5L8,5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2L21,7c0,-1.1 -0.9,-2 -2,-2zM19,21L8,21L8,7h11v14z"/>
</vector>
33 changes: 21 additions & 12 deletions app/src/main/res/layout/payload_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,29 @@
android:textStyle="bold"
tools:text="#1 [TX] 11:02:03.154 — 120 B" />

<TextView
android:id="@+id/content_type"
<View
android:layout_width="0dp"
android:layout_weight="1"
android:textAlignment="textEnd"
android:layout_marginStart="16dp"
android:layout_marginEnd="2dp"
android:layout_height="0dp"
android:layout_weight="1" />

<com.google.android.material.button.MaterialButton
android:id="@+id/copy_button"
android:insetTop="0dp"
android:insetBottom="0dp"
android:insetLeft="0dp"
android:insetRight="0dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:paddingVertical="0dp"
android:paddingHorizontal="0dp"
android:layout_marginHorizontal="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:textSize="11sp"
android:textStyle="italic"
android:singleLine="true"
android:ellipsize="start"
tools:text="application/javascript" />
app:icon="@drawable/ic_content_copy_small"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconTint="@color/colorTabText"
style="@style/Widget.MaterialComponents.Button.TextButton" />
</LinearLayout>

<RelativeLayout
Expand Down

0 comments on commit ef09ebc

Please sign in to comment.