[core] Move package compile to daemon (#1262)

This commit is contained in:
LoveSy 2021-10-13 14:41:45 +08:00 committed by GitHub
parent 4469f6fc99
commit 415408f8f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 42 deletions

View File

@ -26,7 +26,6 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
@ -41,16 +40,12 @@ import com.google.android.material.snackbar.Snackbar;
import org.lsposed.manager.App;
import org.lsposed.manager.R;
import org.lsposed.manager.databinding.FragmentCompileDialogBinding;
import org.lsposed.manager.receivers.LSPManagerServiceHolder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
@SuppressWarnings("deprecation")
public class CompileDialogFragment extends AppCompatDialogFragment {
private static final String[] COMPILE_RESET_COMMAND = new String[]{"cmd", "package", "compile", "-f", "-m", "speed", ""};
private ApplicationInfo appInfo;
private View snackBar;
@ -82,12 +77,10 @@ public class CompileDialogFragment extends AppCompatDialogFragment {
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
String[] command = COMPILE_RESET_COMMAND;
command[6] = appInfo.packageName;
new CompileTask(this).executeOnExecutor(App.getExecutorService(), command);
new CompileTask(this).executeOnExecutor(App.getExecutorService(), appInfo.packageName);
}
private static class CompileTask extends AsyncTask<String, Void, String> {
private static class CompileTask extends AsyncTask<String, Void, Throwable> {
WeakReference<CompileDialogFragment> outerRef;
@ -96,47 +89,28 @@ public class CompileDialogFragment extends AppCompatDialogFragment {
}
@Override
protected String doInBackground(String... commands) {
protected Throwable doInBackground(String... commands) {
try {
Process process = Runtime.getRuntime().exec(commands);
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuilder err = new StringBuilder();
while ((read = errorReader.read(buffer)) > 0) {
err.append(buffer, 0, read);
}
StringBuilder input = new StringBuilder();
while ((read = inputReader.read(buffer)) > 0) {
input.append(buffer, 0, read);
}
errorReader.close();
inputReader.close();
process.waitFor();
String result = "";
if (process.exitValue() != 0) {
result = "Error ";
}
if (TextUtils.isEmpty(err)) {
return result + input;
if (LSPManagerServiceHolder.getService().performDexOptMode(commands[0])) {
return null;
} else {
return result + err;
return new UnknownError();
}
} catch (Exception e) {
e.printStackTrace();
return "Error " + e.getCause();
} catch (Throwable e) {
return e;
}
}
@Override
protected void onPostExecute(String result) {
protected void onPostExecute(Throwable result) {
Context context = App.getInstance();
String text;
if (result.length() == 0) {
if (result != null) {
if (result instanceof UnknownError) {
text = context.getString(R.string.compile_failed);
} else if (result.length() >= 5 && "Error".equals(result.substring(0, 5))) {
text = context.getString(R.string.compile_failed_with_info) + " " + result.substring(6);
} else {
text = context.getString(R.string.compile_failed_with_info) + result.toString();
}
} else {
text = context.getString(R.string.compile_done);
}

View File

@ -744,4 +744,9 @@ public class LSPManagerService extends ILSPManagerService.Stub {
Log.e(TAG, "flashZip: ", e);
}
}
@Override
public boolean performDexOptMode(String packageName) throws RemoteException {
return PackageService.performDexOptMode(packageName);
}
}

View File

@ -41,6 +41,7 @@ import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.util.Log;
import android.util.Pair;
@ -290,4 +291,12 @@ public class PackageService {
ris.getList().get(0).activityInfo.name);
return intent;
}
public static boolean performDexOptMode(String packageName) throws RemoteException {
IPackageManager pm = getPackageManager();
if (pm == null) return false;
return pm.performDexOptMode(packageName,
SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false),
"speed", true, true, null);
}
}

View File

@ -66,6 +66,10 @@ public interface IPackageManager extends IInterface {
ParceledListSlice<ResolveInfo> queryIntentActivities(Intent intent,
String resolvedType, int flags, int userId) throws RemoteException;
boolean performDexOptMode(String packageName, boolean checkProfiles,
String targetCompilerFilter, boolean force, boolean bootComplete, String splitName)
throws RemoteException;
abstract class Stub extends Binder implements IPackageManager {
public static IPackageManager asInterface(IBinder obj) {

View File

@ -15,4 +15,8 @@ public class SystemProperties {
public static void set(@NonNull String key, @Nullable String val) {
throw new UnsupportedOperationException("Stub");
}
public static boolean getBoolean(@NonNull String key, boolean def) {
throw new UnsupportedOperationException("Stub");
}
}

View File

@ -73,4 +73,6 @@ interface ILSPManagerService {
void setAddShortcut(boolean enabled) = 38;
oneway void flashZip(String zipPath, in ParcelFileDescriptor outputStream) = 39;
boolean performDexOptMode(String packageName) = 40;
}