use of org.havenapp.main.model.Event in project haven by guardianproject.
the class MonitorService method alert.
/**
* Sends an alert according to type of connectivity
*/
public synchronized void alert(int alertType, String path) {
Date now = new Date();
boolean doNotification = false;
if (mLastEvent == null) {
mLastEvent = new Event();
mLastEvent.save();
doNotification = true;
} else if (mPrefs.getNotificationTimeMs() == 0) {
doNotification = true;
} else if (mPrefs.getNotificationTimeMs() > 0 && mLastNotification != null) {
// check if time window is within configured notification time window
doNotification = ((now.getTime() - mLastNotification.getTime()) > mPrefs.getNotificationTimeMs());
} else {
doNotification = true;
}
EventTrigger eventTrigger = new EventTrigger();
eventTrigger.setType(alertType);
eventTrigger.setPath(path);
mLastEvent.addEventTrigger(eventTrigger);
// we don't need to resave the event, only the trigger
eventTrigger.save();
if (doNotification) {
mLastNotification = new Date();
/*
* If SMS mode is on we send an SMS or Signal alert to the specified
* number
*/
StringBuilder alertMessage = new StringBuilder();
alertMessage.append(getString(R.string.intrusion_detected, eventTrigger.getStringType(this)));
if (mPrefs.getSignalUsername() != null) {
// since this is a secure channel, we can add the Onion address
if (mPrefs.getRemoteAccessActive() && (!TextUtils.isEmpty(mPrefs.getRemoteAccessOnion()))) {
alertMessage.append(" http://").append(mPrefs.getRemoteAccessOnion()).append(':').append(WebServer.LOCAL_PORT);
}
SignalSender sender = SignalSender.getInstance(this, mPrefs.getSignalUsername());
ArrayList<String> recips = new ArrayList<>();
StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(), ",");
while (st.hasMoreTokens()) recips.add(st.nextToken());
String attachment = null;
if (eventTrigger.getType() == EventTrigger.CAMERA) {
attachment = eventTrigger.getPath();
} else if (eventTrigger.getType() == EventTrigger.MICROPHONE) {
attachment = eventTrigger.getPath();
} else if (eventTrigger.getType() == EventTrigger.CAMERA_VIDEO) {
attachment = eventTrigger.getPath();
}
sender.sendMessage(recips, alertMessage.toString(), attachment);
} else if (mPrefs.getSmsActivation()) {
SmsManager manager = SmsManager.getDefault();
StringTokenizer st = new StringTokenizer(mPrefs.getSmsNumber(), ",");
while (st.hasMoreTokens()) manager.sendTextMessage(st.nextToken(), null, alertMessage.toString(), null, null);
}
}
}
use of org.havenapp.main.model.Event in project haven by guardianproject.
the class WebServer method serve.
@Override
public Response serve(IHTTPSession session) {
StringBuffer page = new StringBuffer();
Cookie cookie = null;
if (mPassword != null) {
// We have to use session.parseBody() to obtain POST data.
// See https://github.com/NanoHttpd/nanohttpd/issues/427
Map<String, String> content = new HashMap<>();
Method method = session.getMethod();
if (Method.PUT.equals(method) || Method.POST.equals(method))
try {
session.parseBody(content);
} catch (IOException ioe) {
Log.e(TAG, "unable to parse body of request", ioe);
} catch (ResponseException re) {
Log.e(TAG, "unable to parse body of request", re);
}
String inPassword = session.getParms().get("p");
String inSid = session.getCookies().read("sid");
if (inPassword != null && safeEquals(inPassword, mPassword)) {
mSession = UUID.randomUUID().toString();
cookie = new OnionCookie("sid", mSession, 100000);
session.getCookies().set(cookie);
} else if (inSid == null || mSession == null || (inSid != null && (!safeEquals(inSid, mSession)))) {
showLogin(page);
return newFixedLengthResponse(page.toString());
}
}
Uri uri = Uri.parse(session.getUri());
List<String> pathSegs = uri.getPathSegments();
if (pathSegs.size() == 4 && pathSegs.get(2).equals("trigger")) {
// long eventId = Long.parseLong(pathSegs.get(1));
long eventTriggerId = Long.parseLong(pathSegs.get(3));
EventTrigger eventTrigger = EventTrigger.findById(EventTrigger.class, eventTriggerId);
try {
File fileMedia = new File(eventTrigger.getPath());
FileInputStream fis = new FileInputStream(fileMedia);
return newChunkedResponse(Response.Status.OK, getMimeType(eventTrigger), fis);
} catch (IOException ioe) {
Log.e(TAG, "unable to return media file", ioe);
}
} else if (uri.getPath().startsWith("/feed")) {
// do RSS feed
} else {
page.append("<html><head><title>").append(appTitle).append("</title>");
page.append("<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\" />");
page.append("<meta name = \"viewport\" content = \"user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=device-width\">");
page.append("</head><body>");
if (TextUtils.isEmpty(uri.getPath()) || uri.getPath().equals("/"))
showEvents(page);
else {
try {
if (pathSegs.size() == 2 && pathSegs.get(0).equals("event")) {
long eventId = Long.parseLong(pathSegs.get(1));
Event event = Event.findById(Event.class, eventId);
showEvent(event, page);
}
} catch (Exception e) {
Log.e(TAG, "Something went wrong with parsing the path", e);
}
}
page.append("</body></html>\n");
Response response = newFixedLengthResponse(page.toString());
session.getCookies().unloadQueue(response);
return response;
}
Response response = newFixedLengthResponse(Response.Status.INTERNAL_ERROR, "text/plain", "Error");
session.getCookies().unloadQueue(response);
return response;
}
use of org.havenapp.main.model.Event in project haven by guardianproject.
the class EventAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(EventVH holder, int position) {
Event event = events.get(position);
String title = event.getStartTime().toLocaleString();
String desc = event.getEventTriggers().size() + " " + context.getString(R.string.detection_events);
holder.title.setText(title);
holder.note.setText(desc);
}
use of org.havenapp.main.model.Event in project haven by guardianproject.
the class ListActivity method onResume.
@Override
protected void onResume() {
super.onResume();
final long newCount = Event.count(Event.class);
if (newCount > events.size()) {
events = Event.listAll(Event.class, "id DESC");
adapter = new EventAdapter(ListActivity.this, events);
recyclerView.setAdapter(adapter);
adapter.SetOnItemClickListener(new EventAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent i = new Intent(ListActivity.this, EventActivity.class);
i.putExtra("eventid", events.get(position).getId());
modifyPos = position;
startActivity(i);
}
});
/**
* // Just load the last added note (new)
* Event event = Event.last(Event.class);
*
* events.add(0,event);
* adapter.notifyItemInserted(0);
* adapter.notifyDataSetChanged();
*
* initialCount = newCount;
*/
recyclerView.setVisibility(View.VISIBLE);
findViewById(R.id.empty_view).setVisibility(View.GONE);
} else if (newCount == 0) {
recyclerView.setVisibility(View.GONE);
findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
}
if (modifyPos != -1) {
// Event.set(modifyPos, Event.listAll(Event.class).get(modifyPos));
adapter.notifyItemChanged(modifyPos);
}
}
use of org.havenapp.main.model.Event in project haven by guardianproject.
the class ListActivity method removeAllEvents.
private void removeAllEvents() {
final List<Event> removedEvents = new ArrayList<Event>();
final Runnable runnableDelete = new Runnable() {
public void run() {
for (Event event : removedEvents) {
event.delete();
}
}
};
for (int i = 0, size = events.size(); i < size; i++) {
removedEvents.add(events.remove(0));
adapter.notifyItemRemoved(0);
}
handler.postDelayed(runnableDelete, 3000);
Snackbar.make(recyclerView, getString(R.string.events_deleted), Snackbar.LENGTH_SHORT).setAction(getString(R.string.undo), new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.removeCallbacks(runnableDelete);
for (Event event : removedEvents) {
event.save();
events.add(event);
adapter.notifyItemInserted(events.size() - 1);
}
}
}).show();
}
Aggregations