// Path: app/src/main/java/org/ethack/orwall/database/natDBHelper.java
// public class natDBHelper extends SQLiteOpenHelper {
// 
//     public static final String NAT_TABLE_NAME = "rules";
//     public static final String COLUMN_APPUID = "appUID";
//     public static final String COLUMN_APPNAME = "appName";
//     public static final String COLUMN_ONIONTYPE = "onionType";
//     public static final String COLUMN_LOCALHOST = "localhost";
//     public static final String COLUMN_LOCALNETWORK = "localnetwork";
// 
// /*
//     @Deprecated
//     private static final String COLUMN_ONIONPORT = "onionPort";
//     @Deprecated
//     private static final String DB_PORT_TYPE_FENCED = "Fenced";
//     @Deprecated
//     private static final String COLUMN_PORTTYPE = "portType";
// 
//     private static final String NAT_TABLE_CREATE_V1 =
//             String.format(
//                     "CREATE TABLE %s (" +
//                             "%s INTEGER PRIMARY KEY," +
//                             "%s TEXT NOT NULL," +
//                             "%s TEXT NOT NULL DEFAULT \"%s\"," +
//                             "%s INTEGER NOT NULL DEFAULT '%d'," +
//                             "%s TEXT NOT NULL DEFAULT \"TransProxy\")",
//                     NAT_TABLE_NAME,
//                     COLUMN_APPUID,
//                     COLUMN_APPNAME,
//                     COLUMN_ONIONTYPE, Constants.DB_ONION_TYPE_TOR,
//                     COLUMN_ONIONPORT, Constants.ORBOT_TRANSPROXY,
//                     COLUMN_PORTTYPE
//             );
// */
//     private static final String NAT_TABLE_CREATE_V2 =
//             String.format(
//                     "CREATE TABLE %s (" +
//                             "%s INTEGER PRIMARY KEY," +
//                             "%s TEXT NOT NULL," +
//                             "%s TEXT," +
//                             "%s INTEGER," +
//                             "%s INTEGER)",
//                     NAT_TABLE_NAME,
//                     COLUMN_APPUID,
//                     COLUMN_APPNAME,
//                     COLUMN_ONIONTYPE,
//                     COLUMN_LOCALHOST,
//                     COLUMN_LOCALNETWORK
//             );
// 
//     private static final int DATABASE_VERSION = 2;
//     private static final String DB_NAME = "nat.s3db";
// 
//     public natDBHelper(Context context) {
//         super(context, DB_NAME, null, DATABASE_VERSION);
//     }
// 
//     @Override
//     public void onCreate(SQLiteDatabase db) {
//         db.execSQL(NAT_TABLE_CREATE_V2);
//     }
// 
//     @Override
//     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         if (oldVersion == newVersion){
//             return;
//         }
// 
//         db.beginTransaction();
//         try{
//             for(int version = oldVersion; version < newVersion; version++){
//                 switch (version){
//                     // VERSION 1 -----> 2
//                     case 1:
//                         db.execSQL(String.format("ALTER TABLE %s RENAME TO %s_backup;", NAT_TABLE_NAME, NAT_TABLE_NAME));
//                         db.execSQL(NAT_TABLE_CREATE_V2);
//                         db.execSQL(String.format(
//                                 "INSERT INTO %s(%s, %s, %s, %s, %s) SELECT %s, %s, %s, 0, 0 FROM %s_backup;",
//                                 NAT_TABLE_NAME, COLUMN_APPUID, COLUMN_APPNAME, COLUMN_ONIONTYPE, COLUMN_LOCALHOST, COLUMN_LOCALNETWORK,
//                                                 COLUMN_APPUID, COLUMN_APPNAME, COLUMN_ONIONTYPE, NAT_TABLE_NAME));
//                         db.execSQL(String.format("DROP TABLE %s_backup;", NAT_TABLE_NAME));
//                 }
//             }
// 
//             db.setTransactionSuccessful();
//         } finally{
//             db.endTransaction();
//         }
//     }
// 
// }


import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import org.ethack.orwall.database.natDBHelper;
import org.sufficientlysecure.rootcommands.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

        int nb_row = 0;
        try {
            nb_row = db.update(natDBHelper.NAT_TABLE_NAME, contentValues, filter, filterArgs);
        } catch (SQLiteConstraintException e) {
            Log.e(TAG, "Constraint exception");
            Log.e(TAG, e.getMessage());
        }
        db.close();

        return (nb_row == 1);
    }

    public AppRule getAppRule(Long appUID) {
        SQLiteDatabase db = this.dbHelper.getReadableDatabase();

        String[] filterArgs = {
                String.valueOf(appUID)
        };
        String[] selection = {
                natDBHelper.COLUMN_APPNAME,
                natDBHelper.COLUMN_APPUID,
                natDBHelper.COLUMN_ONIONTYPE,
                natDBHelper.COLUMN_LOCALHOST,
                natDBHelper.COLUMN_LOCALNETWORK
        };

        Cursor cursor = db.query(
                natDBHelper.NAT_TABLE_NAME,
                selection,
// Path: app/src/main/java/org/ethack/orwall/database/natDBHelper.java
// public class natDBHelper extends SQLiteOpenHelper {
// 
//     public static final String NAT_TABLE_NAME = "rules";
//     public static final String COLUMN_APPUID = "appUID";
//     public static final String COLUMN_APPNAME = "appName";
//     public static final String COLUMN_ONIONTYPE = "onionType";
//     public static final String COLUMN_LOCALHOST = "localhost";
//     public static final String COLUMN_LOCALNETWORK = "localnetwork";
// 
// /*
//     @Deprecated
//     private static final String COLUMN_ONIONPORT = "onionPort";
//     @Deprecated
//     private static final String DB_PORT_TYPE_FENCED = "Fenced";
//     @Deprecated
//     private static final String COLUMN_PORTTYPE = "portType";
// 
//     private static final String NAT_TABLE_CREATE_V1 =
//             String.format(
//                     "CREATE TABLE %s (" +
//                             "%s INTEGER PRIMARY KEY," +
//                             "%s TEXT NOT NULL," +
//                             "%s TEXT NOT NULL DEFAULT \"%s\"," +
//                             "%s INTEGER NOT NULL DEFAULT '%d'," +
//                             "%s TEXT NOT NULL DEFAULT \"TransProxy\")",
//                     NAT_TABLE_NAME,
//                     COLUMN_APPUID,
//                     COLUMN_APPNAME,
//                     COLUMN_ONIONTYPE, Constants.DB_ONION_TYPE_TOR,
//                     COLUMN_ONIONPORT, Constants.ORBOT_TRANSPROXY,
//                     COLUMN_PORTTYPE
//             );
// */
//     private static final String NAT_TABLE_CREATE_V2 =
//             String.format(
//                     "CREATE TABLE %s (" +
//                             "%s INTEGER PRIMARY KEY," +
//                             "%s TEXT NOT NULL," +
//                             "%s TEXT," +
//                             "%s INTEGER," +
//                             "%s INTEGER)",
//                     NAT_TABLE_NAME,
//                     COLUMN_APPUID,
//                     COLUMN_APPNAME,
//                     COLUMN_ONIONTYPE,
//                     COLUMN_LOCALHOST,
//                     COLUMN_LOCALNETWORK
//             );
// 
//     private static final int DATABASE_VERSION = 2;
//     private static final String DB_NAME = "nat.s3db";
// 
//     public natDBHelper(Context context) {
//         super(context, DB_NAME, null, DATABASE_VERSION);
//     }
// 
//     @Override
//     public void onCreate(SQLiteDatabase db) {
//         db.execSQL(NAT_TABLE_CREATE_V2);
//     }
// 
//     @Override
//     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         if (oldVersion == newVersion){
//             return;
//         }
// 
//         db.beginTransaction();
//         try{
//             for(int version = oldVersion; version < newVersion; version++){
//                 switch (version){
//                     // VERSION 1 -----> 2
//                     case 1:
//                         db.execSQL(String.format("ALTER TABLE %s RENAME TO %s_backup;", NAT_TABLE_NAME, NAT_TABLE_NAME));
//                         db.execSQL(NAT_TABLE_CREATE_V2);
//                         db.execSQL(String.format(
//                                 "INSERT INTO %s(%s, %s, %s, %s, %s) SELECT %s, %s, %s, 0, 0 FROM %s_backup;",
//                                 NAT_TABLE_NAME, COLUMN_APPUID, COLUMN_APPNAME, COLUMN_ONIONTYPE, COLUMN_LOCALHOST, COLUMN_LOCALNETWORK,
//                                                 COLUMN_APPUID, COLUMN_APPNAME, COLUMN_ONIONTYPE, NAT_TABLE_NAME));
//                         db.execSQL(String.format("DROP TABLE %s_backup;", NAT_TABLE_NAME));
//                 }
//             }
// 
//             db.setTransactionSuccessful();
//         } finally{
//             db.endTransaction();
//         }
//     }
// 
// }


// Path: app/src/main/java/org/ethack/orwall/lib/NatRules.java
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import org.ethack.orwall.database.natDBHelper;
import org.sufficientlysecure.rootcommands.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;


        int nb_row = 0;
        try {
            nb_row = db.update(natDBHelper.NAT_TABLE_NAME, contentValues, filter, filterArgs);
        } catch (SQLiteConstraintException e) {
            Log.e(TAG, "Constraint exception");
            Log.e(TAG, e.getMessage());
        }
        db.close();

        return (nb_row == 1);
    }

    public AppRule getAppRule(Long appUID) {
        SQLiteDatabase db = this.dbHelper.getReadableDatabase();

        String[] filterArgs = {
                String.valueOf(appUID)
        };
        String[] selection = {
                natDBHelper.COLUMN_APPNAME,
                natDBHelper.COLUMN_APPUID,
                natDBHelper.COLUMN_ONIONTYPE,
                natDBHelper.COLUMN_LOCALHOST,
                natDBHelper.COLUMN_LOCALNETWORK
        };

        Cursor cursor = db.query(
                natDBHelper.NAT_TABLE_NAME,
                selection,
                natDBHelper.COLUMN_APPUID + "=?",