Search in sources :

Example 1 with SubnetScanner

use of com.amaze.filemanager.utils.SubnetScanner in project AmazeFileManager by TeamAmaze.

the class SmbSearchDialog method onCreateDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity());
    builder.title(R.string.searchingdevices);
    builder.negativeColor(accentColor);
    builder.negativeText(R.string.cancel);
    builder.onNegative((dialog, which) -> {
        if (subnetScanner != null)
            subnetScanner.interrupt();
        dismiss();
    });
    builder.onPositive((dialog, which) -> {
        if (subnetScanner != null)
            subnetScanner.interrupt();
        if (getActivity() != null && getActivity() instanceof MainActivity) {
            dismiss();
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.showSMBDialog("", "", false);
        }
    });
    builder.positiveText(R.string.use_custom_ip);
    builder.positiveColor(accentColor);
    computers.add(new ComputerParcelable("-1", "-1"));
    listViewAdapter = new ListViewAdapter(getActivity(), computers);
    subnetScanner = new SubnetScanner(getActivity());
    subnetScanner.setObserver(new SubnetScanner.ScanObserver() {

        @Override
        public void computerFound(final ComputerParcelable computer) {
            if (getActivity() != null)
                getActivity().runOnUiThread(() -> {
                    if (!computers.contains(computer))
                        computers.add(computer);
                    listViewAdapter.notifyDataSetChanged();
                });
        }

        @Override
        public void searchFinished() {
            if (getActivity() != null) {
                getActivity().runOnUiThread(() -> {
                    if (computers.size() == 1) {
                        dismiss();
                        Toast.makeText(getActivity(), getResources().getString(R.string.nodevicefound), Toast.LENGTH_SHORT).show();
                        MainActivity mainActivity = (MainActivity) getActivity();
                        mainActivity.showSMBDialog("", "", false);
                        return;
                    }
                    computers.remove(computers.size() - 1);
                    listViewAdapter.notifyDataSetChanged();
                });
            }
        }
    });
    subnetScanner.start();
    builder.adapter(listViewAdapter, null);
    return builder.build();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) MainActivity(com.amaze.filemanager.activities.MainActivity) ComputerParcelable(com.amaze.filemanager.utils.ComputerParcelable) SubnetScanner(com.amaze.filemanager.utils.SubnetScanner)

Aggregations

MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 MainActivity (com.amaze.filemanager.activities.MainActivity)1 ComputerParcelable (com.amaze.filemanager.utils.ComputerParcelable)1 SubnetScanner (com.amaze.filemanager.utils.SubnetScanner)1