Skip to content

Commit

Permalink
2 way sync and new icon color
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaeus committed Aug 29, 2021
1 parent 3cb6a18 commit ea7c051
Show file tree
Hide file tree
Showing 47 changed files with 261 additions and 147 deletions.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@
</intent-filter>
</service>

<!-- <receiver android:name=".DNDChangedReceiver" android:exported="true">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.app.action.INTERRUPTION_FILTER_CHANGED" />-->
<!-- <action android:name="android.media.RINGER_MODE_CHANGED" />-->
<!-- </intent-filter>-->
<!-- </receiver>-->
<service android:name=".DNDSyncListenerService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*"
android:path="/wear-dnd-sync" />
</intent-filter>
</service>

</application>


Expand Down
Binary file modified mobile/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions mobile/src/main/java/de/rhaeus/dndsync/DNDSyncListenerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package de.rhaeus.dndsync;

import android.app.NotificationManager;
import android.content.Context;
import android.os.Handler;
import android.os.PowerManager;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

public class DNDSyncListenerService extends WearableListenerService {
private static final String TAG = "DNDSyncListenerService";
private static final String DND_SYNC_MESSAGE_PATH = "/wear-dnd-sync";

@Override
public void onMessageReceived (MessageEvent messageEvent) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "onMessageReceived: " + messageEvent);
}

if (messageEvent.getPath().equalsIgnoreCase(DND_SYNC_MESSAGE_PATH)) {

byte[] data = messageEvent.getData();
// data[0] contains if dnd or bedtime mode
// 0 = dnd
// 1 = bedtime mode
// data[1] contains dnd mode of phone
// 0 = INTERRUPTION_FILTER_UNKNOWN
// 1 = INTERRUPTION_FILTER_ALL (all notifications pass)
// 2 = INTERRUPTION_FILTER_PRIORITY
// 3 = INTERRUPTION_FILTER_NONE (no notification passes)
// 4 = INTERRUPTION_FILTER_ALARMS
byte mode = data[0];
Log.d(TAG, "mode: " + mode);
byte dndStatePhone = data[1];
Log.d(TAG, "dndStatePhone: " + dndStatePhone);

// get dnd state
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
// Toast.makeText(getApplicationContext(), "DNDSync missing DND permission!", Toast.LENGTH_SHORT).show();
// Log.d(TAG, "DNDSync missing DND permission!");
// }

int filterState = mNotificationManager.getCurrentInterruptionFilter();
if (filterState < 0 || filterState > 4) {
Log.d(TAG, "DNDSync weird current dnd state: " + filterState);
}
byte currentDndState = (byte) filterState;
Log.d(TAG, "currentDndState: " + currentDndState);

if (mode == 0) { //dnd
if (dndStatePhone != currentDndState) {
mNotificationManager.setInterruptionFilter(dndStatePhone);
// Toast.makeText(getApplicationContext(), "DND set to " + dndStatePhone, Toast.LENGTH_LONG).show();
Log.d(TAG, "DND set to " + dndStatePhone);
}
} else {
Log.d(TAG, "DNDSync mode went wrong: " + mode);
}
} else {
super.onMessageReceived(messageEvent);
}
}

}
Binary file modified mobile/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-hdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-hdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-mdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-mdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mobile/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion mobile/src/main/res/values/ic_launcher_background.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#3DAADC</color>
<color name="ic_launcher_background">#4272F5</color>
</resources>
6 changes: 6 additions & 0 deletions mobile/src/main/res/values/wear.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@array/android_wear_capabilities">
<string-array name="android_wear_capabilities">
<item>dnd_sync</item>
</string-array>
</resources>
10 changes: 6 additions & 4 deletions wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
-->
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
android:value="false" />

<activity
android:name=".MainActivity"
Expand Down Expand Up @@ -53,23 +53,25 @@
</service>

<service android:name=".DNDSyncListenerService"
android:exported="true">
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data android:scheme="wear" android:host="*"
android:path="/wear-dnd-sync" />
</intent-filter>
</service>

<!-- A workaround used to be able to set the permissions we need. Is later disabled. -->
<service android:name=".DummyNotificationListener"
<!-- grant permission with adb shell cmd notification allow_listener de.rhaeus.dndsync/de.rhaeus.dndsync.DNDNotificationService-->
<service android:name=".DNDNotificationService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

</application>

</manifest>
Binary file modified wear/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.rhaeus.dndsync;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.service.notification.NotificationListenerService;
import android.util.Log;
import android.widget.Toast;

Expand All @@ -18,50 +18,65 @@
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;


public class DNDChangedReceiver extends BroadcastReceiver{
private static final String TAG = "DNDChangedReceiver";
public class DNDNotificationService extends NotificationListenerService {
private static final String TAG = "DNDNotificationService";
private static final String DND_SYNC_CAPABILITY_NAME = "dnd_sync";
private static final String DND_SYNC_MESSAGE_PATH = "/wear-dnd-sync";

@Override
public void onListenerConnected() {
Log.d(TAG, "listener connected");
// // We don't want to run a background service so disable and stop it
// // to avoid running this service in the background
// disableServiceComponent();
// Log.i(TAG, "Disabling service");
//
// try {
// stopSelf();
// } catch(SecurityException e) {
// Log.e(TAG, "Failed to stop service");
// }
}
private void disableServiceComponent() {
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, DNDNotificationService.class);
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}



@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int dndState = mNotificationManager.getCurrentInterruptionFilter();
Log.d(TAG, "Current DND state is: " + dndState);

new Thread(new Runnable() {
public void run() {
sendSyncRequest(context, dndState);
}
}).start();
}
public void onInterruptionFilterChanged (int interruptionFilter) {
Log.d(TAG, "interruption filter changed to " + interruptionFilter);
// Unable to retrieve node with transcription capability
// Toast.makeText(getApplicationContext(), "interruption filter changed to " + interruptionFilter, Toast.LENGTH_LONG).show();

new Thread(new Runnable() {
public void run() {
sendDNDSync(interruptionFilter);
}
}).start();
}

private void sendSyncRequest(Context context, int dndState) {
private void sendDNDSync(int dndState) {
// https://developer.android.com/training/wearables/data/messages

// search nodes for sync
CapabilityInfo capabilityInfo = null;
try {
capabilityInfo = Tasks.await(
Wearable.getCapabilityClient(context).getCapability(
Wearable.getCapabilityClient(this).getCapability(
DND_SYNC_CAPABILITY_NAME, CapabilityClient.FILTER_REACHABLE));
} catch (ExecutionException e) {
e.printStackTrace();
Log.e(TAG, "oh god1", e);
Log.e(TAG, "execution error while searching nodes", e);
return;
} catch (InterruptedException e) {
e.printStackTrace();
Log.e(TAG, "oh god2", e);
Log.e(TAG, "interruption error while searching nodes", e);
return;
}

Expand All @@ -75,18 +90,18 @@ private void sendSyncRequest(Context context, int dndState) {
for (Node node : connectedNodes) {
if (node.isNearby()) {
byte[] data = new byte[2];
data[0] = 1; // bedtime mode
data[0] = 0; // dnd mode
data[1] = (byte) dndState;
Task<Integer> sendTask =
Wearable.getMessageClient(context).sendMessage(node.getId(), DND_SYNC_MESSAGE_PATH, data);
// You can add success and/or failure listeners,
// Or you can call Tasks.await() and catch ExecutionException
Wearable.getMessageClient(this).sendMessage(node.getId(), DND_SYNC_MESSAGE_PATH, data);

sendTask.addOnSuccessListener(new OnSuccessListener<Integer>() {
@Override
public void onSuccess(Integer integer) {
Log.d(TAG, "send successful!");
}
});

sendTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Expand All @@ -97,5 +112,4 @@ public void onFailure(@NonNull Exception e) {
}
}
}

}
Loading

0 comments on commit ea7c051

Please sign in to comment.