Skip to content

Commit

Permalink
add nice watch UI
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaeus committed Aug 29, 2021
1 parent 9d769a7 commit bf093b1
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 189 deletions.
4 changes: 4 additions & 0 deletions .idea/misc.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ private void sendDNDSync(int dndState) {
} else {
for (Node node : connectedNodes) {
if (node.isNearby()) {
byte[] data = new byte[2];
data[0] = 1; // bedtime mode
data[1] = (byte) dndState;
byte[] data = new byte[1];
data[0] = (byte) dndState;
Task<Integer> sendTask =
Wearable.getMessageClient(this).sendMessage(node.getId(), DND_SYNC_MESSAGE_PATH, data);

Expand Down
20 changes: 6 additions & 14 deletions mobile/src/main/java/de/rhaeus/dndsync/DNDSyncListenerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ public void onMessageReceived (MessageEvent 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
// data[0] 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];
byte dndStatePhone = data[0];
Log.d(TAG, "dndStatePhone: " + dndStatePhone);

// get dnd state
Expand All @@ -54,15 +49,12 @@ public void onMessageReceived (MessageEvent messageEvent) {
byte currentDndState = (byte) filterState;
Log.d(TAG, "currentDndState: " + currentDndState);

if (mode == 0) { //dnd
if (dndStatePhone != currentDndState) {
mNotificationManager.setInterruptionFilter(dndStatePhone);
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);
Log.d(TAG, "DND set to " + dndStatePhone);
}

} else {
super.onMessageReceived(messageEvent);
}
Expand Down
2 changes: 1 addition & 1 deletion mobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<string name="dnd_permission_not_allowed">DND access denied</string>

<!-- Sync Preferences -->
<string name="sync_title">Sync DND state with watch</string>
<string name="sync_title">Sync DND state to watch</string>

</resources>
3 changes: 3 additions & 0 deletions wear/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.wear:wear:1.1.0'
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
wearApp project(":wear")
}
55 changes: 32 additions & 23 deletions wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature android:name="android.hardware.type.watch" />

Expand All @@ -14,10 +14,21 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.DeviceDefault">
<activity
android:name=".SettingsActivity"
android:exported="true"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<uses-library
android:name="com.google.android.wearable"
android:required="true" />

<!--
Set to true if your app is Standalone, that is, it does not require the handheld
app to run.
Expand All @@ -30,48 +41,46 @@
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:description="@string/access_desc"
android:name=".DNDSyncAccessService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="DNDSync"
android:description="@string/access_desc"
android:enabled="true"
android:exported="false" >
android:exported="false"
android:label="DNDSync"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>

<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config"/>
android:resource="@xml/accessibility_service_config" />
</service>

<service android:name=".DNDSyncListenerService"
android:exported="true"
android:enabled="true">
<service
android:name=".DNDSyncListenerService"
android:enabled="true"
android:exported="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>

<!-- grant permission with adb shell cmd notification allow_listener de.rhaeus.dndsync/de.rhaeus.dndsync.DNDNotificationService-->
<service android:name=".DNDNotificationService"
<data
android:host="*"
android:path="/wear-dnd-sync"
android:scheme="wear" />
</intent-filter>
</service> <!-- 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>
19 changes: 12 additions & 7 deletions wear/src/main/java/de/rhaeus/dndsync/DNDNotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@


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

import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
Expand Down Expand Up @@ -54,11 +56,15 @@ public void onInterruptionFilterChanged (int 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();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean syncDnd = prefs.getBoolean("dnd_sync_key", true);
if(syncDnd) {
new Thread(new Runnable() {
public void run() {
sendDNDSync(interruptionFilter);
}
}).start();
}
}

private void sendDNDSync(int dndState) {
Expand Down Expand Up @@ -90,8 +96,7 @@ private void sendDNDSync(int dndState) {
for (Node node : connectedNodes) {
if (node.isNearby()) {
byte[] data = new byte[2];
data[0] = 0; // dnd mode
data[1] = (byte) dndState;
data[0] = (byte) dndState;
Task<Integer> sendTask =
Wearable.getMessageClient(this).sendMessage(node.getId(), DND_SYNC_MESSAGE_PATH, data);

Expand Down
60 changes: 14 additions & 46 deletions wear/src/main/java/de/rhaeus/dndsync/DNDSyncListenerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
Expand All @@ -10,6 +11,8 @@
import android.util.Log;
import android.widget.Toast;

import androidx.preference.PreferenceManager;

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

Expand All @@ -28,26 +31,17 @@ public void onMessageReceived (MessageEvent messageEvent) {
vibrate();

byte[] data = messageEvent.getData();
// data[0] contains if dnd or bedtime mode
// 0 = dnd
// 1 = bedtime mode
// data[1] contains dnd mode of phone
// data[0] 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];
byte dndStatePhone = data[0];
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) {
Expand All @@ -56,43 +50,17 @@ public void onMessageReceived (MessageEvent messageEvent) {
byte currentDndState = (byte) filterState;
Log.d(TAG, "currentDndState: " + currentDndState);

switch (mode) {
case 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);
}
break;
}
case 1: //bedtime
{
if (dndStatePhone != currentDndState) {
toggleBedtimeMode();
// set DND just in case bedtime toggle does not work to have at least DND
mNotificationManager.setInterruptionFilter(dndStatePhone);
Log.d(TAG, "DND set to " + dndStatePhone);
}

// // now toggle bedtime mode
// Log.d(TAG, "toggle bedtime mode");
// if(currentDndState == NotificationManager.INTERRUPTION_FILTER_ALL && dndStatePhone != NotificationManager.INTERRUPTION_FILTER_ALL) {
// // watch dnd is off but phone dnd is not off -> turn bedtime mode on
// Log.d(TAG, "DNDSync enables bedtime mode");
// toggleBedtimeMode();
// } else if (currentDndState != NotificationManager.INTERRUPTION_FILTER_ALL && dndStatePhone == NotificationManager.INTERRUPTION_FILTER_ALL) {
// // watch dnd is not off but phone dnd is off -> turn bedtime mode off
// Log.d(TAG, "DNDSync disables bedtime mode");
// toggleBedtimeMode();
// }
break;
}
default:
{
Log.d(TAG, "DNDSync mode went wrong: " + mode);
if (dndStatePhone != currentDndState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean useBedtimeMode = prefs.getBoolean("bedtime_key", true);
if (useBedtimeMode) {
toggleBedtimeMode();
}
// set DND anyways, also in case bedtime toggle does not work to have at least DND
mNotificationManager.setInterruptionFilter(dndStatePhone);
Log.d(TAG, "DND set to " + dndStatePhone);
}

} else {
super.onMessageReceived(messageEvent);
}
Expand Down
Loading

0 comments on commit bf093b1

Please sign in to comment.