Skip to content

Commit

Permalink
Add double quotes to wifi config's SSID and password when saving.
Browse files Browse the repository at this point in the history
Bug: 79314491
Test: WifiConfigurationStringsTest
Change-Id: If41fe437ca06fe3a6fa41b582e08666d81c5b48a
  • Loading branch information
Doug Sigelbaum committed May 7, 2018
1 parent d0b0b83 commit 445c9f8
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@ import android.app.Activity
import android.content.res.Resources
import android.databinding.ObservableBoolean
import android.databinding.ViewDataBinding
import android.graphics.drawable.Drawable
import android.net.wifi.WifiConfiguration
import android.support.annotation.DimenRes
import android.support.annotation.DrawableRes
import android.text.StaticLayout
import android.util.TypedValue
import android.view.View
import androidx.view.postDelayed
import com.google.samples.apps.iosched.shared.domain.internal.DefaultScheduler

fun ObservableBoolean.hasSameValue(other: ObservableBoolean) = get() == other.get()

fun Int.isEven() = this % 2 == 0

fun View.isRtl() = layoutDirection == View.LAYOUT_DIRECTION_RTL

inline fun <T: ViewDataBinding> T.executeAfter(block: T.() -> Unit) {
inline fun <T : ViewDataBinding> T.executeAfter(block: T.() -> Unit) {
block()
executePendingBindings()
}
Expand All @@ -53,7 +51,7 @@ fun Activity.postponeEnterTransition(timeout: Long) {
/**
* Calculated the widest line in a [StaticLayout].
*/
fun StaticLayout.textWidth() : Int {
fun StaticLayout.textWidth(): Int {
var width = 0f
for (i in 0 until lineCount) {
width = width.coerceAtLeast(getLineWidth(i))
Expand All @@ -76,3 +74,54 @@ fun Resources.getFloat(@DimenRes resId: Int): Float {
getValue(resId, outValue, true)
return outValue.float
}

/**
* Return the Wifi config wrapped in quotes.
*/
fun WifiConfiguration.quoteSsidAndPassword(): WifiConfiguration {
return WifiConfiguration().apply {
SSID = this@quoteSsidAndPassword.SSID.wrapInQuotes()
preSharedKey = this@quoteSsidAndPassword.preSharedKey.wrapInQuotes()
}
}

/**
* Return the Wifi config without quotes.
*/
fun WifiConfiguration.unquoteSsidAndPassword(): WifiConfiguration {
return WifiConfiguration().apply {
SSID = this@unquoteSsidAndPassword.SSID.unwrapQuotes()
preSharedKey = this@unquoteSsidAndPassword.preSharedKey.unwrapQuotes()
}
}

fun String.wrapInQuotes(): String {
var formattedConfigString: String = this
if (!startsWith("\"")) {
formattedConfigString = "\"${formattedConfigString}"
}
if (!endsWith("\"")) {
formattedConfigString = "${formattedConfigString}\""
}
return formattedConfigString
}

fun String.unwrapQuotes(): String {
var formattedConfigString: String = this
if (formattedConfigString.startsWith("\"")) {
if (formattedConfigString.length > 1) {
formattedConfigString = formattedConfigString.substring(1)
} else {
formattedConfigString = ""
}
}
if (formattedConfigString.endsWith("\"")) {
if (formattedConfigString.length > 1) {
formattedConfigString =
formattedConfigString.substring(0, formattedConfigString.length - 1)
} else {
formattedConfigString = ""
}
}
return formattedConfigString
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiManager
import com.google.samples.apps.iosched.util.quoteSsidAndPassword
import com.google.samples.apps.iosched.util.unquoteSsidAndPassword
import javax.inject.Inject

/**
Expand All @@ -29,7 +31,8 @@ class WifiInstaller @Inject constructor(
private val wifiManager: WifiManager,
private val clipboardManager: ClipboardManager
) {
fun installConferenceWifi(conferenceWifiConfig: WifiConfiguration): Boolean {
fun installConferenceWifi(rawWifiConfig: WifiConfiguration): Boolean {
val conferenceWifiConfig = rawWifiConfig.quoteSsidAndPassword()
if (!wifiManager.isWifiEnabled) {
wifiManager.isWifiEnabled = true
}
Expand All @@ -40,7 +43,10 @@ class WifiInstaller @Inject constructor(
success = true
} else {
val clip: ClipData =
ClipData.newPlainText("wifi_password", conferenceWifiConfig.preSharedKey)
ClipData.newPlainText(
"wifi_password",
conferenceWifiConfig.unquoteSsidAndPassword().preSharedKey
)
clipboardManager.primaryClip = clip
}
return success
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.google.samples.apps.iosched.util

import junit.framework.Assert.assertEquals
import org.junit.Test

class WifiConfigurationStringsTest {
@Test
fun testWrapQuotes_emptyString() {
val original = ""
val expected = "\"\""
val quoted = original.wrapInQuotes()
assertEquals(expected, quoted)
}

@Test
fun testWrapQuotes_alreadyQuotes() {
val original = "\"io2018\""
val expected = "\"io2018\""
val quoted = original.wrapInQuotes()
assertEquals(expected, quoted)
}

@Test
fun testWrapQuotes_noQuotes() {
val original = "io2018"
val expected = "\"io2018\""
val quoted = original.wrapInQuotes()
assertEquals(expected, quoted)
}

@Test
fun testWrapQuotes_oneStartingQuote() {
val original = "\"io2018"
val expected = "\"io2018\""
val quoted = original.wrapInQuotes()
assertEquals(expected, quoted)
}

@Test
fun testWrapQuotes_oneEndingQuote() {
val original = "io2018\""
val expected = "\"io2018\""
val quoted = original.wrapInQuotes()
assertEquals(expected, quoted)
}

@Test
fun testUnwrapQuotes_emptyString() {
val original = ""
val expected = ""
val unquoted = original.unwrapQuotes()
assertEquals(expected, unquoted)
}

@Test
fun testUnwrapQuotes_alreadyQuotes() {
val original = "\"io2018\""
val expected = "io2018"
val unquoted = original.unwrapQuotes()
assertEquals(expected, unquoted)
}

@Test
fun testUnwrapQuotes_noQuotes() {
val original = "io2018"
val expected = "io2018"
val unquoted = original.unwrapQuotes()
assertEquals(expected, unquoted)
}

@Test
fun testUnwrapQuotes_oneStartingQuote() {
val original = "\"io2018"
val expected = "io2018"
val quoted = original.unwrapQuotes()
assertEquals(expected, quoted)
}

@Test
fun testUnwrapQuotes_oneEndingQuote() {
val original = "io2018\""
val expected = "io2018"
val quoted = original.unwrapQuotes()
assertEquals(expected, quoted)
}
}

0 comments on commit 445c9f8

Please sign in to comment.