Skip to content

Commit

Permalink
fix: Make static method StringUtil.format to avoid lint warnings for …
Browse files Browse the repository at this point in the history
…Locale with "%d".

Signed-off-by: jefft0 <jeff@thefirst.org>
  • Loading branch information
jefft0 committed Sep 6, 2022
1 parent 9537f9d commit 90c084d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.ref.WeakReference;
import java.util.Random;
import java.util.Locale;

import ipfs.gomobile.android.IPFS;

Expand Down Expand Up @@ -57,7 +58,7 @@ protected String doInBackground(Void... v) {
}

int randomIndex = random.nextInt(XKCDLatest) + 1;
String formattedIndex = String.format("%04d", randomIndex);
String formattedIndex = String.format(Locale.US, "%04d", randomIndex);

byte[] infoRaw = ipfs.newRequest("cat")
.withArgument(String.format("%s/%s/info.json", XKCDIPNS, formattedIndex))
Expand All @@ -74,7 +75,7 @@ protected String doInBackground(Void... v) {
.withArgument(String.format("%s/%s/image.%s", XKCDIPNS, formattedIndex, imgExt))
.send();

return String.format("%d. %s", randomIndex, title);
return String.format(Locale.US, "%d. %s", randomIndex, title);
} catch (Exception err) {
backgroundError = true;
return MainActivity.exceptionToString(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public synchronized boolean add(Runnable r, Callback callback,long delay, Runnab
boolean result = mCommandQueue.add(task);

if (result) {
mLogger.d(TAG, String.format("id=%s add: index=%d", mLogger.sensitiveObject(mId), task.index));
mLogger.d(TAG, StringUtil.format("id=%s add: index=%d", mLogger.sensitiveObject(mId), task.index));
nextCommand();
} else {
mLogger.e(TAG, String.format("id=%s add error: could not enqueue task command", mLogger.sensitiveObject(mId)));
Expand All @@ -62,9 +62,9 @@ public synchronized void completedCommand(int status) {
mLogger.e(TAG, String.format("id=%s completedCommand error: no task found", mLogger.sensitiveObject(mId)));
return;
}
mLogger.d(TAG, String.format("id=%s completedCommand: index=%d", mLogger.sensitiveObject(mId), currentCommand.index));
mLogger.d(TAG, StringUtil.format("id=%s completedCommand: index=%d", mLogger.sensitiveObject(mId), currentCommand.index));
if (currentCommand.callback != null) {
mLogger.d(TAG, String.format("id=%s completedCommand: callback for index=%d", mLogger.sensitiveObject(mId), currentCommand.index));
mLogger.d(TAG, StringUtil.format("id=%s completedCommand: callback for index=%d", mLogger.sensitiveObject(mId), currentCommand.index));
mHandler.post(() -> currentCommand.callback.run(status));
}
mIsRetrying = false;
Expand Down Expand Up @@ -147,7 +147,7 @@ public synchronized void nextCommand() {
return;
}

mLogger.d(TAG, String.format("id=%s nextCommand: running index=%d", mLogger.sensitiveObject(mId), bluetoothCommand.index));
mLogger.d(TAG, StringUtil.format("id=%s nextCommand: running index=%d", mLogger.sensitiveObject(mId), bluetoothCommand.index));

// Execute the next command in the queue
mCommandQueueBusy = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean start(final String peerID) {
try {
mBluetoothServerSocket = mBluetoothManager.getAdapter().listenUsingInsecureL2capChannel();
PSM = mBluetoothServerSocket.getPsm();
mLogger.d(TAG, String.format("start: listenUsingL2capChannel: PSM=%d", PSM));
mLogger.d(TAG, StringUtil.format("start: listenUsingL2capChannel: PSM=%d", PSM));
} catch (IOException e) {
mLogger.e(TAG, "start error: listenUsingL2capChannel: ", e);
mBluetoothServerSocket = null;
Expand Down Expand Up @@ -266,7 +266,7 @@ public void stop() {

private boolean _writeAndNotify(PeerDevice device, byte[] payload) {
if (mLogger.showSensitiveData()) {
mLogger.v(TAG, String.format("_writeAndNotify: writing chunk of data: device=%s base64=%s value=%s length=%d", device.getMACAddress(), Base64.encodeToString(payload, Base64.DEFAULT), BleDriver.bytesToHex(payload), payload.length));
mLogger.v(TAG, StringUtil.format("_writeAndNotify: writing chunk of data: device=%s base64=%s value=%s length=%d", device.getMACAddress(), Base64.encodeToString(payload, Base64.DEFAULT), BleDriver.bytesToHex(payload), payload.length));
} else {
mLogger.v(TAG, "_writeAndNotify called");
}
Expand Down Expand Up @@ -322,7 +322,7 @@ private boolean _writeAndNotify(PeerDevice device, byte[] payload) {

public boolean writeAndNotify(PeerDevice device, byte[] payload) {
if (mLogger.showSensitiveData()) {
mLogger.v(TAG, String.format("writeAndNotify: device=%s base64=%s value=%s length=%d", device.getMACAddress(), Base64.encodeToString(payload, Base64.DEFAULT), BleDriver.bytesToHex(payload), payload.length));
mLogger.v(TAG, StringUtil.format("writeAndNotify: device=%s base64=%s value=%s length=%d", device.getMACAddress(), Base64.encodeToString(payload, Base64.DEFAULT), BleDriver.bytesToHex(payload), payload.length));
} else {
mLogger.v(TAG, "writeAndNotify called");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onServiceAdded(int status, BluetoothGattService service) {
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);

mLogger.v(TAG, String.format("onConnectionStateChange: device=%s status=%d newState=%d", mLogger.sensitiveObject(device), status, newState));
mLogger.v(TAG, StringUtil.format("onConnectionStateChange: device=%s status=%d newState=%d", mLogger.sensitiveObject(device), status, newState));
PeerDevice peerDevice = mBleDriver.deviceManager().get(device.getAddress());

if (status == GATT_SUCCESS) {
Expand Down Expand Up @@ -97,7 +97,7 @@ public void onConnectionStateChange(BluetoothDevice device, int status, int newS
}
}
} else {
mLogger.e(TAG, String.format("onConnectionStateChange error=%d", status));
mLogger.e(TAG, StringUtil.format("onConnectionStateChange error=%d", status));
}
}

Expand Down Expand Up @@ -163,7 +163,7 @@ public void onCharacteristicReadRequest(BluetoothDevice device,
int offset,
BluetoothGattCharacteristic characteristic) {
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
mLogger.v(TAG, String.format("onCharacteristicReadRequest called: device=%s requestId=%d offset=%d", mLogger.sensitiveObject(device.getAddress()), requestId, offset));
mLogger.v(TAG, StringUtil.format("onCharacteristicReadRequest called: device=%s requestId=%d offset=%d", mLogger.sensitiveObject(device.getAddress()), requestId, offset));

if (!BleDriver.mCallbacksHandler.post(() -> {
PeerDevice peerDevice;
Expand Down Expand Up @@ -210,7 +210,7 @@ public void onCharacteristicReadRequest(BluetoothDevice device,
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
try {
mLogger.v(TAG, String.format("onCharacteristicReadRequest: PSM=%d pid=%s", mGattServer.getL2capPSM(), mLogger.sensitiveObject(getLocalPID())));
mLogger.v(TAG, StringUtil.format("onCharacteristicReadRequest: PSM=%d pid=%s", mGattServer.getL2capPSM(), mLogger.sensitiveObject(getLocalPID())));
dos.writeInt(mGattServer.getL2capPSM());
dos.write(getLocalPID().getBytes());
dos.flush();
Expand All @@ -229,15 +229,15 @@ public void onCharacteristicReadRequest(BluetoothDevice device,
}

if ((payload.length - offset) <= (peerDevice.getMtu() - ATT_HEADER_READ_SIZE)) {
mLogger.d(TAG, String.format("onCharacteristicReadRequest: MTU is big enough: MTU=%d dataLength=%d", peerDevice.getMtu(), payload.length - offset));
mLogger.d(TAG, StringUtil.format("onCharacteristicReadRequest: MTU is big enough: MTU=%d dataLength=%d", peerDevice.getMtu(), payload.length - offset));
} else {
mLogger.d(TAG, String.format("onCharacteristicReadRequest: MTU is too small: MTU=%d dataLength=%d", peerDevice.getMtu(), payload.length - offset));
mLogger.d(TAG, StringUtil.format("onCharacteristicReadRequest: MTU is too small: MTU=%d dataLength=%d", peerDevice.getMtu(), payload.length - offset));
}

final byte[] toWrite = Arrays.copyOfRange(payload, offset, payload.length);

if (mLogger.showSensitiveData()) {
mLogger.v(TAG, String.format("onCharacteristicReadRequest: writing data: device=%s base64=%s value=%s length=%d offset=%d", peerDevice.getMACAddress(), Base64.encodeToString(toWrite, Base64.DEFAULT), BleDriver.bytesToHex(toWrite), toWrite.length, offset));
mLogger.v(TAG, StringUtil.format("onCharacteristicReadRequest: writing data: device=%s base64=%s value=%s length=%d offset=%d", peerDevice.getMACAddress(), Base64.encodeToString(toWrite, Base64.DEFAULT), BleDriver.bytesToHex(toWrite), toWrite.length, offset));
}
mGattServer.getGattServer().sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, toWrite);
} else {
Expand Down Expand Up @@ -267,7 +267,7 @@ public void onCharacteristicWriteRequest(BluetoothDevice device,
byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, prepareWrite,
responseNeeded, offset, value);
mLogger.v(TAG, String.format("onCharacteristicWriteRequest called: device=%s characteristic=%s requestId=%d preparedWrite=%b needResponse=%b", mLogger.sensitiveObject(device.getAddress()), characteristic.getUuid(), requestId, prepareWrite, responseNeeded));
mLogger.v(TAG, StringUtil.format("onCharacteristicWriteRequest called: device=%s characteristic=%s requestId=%d preparedWrite=%b needResponse=%b", mLogger.sensitiveObject(device.getAddress()), characteristic.getUuid(), requestId, prepareWrite, responseNeeded));

PeerDevice peerDevice;
boolean status = false;
Expand All @@ -278,7 +278,7 @@ public void onCharacteristicWriteRequest(BluetoothDevice device,
mLogger.w(TAG, String.format("onCharacteristicWriteRequest: device=%s not connected", mLogger.sensitiveObject(device.getAddress())));
} else {
if (mLogger.showSensitiveData()) {
mLogger.d(TAG, String.format("onCharacteristicWriteRequest: device=%s base64=%s value=%s length=%d offset=%d", device.getAddress(), Base64.encodeToString(value, Base64.DEFAULT), BleDriver.bytesToHex(value), value.length, offset));
mLogger.d(TAG, StringUtil.format("onCharacteristicWriteRequest: device=%s base64=%s value=%s length=%d offset=%d", device.getAddress(), Base64.encodeToString(value, Base64.DEFAULT), BleDriver.bytesToHex(value), value.length, offset));
}

if (prepareWrite) {
Expand Down Expand Up @@ -311,7 +311,7 @@ public void onCharacteristicWriteRequest(BluetoothDevice device,
@Override
public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
super.onExecuteWrite(device, requestId, execute);
mLogger.v(TAG, String.format("onExecuteWrite called: device=%s requestId=%d execute=%s", mLogger.sensitiveObject(device.getAddress()), requestId, execute));
mLogger.v(TAG, StringUtil.format("onExecuteWrite called: device=%s requestId=%d execute=%s", mLogger.sensitiveObject(device.getAddress()), requestId, execute));

boolean status = false;

Expand All @@ -327,7 +327,7 @@ public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execut
mLogger.e(TAG, String.format("onExecuteWrite() error: device=%s: buffer is null", mLogger.sensitiveObject(device.getAddress())));
} else {
if (mLogger.showSensitiveData()) {
mLogger.d(TAG, String.format("onExecuteWrite: device=%s base64=%s value=%s length=%d", device.getAddress(), Base64.encodeToString(peerDevice.getInDataBuffer(), Base64.DEFAULT), BleDriver.bytesToHex(peerDevice.getInDataBuffer()), peerDevice.getInDataBuffer().length));
mLogger.d(TAG, StringUtil.format("onExecuteWrite: device=%s base64=%s value=%s length=%d", device.getAddress(), Base64.encodeToString(peerDevice.getInDataBuffer(), Base64.DEFAULT), BleDriver.bytesToHex(peerDevice.getInDataBuffer()), peerDevice.getInDataBuffer().length));
}
status = peerDevice.handleDataReceived(peerDevice.flushInDataBuffer());
}
Expand All @@ -347,7 +347,7 @@ public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execut
@Override
public void onMtuChanged(BluetoothDevice device, int mtu) {
super.onMtuChanged(device, mtu);
mLogger.v(TAG, String.format("onMtuChanged called for device %s and mtu=%d", mLogger.sensitiveObject(device.getAddress()), mtu));
mLogger.v(TAG, StringUtil.format("onMtuChanged called for device %s and mtu=%d", mLogger.sensitiveObject(device.getAddress()), mtu));
PeerDevice peerDevice;

if ((peerDevice = mBleDriver.deviceManager().get(device.getAddress())) == null) {
Expand All @@ -369,7 +369,7 @@ public void onNotificationSent(BluetoothDevice device, int status) {
mLogger.v(TAG, String.format("onNotificationSent called: device=%s", mLogger.sensitiveObject(device.getAddress())));

if (status != GATT_SUCCESS) {
mLogger.e(TAG, String.format("onNotificationSent status error=%d device=%s", status, mLogger.sensitiveObject(device)));
mLogger.e(TAG, StringUtil.format("onNotificationSent status error=%d device=%s", status, mLogger.sensitiveObject(device)));
}

PeerDevice peerDevice = mBleDriver.deviceManager().get(device.getAddress());
Expand Down

0 comments on commit 90c084d

Please sign in to comment.