-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOOBProcessor.java
More file actions
107 lines (95 loc) · 4.55 KB
/
Copy pathOOBProcessor.java
File metadata and controls
107 lines (95 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.example.oobexampleapp;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract;
import android.provider.MediaStore;
import android.telephony.SmsManager;
import android.util.Log;
public class OOBProcessor {
MainActivity activity;
Exception exception = null;
static final int REQUEST_IMAGE_CAPTURE = 1;
public OOBProcessor(MainActivity activity) {
this.activity = activity;
}
// remove the oob tags and send it along to the processor
public void removeOobTags(String output) throws Exception {
if (output != null) {
Pattern pattern = Pattern.compile("<oob>(.*)</oob>");
Matcher matcher = pattern.matcher(output);
if (matcher.find()) {
String oobContent = matcher.group(1);
processInnerOobTags(oobContent);
activity.showBotResponse(matcher.replaceAll(""));
}
}
}
// check inner oob command and take appropriate action
public void processInnerOobTags(String oobContent) throws Exception {
if (oobContent.contains("<camera>")) {
oobCamera();
}
if (oobContent.contains("<calendar>")) {
oobCalendar(oobContent);
}
}
// open the camera
public void oobCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
// make the calendar event
@SuppressLint("NewApi")
public void oobCalendar(String oobContent) {
//match and extract the content in the various fields within the calendar event
Pattern bHTimePattern = Pattern.compile("<starthour>(.*)</starthour>");
Matcher bHTimeMatcher = bHTimePattern.matcher(oobContent);
Pattern bMTimePattern = Pattern.compile("<startminute>(.*)</startminute>");
Matcher bMTimeMatcher = bMTimePattern.matcher(oobContent);
Pattern eHTimePattern = Pattern.compile("<endhour>(.*)</endhour>");
Matcher eHTimeMatcher = eHTimePattern.matcher(oobContent);
Pattern eMTimePattern = Pattern.compile("<endminutes>(.*)</endminutes>");
Matcher eMTimeMatcher = eMTimePattern.matcher(oobContent);
Pattern dayPattern = Pattern.compile("<day>(.*)</day>");
Matcher dayMatcher = dayPattern.matcher(oobContent);
Pattern yearPattern = Pattern.compile("<year>(.*)</year>");
Matcher yearMatcher = yearPattern.matcher(oobContent);
Pattern monthPattern = Pattern.compile("<month>(.*)</month>");
Matcher monthMatcher = monthPattern.matcher(oobContent);
Pattern titlePattern = Pattern.compile("<title>(.*)</title>");
Matcher titleMatcher = titlePattern.matcher(oobContent);
Pattern locPattern = Pattern.compile("<location>(.*)</location>");
Matcher locMatcher = locPattern.matcher(oobContent);
if (bHTimeMatcher.find() && bMTimeMatcher.find() && eHTimeMatcher.find() && eMTimeMatcher.find() && dayMatcher.find() && yearMatcher.find() && monthMatcher.find() && titleMatcher.find() && locMatcher.find()) {
Intent calendarIntent = new Intent(Intent.ACTION_EDIT);
calendarIntent.setType("vnd.android.cursor.item/event");
try {
// create the calendar intent with the data as specified from the conversation
int year = Integer.parseInt(yearMatcher.group(1).toString());
int day = Integer.parseInt(dayMatcher.group(1).toString());
int month = Integer.parseInt(monthMatcher.group(1).toString());
int hourOfDay = Integer.parseInt(bHTimeMatcher.group(1).toString());
int minute = Integer.parseInt(bMTimeMatcher.group(1).toString());
Calendar beginTime = Calendar.getInstance();
beginTime.set(year, month, day, hourOfDay, minute);
hourOfDay = Integer.parseInt(eHTimeMatcher.group(1).toString());
minute = Integer.parseInt(eMTimeMatcher.group(1).toString());
Calendar endTime = Calendar.getInstance();
endTime.set(year, month, day, hourOfDay, minute);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(Events.TITLE, titleMatcher.group(1).toString());
calendarIntent.putExtra(Events.EVENT_LOCATION, locMatcher.group(1).toString());
activity.startActivity(calendarIntent);
} catch (Exception ex) {
activity.processBotResponse("There was an error in scheduling your event.");
}
} else activity.showBotResponse("There was an issue with one of the details you specificied. Please try and schedule your event again.");
}
}