Skip to content

Commit

Permalink
Revert "Remove external components"
Browse files Browse the repository at this point in the history
This reverts commit f7fc3c8.
  • Loading branch information
VicDominguez committed May 16, 2024
1 parent b3e79d2 commit 098b46f
Show file tree
Hide file tree
Showing 19 changed files with 797 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.health.READ_SLEEP"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_STEPS"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ suspend fun firstTimeExecution(
workAdministrator.scheduleDailyNightNotificationWorker()
workAdministrator.scheduleOneOffNotificationWorker()
workAdministrator.scheduleUploadWorker()
workAdministrator.scheduleUploadPhoneDataWorker()
workAdministrator.scheduleUploadTrafficDataWorker()


//insert values in last upload table
val now = obtainTimestamp(Instant.now(), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ private fun generateRawUUID(): String = UUID.randomUUID().toString()
* Generates a User ID using randomUUID and sha256 hash
* @return User ID in hexadecimal format
*/
fun generateUID(): String = sha512hash(generateRawUUID())
fun generateUID(): String = sha512hash(generateRawUUID())

/**
* Encrypt private user data
* @return User ID in String format
*/
fun securePrivateData(message: String): String = sha512hash(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package es.upm.bienestaremocional.data.phonecalls

import android.Manifest.permission.READ_CALL_LOG
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.database.Cursor
import android.provider.CallLog.Calls.*
import androidx.core.content.ContextCompat
import es.upm.bienestaremocional.data.crypto.securePrivateData

class PhoneInfo {

fun getCallLogs(context: Context): String {
//check permissions
if (checkPermissions(context)) {
val c = context.applicationContext
val projection = arrayOf(CACHED_NAME, NUMBER, DATE, DURATION)

val cursor = c.contentResolver.query(
CONTENT_URI,
projection,
null,
null,
null,
null
)
return cursorToList(cursor)
}
return "N/A"
}

private fun cursorToList(cursor: Cursor?): String {
var message = "{"
cursor?.use {
while (it.moveToNext()) {
var json: String = " \"Call\": {\"Name\": \"" + securePrivateData(
it.getStringFromColumn(CACHED_NAME)
) +
"\", \"Number\": \"" + securePrivateData(it.getStringFromColumn(NUMBER)) +
"\", \"Date\": \"" + it.getStringFromColumn(DATE) +
"\", \"Duration\": \"" + it.getStringFromColumn(DURATION) +
"\"}"

if (it.moveToNext())
json += ","

message += json
}
message += "}"
}
return message
}

@SuppressLint("Range")
private fun Cursor.getStringFromColumn(columnName: String) =
getString(getColumnIndex(columnName))

private fun checkPermissions(context: Context): Boolean {
val permission = ContextCompat.checkSelfPermission(
context,
READ_CALL_LOG
)

return permission == PackageManager.PERMISSION_GRANTED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ interface RemoteAPI {
@POST("/one_off_questionnaires")
suspend fun postOneOffQuestionnaires(@Body data: OneOffQuestionnairesRequest):
Response<OneOffQuestionnairesResponse.Timestamps>

@POST("/bg_data")
suspend fun postBackgroundData(@Body message: String): Response<Unit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package es.upm.bienestaremocional.data.trafficstats

import android.annotation.SuppressLint
import android.net.TrafficStats

class Traffic {

fun init(): String {
if (TrafficStats.getTotalRxBytes() != TrafficStats.UNSUPPORTED.toLong() &&
TrafficStats.getTotalTxBytes() != TrafficStats.UNSUPPORTED.toLong()) {
return run()
}
return "{\"WiFI\": \"N/A Kb\", \"Mobile\": \"N/A Kb\"}"
}

@SuppressLint("SetTextI18n")
fun run(): String {
val mobile = TrafficStats.getMobileRxBytes() + TrafficStats.getMobileTxBytes()
val total = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes()
val wiFi: Long = (total - mobile) / 1024
val mobileData: Long = mobile / 1024

return "{\"WiFI\": \"$wiFi Kb\", \"Mobile\": \"$mobileData Kb\"}"
}

}
206 changes: 206 additions & 0 deletions app/src/main/java/es/upm/bienestaremocional/data/usage/Usage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package es.upm.bienestaremocional.data.usage

import android.app.usage.UsageStats
import android.app.usage.UsageStatsManager
import android.content.Context
import android.content.Context.USAGE_STATS_SERVICE
import android.os.Build
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.RequiresApi
import java.util.*

class Usage(
private val logTag: String
) {
private var mUsageStatsManager: UsageStatsManager? = null
private var mAdapter: Usage.UsageStatsAdapter? = null

var usageInfo: String = ""

@RequiresApi(Build.VERSION_CODES.Q)
internal inner class UsageStatsAdapter : BaseAdapter() {
private val mPackageStats = ArrayList<UsageStats>()

override fun getCount(): Int {
return mPackageStats.size
}

override fun getItem(position: Int): Any {
return mPackageStats[position]
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getView(position: Int, convertView: View, parent: ViewGroup): View {

return convertView
}

init {
val cal = Calendar.getInstance()
cal.add(Calendar.HOUR, -6)
val stats = mUsageStatsManager!!.queryUsageStats(
UsageStatsManager.INTERVAL_BEST,
cal.timeInMillis, System.currentTimeMillis()
)
if (stats != null) {
val statCount = stats.size

for (i in 0 until statCount) {
val pkgStats = stats[i]
val type = findApp(pkgStats.packageName)
var message: String
if ((type != "") && (pkgStats.totalTimeVisible > 0)) {
if (usageInfo != "")
usageInfo += ", "
message = "\"Apps\": { \"AppName\": \"" + pkgStats.packageName +
"\", \"firstTimeStamp\": " + pkgStats.firstTimeStamp +
", \"lastTimeStamp\": " + pkgStats.lastTimeStamp +
", \"lastTimeUsed\": " + pkgStats.lastTimeUsed +
", \"lastTimeVisible\": " + pkgStats.lastTimeVisible +
", \"totalTimeVisible\": " + pkgStats.totalTimeVisible +
", \"AppType\": \"" + type + "\"}"

usageInfo += message
}
}
}
else
{
Log.d(logTag, "Usage data not available")
}
}
}

@RequiresApi(Build.VERSION_CODES.Q)
fun getAppUsage(context: Context): String {
mUsageStatsManager = context.getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager

mAdapter = UsageStatsAdapter()

if (usageInfo != "")
return usageInfo

return "\"Apps\": \"N/A\""
}

fun findApp(appName: String): String {
val type = "N/A"
val rRSS = listOf(
"facebook",
"twitter",
"instagram",
"tiktok",
"snapchat",
"whatsapp",
"messenger",
"telegram"
)
val dating = listOf("tinder", "badoo", "meetic", "bumble", "grindr")
val games = listOf(
"candy",
"mine",
"treasure",
"crush",
"sudoku",
"game",
"pokemongo",
"impact",
"scape",
"among",
"otome",
"madness",
"zombies"
)
val entertaining = listOf(
"youtube",
"netflix",
"hbo",
"disney",
"prime",
"video",
"ivoox",
"tiktok",
"audible",
"book",
"crunchyroll",
"firefox",
"opera",
"chrome",
"9gag",
"los40",
"spotify",
"rtve",
"bbc",
"duolingo"
)
val house = listOf(
"santander",
"bbva",
"bankinter",
"openbank",
"repsol",
"naturgy",
"iberdrola",
"tapo",
"tplink",
"aeat",
"amazon",
"cl@ve",
"sodexo",
"zooplus",
"wallapop"
)
val work = listOf(
"office",
"word",
"excel",
"powerpoint",
"authenticator",
"teams",
"slack",
"zoom",
"moodle"
)

val rRSSCount = rRSS.size
for (i in 0 until rRSSCount) {
if (appName.contains(rRSS[i]))
return "RRSS"
}
val gamesCount = games.size
for (i in 0 until gamesCount) {
if (appName.contains(games[i]))
return "games"
}
val datingCount = dating.size
for (i in 0 until datingCount) {
if (appName.contains(dating[i]))
return "dating"
}
val houseCount = house.size
for (i in 0 until houseCount) {
if (appName.contains(house[i]))
return "house"
}
val workCount = work.size
for (i in 0 until workCount) {
if (appName.contains(work[i]))
return "work"
}
val entertainingCount = entertaining.size
for (i in 0 until entertainingCount) {
if (appName.contains(entertaining[i]))
return "entertaining"
}

return type
}


}
Loading

0 comments on commit 098b46f

Please sign in to comment.