Messaging: Let there be lambdas

Change-Id: Iee1fc46c92ea9159abb10d92d34baee937bdee0c
This commit is contained in:
Michael W
2024-12-26 14:55:12 +01:00
parent c1b4aa4dfe
commit d43b6ff1c4
84 changed files with 991 additions and 1697 deletions
@@ -17,7 +17,6 @@
package android.support.v7.mms;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
@@ -403,28 +402,25 @@ class DefaultApnSettingsLoader implements ApnSettingsLoader {
XmlResourceParser xml = null;
try {
xml = mContext.getResources().getXml(R.xml.apns);
new ApnsXmlParser(xml, new ApnsXmlParser.ApnProcessor() {
@Override
public void process(ContentValues apnValues) {
final String mcc = trimWithNullCheck(apnValues.getAsString(APN_MCC));
final String mnc = trimWithNullCheck(apnValues.getAsString(APN_MNC));
final String apn = trimWithNullCheck(apnValues.getAsString(APN_APN));
try {
if (mccMnc[0] == Integer.parseInt(mcc) &&
mccMnc[1] == Integer.parseInt(mnc) &&
(TextUtils.isEmpty(apnName) || apnName.equalsIgnoreCase(apn))) {
final String type = apnValues.getAsString(APN_TYPE);
final String mmsc = apnValues.getAsString(APN_MMSC);
final String mmsproxy = apnValues.getAsString(APN_MMSPROXY);
final String mmsport = apnValues.getAsString(APN_MMSPORT);
final Apn newApn = MemoryApn.from(apns, type, mmsc, mmsproxy, mmsport);
if (newApn != null) {
apns.add(newApn);
}
new ApnsXmlParser(xml, apnValues -> {
final String mcc = trimWithNullCheck(apnValues.getAsString(APN_MCC));
final String mnc = trimWithNullCheck(apnValues.getAsString(APN_MNC));
final String apn = trimWithNullCheck(apnValues.getAsString(APN_APN));
try {
if (mccMnc[0] == Integer.parseInt(mcc) &&
mccMnc[1] == Integer.parseInt(mnc) &&
(TextUtils.isEmpty(apnName) || apnName.equalsIgnoreCase(apn))) {
final String type = apnValues.getAsString(APN_TYPE);
final String mmsc = apnValues.getAsString(APN_MMSC);
final String mmsproxy = apnValues.getAsString(APN_MMSPROXY);
final String mmsport = apnValues.getAsString(APN_MMSPORT);
final Apn newApn = MemoryApn.from(apns, type, mmsc, mmsproxy, mmsport);
if (newApn != null) {
apns.add(newApn);
}
} catch (final NumberFormatException e) {
// Ignore
}
} catch (final NumberFormatException e) {
// Ignore
}
}).parse();
} catch (final Resources.NotFoundException e) {
@@ -95,21 +95,18 @@ class DefaultCarrierConfigValuesLoader implements CarrierConfigValuesLoader {
XmlResourceParser xml = null;
try {
xml = subContext.getResources().getXml(R.xml.mms_config);
new CarrierConfigXmlParser(xml, new CarrierConfigXmlParser.KeyValueProcessor() {
@Override
public void process(String type, String key, String value) {
try {
if (KEY_TYPE_INT.equals(type)) {
values.putInt(key, Integer.parseInt(value));
} else if (KEY_TYPE_BOOL.equals(type)) {
values.putBoolean(key, Boolean.parseBoolean(value));
} else if (KEY_TYPE_STRING.equals(type)) {
values.putString(key, value);
}
} catch (final NumberFormatException e) {
Log.w(MmsService.TAG, "Load carrier value from resources: "
+ "invalid " + key + "," + value + "," + type);
new CarrierConfigXmlParser(xml, (type, key, value) -> {
try {
if (KEY_TYPE_INT.equals(type)) {
values.putInt(key, Integer.parseInt(value));
} else if (KEY_TYPE_BOOL.equals(type)) {
values.putBoolean(key, Boolean.parseBoolean(value));
} else if (KEY_TYPE_STRING.equals(type)) {
values.putString(key, value);
}
} catch (final NumberFormatException e) {
Log.w(MmsService.TAG, "Load carrier value from resources: "
+ "invalid " + key + "," + value + "," + type);
}
}).parse();
} catch (final Resources.NotFoundException e) {
+17 -19
View File
@@ -82,25 +82,23 @@ class DownloadRequest extends MmsRequest {
if (contentUri == null || pdu == null) {
return false;
}
final Callable<Boolean> copyDownloadedPduToOutput = new Callable<Boolean>() {
public Boolean call() {
ParcelFileDescriptor.AutoCloseOutputStream outStream = null;
try {
final ContentResolver cr = context.getContentResolver();
final ParcelFileDescriptor pduFd = cr.openFileDescriptor(contentUri, "w");
outStream = new ParcelFileDescriptor.AutoCloseOutputStream(pduFd);
outStream.write(pdu);
return true;
} catch (IOException e) {
Log.e(MmsService.TAG, "Writing PDU to downloader: IO exception", e);
return false;
} finally {
if (outStream != null) {
try {
outStream.close();
} catch (IOException ex) {
// Ignore
}
final Callable<Boolean> copyDownloadedPduToOutput = () -> {
ParcelFileDescriptor.AutoCloseOutputStream outStream = null;
try {
final ContentResolver cr = context.getContentResolver();
final ParcelFileDescriptor pduFd = cr.openFileDescriptor(contentUri, "w");
outStream = new ParcelFileDescriptor.AutoCloseOutputStream(pduFd);
outStream.write(pdu);
return true;
} catch (IOException e) {
Log.e(MmsService.TAG, "Writing PDU to downloader: IO exception", e);
return false;
} finally {
if (outStream != null) {
try {
outStream.close();
} catch (IOException ex) {
// Ignore
}
}
}
+15 -23
View File
@@ -251,12 +251,7 @@ public class MmsService extends Service {
// Handler for scheduling service stop
private final Handler mHandler = new Handler();
// Service stop task
private final Runnable mServiceStopRunnable = new Runnable() {
@Override
public void run() {
tryStopService();
}
};
private final Runnable mServiceStopRunnable = this::tryStopService;
/**
* Start the service with a request
@@ -325,24 +320,21 @@ public class MmsService extends Service {
final MmsRequest request = intent.getParcelableExtra(EXTRA_REQUEST);
if (request != null) {
try {
retainService(request, new Runnable() {
@Override
public void run() {
try {
request.execute(
MmsService.this,
mNetworkManager,
getApnSettingsLoader(),
getCarrierConfigValuesLoader(),
getUserAgentInfoLoader());
} catch (Exception e) {
Log.w(TAG, "Unexpected execution failure", e);
} finally {
if (request.getUseWakeLock()) {
releaseWakeLock();
}
releaseService();
retainService(request, () -> {
try {
request.execute(
MmsService.this,
mNetworkManager,
getApnSettingsLoader(),
getCarrierConfigValuesLoader(),
getUserAgentInfoLoader());
} catch (Exception e) {
Log.w(TAG, "Unexpected execution failure", e);
} finally {
if (request.getUseWakeLock()) {
releaseWakeLock();
}
releaseService();
}
});
scheduled = true;
+29 -31
View File
@@ -101,38 +101,36 @@ class SendRequest extends MmsRequest {
if (contentUri == null) {
return null;
}
final Callable<byte[]> copyPduToArray = new Callable<byte[]>() {
public byte[] call() {
ParcelFileDescriptor.AutoCloseInputStream inStream = null;
try {
final ContentResolver cr = context.getContentResolver();
final ParcelFileDescriptor pduFd = cr.openFileDescriptor(contentUri, "r");
inStream = new ParcelFileDescriptor.AutoCloseInputStream(pduFd);
// Request one extra byte to make sure file not bigger than maxSize
final byte[] readBuf = new byte[maxSize+1];
final int bytesRead = inStream.read(readBuf, 0, maxSize+1);
if (bytesRead <= 0) {
Log.e(MmsService.TAG, "Reading PDU from sender: empty PDU");
return null;
}
if (bytesRead > maxSize) {
Log.e(MmsService.TAG, "Reading PDU from sender: PDU too large");
return null;
}
// Copy and return the exact length of bytes
final byte[] result = new byte[bytesRead];
System.arraycopy(readBuf, 0, result, 0, bytesRead);
return result;
} catch (IOException e) {
Log.e(MmsService.TAG, "Reading PDU from sender: IO exception", e);
final Callable<byte[]> copyPduToArray = () -> {
ParcelFileDescriptor.AutoCloseInputStream inStream = null;
try {
final ContentResolver cr = context.getContentResolver();
final ParcelFileDescriptor pduFd = cr.openFileDescriptor(contentUri, "r");
inStream = new ParcelFileDescriptor.AutoCloseInputStream(pduFd);
// Request one extra byte to make sure file not bigger than maxSize
final byte[] readBuf = new byte[maxSize+1];
final int bytesRead = inStream.read(readBuf, 0, maxSize+1);
if (bytesRead <= 0) {
Log.e(MmsService.TAG, "Reading PDU from sender: empty PDU");
return null;
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException ex) {
// Ignore
}
}
if (bytesRead > maxSize) {
Log.e(MmsService.TAG, "Reading PDU from sender: PDU too large");
return null;
}
// Copy and return the exact length of bytes
final byte[] result = new byte[bytesRead];
System.arraycopy(readBuf, 0, result, 0, bytesRead);
return result;
} catch (IOException e) {
Log.e(MmsService.TAG, "Reading PDU from sender: IO exception", e);
return null;
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException ex) {
// Ignore
}
}
}