Move more code to app-core

This commit is contained in:
topjohnwu 2019-01-31 03:24:18 -05:00
parent 4091687733
commit af88b7c807
3 changed files with 81 additions and 64 deletions

View File

@ -0,0 +1,61 @@
package com.topjohnwu.magisk.utils;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.text.TextUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public abstract class SuConnector {
private LocalSocket socket;
protected DataOutputStream out;
protected DataInputStream in;
protected SuConnector(String name) throws IOException {
socket = new LocalSocket();
socket.connect(new LocalSocketAddress(name, LocalSocketAddress.Namespace.ABSTRACT));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}
private String readString() throws IOException {
int len = in.readInt();
byte[] buf = new byte[len];
in.readFully(buf);
return new String(buf, "UTF-8");
}
public Bundle readSocketInput() throws IOException {
Bundle bundle = new Bundle();
while (true) {
String name = readString();
if (TextUtils.equals(name, "eof"))
break;
bundle.putString(name, readString());
}
return bundle;
}
public void response() {
try {
onResponse();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
out.close();
socket.close();
} catch (IOException ignored) { }
}
protected abstract void onResponse() throws IOException;
}

View File

@ -2,74 +2,20 @@ package com.topjohnwu.magisk.utils;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process; import android.os.Process;
import android.text.TextUtils;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.Config;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.container.Policy; import com.topjohnwu.magisk.container.Policy;
import com.topjohnwu.magisk.container.SuLogEntry; import com.topjohnwu.magisk.container.SuLogEntry;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date; import java.util.Date;
public abstract class SuConnector { public abstract class SuLogger {
private LocalSocket socket; public void handleLogs(Intent intent) {
protected DataOutputStream out;
protected DataInputStream in;
public SuConnector(String name) throws IOException {
socket = new LocalSocket();
socket.connect(new LocalSocketAddress(name, LocalSocketAddress.Namespace.ABSTRACT));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}
private String readString() throws IOException {
int len = in.readInt();
byte[] buf = new byte[len];
in.readFully(buf);
return new String(buf, "UTF-8");
}
public Bundle readSocketInput() throws IOException {
Bundle bundle = new Bundle();
while (true) {
String name = readString();
if (TextUtils.equals(name, "eof"))
break;
bundle.putString(name, readString());
}
return bundle;
}
public void response() {
try {
onResponse();
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
out.close();
socket.close();
} catch (IOException ignored) { }
}
protected abstract void onResponse() throws IOException;
public static void handleLogs(Intent intent) {
int fromUid = intent.getIntExtra("from.uid", -1); int fromUid = intent.getIntExtra("from.uid", -1);
if (fromUid < 0) return; if (fromUid < 0) return;
@ -118,15 +64,13 @@ public abstract class SuConnector {
app.mDB.addLog(log); app.mDB.addLog(log);
} }
private static void handleNotify(Policy policy) { private void handleNotify(Policy policy) {
String message = App.self.getString(policy.policy == Policy.ALLOW ?
R.string.su_allow_toast : R.string.su_deny_toast, policy.appName);
if (policy.notification && if (policy.notification &&
(int) Config.get(Config.Key.SU_NOTIFICATION) == Config.Value.NOTIFICATION_TOAST) (int) Config.get(Config.Key.SU_NOTIFICATION) == Config.Value.NOTIFICATION_TOAST)
Utils.toast(message, Toast.LENGTH_SHORT); Utils.toast(getMessage(policy), Toast.LENGTH_SHORT);
} }
public static void handleNotify(Intent intent) { public void handleNotify(Intent intent) {
int fromUid = intent.getIntExtra("from.uid", -1); int fromUid = intent.getIntExtra("from.uid", -1);
if (fromUid < 0) return; if (fromUid < 0) return;
if (fromUid == Process.myUid()) return; if (fromUid == Process.myUid()) return;
@ -137,4 +81,6 @@ public abstract class SuConnector {
handleNotify(policy); handleNotify(policy);
} catch (PackageManager.NameNotFoundException ignored) {} } catch (PackageManager.NameNotFoundException ignored) {}
} }
public abstract String getMessage(Policy policy);
} }

View File

@ -9,16 +9,26 @@ import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.ClassMap; import com.topjohnwu.magisk.ClassMap;
import com.topjohnwu.magisk.Config; import com.topjohnwu.magisk.Config;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SuRequestActivity; import com.topjohnwu.magisk.SuRequestActivity;
import com.topjohnwu.magisk.container.Policy;
import com.topjohnwu.magisk.uicomponents.Notifications; import com.topjohnwu.magisk.uicomponents.Notifications;
import com.topjohnwu.magisk.uicomponents.Shortcuts; import com.topjohnwu.magisk.uicomponents.Shortcuts;
import com.topjohnwu.magisk.utils.DownloadApp; import com.topjohnwu.magisk.utils.DownloadApp;
import com.topjohnwu.magisk.utils.SuConnector; import com.topjohnwu.magisk.utils.SuLogger;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.ShellUtils;
public class GeneralReceiver extends BroadcastReceiver { public class GeneralReceiver extends BroadcastReceiver {
private static SuLogger SU_LOGGER = new SuLogger() {
@Override
public String getMessage(Policy policy) {
return App.self.getString(policy.policy == Policy.ALLOW ?
R.string.su_allow_toast : R.string.su_deny_toast, policy.appName);
}
};
private String getPkg(Intent i) { private String getPkg(Intent i) {
return i.getData() == null ? "" : i.getData().getEncodedSchemeSpecificPart(); return i.getData() == null ? "" : i.getData().getEncodedSchemeSpecificPart();
} }
@ -44,10 +54,10 @@ public class GeneralReceiver extends BroadcastReceiver {
app.startActivity(i); app.startActivity(i);
break; break;
case "log": case "log":
SuConnector.handleLogs(intent); SU_LOGGER.handleLogs(intent);
break; break;
case "notify": case "notify":
SuConnector.handleNotify(intent); SU_LOGGER.handleNotify(intent);
break; break;
case "boot": case "boot":
default: default: