Created
April 15, 2022 08:47
-
-
Save massimilianochiodi/ceaf6c31aadd19f876d7a0d838f19651 to your computer and use it in GitHub Desktop.
Show simple dialog for enter mac address ( java )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void showEnterDialog() { | |
final Dialog dialog = new Dialog(requireContext()); | |
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
dialog.setCancelable(false); | |
dialog.setContentView(R.layout.dialog); | |
EditText mMacEdit = dialog.findViewById(R.id.campo); | |
mMacEdit.addTextChangedListener(new TextWatcher() { | |
String mPreviousMac = null; | |
@Override | |
public void afterTextChanged(Editable arg0) { | |
} | |
@Override | |
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { | |
} | |
@SuppressLint("DefaultLocale") | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
String enteredMac = mMacEdit.getText().toString().toUpperCase(); | |
String cleanMac = clearNonMacCharacters(enteredMac); | |
String formattedMac = formatMacAddress(cleanMac); | |
int selectionStart = mMacEdit.getSelectionStart(); | |
formattedMac = handleColonDeletion(enteredMac, formattedMac, selectionStart); | |
int lengthDiff = formattedMac.length() - enteredMac.length(); | |
setMacEdit(cleanMac, formattedMac, selectionStart, lengthDiff); | |
} | |
/** | |
* Strips all characters from a string except A-F and 0-9. | |
* @param mac User input string. | |
* @return String containing MAC-allowed characters. | |
*/ | |
private String clearNonMacCharacters(String mac) { | |
return mac.replaceAll("[^A-Fa-f0-9]", ""); | |
} | |
/** | |
* Adds a colon character to an unformatted MAC address after | |
* every second character (strips full MAC trailing colon) | |
* @param cleanMac Unformatted MAC address. | |
* @return Properly formatted MAC address. | |
*/ | |
private String formatMacAddress(String cleanMac) { | |
int grouppedCharacters = 0; | |
StringBuilder formattedMac = new StringBuilder(); | |
for (int i = 0; i < cleanMac.length(); ++i) { | |
formattedMac.append(cleanMac.charAt(i)); | |
++grouppedCharacters; | |
if (grouppedCharacters == 2) { | |
formattedMac.append(":"); | |
grouppedCharacters = 0; | |
} | |
} | |
// Removes trailing colon for complete MAC address | |
if (cleanMac.length() == 12) | |
formattedMac = new StringBuilder(formattedMac.substring(0, formattedMac.length() - 1)); | |
return formattedMac.toString(); | |
} | |
/** | |
* Upon users colon deletion, deletes MAC character preceding deleted colon as well. | |
* @param enteredMac User input MAC. | |
* @param formattedMac Formatted MAC address. | |
* @param selectionStart MAC EditText field cursor position. | |
* @return Formatted MAC address. | |
*/ | |
private String handleColonDeletion(String enteredMac, String formattedMac, int selectionStart) { | |
if (mPreviousMac != null && mPreviousMac.length() > 1) { | |
int previousColonCount = colonCount(mPreviousMac); | |
int currentColonCount = colonCount(enteredMac); | |
if (currentColonCount < previousColonCount) { | |
formattedMac = formattedMac.substring(0, selectionStart - 1) + formattedMac.substring(selectionStart); | |
String cleanMac = clearNonMacCharacters(formattedMac); | |
formattedMac = formatMacAddress(cleanMac); | |
} | |
} | |
return formattedMac; | |
} | |
/** | |
* Gets MAC address current colon count. | |
* @param formattedMac Formatted MAC address. | |
* @return Current number of colons in MAC address. | |
*/ | |
private int colonCount(String formattedMac) { | |
return formattedMac.replaceAll("[^:]", "").length(); | |
} | |
/** | |
* Removes TextChange listener, sets MAC EditText field value, | |
* sets new cursor position and re-initiates the listener. | |
* @param cleanMac Clean MAC address. | |
* @param formattedMac Formatted MAC address. | |
* @param selectionStart MAC EditText field cursor position. | |
* @param lengthDiff Formatted/Entered MAC number of characters difference. | |
*/ | |
private void setMacEdit(String cleanMac, String formattedMac, int selectionStart, int lengthDiff) { | |
mMacEdit.removeTextChangedListener(this); | |
if (cleanMac.length() <= 12) { | |
mMacEdit.setText(formattedMac); | |
mMacEdit.setSelection(selectionStart + lengthDiff); | |
mPreviousMac = formattedMac; | |
} else { | |
mMacEdit.setText(mPreviousMac); | |
mMacEdit.setSelection(mPreviousMac.length()); | |
} | |
mMacEdit.addTextChangedListener(this); | |
} | |
}); | |
Button dialogButton = dialog.findViewById(R.id.okbutton); | |
dialogButton.setOnClickListener(v -> { | |
String indirizzo = mMacEdit.getText().toString(); | |
aggiungiindirizzomac(indirizzo); | |
dialog.dismiss(); | |
}); | |
dialog.show(); | |
Window window = dialog.getWindow(); | |
assert window != null; | |
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment