Search in sources :

Example 1 with DBCustomCommand

use of ai.saiy.android.database.DBCustomCommand in project Saiy-PS by brandall76.

the class CustomCommandHelper method deleteCustomCommand.

public static void deleteCustomCommand(@NonNull final Context ctx, final long rowId) {
    synchronized (lock) {
        final DBCustomCommand dbCustomCommand = new DBCustomCommand(ctx);
        dbCustomCommand.deleteRow(rowId);
    }
}
Also used : DBCustomCommand(ai.saiy.android.database.DBCustomCommand)

Example 2 with DBCustomCommand

use of ai.saiy.android.database.DBCustomCommand in project Saiy-PS by brandall76.

the class CustomCommandHelper method setCommand.

/**
 * Insert a new {@link CustomCommand} in the {@link DBCustomCommand} synchronising with a basic
 * lock object in a vain attempt to prevent concurrency issues.
 *
 * @param ctx           the application context
 * @param customCommand to be set
 * @return true if the insertion was successful
 */
public static Pair<Boolean, Long> setCommand(@NonNull final Context ctx, @NonNull final CustomCommand customCommand, final long rowId) {
    synchronized (lock) {
        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        final String gsonString = gson.toJson(customCommand);
        final DBCustomCommand dbCustomCommand = new DBCustomCommand(ctx);
        final Pair<Boolean, Long> duplicatePair;
        if (rowId > -1) {
            duplicatePair = new Pair<>(true, rowId);
        } else {
            duplicatePair = commandExists(dbCustomCommand, customCommand);
        }
        return dbCustomCommand.insertPopulatedRow(customCommand.getKeyphrase(), customCommand.getRegex(), gsonString, duplicatePair.first, duplicatePair.second);
    }
}
Also used : DBCustomCommand(ai.saiy.android.database.DBCustomCommand) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 3 with DBCustomCommand

use of ai.saiy.android.database.DBCustomCommand in project Saiy-PS by brandall76.

the class CustomCommandHelper method getCustomCommands.

/**
 * Extract all of the user's serialised {@link CustomCommand} from {@link DBCustomCommand} into
 * an array of {@link CustomCommandContainer}
 *
 * @param ctx the application context
 * @return an array of {@link CustomCommandContainer}
 */
public ArrayList<CustomCommandContainer> getCustomCommands(@NonNull final Context ctx) {
    synchronized (lock) {
        final ArrayList<CustomCommandContainer> customCommandContainerArray;
        final DBCustomCommand dbCustomCommand = new DBCustomCommand(ctx);
        if (dbCustomCommand.databaseExists()) {
            customCommandContainerArray = dbCustomCommand.getKeyphrases();
            if (DEBUG) {
                MyLog.i(CLS_NAME, "databaseExists: true: with " + customCommandContainerArray.size() + " commands.");
            }
        } else {
            if (DEBUG) {
                MyLog.i(CLS_NAME, "databaseExists: false");
            }
            customCommandContainerArray = new ArrayList<>();
        }
        return customCommandContainerArray;
    }
}
Also used : DBCustomCommand(ai.saiy.android.database.DBCustomCommand)

Example 4 with DBCustomCommand

use of ai.saiy.android.database.DBCustomCommand in project Saiy-PS by brandall76.

the class CustomCommandHelper method deleteCommandsForPackage.

/**
 * Delete all of the {@link CustomCommand} for the given package names.
 *
 * @param ctx          the application context
 * @param packageNames the package names for which commands should be deleted
 */
public static void deleteCommandsForPackage(@NonNull final Context ctx, @NonNull final ArrayList<String> packageNames) {
    synchronized (lock) {
        final long then = System.nanoTime();
        final DBCustomCommand dbCustomCommand = new DBCustomCommand(ctx);
        if (dbCustomCommand.databaseExists()) {
            final ArrayList<CustomCommandContainer> customCommandContainerArray = dbCustomCommand.getKeyphrases();
            final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
            final ArrayList<Long> rowIds = new ArrayList<>();
            CustomCommand customCommand;
            for (final CustomCommandContainer container : customCommandContainerArray) {
                Intent remoteIntent = null;
                try {
                    customCommand = gson.fromJson(container.getSerialised(), CustomCommand.class);
                    if (customCommand.getCustomAction() == CUSTOM_INTENT_SERVICE) {
                        remoteIntent = Intent.parseUri(customCommand.getIntent(), 0);
                    }
                } catch (final URISyntaxException e) {
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "remoteIntent.parseUri: URISyntaxException");
                        e.printStackTrace();
                    }
                } catch (final JsonSyntaxException e) {
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "gson.fromJson: JsonSyntaxException");
                        e.printStackTrace();
                    }
                }
                if (remoteIntent != null && packageNames.contains(remoteIntent.getPackage())) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "adding " + remoteIntent.getPackage() + " to be deleted");
                    }
                    rowIds.add(container.getRowId());
                }
            }
            if (!rowIds.isEmpty()) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "deleting " + rowIds.size() + " commands");
                }
                dbCustomCommand.deleteRows(rowIds);
            } else {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "no commands for packages");
                }
            }
        } else {
            if (DEBUG) {
                MyLog.i(CLS_NAME, "databaseExists: false");
            }
        }
        if (DEBUG) {
            MyLog.getElapsed("deleteCommandsForPackage", then);
        }
    }
}
Also used : DBCustomCommand(ai.saiy.android.database.DBCustomCommand) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Intent(android.content.Intent) URISyntaxException(java.net.URISyntaxException) DBCustomCommand(ai.saiy.android.database.DBCustomCommand) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Aggregations

DBCustomCommand (ai.saiy.android.database.DBCustomCommand)4 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 Intent (android.content.Intent)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1