Skip to content

Commit

Permalink
Rework Status fragment icons to fix sizing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele-f committed Feb 1, 2024
1 parent 8ac6428 commit 54d6ce4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.method.LinkMovementMethod;
import android.util.Pair;
import android.view.LayoutInflater;
Expand All @@ -35,7 +33,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -67,13 +65,15 @@

public class StatusFragment extends Fragment implements AppStateListener, MenuProvider {
private static final String TAG = "StatusFragment";
private Handler mHandler;
private Menu mMenu;
private MenuItem mStartBtn;
private MenuItem mStopBtn;
private ImageView mFilterIcon;
private MenuItem mMenuSettings;
private TextView mInterfaceInfo;
private TextView mCollectorInfo;
private View mCollectorInfoLayout;
private TextView mCollectorInfoText;
private ImageView mCollectorInfoIcon;
private TextView mCaptureStatus;
private View mQuickSettings;
private MainActivity mActivity;
Expand Down Expand Up @@ -112,9 +112,10 @@ public View onCreateView(LayoutInflater inflater,
@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
mHandler = new Handler(Looper.getMainLooper());
mInterfaceInfo = view.findViewById(R.id.interface_info);
mCollectorInfo = view.findViewById(R.id.collector_info);
mCollectorInfoLayout = view.findViewById(R.id.collector_info_layout);
mCollectorInfoText = mCollectorInfoLayout.findViewById(R.id.collector_info_text);
mCollectorInfoIcon = mCollectorInfoLayout.findViewById(R.id.collector_info_icon);
mCaptureStatus = view.findViewById(R.id.status_view);
mQuickSettings = view.findViewById(R.id.quick_settings);
mFilterRootDecryptionWarning = view.findViewById(R.id.app_filter_root_decryption_warning);
Expand All @@ -129,24 +130,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
View filterRow = view.findViewById(R.id.app_filter_text);
TextView filterTitle = filterRow.findViewById(R.id.title);
mFilterDescription = filterRow.findViewById(R.id.description);

// Needed to update the filter icon after mFilterDescription is measured
final ViewTreeObserver vto = mFilterDescription.getViewTreeObserver();
if(vto.isAlive()) {
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
refreshFilterInfo();

final ViewTreeObserver vto = mFilterDescription.getViewTreeObserver();

if(vto.isAlive()) {
vto.removeOnGlobalLayoutListener(this);
Log.d(TAG, "removeOnGlobalLayoutListener called");
}
}
});
}
mFilterIcon = filterRow.findViewById(R.id.icon);

filterTitle.setText(R.string.target_apps);

Expand All @@ -167,7 +151,7 @@ public void onGlobalLayout() {
CaptureService.observeStats(this, this::onStatsUpdate);

// Make URLs clickable
mCollectorInfo.setMovementMethod(LinkMovementMethod.getInstance());
mCollectorInfoText.setMovementMethod(LinkMovementMethod.getInstance());

/* Important: call this after all the fields have been initialized */
mActivity.setAppStateListener(this);
Expand Down Expand Up @@ -215,7 +199,7 @@ private void refreshFilterInfo() {

if((mAppFilter == null) || (mAppFilter.isEmpty())) {
mFilterDescription.setText(R.string.capture_all_apps);
mFilterDescription.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
mFilterIcon.setVisibility(View.GONE);
mAppFilterSwitch.setChecked(false);
return;
}
Expand All @@ -227,14 +211,8 @@ private void refreshFilterInfo() {
mFilterDescription.setText(pair.first);

if (pair.second != null) {
int height = mFilterDescription.getMeasuredHeight();

if(height > 0) {
Drawable drawable = Utils.scaleDrawable(context.getResources(), pair.second, height, height);

if(drawable != null)
mFilterDescription.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
}
mFilterIcon.setImageDrawable(pair.second);
mFilterIcon.setVisibility(View.VISIBLE);
}
}

Expand Down Expand Up @@ -281,7 +259,7 @@ private Pair<String, Drawable> getAppFilterTextAndIcon(@NonNull Context context)
return new Pair<>(text, icon);
}

private void refreshPcapDumpInfo() {
private void refreshPcapDumpInfo(Context context) {
String info = "";

Prefs.DumpMode mode = CaptureService.getDumpMode();
Expand All @@ -307,36 +285,26 @@ private void refreshPcapDumpInfo() {
break;
}

mCollectorInfo.setText(info);

// Rendering after mCollectorInfo.setText is deferred, so getMeasuredHeight must be postponed
mHandler.post(() -> {
Context context = getContext();
if(context == null)
return;
mCollectorInfoText.setText(info);

// Check if a filter is set
if((mAppFilter != null) && (!mAppFilter.isEmpty())) {
Pair<String, Drawable> pair = getAppFilterTextAndIcon(context);

if (pair.second != null) {
// scale and set
int height = mCollectorInfo.getMeasuredHeight();
Drawable drawable = Utils.scaleDrawable(getResources(), pair.second, height, height);

if (drawable != null)
mCollectorInfo.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
}
}
});
// Check if a filter is set
Drawable drawable = null;
if((mAppFilter != null) && (!mAppFilter.isEmpty())) {
Pair<String, Drawable> pair = getAppFilterTextAndIcon(context);
drawable = pair.second;
}

// will be overriden in the above handler if necessary
mCollectorInfo.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
if (drawable != null) {
mCollectorInfoIcon.setImageDrawable(drawable);
mCollectorInfoIcon.setVisibility(View.VISIBLE);
} else
mCollectorInfoIcon.setVisibility(View.GONE);
}

@Override
public void appStateChanged(AppState state) {
if(getContext() == null)
Context context = getContext();
if(context == null)
return;

if(mMenu != null) {
Expand All @@ -356,7 +324,7 @@ public void appStateChanged(AppState state) {
switch(state) {
case ready:
mCaptureStatus.setText(R.string.ready);
mCollectorInfo.setVisibility(View.GONE);
mCollectorInfoLayout.setVisibility(View.GONE);
mInterfaceInfo.setVisibility(View.GONE);
mQuickSettings.setVisibility(View.VISIBLE);
mAppFilter = Prefs.getAppFilter(mPrefs);
Expand All @@ -372,7 +340,7 @@ public void appStateChanged(AppState state) {
break;
case running:
mCaptureStatus.setText(Utils.formatBytes(CaptureService.getBytes()));
mCollectorInfo.setVisibility(View.VISIBLE);
mCollectorInfoLayout.setVisibility(View.VISIBLE);
mQuickSettings.setVisibility(View.GONE);
CaptureService service = CaptureService.requireInstance();

Expand All @@ -397,7 +365,7 @@ else if(capiface.equals("any"))
mInterfaceInfo.setVisibility(View.GONE);

mAppFilter = CaptureService.getAppFilter();
refreshPcapDumpInfo();
refreshPcapDumpInfo(context);
break;
default:
break;
Expand Down
30 changes: 24 additions & 6 deletions app/src/main/res/layout/quick_settings_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@
android:layout_marginBottom="2dp"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/description"
tools:text="Description"
android:drawablePadding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

<ImageView
android:id="@+id/icon"
android:visibility="gone"
android:adjustViewBounds="false"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_marginVertical="1dp"
android:layout_marginEnd="2dp"
tools:src="@drawable/ic_apps"
tools:tint="?attr/colorAccent" />

<TextView
android:id="@+id/description"
tools:text="Description"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
32 changes: 24 additions & 8 deletions app/src/main/res/layout/status.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,35 @@
android:layout_height="wrap_content"
tools:context="com.emanuelef.remote_capture.activities.MainActivity">

<TextView
android:id="@+id/collector_info"
<LinearLayout
android:id="@+id/collector_info_layout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:autoLink="web"
android:drawablePadding="10dp"
tools:text="Collector Info"
tools:visibility="gone"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/status_view" />
app:layout_constraintTop_toBottomOf="@id/status_view">

<TextView
android:id="@+id/collector_info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
tools:text="Collector Info" />

<ImageView
android:id="@+id/collector_info_icon"
android:adjustViewBounds="false"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_marginVertical="1dp"
android:layout_marginHorizontal="2dp"
tools:src="@drawable/ic_apps"
tools:tint="?attr/colorAccent" />
</LinearLayout>

<TextView
android:id="@+id/interface_info"
Expand All @@ -38,7 +54,7 @@
tools:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/collector_info" />
app:layout_constraintTop_toBottomOf="@id/collector_info_layout" />

<TextView
android:id="@+id/status_view"
Expand Down

0 comments on commit 54d6ce4

Please sign in to comment.