|
|
@@ -2,15 +2,20 @@ package com.example.david.libretasker.Actions;
|
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.media.AudioManager;
|
|
|
import android.media.Ringtone;
|
|
|
import android.media.RingtoneManager;
|
|
|
import android.net.Uri;
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
+import com.example.david.libretasker.Events.Event;
|
|
|
import com.example.david.libretasker.R;
|
|
|
+import com.example.david.libretasker.State.State;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import static android.content.Context.NOTIFICATION_SERVICE;
|
|
|
@@ -25,6 +30,7 @@ public class Actions {
|
|
|
ret.add("Toast");
|
|
|
ret.add("Notification");
|
|
|
ret.add("Beep");
|
|
|
+ ret.add("State");
|
|
|
/*
|
|
|
* Display: Lock, Auto brightness
|
|
|
* Media: Music Play | skip | stop, Record audio, Record audio stop, Take Photo
|
|
|
@@ -39,15 +45,17 @@ public class Actions {
|
|
|
Toast.makeText(context, (String)payload, Toast.LENGTH_SHORT).show();
|
|
|
return;
|
|
|
case "Notification":
|
|
|
- HashMap<String, Object> tval = (HashMap<String, Object>)payload;
|
|
|
- assert(tval.get("Title") != null);
|
|
|
- assert(tval.get("Value") != null);
|
|
|
+ HashMap<String, String> tval = (HashMap<String, String>)payload;
|
|
|
+ if (tval.get("Title") == null)
|
|
|
+ throw new AssertionError("Title");
|
|
|
+ if (tval.get("Value") == null)
|
|
|
+ throw new AssertionError("Value");
|
|
|
|
|
|
NotificationCompat.Builder mBuilder =
|
|
|
new NotificationCompat.Builder(context)
|
|
|
.setSmallIcon(R.drawable.ic_stat_name)
|
|
|
- .setContentTitle((String)tval.get("Title"))
|
|
|
- .setContentText((String)tval.get("Value"));
|
|
|
+ .setContentTitle(tval.get("Title"))
|
|
|
+ .setContentText(tval.get("Value"));
|
|
|
NotificationManager mNotifyMgr =
|
|
|
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
|
|
|
// Builds the notification and issues it.
|
|
|
@@ -59,6 +67,37 @@ public class Actions {
|
|
|
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
|
|
|
r.play();
|
|
|
return;
|
|
|
+ case "State":
|
|
|
+ HashMap<String, Object> s = (HashMap<String, Object>)payload;
|
|
|
+ if (State.set((String) s.get("Key"), s.get("Value"))) { //Did the state change? Trigger events
|
|
|
+ Intent i = new Intent(Event.STATE_CHANGED);
|
|
|
+ i.setData(Uri.parse("State"));
|
|
|
+ context.sendBroadcast(i);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ case "Volume":
|
|
|
+ HashMap<String, Integer> v = (HashMap<String, Integer>)payload;
|
|
|
+ AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
|
|
+
|
|
|
+ if (v.get("Stream") == null) throw new AssertionError("Stream");
|
|
|
+ if (v.get("Volume") == null) throw new AssertionError("Volume");
|
|
|
+
|
|
|
+ int streamType = v.get("Stream");
|
|
|
+
|
|
|
+ if (!Arrays.asList(
|
|
|
+ AudioManager.STREAM_RING,
|
|
|
+ AudioManager.STREAM_ALARM,
|
|
|
+ AudioManager.STREAM_MUSIC,
|
|
|
+ AudioManager.STREAM_NOTIFICATION,
|
|
|
+ AudioManager.STREAM_SYSTEM).contains(streamType))
|
|
|
+ throw new AssertionError("Invalid streamType");
|
|
|
+
|
|
|
+ int desiredPct = v.get("Volume"); // in %
|
|
|
+ int maxVol = audio.getStreamMaxVolume(streamType);
|
|
|
+ int finalVolume = (int)(((float)desiredPct/100)*maxVol);
|
|
|
+ audio.setStreamVolume(streamType, finalVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
|
|
|
+ //audio.setStreamVolume(streamType, finalVolume, AudioManager.FLAG_SHOW_UI);
|
|
|
+ return;
|
|
|
default:
|
|
|
Toast.makeText(context, name + " " + (String)payload, Toast.LENGTH_SHORT).show();
|
|
|
return;
|