Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 265bf4e4 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

Fix tracker stats, Add e trackers list to demo app.

parent 177c229a
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ dependencies {
    implementation project(':privacymoduletrackers')
    implementation 'foundation.e:privacymodule.api:0.5.0-dev'

    implementation 'com.google.code.gson:gson:2.9.0'

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
+1 −0

File added.

Preview size limit exceeded, changes collapsed.

+84 −10
Original line number Diff line number Diff line
@@ -2,30 +2,104 @@ package foudation.e.privacymodules.trackers.demoapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import foundation.e.privacymodules.trackers.StatsDatabase
import foundation.e.privacymodules.trackers.Tracker
import foundation.e.privacymodules.trackers.api.TrackTrackersPrivacyModule
import java.io.InputStreamReader
import java.lang.Exception

class MainActivity : AppCompatActivity() {

    private var mBtnStart: Button? = null

    private var mTvLog: TextView? = null
    private var trackers = emptyList<Tracker>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mBtnStart = findViewById(R.id.button_start)
        initFromAssets()

        mBtnStart = findViewById(R.id.button_start)
        mTvLog = findViewById(R.id.tv_log)
        mBtnStart?.setOnClickListener { v: View? ->
            TrackTrackersPrivacyModule(applicationContext).start(listOf(
                Tracker(
                    id = "1",
                    hostnames = setOf("lemonde.fr"),
                    label = "Test 1",
                    description = null,
                    website = null)
            ), true);
            TrackTrackersPrivacyModule(applicationContext).start(trackers,
//            listOf(
//                Tracker(
//                    id = "1",
//                    hostnames = setOf("lemonde.fr"),
//                    label = "Test 1",
//                    description = null,
//                    website = null)
//            ),
            true);
        }




    }

    override fun onResume() {
        super.onResume()

        mTvLog?.text = "Trackers: " + StatsDatabase.getInstance(this).allTrackers.map {
            it.label
        }.joinToString("\n")
    }



    private fun initFromAssets() {
        try {
            val reader = InputStreamReader(getAssets().open("e_trackers.json"), "UTF-8")

            val trackerResponse =
                Gson().fromJson(reader, ETrackersResponse::class.java)

            trackers = mapper(trackerResponse)
        } catch(e: Exception) {
            Log.e("TrackersRepository", "While parsing trackers in assets", e)
        }
    }
    private fun mapper(response: ETrackersResponse): List<Tracker> {
        return response.trackers.mapNotNull {
            try {
                it.toTracker()
            } catch (e: Exception) {
                null
            }
        }
    }

    fun ETrackersResponse.ETracker.toTracker(): Tracker {
        return Tracker(
            id = id!!,
            hostnames = hostnames!!.toSet(),
            label = name!!,
            description = description,
            website = website,
        )
    }

    data class ETrackersResponse(val trackers: List<ETracker>) {
        data class ETracker(
            val id: String?,
            val hostnames: List<String>?,
            val name: String?,

            val description: String?,
            @SerializedName("creation_date") val creationDate: String?,
            @SerializedName("code_signature") val codeSignature: String?,
            @SerializedName("network_signature") val networkSignature: String?,
            val website: String?,
            val categories: List<String>?,
        )
    }

}
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
@@ -19,5 +19,10 @@
            android:layout_marginEnd="24dp"
            android:text="Start"
            />
        <TextView
            android:id="@+id/tv_log"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
</androidx.core.widget.NestedScrollView>
+3 −3
Original line number Diff line number Diff line
@@ -275,7 +275,7 @@ public class StatsDatabase extends SQLiteOpenHelper {
                    AppTrackerEntry.COLUMN_NAME_YEAR + " = ? AND " + AppTrackerEntry.COLUMN_NAME_APP_UID + " = ? AND " +
                    AppTrackerEntry.COLUMN_NAME_TRACKER + " = ? ";

            String[] selectionArg = new String[]{"" + hour, "" + day, "" + month, "" + year, "" + appUid, "" + trackerId};
            String[] selectionArg = new String[]{"" + hour, "" + day, "" + month, "" + year, "" + appUid, trackerId};

            Cursor cursor = db.query(
                    AppTrackerEntry.TABLE_NAME,
@@ -384,8 +384,8 @@ public class StatsDatabase extends SQLiteOpenHelper {
            );
            List<Tracker> trackers = new ArrayList<>();
            while (cursor.moveToNext()) {
                int trackerId = cursor.getInt(cursor.getColumnIndex(AppTrackerEntry.COLUMN_NAME_TRACKER));
                Tracker tracker = trackersRepository.getTracker("" + trackerId);
                String trackerId = cursor.getString(cursor.getColumnIndex(AppTrackerEntry.COLUMN_NAME_TRACKER));
                Tracker tracker = trackersRepository.getTracker(trackerId);
                if (tracker != null)
                    trackers.add(tracker);
            }
Loading