Skip to content

Commit

Permalink
[Settings] Add a dedicated page for mode of operations
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Apr 4, 2024
1 parent ed07d3e commit 46ccca1
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.MainThread;
Expand Down Expand Up @@ -211,7 +210,7 @@ private static Runnable asRunnable(Shell.Task task) {
Log.e(TAG, "Couldn't start service using root.");
}
}
} catch (IOException | RemoteException e) {
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,7 @@ private Shell.Task startRootProcess(ComponentName name, String action) {
FileUtils.chmod644(mainJar);

StringBuilder env = new StringBuilder();
String params = "";

if (Utils.vLog()) {
env.append(LOGGING_ENV + "=1 ");
}

// Only support debugging on SDK >= 27
if (Build.VERSION.SDK_INT >= 27 && Debug.isDebuggerConnected()) {
env.append(DEBUG_ENV + "=1 ");
// Reference of the params to start jdwp:
// https://developer.android.com/ndk/guides/wrap-script#debugging_when_using_wrapsh
if (Build.VERSION.SDK_INT == 27) {
params = API_27_DEBUG;
} else {
params = API_28_DEBUG;
}
}

// Disable image dex2oat as it can be quite slow in some ROMs if triggered
params += " -Xnoimage-dex2oat";
String params = getParams(env);

// Classpath
env.append(CLASSPATH_ENV + "=").append(Ops.isSystem() ? mainJar : stagingMainJar).append(" ");
Expand All @@ -221,6 +202,31 @@ private Shell.Task startRootProcess(ComponentName name, String action) {
};
}

@SuppressLint("RestrictedApi")
@NonNull
private static String getParams(StringBuilder env) {
String params = "";

if (Utils.vLog()) {
env.append(LOGGING_ENV + "=1 ");
}

// Only support debugging on SDK >= 27
if (Build.VERSION.SDK_INT >= 27 && Debug.isDebuggerConnected()) {
env.append(DEBUG_ENV + "=1 ");
// Reference of the params to start jdwp:
// https://developer.android.com/ndk/guides/wrap-script#debugging_when_using_wrapsh
if (Build.VERSION.SDK_INT == 27) {
params = API_27_DEBUG;
} else {
params = API_28_DEBUG;
}
}

// Disable image dex2oat as it can be quite slow in some ROMs if triggered
return params + " -Xnoimage-dex2oat";
}

@NonNull
private String getRunnerScript(@NonNull String env,
@NonNull File mainJar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.RemoteException;
import android.os.UserHandleHidden;

import androidx.annotation.AnyThread;
Expand All @@ -18,7 +17,6 @@
import java.net.SocketTimeoutException;

import io.github.muntashirakon.AppManager.BuildConfig;
import io.github.muntashirakon.AppManager.ipc.LocalServices;
import io.github.muntashirakon.AppManager.logs.Log;
import io.github.muntashirakon.AppManager.misc.NoOps;
import io.github.muntashirakon.AppManager.server.common.Caller;
Expand All @@ -39,16 +37,13 @@ public class LocalServer {
@GuardedBy("lockObject")
@WorkerThread
@NoOps(used = true)
public static LocalServer getInstance() throws RemoteException, IOException {
public static LocalServer getInstance() throws IOException {
// Non-null check must be done outside the synchronised block to prevent deadlock on ADB over TCP mode.
if (sLocalServer != null) return sLocalServer;
synchronized (sLock) {
try {
Log.d("IPC", "Init: Local server");
sLocalServer = new LocalServer();
// This calls the AdbShell class which has dependencies on LocalServer which might cause deadlock
// if not careful (see comment above on non-null check)
LocalServices.bindServicesIfNotAlready();
} finally {
sLock.notifyAll();
}
Expand Down Expand Up @@ -120,7 +115,7 @@ public void checkConnect() throws IOException {
}
}

public Shell.Result runCommand(String command) throws IOException, RemoteException {
public Shell.Result runCommand(String command) throws IOException {
ShellCaller shellCaller = new ShellCaller(command);
CallerResult callerResult = exec(shellCaller);
Throwable th = callerResult.getThrowable();
Expand Down Expand Up @@ -168,13 +163,12 @@ private void checkFile() throws IOException {

@WorkerThread
@NoOps(used = true)
public static void restart() throws IOException, RemoteException {
public static void restart() throws IOException {
if (sLocalServer != null) {
LocalServerManager manager = sLocalServer.mLocalServerManager;
manager.closeBgServer();
manager.stop();
manager.start();
LocalServices.bindServicesIfNotAlready();
} else {
getInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,27 @@

package io.github.muntashirakon.AppManager.settings;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.collection.ArrayMap;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.Preference;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.misc.DeviceInfo2;
import io.github.muntashirakon.AppManager.self.life.BuildExpiryChecker;
import io.github.muntashirakon.AppManager.self.life.FundingCampaignChecker;
import io.github.muntashirakon.AppManager.servermanager.ServerConfig;
import io.github.muntashirakon.AppManager.utils.LangUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
import io.github.muntashirakon.AppManager.utils.appearance.AppearanceUtils;
import io.github.muntashirakon.dialog.AlertDialogBuilder;
import io.github.muntashirakon.dialog.SearchableSingleChoiceDialogBuilder;
Expand All @@ -53,10 +48,7 @@ public static MainPreferences getInstance(@Nullable String key) {

private FragmentActivity mActivity;
private String mCurrentLang;
@Ops.Mode
private String mCurrentMode;
private MainPreferencesViewModel mModel;
private AlertDialog mModeOfOpsAlertDialog;
private Preference mModePref;
private String[] mModes;

Expand Down Expand Up @@ -104,33 +96,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
});
// Mode of operation
mModePref = requirePreference("mode_of_operations");
mModeOfOpsAlertDialog = UIUtils.getProgressDialog(mActivity, getString(R.string.loading), true);
mModes = getResources().getStringArray(R.array.modes);
mCurrentMode = Ops.getMode();
mModePref.setSummary(getString(R.string.mode_of_op_with_inferred_mode_of_op, mModes[MODE_NAMES.indexOf(mCurrentMode)],
Ops.getInferredMode(mActivity)));
mModePref.setOnPreferenceClickListener(preference -> {
new SearchableSingleChoiceDialogBuilder<>(mActivity, MODE_NAMES, mModes)
.setTitle(R.string.pref_mode_of_operations)
.setSelection(mCurrentMode)
.addDisabledItems(Build.VERSION.SDK_INT < Build.VERSION_CODES.R ?
Collections.singletonList(Ops.MODE_ADB_WIFI) : Collections.emptyList())
.setPositiveButton(R.string.apply, (dialog, which, selectedItem) -> {
if (selectedItem != null) {
mCurrentMode = selectedItem;
if (Ops.MODE_ADB_OVER_TCP.equals(mCurrentMode)) {
ServerConfig.setAdbPort(ServerConfig.DEFAULT_ADB_PORT);
}
Ops.setMode(mCurrentMode);
mModePref.setSummary(mModes[MODE_NAMES.indexOf(mCurrentMode)]);
mModeOfOpsAlertDialog.show();
mModel.setModeOfOps();
}
})
.setNegativeButton(R.string.cancel, null)
.show();
return true;
});
// About device
requirePreference("about_device").setOnPreferenceClickListener(preference -> {
mModel.loadDeviceInfo(new DeviceInfo2(mActivity));
Expand All @@ -149,40 +115,6 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Preference loaders
// Mode of ops
mModel.getModeOfOpsStatus().observe(getViewLifecycleOwner(), status -> {
switch (status) {
case Ops.STATUS_AUTO_CONNECT_WIRELESS_DEBUGGING:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
mModel.autoConnectAdb(Ops.STATUS_WIRELESS_DEBUGGING_CHOOSER_REQUIRED);
return;
} // fall-through
case Ops.STATUS_WIRELESS_DEBUGGING_CHOOSER_REQUIRED:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
mModeOfOpsAlertDialog.dismiss();
Ops.connectWirelessDebugging(mActivity, mModel);
return;
} // fall-through
case Ops.STATUS_ADB_CONNECT_REQUIRED:
mModeOfOpsAlertDialog.dismiss();
Ops.connectAdbInput(mActivity, mModel);
return;
case Ops.STATUS_ADB_PAIRING_REQUIRED:
mModeOfOpsAlertDialog.dismiss();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Ops.pairAdbInput(mActivity, mModel);
return;
} // fall-through
case Ops.STATUS_FAILURE_ADB_NEED_MORE_PERMS:
Ops.displayIncompleteUsbDebuggingMessage(requireActivity());
case Ops.STATUS_SUCCESS:
case Ops.STATUS_FAILURE:
mModeOfOpsAlertDialog.dismiss();
mCurrentMode = Ops.getMode();
mModePref.setSummary(getString(R.string.mode_of_op_with_inferred_mode_of_op,
mModes[MODE_NAMES.indexOf(mCurrentMode)], Ops.getInferredMode(mActivity)));
}
});
// Device info
mModel.getDeviceInfo().observe(getViewLifecycleOwner(), deviceInfo -> {
View v = View.inflate(mActivity, io.github.muntashirakon.ui.R.layout.dialog_scrollable_text_view, null);
Expand All @@ -192,6 +124,15 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
});
}

@Override
public void onStart() {
super.onStart();
if (mModePref != null) {
mModePref.setSummary(getString(R.string.mode_of_op_with_inferred_mode_of_op,
mModes[MODE_NAMES.indexOf(Ops.getMode())], Ops.getInferredMode(mActivity)));
}
}

@Override
public int getTitle() {
return R.string.settings;
Expand Down
Loading

0 comments on commit 46ccca1

Please sign in to comment.