-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Shingyx/mac-address-dialog-colons
Auto insert MAC addr colons in dialog
- Loading branch information
Showing
2 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
wakeonlan/src/main/java/com/github/shingyx/wakeonlan/ui/MacAddressTextWatcher.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.shingyx.wakeonlan.ui | ||
|
||
import android.text.Editable | ||
import android.text.TextWatcher | ||
|
||
class MacAddressTextWatcher : TextWatcher { | ||
private var previousText: String = "" | ||
|
||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { | ||
previousText = s.toString() | ||
} | ||
|
||
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} | ||
|
||
override fun afterTextChanged(editable: Editable) { | ||
val text = editable.toString() | ||
val formattedText = formatMacAddress(text) | ||
if (formattedText != text) { | ||
editable.replace(0, editable.length, formattedText) | ||
} | ||
} | ||
|
||
private fun formatMacAddress(text: String): String { | ||
val builder = StringBuilder() | ||
var digits = 0 | ||
for (char in text) { | ||
if (char.isLetterOrDigit()) { | ||
if (digits % 2 == 0 && digits > 0 && digits < 12) { | ||
builder.append(':') | ||
} | ||
builder.append(char) | ||
digits++ | ||
} | ||
} | ||
if (text.lastOrNull() == ':') { | ||
builder.append(':') | ||
} | ||
return builder.toString() | ||
} | ||
} |
This file contains 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