Skip to content

Commit 06d3157

Browse files
committed
settings: add option to change ADB local server port
Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
1 parent c9ba1d1 commit 06d3157

9 files changed

Lines changed: 50 additions & 11 deletions

File tree

app/src/main/java/io/github/muntashirakon/AppManager/servermanager/LocalServer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import android.annotation.SuppressLint;
66
import android.content.Context;
7-
import android.os.UserHandleHidden;
87

98
import androidx.annotation.AnyThread;
109
import androidx.annotation.GuardedBy;
@@ -87,7 +86,7 @@ private LocalServer() throws IOException, AdbPairingRequiredException {
8786
mContext = ContextUtils.getDeContext(ContextUtils.getContext());
8887
mLocalServerManager = LocalServerManager.getInstance(mContext);
8988
// Initialise necessary files and permissions
90-
ServerConfig.init(mContext, UserHandleHidden.myUserId());
89+
ServerConfig.init(mContext);
9190
// Start server if not already
9291
checkConnect();
9392
}

app/src/main/java/io/github/muntashirakon/AppManager/servermanager/ServerConfig.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package io.github.muntashirakon.AppManager.servermanager;
44

55
import android.annotation.SuppressLint;
6-
import android.annotation.UserIdInt;
76
import android.content.Context;
87
import android.content.SharedPreferences;
98
import android.os.Build;
@@ -25,6 +24,7 @@
2524
import io.github.muntashirakon.AppManager.logs.Log;
2625
import io.github.muntashirakon.AppManager.misc.NoOps;
2726
import io.github.muntashirakon.AppManager.server.common.Constants;
27+
import io.github.muntashirakon.AppManager.settings.Prefs;
2828
import io.github.muntashirakon.AppManager.utils.ContextUtils;
2929
import io.github.muntashirakon.AppManager.utils.FileUtils;
3030

@@ -34,18 +34,16 @@ public final class ServerConfig {
3434

3535
public static final int DEFAULT_ADB_PORT = 5555;
3636
static final String SERVER_RUNNER_EXEC_NAME = "run_server.sh";
37-
private static final int DEFAULT_LOCAL_SERVER_PORT = 60001;
3837
private static final String LOCAL_TOKEN = "l_token";
3938
private static final File[] SERVER_RUNNER_EXEC = new File[2];
4039
private static final File[] SERVER_RUNNER_JAR = new File[2];
4140
private static final SharedPreferences sPreferences = ContextUtils.getContext()
4241
.getSharedPreferences("server_config", Context.MODE_PRIVATE);
43-
private static int sServerPort = DEFAULT_LOCAL_SERVER_PORT;
4442
private static volatile boolean sInitialised = false;
4543

4644
@WorkerThread
4745
@NoOps
48-
public static void init(@NonNull Context context, @UserIdInt int userHandle) throws IOException {
46+
public static void init(@NonNull Context context) throws IOException {
4947
if (sInitialised) {
5048
return;
5149
}
@@ -69,9 +67,6 @@ public static void init(@NonNull Context context, @UserIdInt int userHandle) thr
6967
FileUtils.chmod711(deStorage);
7068
FileUtils.chmod644(SERVER_RUNNER_JAR[1]);
7169
FileUtils.chmod644(SERVER_RUNNER_EXEC[1]);
72-
if (userHandle != 0) {
73-
sServerPort += userHandle;
74-
}
7570

7671
sInitialised = true;
7772
}
@@ -133,7 +128,7 @@ public static void setAdbPort(@IntRange(from = 0, to = 65535) int port) {
133128

134129
@AnyThread
135130
public static int getLocalServerPort() {
136-
return sServerPort;
131+
return Prefs.Misc.getAdbLocalServerPort();
137132
}
138133

139134
@WorkerThread

app/src/main/java/io/github/muntashirakon/AppManager/settings/AdvancedPreferences.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable S
126126
.show();
127127
return true;
128128
});
129+
// ADB local server port
130+
Preference adbLsPort = Objects.requireNonNull(findPreference("adb_local_server_port"));
131+
int port = Prefs.Misc.getAdbLocalServerPort();
132+
adbLsPort.setSummary(String.valueOf(port));
133+
adbLsPort.setOnPreferenceClickListener(pref -> {
134+
new TextInputDialogBuilder(requireActivity(), null)
135+
.setTitle(R.string.adb_local_server_port)
136+
.setInputText(String.valueOf(port))
137+
.setInputInputType(InputType.TYPE_CLASS_NUMBER)
138+
.setInputImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING)
139+
.setNegativeButton(R.string.cancel, null)
140+
.setPositiveButton(R.string.save, (dialog, which, inputText, isChecked) -> {
141+
if (inputText != null && TextUtils.isDigitsOnly(inputText)) {
142+
int c = Integer.decode(inputText.toString());
143+
Prefs.Misc.setAdbLocalServerPort(c);
144+
adbLsPort.setSummary(String.valueOf(c));
145+
}
146+
})
147+
.show();
148+
return true;
149+
});
129150
// Import/export App Manager's KeyStore
130151
((Preference) Objects.requireNonNull(findPreference("import_export_keystore")))
131152
.setOnPreferenceClickListener(preference -> {

app/src/main/java/io/github/muntashirakon/AppManager/settings/MainPreferencesViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public MutableLiveData<String> getCustomCommand1() {
129129
public void loadCustomCommands() {
130130
mExecutor.submit(() -> {
131131
try {
132-
ServerConfig.init(getApplication(), UserHandleHidden.myUserId());
132+
ServerConfig.init(getApplication());
133133
mCustomCommand0.postValue(ServerConfig.getServerRunnerCommand(0));
134134
mCustomCommand1.postValue(ServerConfig.getServerRunnerCommand(1));
135135
} catch (Throwable e) {

app/src/main/java/io/github/muntashirakon/AppManager/settings/Prefs.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,14 @@ public static void setSelectedUsers(@Nullable int[] users) {
573573
public static boolean sendNotificationsToConnectedDevices() {
574574
return AppPref.getBoolean(AppPref.PrefKey.PREF_SEND_NOTIFICATIONS_TO_CONNECTED_DEVICES_BOOL);
575575
}
576+
577+
public static void setAdbLocalServerPort(int port) {
578+
AppPref.set(AppPref.PrefKey.PREF_ADB_LOCAL_SERVER_PORT_INT, port);
579+
}
580+
581+
public static int getAdbLocalServerPort() {
582+
return AppPref.getInt(AppPref.PrefKey.PREF_ADB_LOCAL_SERVER_PORT_INT);
583+
}
576584
}
577585

578586
public static final class RunningApps {

app/src/main/java/io/github/muntashirakon/AppManager/utils/AppPref.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.net.Uri;
1111
import android.os.Build;
1212
import android.os.Environment;
13+
import android.os.UserHandleHidden;
1314
import android.util.Log;
1415
import android.view.View;
1516

@@ -54,6 +55,7 @@ public class AppPref {
5455
*/
5556
@Keep
5657
public enum PrefKey {
58+
PREF_ADB_LOCAL_SERVER_PORT_INT,
5759
PREF_APP_OP_SHOW_DEFAULT_BOOL,
5860
PREF_APP_OP_SORT_ORDER_INT,
5961
PREF_APP_THEME_INT,
@@ -373,6 +375,8 @@ private void init() {
373375
@NonNull
374376
public Object getDefaultValue(@NonNull PrefKey key) {
375377
switch (key) {
378+
case PREF_ADB_LOCAL_SERVER_PORT_INT:
379+
return (UserHandleHidden.myUserId() + 60001);
376380
case PREF_BACKUP_FLAGS_INT:
377381
return BackupFlags.BACKUP_INT_DATA | BackupFlags.BACKUP_RULES
378382
| BackupFlags.BACKUP_APK_FILES | BackupFlags.BACKUP_EXTRAS;

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,4 +1552,9 @@
15521552
<string name="modify_last_modification_time">Modify last modification time</string>
15531553
<string name="modify_last_access_time">Modify last access time</string>
15541554
<string name="change_id">Change ID</string>
1555+
<string name="memory_usage_accessibility_description">The system is currently using %s memory out of %s, and the amount of available memory is %s. Among the occupied memory, the applications are using %s, %s are cached, and %s are used as memory buffers.</string>
1556+
<string name="memory_usage_unavailable">Memory usage is unavailable right now.</string>
1557+
<string name="swap_usage_accessibility_description">The system has allocated %s for swap and currently using %s.</string>
1558+
<string name="selected_items_accessibility_description">Selected %d out of %d items</string>
1559+
<string name="adb_local_server_port">ADB local server port</string>
15551560
</resources>

app/src/main/res/xml/preferences_advanced.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
tools:summary="Execute at most 5 operations in parallel"
2020
app:iconSpaceReserved="false" />
2121

22+
<Preference
23+
app:key="adb_local_server_port"
24+
app:title="@string/adb_local_server_port"
25+
tools:summary="60001"
26+
app:iconSpaceReserved="false" />
27+
2228
<Preference
2329
app:key="import_export_keystore"
2430
app:title="@string/pref_import_export_keystore"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<!-- SPDX-License-Identifier: GPL-3.0-or-later -->
22
<resources>
33
<string name="select_all">Select all</string>
4+
<string name="selected_items_accessibility_description">Selected %d out of %d items</string>
45
</resources>

0 commit comments

Comments
 (0)