Skip to content

Commit 23acca5

Browse files
authored
Merge branch 'tytydraco:main' into main
2 parents 8bda609 + 6ec81c4 commit 23acca5

File tree

15 files changed

+123
-257
lines changed

15 files changed

+123
-257
lines changed

app/build.gradle

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
applicationId "com.draco.ladb"
1313
minSdk 26
1414
targetSdk 33
15-
versionCode 39
16-
versionName "2.1"
15+
versionCode 41
16+
versionName "2.2.1"
1717

1818
ndk {
1919
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
@@ -58,15 +58,15 @@ dependencies {
5858
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
5959

6060
implementation 'androidx.core:core-ktx:1.9.0'
61-
implementation 'androidx.appcompat:appcompat:1.5.1'
61+
implementation 'androidx.appcompat:appcompat:1.6.1'
6262
implementation 'androidx.preference:preference-ktx:1.2.0'
63-
implementation 'androidx.fragment:fragment-ktx:1.5.3'
64-
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
65-
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
66-
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
63+
implementation 'androidx.fragment:fragment-ktx:1.5.6'
64+
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
65+
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
66+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
6767

6868
implementation 'com.github.javiersantos:PiracyChecker:1.2.8'
6969

70-
implementation 'com.google.android.material:material:1.6.1'
70+
implementation 'com.google.android.material:material:1.7.0'
7171
implementation 'com.google.android.gms:play-services-oss-licenses:17.0.0'
7272
}

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@
3232
<category android:name="android.intent.category.LAUNCHER" />
3333
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
3434
</intent-filter>
35-
<intent-filter>
36-
<action android:name="android.intent.action.SEND" />
37-
<category android:name="android.intent.category.DEFAULT" />
38-
39-
<data android:mimeType="text/x-sh" />
40-
<data android:mimeType="text/plain" />
41-
</intent-filter>
4235
</activity>
43-
<activity
44-
android:name=".views.PairActivity"
45-
android:label="@string/pair" />
4636
<activity
4737
android:name=".views.HelpActivity"
4838
android:label="@string/more" />

app/src/main/java/com/draco/ladb/utils/ADB.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,21 @@ class ADB(private val context: Context) {
8989
/* Only do wireless debugging steps on compatible versions */
9090
if (secureSettingsGranted) {
9191
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !isWirelessDebuggingEnabled()) {
92-
debug("Enabling wireless debugging...")
9392
Settings.Global.putInt(
9493
context.contentResolver,
9594
"adb_wifi_enabled",
9695
1
9796
)
9897

99-
Thread.sleep(3_000)
98+
Thread.sleep(2_000)
10099
} else if (!isUSBDebuggingEnabled()) {
101-
debug("Enabling USB debugging...")
102100
Settings.Global.putInt(
103101
context.contentResolver,
104102
Settings.Global.ADB_ENABLED,
105103
1
106104
)
107105

108-
Thread.sleep(3_000)
106+
Thread.sleep(2_000)
109107
}
110108
}
111109

@@ -128,23 +126,19 @@ class ADB(private val context: Context) {
128126
}
129127
}
130128

131-
debug("Starting ADB server...")
132129
adb(false, listOf("start-server")).waitFor()
133130
debug("Waiting for device to connect...")
134-
debug("This may take up to 2 minutes")
135-
val waitProcess = adb(false, listOf("wait-for-device")).waitFor(2, TimeUnit.MINUTES)
131+
debug("This may take a minute")
132+
val waitProcess = adb(false, listOf("wait-for-device")).waitFor(1, TimeUnit.MINUTES)
136133
if (!waitProcess) {
137-
debug("Could not detect any devices")
138-
debug("Fix 1) Toggle Wi-Fi or reboot")
139-
debug("Fix 2) Re-enter pairing information (More -> Factory Reset)")
140-
debug("To try again, restart the server (More -> Restart)")
134+
debug("Your device didn't connect to LADB")
135+
debug("If a reboot doesn't work, please contact support")
141136

142137
tryingToPair = false
143138
return false
144139
}
145140
}
146141

147-
debug("Shelling into device")
148142
shellProcess = if (autoShell) {
149143
val argList = if (Build.SUPPORTED_ABIS[0] == "arm64-v8a")
150144
listOf("-t", "1", "shell")
@@ -216,6 +210,11 @@ class ADB(private val context: Context) {
216210
/* Continue once finished pairing (or 10s elapses) */
217211
pairShell.waitFor(10, TimeUnit.SECONDS)
218212
pairShell.destroyForcibly().waitFor()
213+
214+
val killShell = adb(false, listOf("kill-server"))
215+
killShell.waitFor(3, TimeUnit.SECONDS)
216+
killShell.destroyForcibly()
217+
219218
return pairShell.exitValue() == 0
220219
}
221220

app/src/main/java/com/draco/ladb/views/MainActivity.kt

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.draco.ladb.views
22

33
import android.content.Intent
4+
import android.net.Uri
45
import android.os.Bundle
56
import android.view.KeyEvent
67
import android.view.Menu
@@ -12,14 +13,17 @@ import android.view.inputmethod.InputMethodManager
1213
import android.widget.ScrollView
1314
import androidx.activity.result.contract.ActivityResultContracts
1415
import androidx.activity.viewModels
16+
import androidx.appcompat.app.AlertDialog
1517
import androidx.appcompat.app.AppCompatActivity
1618
import androidx.core.content.FileProvider
1719
import androidx.lifecycle.lifecycleScope
1820
import com.draco.ladb.BuildConfig
1921
import com.draco.ladb.R
2022
import com.draco.ladb.databinding.ActivityMainBinding
2123
import com.draco.ladb.viewmodels.MainActivityViewModel
24+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2225
import com.google.android.material.snackbar.Snackbar
26+
import com.google.android.material.textfield.TextInputEditText
2327
import kotlinx.coroutines.Dispatchers
2428
import kotlinx.coroutines.launch
2529
import kotlin.system.exitProcess
@@ -28,36 +32,23 @@ class MainActivity : AppCompatActivity() {
2832
private val viewModel: MainActivityViewModel by viewModels()
2933
private lateinit var binding: ActivityMainBinding
3034

35+
private lateinit var pairDialog: MaterialAlertDialogBuilder
36+
3137
private var lastCommand = ""
3238

3339
private var bookmarkGetResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
3440
val text = it.data?.getStringExtra(Intent.EXTRA_TEXT) ?: return@registerForActivityResult
3541
binding.command.setText(text)
3642
}
3743

38-
private var pairGetResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
39-
val port = result.data?.getStringExtra("port") ?: ""
40-
val code = result.data?.getStringExtra("code") ?: ""
41-
42-
viewModel.adb.debug("Trying to pair...")
43-
lifecycleScope.launch(Dispatchers.IO) {
44-
val success = viewModel.adb.pair(port, code)
45-
46-
if (success) {
47-
viewModel.setPairedBefore(true)
48-
viewModel.startADBServer()
49-
} else {
50-
/* Failed; try again! */
51-
viewModel.setPairedBefore(false)
52-
viewModel.adb.debug("Failed to pair! Trying again...")
53-
runOnUiThread { pairAndStart() }
54-
}
55-
56-
viewModel.isPairing.postValue(false)
57-
}
58-
}
59-
6044
private fun setupUI() {
45+
pairDialog = MaterialAlertDialogBuilder(this)
46+
.setTitle(R.string.pair_title)
47+
.setCancelable(false)
48+
.setView(R.layout.dialog_pair)
49+
.setPositiveButton(R.string.pair, null)
50+
.setNegativeButton(R.string.help, null)
51+
6152
binding.command.setOnKeyListener { _, keyCode, keyEvent ->
6253
if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.action == KeyEvent.ACTION_DOWN) {
6354
sendCommandToADB()
@@ -117,23 +108,23 @@ class MainActivity : AppCompatActivity() {
117108

118109
/* Prepare progress bar, pairing latch, and script executing */
119110
viewModel.adb.started.observe(this) { started ->
120-
if (started == true) {
121-
lifecycleScope.launch(Dispatchers.IO) {
122-
runOnUiThread { setReadyForInput(true) }
123-
executeScriptFromIntent()
124-
}
125-
} else {
126-
runOnUiThread { setReadyForInput(false) }
127-
return@observe
128-
}
111+
setReadyForInput(started == true)
129112
}
130113
}
131114

132115
private fun pairAndStart() {
133116
if (viewModel.needsToPair()) {
134117
viewModel.adb.debug("Requesting pairing information")
135-
viewModel.isPairing.value = true
136-
pairGetResult.launch(Intent(this, PairActivity::class.java))
118+
askToPair { thisPairSuccess ->
119+
if (thisPairSuccess) {
120+
viewModel.setPairedBefore(true)
121+
viewModel.startADBServer()
122+
} else {
123+
/* Failed; try again! */
124+
viewModel.adb.debug("Failed to pair! Trying again...")
125+
runOnUiThread { pairAndStart() }
126+
}
127+
}
137128
} else {
138129
viewModel.startADBServer()
139130
}
@@ -153,22 +144,43 @@ class MainActivity : AppCompatActivity() {
153144
}
154145

155146
/**
156-
* Execute a script from the main intent if one was given
147+
* Ask the user to pair
157148
*/
158-
private fun executeScriptFromIntent() {
159-
if (viewModel.getScriptFromIntent(intent) == null)
160-
return
161-
162-
val code = viewModel.getScriptFromIntent(intent) ?: return
163-
164-
/* Invalidate intent */
165-
intent.type = ""
166-
167-
Snackbar.make(binding.output, getString(R.string.snackbar_file_opened), Snackbar.LENGTH_SHORT)
168-
.setAction(getString(R.string.dismiss)) {}
149+
private fun askToPair(callback: ((Boolean) -> (Unit))? = null) {
150+
pairDialog
151+
.create()
152+
.apply {
153+
setOnShowListener {
154+
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
155+
val port = findViewById<TextInputEditText>(R.id.port)!!.text.toString()
156+
val code = findViewById<TextInputEditText>(R.id.code)!!.text.toString()
157+
dismiss()
158+
159+
lifecycleScope.launch(Dispatchers.IO) {
160+
viewModel.adb.debug("Trying to pair...")
161+
val success = viewModel.adb.pair(port, code)
162+
callback?.invoke(success)
163+
}
164+
}
165+
166+
getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
167+
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.tutorial_url)))
168+
try {
169+
startActivity(intent)
170+
} catch (e: Exception) {
171+
e.printStackTrace()
172+
Snackbar.make(
173+
binding.output,
174+
getString(R.string.snackbar_intent_failed),
175+
Snackbar.LENGTH_SHORT
176+
)
177+
.show()
178+
}
179+
180+
}
181+
}
182+
}
169183
.show()
170-
171-
viewModel.adb.sendScript(code)
172184
}
173185

174186
override fun onOptionsItemSelected(item: MenuItem): Boolean {

app/src/main/java/com/draco/ladb/views/PairActivity.kt

Lines changed: 0 additions & 76 deletions
This file was deleted.
114 KB
Binary file not shown.
23.9 KB
Binary file not shown.

app/src/main/jniLibs/x86/libadb.so

38.6 KB
Binary file not shown.

app/src/main/jniLibs/x86_64/libadb.so

26.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)