Created
May 19, 2014 09:00
-
-
Save bunjix/c1f0eef25255751c54ac to your computer and use it in GitHub Desktop.
Konami Code Helper
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
import android.text.Editable; | |
import android.text.TextUtils; | |
import android.text.TextWatcher; | |
import android.view.Gravity; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
public class KonamiCodeHelper { | |
private static final String KONAMI_CODE = "UUDDLRLRBA"; | |
public interface KonamiCodeCallback { | |
public void onKonamiCodeEntered(); | |
} | |
public static void listenKonamiCode(final EditText editText, final KonamiCodeCallback callback) { | |
if (editText == null || callback == null) { | |
throw new IllegalArgumentException("You must provide an EditText and a KonamiCodeCallback."); | |
} | |
editText.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
if (callback != null | |
&& isValidKonamiCode(editText.getText().toString())) { | |
final Toast toast = Toast.makeText(editText.getContext(), "Konami Code !!", Toast.LENGTH_SHORT); | |
toast.setGravity(Gravity.CENTER, 0, 0); | |
toast.show(); | |
callback.onKonamiCodeEntered(); | |
} | |
} | |
@Override | |
public void afterTextChanged(Editable s) {} | |
}); | |
} | |
private static boolean isValidKonamiCode(String entered) { | |
return !TextUtils.isEmpty(entered) && entered.toUpperCase().equalsIgnoreCase(KONAMI_CODE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment