Created
May 5, 2012 00:30
-
-
Save ktoso/2598757 to your computer and use it in GitHub Desktop.
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
// in onCreate of your Activity | |
phone.number.addListener({ myNumberText := phone.number.get() }, executor) |
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
def telephonyManager: TelephonyManager = _ | |
lazy val number: SettableFuture[String] = { | |
telephonyManager.getLine1Number match { | |
case "" => | |
askUserForHisNumber() // it's empty! Let's ask the user instead... | |
case nr => | |
val future: SettableFuture[String] = SettableFuture.create() | |
future.set(nr) // That's a bit boilerplate-ish, but since we stick to SettableFuture we have to do this | |
future | |
} | |
} |
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
def askUserForHisNumber(): SettableFuture[String] = sharedPreferencesPhoneNumber match { | |
case "" => // so it was not stored in the shared preferences... | |
val futureNumber: SettableFuture[String] = SettableFuture.create() // we'll complete it in a bit | |
// it's always nice to create an edit text "the way it should work", notice how we add hints and set the InputType to "make sense" in this task | |
// I've seen many apps skipping to set the InputType on EditTexts - it's really a pain with these apps... @Devs: just add this one line! :-) | |
val input = new EditText(ctx) | |
input.setText("+") | |
input.setHint("+48555222111") | |
input.setSingleLine() | |
input.setInputType(InputType.TYPE_CLASS_PHONE) | |
val builder = new AlertDialog.Builder(ctx) | |
builder.setView(input) | |
builder.setPositiveButton(ctx.getString(R.string.done), () => { | |
val num = input.getText.toString | |
if(num.matches("\\\\+\\d{8,}")) { // just a quick sanity check, if the number makes sense | |
futureNumber.set(num) // we complete the future | |
sharedPreferencesPhoneNumber = num // that will actually persist the number in the our SharedPreferences, no need for the pref.edit() etc boilerplate when using Scala :-) | |
} else { | |
askUserForHisNumber() // let's try again ;-) | |
} | |
}) | |
builder.setCancelable(false) | |
builder.setTitle(ctx.getString(R.string.enter_your_number)) | |
builder.setMessage(ctx.getString(R.string.please_type_in_your_number)) | |
builder.show() | |
futureNumber | |
case num => // yay, it was already stored in shared preferences, let's use it | |
info("Phone number was already stored in shared preferences: " + num) | |
val future: SettableFuture[String] = SettableFuture.create() | |
future.set(num) | |
future | |
} |
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
val telephonyManager = ctx.getSystemService(Context.TELEPHONY_SERVICE).asInstanceOf[TelephonyManager] | |
val myNumber = telephonyManager.getLine1Number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment