Improve fragments consistency while swiping

While swiping in the viewpager, the fragments onResume may haven't been
called yet. This commit initializes the status fragment before onResume and
also fixes the "No Connections" shown just briefly after the connections
fragment is shown
This commit is contained in:
emanuele-f 2022-01-02 11:36:34 +01:00
parent 72d157d873
commit 1ba0d8008f
4 changed files with 31 additions and 19 deletions

View File

@ -50,6 +50,8 @@ import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.text.SpannableString;
@ -860,4 +862,12 @@ public class Utils {
showToastLong(ctx, R.string.no_intent_handler_found);
}
}
// Runs the specified runnable now if on the UI thread, otherwise enqueue it to the Handler
public static void runOnUi(Runnable r, Handler h) {
if(Looper.getMainLooper().getThread() == Thread.currentThread())
r.run();
else
h.post(r);
}
}

View File

@ -111,7 +111,14 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
public void onResume() {
super.onResume();
if((CaptureService.getConnsRegister() != null) || CaptureService.isServiceActive())
mEmptyText.setText(R.string.no_connections);
else
mEmptyText.setText(R.string.capture_not_running);
registerConnsListener();
mRecyclerView.setEmptyView(mEmptyText); // after registerConnsListener, when the adapter is populated
refreshMenuIcons();
}
@ -120,6 +127,7 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
super.onPause();
unregisterConnsListener();
mRecyclerView.setEmptyView(null);
if(mSearchView != null)
mQueryToApply = mSearchView.getQuery().toString();
@ -174,10 +182,6 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
mApps = new AppsResolver(requireContext());
mEmptyText = view.findViewById(R.id.no_connections);
if((requireActivity() instanceof MainActivity) &&
(((MainActivity) requireActivity()).getState() == AppState.running))
mEmptyText.setText(R.string.no_connections);
mActiveFilter = view.findViewById(R.id.active_filter);
mActiveFilter.setOnCheckedChangeListener((group, checkedId) -> {
if(mAdapter != null) {
@ -189,7 +193,6 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
mAdapter = new ConnectionsAdapter(requireContext(), mApps);
mRecyclerView.setAdapter(mAdapter);
listenerSet = false;
mRecyclerView.setEmptyView(mEmptyText);
registerForContextMenu(mRecyclerView);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(),
@ -576,7 +579,8 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
// Important: must use the provided num_connections rather than accessing the register
// in order to avoid desyncs
mHandler.post(() -> {
// using runOnUi to populate the adapter as soon as registerConnsListener is called
Utils.runOnUi(() -> {
Log.d(TAG, "New connections size: " + num_connections);
mAdapter.connectionsChanges(num_connections);
@ -584,7 +588,7 @@ public class ConnectionsFragment extends Fragment implements ConnectionsListener
if(autoScroll)
scrollToBottom();
});
}, mHandler);
}
@Override

View File

@ -105,11 +105,7 @@ public class StatusFragment extends Fragment implements AppStateListener, AppsLo
@Override
public void onResume() {
super.onResume();
if((mMenu != null) && (mActivity != null))
appStateChanged(mActivity.getState());
recheckFilterWarning();
refreshStatus();
/* Register for stats update */
mReceiver = new BroadcastReceiver() {
@ -219,9 +215,7 @@ public class StatusFragment extends Fragment implements AppStateListener, AppsLo
/* Important: call this after all the fields have been initialized */
mActivity.setAppStateListener(this);
if((mMenu != null) && (mActivity != null))
appStateChanged(mActivity.getState());
refreshStatus();
}
@Override
@ -231,9 +225,7 @@ public class StatusFragment extends Fragment implements AppStateListener, AppsLo
mMenu = menu;
mMenuItemStartBtn = mMenu.findItem(R.id.action_start);
mMenuSettings = mMenu.findItem(R.id.action_settings);
if(mActivity != null)
appStateChanged(mActivity.getState());
refreshStatus();
}
private void recheckFilterWarning() {
@ -395,6 +387,12 @@ private void refreshPcapDumpInfo() {
}
}
private void refreshStatus() {
if(mActivity != null)
appStateChanged(mActivity.getState());
recheckFilterWarning();
}
private void openAppFilterSelector() {
Dialog dialog = Utils.getAppSelectionDialog(mActivity, new ArrayList<>(), this::setAppFilter);
dialog.setOnCancelListener(dialog1 -> setAppFilter(null));

View File

@ -36,7 +36,7 @@
android:layout_marginTop="40dp"
android:textStyle="italic"
android:textSize="15sp"
android:text="@string/capture_not_started">
tools:text="@string/capture_not_started">
</TextView>
<com.emanuelef.remote_capture.views.EmptyRecyclerView