Skip to content

Commit

Permalink
Merge pull request #2 from Jolanrensen/usingSecureSettingsPermission
Browse files Browse the repository at this point in the history
v2.1:
  • Loading branch information
Jolanrensen authored Oct 13, 2019
2 parents 4d9c9e1 + 565b219 commit 80305e0
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 45 deletions.
4 changes: 2 additions & 2 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "nl.jolanrensen.permanentproxy"
minSdkVersion 23
targetSdkVersion 29
versionCode 4
versionName "2.0"
versionCode 5
versionName "2.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name">Permanent Proxy</string>
<string name="wrapper_app">This app is just a phone companion of Permanent Proxy. Please install Permanent Proxy from the Play Store on you Wear OS watch.</string>
<string name="instructions"><![CDATA[To get started, first enable the Developer Options of your watch, which can be achieved by going to Settings -> System -> About and tap the Build number until you are a \"developer\".\nNext, go to Settings -> Developer options and enable \"ADB debugging\" and \"Debug over Bluetooth\".\nFinally start up Permanent Proxy and press \"Allow\" or \"Always allow this Computer\" if prompted.\nAfter the app has requested permission, ADB can be turned off again.\n\nNow you can get started! Simply enter a proxy address and port, enable it (and on boot if you like) and you\'re done!\n\nProxy services can be found online and can be from any country. However, do make sure you completely trust the proxy you chose before you enter it! All the data of your watch might be sent through that proxy, even sensitive data, so act at your own risk.]]></string>
<string name="instructions"><![CDATA[To get started, first enable the Developer Options of your watch, which can be achieved by going to Settings -> System -> About and tap the Build number until you are a \"developer\".\nNext, go to Settings -> Developer options and enable \"ADB debugging\" and \"Debug over Bluetooth\".\nFinally start up Permanent Proxy, request permission and press \"Allow\" or \"Always allow this Computer\" if prompted.\nAfter the app has gotten permission, ADB can be turned off again.\n\nNow you can get started! Simply enter a proxy address and port, enable it (and on boot if you like) and you\'re done!\n\nProxy services can be found online and can be from any country. However, do make sure you completely trust the proxy you chose before you enter it! All the data of your watch might be sent through that proxy, even sensitive data, so act at your own risk.]]></string>
</resources>
4 changes: 2 additions & 2 deletions wear/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "nl.jolanrensen.permanentproxy"
minSdkVersion 23
targetSdkVersion 29
versionCode 4
versionName "2.0"
versionCode 5
versionName "2.1"
}
buildTypes {
release {
Expand Down
Binary file modified wear/release/mobile-release.apk
Binary file not shown.
Binary file modified wear/release/wear-release.aab
Binary file not shown.
Binary file modified wear/release/wear-release.apk
Binary file not shown.
16 changes: 11 additions & 5 deletions wear/src/main/java/nl/jolanrensen/permanentproxy/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ object Constants {
)
}

fun Context.stopProxy() {
Settings.Global.putString(contentResolver, Settings.Global.HTTP_PROXY, null)
Settings.Global.putString(contentResolver, "global_http_proxy_host", null)
Settings.Global.putString(contentResolver, "global_http_proxy_port", null)
fun Context.stopProxy(updateGooglePay: Boolean = true) {
Settings.Secure.putString(
contentResolver,
Settings.Global.HTTP_PROXY, "127.0.0.1:8007")
if (updateGooglePay) sendBroadcast(
Intent("android.server.checkin.CHECKIN")
)
}

fun Context.getCurrentProxy() = try {
Settings.Global.getString(contentResolver, Settings.Global.HTTP_PROXY)
(Settings.Global.getString(contentResolver, "global_http_proxy_host") + ":" +
Settings.Global.getString(contentResolver, "global_http_proxy_port")).let {
if (it == "127.0.0.1:8007") null else it
}
} catch (e: Exception) {
logE("proxy", e)
null
Expand Down
52 changes: 34 additions & 18 deletions wear/src/main/java/nl/jolanrensen/permanentproxy/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import nl.jolanrensen.permanentproxy.Constants.stopProxy
import nl.jolanrensen.permanentproxy.Constants.toast
import kotlin.concurrent.thread
import android.content.pm.PackageManager
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import nl.jolanrensen.permanentproxy.Constants.getCurrentProxy
import nl.jolanrensen.permanentproxy.Constants.toastLong

Expand Down Expand Up @@ -56,7 +59,18 @@ class MainActivity : WearableActivity() {

// first check if app has permission to write Secure Settings, else ask to turn on adb over bluetooth
if (checkCallingOrSelfPermission("android.permission.WRITE_SECURE_SETTINGS")
!= PackageManager.PERMISSION_GRANTED) {
== PackageManager.PERMISSION_GRANTED) {
continueSetup()
}

show_me_how.setOnClickListener {
startActivity(
Intent(this, EnableADBBluetoothActivity::class.java)
)
}

request_permission.setOnClickListener {
loading.isVisible = true
thread(start = true) {
val logs = arrayListOf<String>()
try {
Expand All @@ -71,8 +85,10 @@ class MainActivity : WearableActivity() {
) {
logD(it.toString())
runOnUiThread {
loading.isVisible = false
if (checkCallingOrSelfPermission("android.permission.WRITE_SECURE_SETTINGS")
== PackageManager.PERMISSION_GRANTED) {
toastLong(getString(R.string.permission_granted))
continueSetup()
} else {
toastLong(getString(R.string.something_wrong))
Expand All @@ -82,26 +98,25 @@ class MainActivity : WearableActivity() {
} catch (e: Exception) {
logE("$logs", e)
runOnUiThread {
runOnUiThread {
status.text = getString(R.string.adb_enabled)
show_me_how.isVisible = true
}
loading.isVisible = false
toastLong(getString(R.string.something_wrong))
}
}
}
} else {
continueSetup()
}


show_me_how.setOnClickListener {
startActivity(
Intent(this, EnableADBBluetoothActivity::class.java)
)
}
}

private fun setupStatus(): Boolean {
private fun setupStatus(wait: Boolean = false): Boolean {
if (wait) {
GlobalScope.launch {
delay(100)
runOnUiThread {
val proxy = getCurrentProxy()
status.text = proxy ?: getString(R.string.not_enabled)
}
}
return false
}
val proxy = getCurrentProxy()
status.text = proxy ?: getString(R.string.not_enabled)
return proxy != null
Expand All @@ -110,6 +125,7 @@ class MainActivity : WearableActivity() {
private fun continueSetup() {
setAllEnabled(main_menu)
show_me_how.isVisible = false
request_permission.isVisible = false

val enabled = setupStatus()

Expand All @@ -130,7 +146,7 @@ class MainActivity : WearableActivity() {
updateGooglePay = true
)
}
setupStatus()
setupStatus(wait = true)
}
}

Expand All @@ -148,7 +164,7 @@ class MainActivity : WearableActivity() {
set_proxy_address.setOnClickListener {
if (proxy_switch.isChecked) {
stopProxy()
setupStatus()
setupStatus(wait = true)
proxy_switch.isChecked = false
}
text_input.isVisible = true
Expand Down Expand Up @@ -182,7 +198,7 @@ class MainActivity : WearableActivity() {
set_proxy_port.setOnClickListener {
if (proxy_switch.isChecked) {
stopProxy()
setupStatus()
setupStatus(wait = true)
proxy_switch.isChecked = false
}
port_input.isVisible = true
Expand Down
75 changes: 62 additions & 13 deletions wear/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
android:paddingEnd="8dp">

<LinearLayout
android:layout_width="match_parent"
android:id="@+id/main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="@string/status"
android:textAppearance="@android:style/TextAppearance.Material.Medium"
android:textStyle="bold" />
Expand All @@ -36,38 +36,57 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/requesting_permission"
android:textAppearance="@android:style/TextAppearance.Material.Body1"
tools:text="@string/adb_enabled" />
android:text="@string/wss_permission_needed"
android:textAppearance="@android:style/TextAppearance.Material.Body1" />


<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@color/grey" />

<TextView
android:id="@+id/show_me_how"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:enabled="true"
android:visibility="gone"
tools:visibility="visible"
android:gravity="center_vertical"
android:minHeight="45dp"
android:text="@string/show_me_how"
android:textAppearance="@android:style/TextAppearance.Material.Body1" />
android:textAppearance="@android:style/TextAppearance.Material.Body1"
/>

<TextView
android:id="@+id/request_permission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:enabled="true"
android:gravity="center_vertical"
android:minHeight="45dp"
android:text="@string/request_permission"
android:textAppearance="@android:style/TextAppearance.Material.Body1"
/>

<Switch
android:id="@+id/proxy_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:minHeight="45dp"
android:layout_marginTop="8dp"
android:enabled="false"
android:minHeight="45dp"
android:text="@string/proxy" />

<Switch
android:id="@+id/on_boot_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="45dp"
android:enabled="false"
android:minHeight="45dp"
android:text="@string/start_on_boot" />

<TextView
Expand All @@ -85,11 +104,11 @@
android:id="@+id/set_proxy_port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="45dp"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:enabled="false"
android:gravity="center_vertical"
android:minHeight="45dp"
android:layout_marginBottom="45dp"
android:text="@string/set_proxy_port"
android:textAppearance="@android:style/TextAppearance.Material.Body1" />

Expand All @@ -113,4 +132,34 @@
</LinearLayout>

</ScrollView>

<RelativeLayout
android:id="@+id/loading"
android:visibility="gone"
tools:visibility="visible"
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/requesting_permission"
android:textAlignment="center" />

<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:foregroundGravity="center" />

</RelativeLayout>
</androidx.wear.widget.BoxInsetLayout>
11 changes: 7 additions & 4 deletions wear/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
<string name="set_proxy">Set proxy</string>
<string name="set_proxy_address">Set proxy address</string>
<string name="set_proxy_port">Set proxy port</string>
<string name="not_enabled">Not enabled</string>
<string name="not_enabled">Proxy not enabled</string>
<string name="something_wrong">Something went wrong, try again</string>
<string name="show_me_how">Show me how</string>
<string name="show_me_how">How to enable ADB over Bluetooth?</string>
<string name="title_activity_enable_adbbluetooth">EnableADBBluetoothActivity</string>
<string name="instructions"><![CDATA[To get started, first enable the Developer Options of your watch, which can be achieved by going to Settings -> System -> About and tap the Build number until you are a \"developer\".\nNext, go to Settings -> Developer options and enable \"ADB debugging\" and \"Debug over Bluetooth\".\nFinally start up Permanent Proxy and press \"Allow\" or \"Always allow this Computer\" if prompted.\nAfter the app has requested permission, ADB can be turned off again.\n\nNow you can get started! Simply enter a proxy address and port, enable it (and on boot if you like) and you\'re done!\n\nProxy services can be found online and can be from any country. However, do make sure you completely trust the proxy you chose before you enter it! All the data of your watch might be sent through that proxy, even sensitive data, so act at your own risk.]]></string>
<string name="requesting_permission">Requesting permission…</string>
<string name="instructions"><![CDATA[To get started, first enable the Developer Options of your watch, which can be achieved by going to Settings -> System -> About and tap the Build number until you are a \"developer\".\nNext, go to Settings -> Developer options and enable \"ADB debugging\" and \"Debug over Bluetooth\".\nFinally start up Permanent Proxy, request permission and press \"Allow\" or \"Always allow this Computer\" if prompted.\nAfter the app has gotten permission, ADB can be turned off again.\n\nNow you can get started! Simply enter a proxy address and port, enable it (and on boot if you like) and you\'re done!\n\nProxy services can be found online and can be from any country. However, do make sure you completely trust the proxy you chose before you enter it! All the data of your watch might be sent through that proxy, even sensitive data, so act at your own risk.]]></string>
<string name="requesting_permission">Requesting permission, this can take a while… \"Allow\" or \"Always allow computer\" if prompted.</string>
<string name="wss_permission_needed">Write secure settings permission needed. This can be enabled via ADB over Bluetooth.</string>
<string name="request_permission">ADB over Bluetooth is enabled, request permission!</string>
<string name="permission_granted">Permission granted, ADB can be turned off now.</string>
</resources>

0 comments on commit 80305e0

Please sign in to comment.