How to implement ZenPolicy rules? - java

Does anyone here know anything about ZenPolicy and how to implement a rule? I am trying to replicate the Do Not Disturb setting where calls are not allowed through, but exceptions are possible, eg. Media sounds, Touch sounds etc. I have managed to disallow calls but when I toggle the exceptions settings (from my app), this reverts to "starred contacts only".
I am attaching a screenshot of what I am trying to replicate in my app. Here is the code I am using so far:
3 -> {
notificationManager.setInterruptionFilter(
NotificationManager.INTERRUPTION_FILTER_PRIORITY
)
notificationManager.setNotificationPolicy(
NotificationManager.Policy(
PEOPLE_TYPE_NONE,
PRIORITY_SENDERS_ANY,
PRIORITY_SENDERS_ANY
)
)
}
I have also looked through: https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/service/notification/ZenModeConfig.java;drc=master;l=754 and https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/service/notification/ZenPolicy.java
and came up with this function, but it doesn't do anything...:
fun zenPolicyBuilder(): ZenPolicy.Builder {
val builder = ZenPolicy.Builder()
val calls = PRIORITY_CATEGORY_CALLS
val repeatCallers = PRIORITY_CATEGORY_REPEAT_CALLERS
val alarms = PRIORITY_CATEGORY_ALARMS
val media = PRIORITY_CATEGORY_MEDIA
val system = PRIORITY_CATEGORY_SYSTEM
val events = PRIORITY_CATEGORY_EVENTS
val reminders = PRIORITY_CATEGORY_REMINDERS
return when {
calls != ZenPolicy.PEOPLE_TYPE_UNSET -> {
builder.allowCalls(PEOPLE_TYPE_NONE)
}
repeatCallers != ZenPolicy.STATE_UNSET -> {
builder.allowRepeatCallers(repeatCallers == ZenPolicy.STATE_ALLOW)
}
alarms != ZenPolicy.STATE_UNSET -> {
builder.allowAlarms(alarms == ZenPolicy.STATE_ALLOW)
}
media != ZenPolicy.STATE_UNSET -> {
builder.allowMedia(media == ZenPolicy.STATE_ALLOW)
}
system != ZenPolicy.STATE_UNSET -> {
builder.allowSystem(system == ZenPolicy.STATE_ALLOW)
}
events != ZenPolicy.STATE_UNSET -> {
builder.allowEvents(events == ZenPolicy.STATE_ALLOW)
}
reminders != ZenPolicy.STATE_UNSET -> {
builder.allowReminders(reminders == ZenPolicy.STATE_ALLOW)
}
else -> builder
}
}

For anyone who might be interested in future, the first code snippet that I posted actually works fine. The problem was coming from another part of my code which was interfering with this.

Related

Get the receiver's number from an SMS on Android

I am using the below code to get originatingAddress (the sender's number).
import android.provider.Telephony.Sms.Intents.getMessagesFromIntent
fun getFullMessage(messageParts: Array<SmsMessage>): CompleteSmsMessage {
messageParts = getFullMessage(getMessagesFromIntent(intent))
messageParts.last().also { part ->
return CompleteSmsMessage(
originatingAddress = part.originatingAddress,
body = messageParts
.map { it.messageBody }
.reduce { acc, body -> acc + body }
)
}
}
Since I have multiple SIM slots on my phone, I'd like to get the receiver's number as well. I couldn't find any relevant document here: https://developer.android.com/reference/android/telephony/SmsMessage#getDisplayOriginatingAddress()
Is it possible to get the receiver's number? How?
The incoming intent contains subscription variable, this is the SIM slot:
val slotId = intent.getIntExtra("subscription", -1)

Translate Kotlin to Java BLE callback

Im trying to translate this part of a code to Java, i understand its waiting for a callback from a BLE action, if returns any data will be added to statusReport, only issue is that it seems that is doing a loop somewhere because in the real application gets this callback multiple times but on my side i only get one response.
private val statusReportCallback = DataReceivedCallback { _, data -> convertByteArrayToASCII(data.value)?.let { Endpoint.addToStatusReport(it)
if (nextWillBeICCID) iccid = it.also { notifyDeviceEvent(EventType.ICCID) }.also { nextWillBeICCID = false }
when (it) {
"power off" -> Endpoint.commsTestInProgress.set(false)
"ICCID:" -> nextWillBeICCID = true
"POST: Success" -> Endpoint.onServerConnected.value = Event(true)
}
} }

translate kotlin to java

I'm using a screenshot library (in Github) and it is has been written in Kotlin(I don't know Kotlin very well).
<https://github.com/bolteu/screenshotty>
I don't know how to translate to Java a part of code.
in the read me file :
val screenshotResult = screenshotManager.makeScreenshot()
val subscription = screenshotResult.observe(
onSuccess = { processScreenshot(it) },
onError = { onMakeScreenshotFailed(it) }
)
It says that you can get a screenshot object from "it"?
how can I do that?
please help me...
and how can I translate this code to Java :
fun show(screenshot: Screenshot) {
val bitmap = when (screenshot) {
is ScreenshotBitmap -> screenshot.bitmap
}
screenshotPreview.setImageBitmap(bitmap)
}

Getting NMEA DATA in android

The current issue that I'm getting is that the OnNmeaListener is not returning the correct data from the NMEA using the GpsStatus.Listener therefore I tried the GNSS Status for the peace of mind, it ended up returning gibberish data , so I had to revert back to GpsStatus Listener. I also prior to using OnNmeaListener tried GpsStatus.NmeaListner and when used tried to add it to the GpsStatus Listener it would show up as an error and suggested that i typecast it to OnNmeaListener, when this is done and the app is built and all and then executed from the app, it would crash and then not work at all, and this is why i shifted to OnNmeaListener in the first place. Now to its credit the OnNmeaListener works just fine and doesn't crash at all, but the data it gives out is not at all accurate, I thought maybe this might have to be some sort of fault/error on the device side, I then proceeded to test it on two devices with different API levels of 25 and 28, On the the 25 Level device it just did not give half of the data whereas on the 28 level device the data is full but not accurate and correct.
This is the method that I'm currently using:
OnNmeaMessageListener nmeaMessageListener = new OnNmeaMessageListener () {
#Override
public void onNmeaMessage(String nmea , long timestamp) {
if (trackStarted == true && trackPaused == false) {
if (nmea.startsWith ( "$GPGGA" ) || nmea.startsWith ( "$GPRMC" )) {
//Log.d("TrackManagerService","NMEA:" + nmea);
try {
// out.write(nmea.getBytes());
if (continuesMode) {
dataOut.write ( nmea.getBytes () );
bufferedNMEALines++;
if (bufferedNMEALines >= 10) {
flushContinuesTrack ();
//dataOut.reset();
bufferedNMEALines = 0;
}
} else {
outCompressed.write ( nmea.getBytes () );
}
// Log.d("TrackManagerService","NMEA:" + nmea);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
if (nmea.startsWith ( "$GPGGA" )) {
String[] nmeaSplit = nmea.split ( "," );
if (nmeaSplit.length > 10) {
if (nmeaSplit[9].length () > 0) {
try {
mslAltitude = Float.parseFloat ( nmeaSplit[9] );
} catch (NumberFormatException ex) {
mslAltitude = 0;
}
} else {
mslAltitude = 0.0f;
}
}
}
}
//if (markStarted && hasFix() && nmea.startsWith("$GPGGA")) {
if (markStarted && hasFix () && nmea.startsWith ( "$GPRMC" )) {
markProgress++;
trackListener.onMarkProgress ( markProgress );
if (markProgress >= markMax) {
stopMark ();
}
}
}
}
};
And this is the output it gives when called:
$Start,082719,260220;
$GPGGA,082721.42,2654.2979,N,08056.6065,E,0,100.000,0.000,M,M,0,*44
$GPRMC,082721.42,V,2654.2979,N,08056.6065,E,0.000,0.000,260220,E,N*01
$GPGGA,082722.41,2654.2979,N,08056.6065,E,0,100.000,0.000,M,M,0,*44
$GPRMC,082722.41,V,2654.2979,N,08056.6065,E,0.000,0.000,260220,E,N*01
$End,082802,260220;
$Start,082804,260220;
$GPGGA,082837.42,2654.2979,N,08056.6065,E,0,100.000,0.000,M,M,0,*4C
$GPRMC,082837.42,V,2654.2979,N,08056.6065,E,0.000,0.000,260220,E,N*09
$GPGGA,082838.42,2654.2979,N,08056.6065,E,0,100.000,0.000,M,M,0,*43
$GPGGA,082840.42,2654.2979,N,08056.6065,E,0,100.000,0.000,M,M,0,*4C
$End,082841,260220;
$Start,082921,260220;
$GPGGA,082923.40,2654.2982,N,08056.6059,E,0,100.000,0.000,M,M,0,*41
$GPRMC,082923.40,V,2654.2982,N,08056.6059,E,0.000,0.000,260220,E,N*04
$GPGGA,082924.40,2654.2982,N,08056.6059,E,0,100.000,0.000,M,M,0,*46
$GPRMC,082945.40,V,2654.2982,N,08056.6059,E,0.000,0.000,260220,E,N*04
$End,082945,260220;
And this is the output when called using GNSS listener:
$Start,080450,260220;������������������������������������������������������������������������������������������������������������������������������������������������������������������
Any Sort of way to make this works?
[EDIT]
This is the response from an iOS device, which was accepted by the sever:
$Start,150403,250220;
$GPGGA,150403.69,2654.29986954,N,8056.60312653,E,1,00,0.0,0.0,0,0.0,0,0.0,0000*42;
$GPRMC,150743.99,A,2654.29986954,N,8056.60260391,E,0.0,0.0,250220,0.0,E,A*5;
$GPRMC,150844.26,A,2654.29986954,N,8056.60260391,E,0.0,0.0,250220,0.0,E,A*9;
$GPRMC,150937.91,A,2654.30012894,N,8056.60312653,E,0.0,0.0,250220,0.0,E,A*e;
$GPGGA,150945.04,2654.30429840,N,8056.60781097,E,1,00,0.0,0.0,0,0.0,0000*42;
$GPRMC,150952.01,A,2654.29921722,N,8056.60364532,E,0.0,0.0,250220,0.0,E,A*2;
$End,150953,250220;
$GPGGA,150953.00,2654.29921722,N,8056.60364532,E,1,00,0.0,0.0,0,0.0,0000*42;
$GPRMC,150953.00,A,2654.29921722,N,8056.60364532,E,0.0,0.0,250220,0.0,E,A*2;
Now, I DO NOT have the faintest idea how this works on an iOS device,(a Senior of mine worked on that and gave me this data) but somehow it worked on that and when the same logic was implemented on Android and the data was uploaded to the server it wouldn't accept it, whereas when the iOS data was manipulated in any sort of way ( removing lines, addlines, removing semi-colons, commas,etc.) it would somehow still work.

direct web remoting asynchronous issue

I am using the direct web remoting library to do some ajax calls in my application. I am having a problem that I think comes down to a delayed response from the service call. Below is part of my code that I think is having the problem. The problem is in getDefaultReviewerTypeCode, the return variable isn't set in the call back till after other "stuff" processes. Is there a way to tell DWR to wait for a response before it continues processing the java script?
function makeProtocolReviewerTypesDropDown(reviewerTypes, reviewerIndex) {
var defaultReviewerType = getDefaultReviewerTypeCode();
...
var option = document.createElement('option');
option.setAttribute("value", "");
if (defaultReviewerType == '') {
option.setAttribute("selected", "selected");
}
...
for (var i = 0; i < reviewerTypes.length; i += 2) {
var reviewerType = reviewerTypes[i].replace(/^\t*/, '');
option = document.createElement('option');
option.setAttribute("value", reviewerType);
if (defaultReviewerType == reviewerType) {
option.setAttribute("selected", "selected");
}
option.text = reviewerTypes[i+1];
addSelectOption(selectElement, option);
}
return selectElement;
}
function getDefaultReviewerTypeCode() {
var defaultReviewTyper;
var dwrReply = {
callback:function(data) {
if ( data != null ) {
defaultReviewTyper = data;
} else {
defaultReviewTyper = '';
}
}
};
IacucProtocolActionAjaxService.getDefaultCommitteeReviewTypeCode(dwrReply);
return defaultReviewTyper;
}
The best way is to encapsulate all code that follows the DWR call into a separate method, and call that from the DWR callback. That way, your code that depends on the DWR results is guaranteed to be called only after DWR returns.

Categories

Resources