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

Commit 868bfe3b authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

Update whitelist handling. Remove build app list

parent 5513e6a6
Loading
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -10,6 +10,5 @@ Generate aar
./gradlew --console=verbose publishToMavenLocal
```

### To push release on gitlab

    ./gradlew --console=verbose publish
### Release on gitlab
 Push to main, and let the CI add the build to maven.
+34 −3
Original line number Diff line number Diff line
@@ -10,12 +10,14 @@ import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import foundation.e.privacymodules.trackers.ITrackTrackersPrivacyModule
import foundation.e.privacymodules.trackers.Tracker
import foundation.e.privacymodules.trackers.api.BlockTrackersPrivacyModule
import foundation.e.privacymodules.trackers.api.TrackTrackersPrivacyModule
import java.io.InputStreamReader
import java.lang.Exception

class MainActivity : AppCompatActivity() {

    private val appUid = 10141
    private var mBtnStart: Button? = null
    private var mBtnYearsLeaks: Button? = null
    private var mBtnMonthsLeaks: Button? = null
@@ -25,6 +27,10 @@ class MainActivity : AppCompatActivity() {
    private var mBtnDaysTrackers: Button? = null
    private var mBtnAllTrackers: Button? = null
    private var mBtnTrackersAppUid: Button? = null
    private var mBtnWhitelistApp: Button? = null
    private var mBtnWhitelistTracker: Button? = null
    private var mBtnShowWhitelist: Button? = null

    private var mTvLog: TextView? = null
    private var trackers = emptyList<Tracker>()
    override fun onCreate(savedInstanceState: Bundle?) {
@@ -42,11 +48,13 @@ class MainActivity : AppCompatActivity() {
        mBtnDaysTrackers = findViewById(R.id.button_days_trackers)
        mBtnAllTrackers = findViewById(R.id.button_all_trackers)
        mBtnTrackersAppUid = findViewById(R.id.button_trackers_appuid)


        mBtnWhitelistApp = findViewById(R.id.button_whitelist_app)
        mBtnWhitelistTracker = findViewById(R.id.button_whitelist_tracker)
        mBtnShowWhitelist = findViewById(R.id.button_show_whitelist)
        mTvLog = findViewById(R.id.tv_log)

        val trackTrackersPrivacyModule = TrackTrackersPrivacyModule.getInstance(applicationContext)
        val blockTrackersPrivacyModule = BlockTrackersPrivacyModule.getInstance(applicationContext)

        mBtnStart?.setOnClickListener { v: View? ->
            trackTrackersPrivacyModule.start(trackers,
@@ -90,8 +98,31 @@ class MainActivity : AppCompatActivity() {
        }

        mBtnTrackersAppUid?.setOnClickListener { v: View? ->
            mTvLog?.append("Trackers of appUid: ${trackTrackersPrivacyModule.getTrackersForApp(10144).map {it.label}.joinToString("; ")}")
            mTvLog?.append("Trackers of appUid: ${trackTrackersPrivacyModule.getTrackersForApp(appUid).map {it.label}.joinToString("; ")}")
        }

        mBtnWhitelistApp?.setOnClickListener {
            blockTrackersPrivacyModule.enableBlocking()
            blockTrackersPrivacyModule.setWhiteListed(appUid, !blockTrackersPrivacyModule.isWhitelisted(appUid))
        }

        mBtnWhitelistTracker?.setOnClickListener {
            blockTrackersPrivacyModule.enableBlocking()

            trackers.find { it.label == "OutBrain" }?.let {
                val isWhitelisted = blockTrackersPrivacyModule.getWhiteList(appUid).contains(it)
                blockTrackersPrivacyModule.setWhiteListed(it, appUid, !isWhitelisted)

            mTvLog?.append("Whitelisted ${!isWhitelisted}: ${it.id}")
        }
        }

        mBtnShowWhitelist?.setOnClickListener {
            mTvLog?.append("Whitelisted app: ${blockTrackersPrivacyModule.getWhiteListedApp().joinToString(" ; ")}")
            mTvLog?.append("Whitelisted trackers for $appUid: ${blockTrackersPrivacyModule.getWhiteList(appUid).map { it?.label }.joinToString(" ; ")}")
        }


        trackTrackersPrivacyModule.addListener(object: ITrackTrackersPrivacyModule.Listener {
            override fun onNewData() {

+22 −1
Original line number Diff line number Diff line
@@ -78,7 +78,28 @@
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="24dp"
                android:text="Trackers of 10144"
                android:text="Trackers of uid"
                />
            <Button
                android:id="@+id/button_show_whitelist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="24dp"
                android:text="Show whitelist"
                />
            <Button
                android:id="@+id/button_whitelist_app"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="24dp"
                android:text="Whitelist uid"
                />
            <Button
                android:id="@+id/button_whitelist_tracker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="24dp"
                android:text="Whitelist tracker"
                />
        </GridLayout>
        <TextView
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ android {
        targetSdkVersion 30

        versionCode 1
        versionName "0.2.0"
        versionName "0.3.0"
    }

    buildTypes {
+0 −263
Original line number Diff line number Diff line
/*
 Copyright (C) 2021 ECORP

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 */


package foundation.e.privacymodules.trackers;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

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

import foundation.e.privacymodules.trackers.data.TrackersRepository;

public class AppTrackerWhitelist extends SQLiteOpenHelper{
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "AppTrackerWhitelist.db";
    private static AppTrackerWhitelist sAppTrackerWhitelist;
    private final Context mContext;
    private Object mLock = new Object();
    private List<ApplicationInfo> mIgnoredApps;
    private ArrayList<String> mLauncherAppsList;
    private TrackersRepository trackersRepository;

    public AppTrackerWhitelist(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;
        trackersRepository = TrackersRepository.getInstance();
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(APP_TRACKER_SQL_CREATE_TABLE);
        db.execSQL(APP_SQL_CREATE_TABLE);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onCreate(db);
    }





    public List<Tracker> getWhiteList(int app_uid) {
        synchronized (mLock) {
            List<Tracker> trackers = new ArrayList<>();
            SQLiteDatabase db = getWritableDatabase();
            Cursor cursor = db.query(
                    AppTrackerEntry.APP_TRACKER_WHITELIST_TABLE_NAME,
                    app_tracker_projection,
                    AppTrackerEntry.COLUMN_NAME_APP_UID + " = ?",
                    new String[]{"" + app_uid},
                    null,
                    null,
                    null
            );
            while (cursor.moveToNext()) {
                Tracker tracker = trackersRepository.getTracker(
                        cursor.getString(cursor.getColumnIndex(AppTrackerEntry.COLUMN_NAME_TRACKER))
                );
                if (tracker != null)
                    trackers.add(tracker);

            }
            return trackers;
        }
    }



    public void setWhiteListed(Tracker tracker, int app_uid, boolean addToWhitelist) {
        synchronized (mLock) {
            SQLiteDatabase db = getWritableDatabase();
            if (addToWhitelist) {
                ContentValues values = new ContentValues();
                values.put(AppTrackerEntry.COLUMN_NAME_APP_UID, app_uid);
                values.put(AppTrackerEntry.COLUMN_NAME_TRACKER, tracker.getId());

                db.insert(AppTrackerEntry.APP_TRACKER_WHITELIST_TABLE_NAME, null, values);

            } else {
                db.delete(AppTrackerEntry.APP_TRACKER_WHITELIST_TABLE_NAME, AppTrackerEntry.COLUMN_NAME_APP_UID + " = ? AND " + AppTrackerEntry.COLUMN_NAME_TRACKER + " = ?", new String[]{app_uid + "", tracker.getId()});
            }
        }
    }

    public void setWhiteListed(int app_uid, boolean addToWhitelist) {
        synchronized (mLock) {
            SQLiteDatabase db = getWritableDatabase();
            if (addToWhitelist) {
                ContentValues values = new ContentValues();
                values.put(AppTrackerEntry.COLUMN_NAME_APP_UID, app_uid);
                db.insert(AppTrackerEntry.APP_WHITELIST_TABLE_NAME, null, values);
            } else {
                ContentValues values = new ContentValues();
                values.put(AppTrackerEntry.COLUMN_NAME_APP_UID, app_uid);
                db.delete(AppTrackerEntry.APP_WHITELIST_TABLE_NAME, AppTrackerEntry.COLUMN_NAME_APP_UID + " = ?", new String[]{app_uid + ""});
            }
        }
    }

    public boolean isAppWhitelisted(int appUid) {
        synchronized (mLock) {
            SQLiteDatabase db = getWritableDatabase();
            Cursor cursor = db.query(
                    AppTrackerEntry.APP_WHITELIST_TABLE_NAME,
                    app_projection,
                    AppTrackerEntry.COLUMN_NAME_APP_UID + " = ?",
                    new String[]{"" + appUid},
                    null,
                    null,
                    null
            );

            return cursor.getCount() > 0;
        }
    }

    public List<Integer> getWhiteListedApps() {
        synchronized (mLock) {
            List<Integer> apps = new ArrayList<>();
            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.query(
                    AppTrackerEntry.APP_WHITELIST_TABLE_NAME,
                    app_projection,
                    null,
                    null,
                    null,
                    null,
                    null
            );
            while (cursor.moveToNext()) {
                apps.add(cursor.getInt(cursor
                        .getColumnIndex(AppTrackerEntry.COLUMN_NAME_APP_UID)));

            }
            return apps;
        }
    }


    public static class AppTrackerEntry implements BaseColumns {
        public static final String APP_TRACKER_WHITELIST_TABLE_NAME = "app_tracker_whitelist";
        public static final String APP_WHITELIST_TABLE_NAME = "app_whitelist";
        public static final String COLUMN_NAME_APP_UID = "app_uid";
        public static final String COLUMN_NAME_TRACKER = "tracker";
    }

    String[] app_tracker_projection = {
            AppTrackerEntry.COLUMN_NAME_APP_UID,
            AppTrackerEntry.COLUMN_NAME_TRACKER
    };
    String[] app_projection = {
            AppTrackerEntry.COLUMN_NAME_APP_UID
    };

    private static final String APP_TRACKER_SQL_CREATE_TABLE =
            "CREATE TABLE " + AppTrackerEntry.APP_TRACKER_WHITELIST_TABLE_NAME + " (" +
                    AppTrackerEntry._ID + " INTEGER PRIMARY KEY," +
                    AppTrackerEntry.COLUMN_NAME_APP_UID + " INTEGER," +
                    AppTrackerEntry.COLUMN_NAME_TRACKER + " TEXT)";

    private static final String APP_SQL_CREATE_TABLE =
            "CREATE TABLE " + AppTrackerEntry.APP_WHITELIST_TABLE_NAME + " (" +
                    AppTrackerEntry.COLUMN_NAME_APP_UID + " INTEGER UNIQUE)";



    public boolean isTrackerWhitelistedForApp(String trackerId, int uid){
        synchronized (mLock) {
            SQLiteDatabase db = getWritableDatabase();
            Cursor cursor = db.query(
                    AppTrackerEntry.APP_TRACKER_WHITELIST_TABLE_NAME,
                    app_tracker_projection,
                    AppTrackerEntry.COLUMN_NAME_TRACKER + " = ? AND " + AppTrackerEntry.COLUMN_NAME_APP_UID + " = ?",
                    new String[]{"" + trackerId, "" + uid},
                    null,
                    null,
                    null
            );

            return cursor.getCount() > 0;
        }
    }

    private boolean isWhitelistable(ApplicationInfo app){

        /*if (AppUtils.isInstant(app)) {
            return false;
        } else*/ if (hasFlag(app.flags, ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) {
            return true;
        } else if (!hasFlag(app.flags, ApplicationInfo.FLAG_SYSTEM)) {
            return true;
        } else if (mLauncherAppsList.contains(app.packageName)) {
            return true;
        } else if (false/* hasFlag(app.flags, ApplicationInfo.FLAG_SYSTEM)&& app.isHomeApp*/) {
            return true;
        }
        return false;
    }

    private static boolean hasFlag(int flags, int flag) {
        return (flags & flag) != 0;
    }



    public List<ApplicationInfo> getWhitelistableApps(boolean ignoredApps){
        if(ignoredApps && mIgnoredApps != null)
            return mIgnoredApps;
        List<ApplicationInfo> returnApps = new ArrayList<>();
        PackageManager pm = mContext.getPackageManager();
        mLauncherAppsList = new ArrayList<String>();
        Intent i = new Intent(Intent.ACTION_MAIN, null);
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> allApps = pm.queryIntentActivities(i, 0);
        for(ResolveInfo ri:allApps) {
                mLauncherAppsList.add(ri.activityInfo.packageName);
        }
        List<ApplicationInfo> apps = mContext.getPackageManager().getInstalledApplications(0);
        for (ApplicationInfo app : apps) {
            if(isWhitelistable(app) && ! ignoredApps
                    || ignoredApps && !isWhitelistable(app)) {
                returnApps.add(app);
            }
        }
        if(ignoredApps)
            mIgnoredApps = returnApps;
        return returnApps;
    }

    public static AppTrackerWhitelist getInstance(Context ct){
        if(sAppTrackerWhitelist == null){
            sAppTrackerWhitelist = new AppTrackerWhitelist(ct);
        }
        return sAppTrackerWhitelist;
    }


}
Loading