Skip to content

Commit

Permalink
update disable nfc sound
Browse files Browse the repository at this point in the history
  • Loading branch information
cinit committed Dec 22, 2021
1 parent 49ee293 commit c55776e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ constexpr int MAX_PACKET_SIZE = 65536;
const char *const LOG_TAG = "BaseRemoteAndroidService";

std::vector<uint8_t> BaseRemoteAndroidService::RequestPacket::toByteArray() const {
const uint32_t type = BaseRemoteAndroidService::TYPE_RESPONSE;
const uint32_t type = BaseRemoteAndroidService::TYPE_REQUEST;
std::vector<uint8_t> data;
data.reserve(sizeof(uint32_t) * 4 + payload.size());
data.insert(data.end(), (uint8_t *) &type, (uint8_t *) &type + sizeof(uint32_t));
Expand All @@ -28,7 +28,7 @@ std::vector<uint8_t> BaseRemoteAndroidService::RequestPacket::toByteArray() cons
}

std::vector<uint8_t> BaseRemoteAndroidService::RequestPacket::toByteArray(uint32_t overrideSequence) const {
const uint32_t type = BaseRemoteAndroidService::TYPE_RESPONSE;
const uint32_t type = BaseRemoteAndroidService::TYPE_REQUEST;
std::vector<uint8_t> data;
data.reserve(sizeof(uint32_t) * 4 + payload.size());
data.insert(data.end(), (uint8_t *) &type, (uint8_t *) &type + sizeof(uint32_t));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cc.ioctl.nfcdevicehost.xposed;

import android.app.AndroidAppHelper;
import android.content.Context;
import android.net.Credentials;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -12,25 +15,44 @@

public class SysNfcSvcPatch {

private static final String TAG = "SysNfcSvcPatch";
private static final String PREF_DISABLE_NFC_DISCOVERY_SOUND = "disable_nfc_discovery_sound";

private static Thread sWorkingThread = null;
public static boolean sDisableNfcDiscoverySound = false;
private static boolean sShouldRunning = true;

static void run() {
try {
// wait for system service to start
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
try {
initMisc();
} catch (RuntimeException | LinkageError e) {
XposedBridge.log("SysNfcSvcPatch: " + e.getMessage());
}
try {
int myPid = android.os.Process.myPid();
Log.d(TAG, "SysNfcSvcPatch.run: myPid = " + myPid);
while (sShouldRunning) {
LocalServerSocket localServerSocket;
try {
localServerSocket = new LocalServerSocket("com.android.nfc/cc.ioctl.nfcdevicehost.xposed.SysNfcSvcPatch@" + myPid);
while (sShouldRunning) {
Log.i(TAG, "SysNfcSvcPatch.run: waiting for connection");
LocalSocket localSocket = localServerSocket.accept();
Credentials cred = null;
try {
cred = localSocket.getPeerCredentials();
} catch (IOException ignored) {
}
if (cred != null && cred.getUid() == 0) {
Log.i(TAG, "SysNfcSvcPatch.run: connection accepted, uid = " + cred.getUid()
+ ", pid = " + cred.getPid() + ", gid = " + cred.getGid());
localServerSocket.close();
handleTransaction(localSocket);
// socket is closed in handleTransaction
Expand Down Expand Up @@ -69,6 +91,7 @@ static void handleTransaction(LocalSocket socket) {
// read 4 bytes length
readFully(is, buffer, 0, 4);
int length = getInt32Le(buffer, 0);
Log.d(TAG, "handleTransaction: length = " + length);
if (length < 16 || length > 65535) {
break;
}
Expand Down Expand Up @@ -96,6 +119,7 @@ static void handleTransaction(LocalSocket socket) {
case TYPE_RESPONSE:
case TYPE_EVENT:
default:
XposedBridge.log("SysNfcSvcPatch: unknown type " + type);
break;
}
}
Expand All @@ -112,6 +136,12 @@ private static void handleRequest(LocalSocket socket, int sequence, int requestC
switch (requestCode) {
case REQUEST_SET_NFC_SOUND_DISABLE: {
sDisableNfcDiscoverySound = (arg0 != 0);
try {
Context ctx = getApplicationContext();
setStringConfig(ctx, PREF_DISABLE_NFC_DISCOVERY_SOUND, sDisableNfcDiscoverySound ? "1" : "0");
} catch (RuntimeException | LinkageError e) {
XposedBridge.log(e);
}
sendResponse(socket, sequence, 0, 0, null);
break;
}
Expand All @@ -120,6 +150,7 @@ private static void handleRequest(LocalSocket socket, int sequence, int requestC
break;
}
default: {
Log.e(TAG, "handleRequest: requestCode = " + requestCode + ", arg0 = " + arg0);
sendResponse(socket, sequence, -1, -1, null);
}
}
Expand Down Expand Up @@ -191,4 +222,29 @@ static void putInt32Le(byte[] buffer, int offset, int value) {
buffer[offset + 2] = (byte) ((value >> 16) & 0xFF);
buffer[offset + 3] = (byte) ((value >> 24) & 0xFF);
}

private static void initMisc() {
Context ctx = getApplicationContext();
String disableNfcDiscoverySound = getStringConfig(ctx, PREF_DISABLE_NFC_DISCOVERY_SOUND, "0");
sDisableNfcDiscoverySound = "1".equals(disableNfcDiscoverySound);
}

public static Context getApplicationContext() {
Context ctx = AndroidAppHelper.currentApplication();
if (ctx == null) {
IllegalStateException t = new IllegalStateException("SysNfcSvcPatch: getApplicationContext() failed");
XposedBridge.log(t);
Log.e(TAG, "SysNfcSvcPatch: getApplicationContext() failed", t);
throw t;
}
return ctx;
}

private static String getStringConfig(Context ctx, String key, String def) {
return ctx.getSharedPreferences("cc.ioctl.nfcdevicehost", Context.MODE_PRIVATE).getString(key, def);
}

private static boolean setStringConfig(Context ctx, String key, String value) {
return ctx.getSharedPreferences("cc.ioctl.nfcdevicehost", Context.MODE_PRIVATE).edit().putString(key, value).commit();
}
}

0 comments on commit c55776e

Please sign in to comment.