|
|
@@ -0,0 +1,65 @@
|
|
|
+package com.example.david.libretasker.Events;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.telephony.TelephonyManager;
|
|
|
+
|
|
|
+import com.example.david.libretasker.Helpers;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by david on 03/01/17.
|
|
|
+ */
|
|
|
+
|
|
|
+public class PhoneEvent implements Event {
|
|
|
+ private String failureReason = "";
|
|
|
+ @Override
|
|
|
+ public boolean trigger(Context ctx, HashMap<String, Object> hm) {
|
|
|
+ failureReason = "";
|
|
|
+
|
|
|
+ Intent intent = (Intent) hm.get("Intent");
|
|
|
+ Bundle bundle = intent.getExtras();
|
|
|
+ if (bundle == null) {
|
|
|
+ failureReason = "Bundle is null";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String state = bundle.getString(TelephonyManager.EXTRA_STATE);
|
|
|
+ if (hm.get("State") != null && !state.equals(hm.get("State"))) {
|
|
|
+ failureReason = "States don't match";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String number = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
|
|
|
+ if (number == null) {
|
|
|
+ failureReason = "Number is null";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String requestedNumber = (String)hm.get("Number");
|
|
|
+
|
|
|
+ if (requestedNumber != null && !requestedNumber.equals(number)) {
|
|
|
+ failureReason = "Numbers don't match";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String requestedName = (String)hm.get("Name");
|
|
|
+ String name = Helpers.getContactName(ctx, number);
|
|
|
+
|
|
|
+ if (requestedName == null)
|
|
|
+ return true;
|
|
|
+
|
|
|
+ if (!requestedName.equals(name)){
|
|
|
+ failureReason = "Names don't match";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public String getFailureReason() {
|
|
|
+ return failureReason;
|
|
|
+ }
|
|
|
+}
|