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

Commit 8d9ce61a authored by Adnan Begovic's avatar Adnan Begovic
Browse files

content: Add "--show-type" argument for content queries.

  Allows you to append the argument "--show-type" to a
  query command which can detail each projection columns
  value type in the response.

Change-Id: I10640b25203e2ff8202b93707dc85923ad9e990f
parent a5a5a983
Loading
Loading
Loading
Loading
+21 −3
Original line number Diff line number Diff line
@@ -103,10 +103,12 @@ public class Content {
                + "--where \"name=\'new_setting\'\"\n"
        + "\n"
        + "usage: adb shell content query --uri <URI> [--user <USER_ID>]"
                + " [--projection <PROJECTION>] [--where <WHERE>] [--sort <SORT_ORDER>]\n"
                + " [--projection <PROJECTION>] [--where <WHERE>] [--sort <SORT_ORDER>] "
                + " [--show-type <SHOW-TYPE>] \n"
        + "  <PROJECTION> is a list of colon separated column names and is formatted:\n"
        + "  <COLUMN_NAME>[:<COLUMN_NAME>...]\n"
        + "  <SORT_ORDER> is the order in which rows in the result should be sorted.\n"
        + "  <SHOW-TYPE> if true shows the type of value of each projection column"
        + "  Example:\n"
        + "  # Select \"name\" and \"value\" columns from secure settings where \"name\" is "
                + "equal to \"new_setting\" and sort the result by name in ascending order.\n"
@@ -142,6 +144,7 @@ public class Content {
        private static final String ARGUMENT_METHOD = "--method";
        private static final String ARGUMENT_ARG = "--arg";
        private static final String ARGUMENT_EXTRA = "--extra";
        private static final String ARGUMENT_SHOW_TYPE = "--show-type";
        private static final String TYPE_BOOLEAN = "b";
        private static final String TYPE_STRING = "s";
        private static final String TYPE_INTEGER = "i";
@@ -316,6 +319,7 @@ public class Content {
            String[] projection = null;
            String sort = null;
            String where = null;
            boolean showType = false;
            for (String argument; (argument = mTokenizer.nextArg())!= null;) {
                if (ARGUMENT_URI.equals(argument)) {
                    uri = Uri.parse(argumentValueRequired(argument));
@@ -327,6 +331,8 @@ public class Content {
                    sort = argumentValueRequired(argument);
                } else if (ARGUMENT_PROJECTION.equals(argument)) {
                    projection = argumentValueRequired(argument).split("[\\s]*:[\\s]*");
                } else if (ARGUMENT_SHOW_TYPE.equals(argument)) {
                    showType = argumentValueRequiredForBoolean(argument);
                } else {
                    throw new IllegalArgumentException("Unsupported argument: " + argument);
                }
@@ -335,7 +341,7 @@ public class Content {
                throw new IllegalArgumentException("Content provider URI not specified."
                        + " Did you specify --uri argument?");
            }
            return new QueryCommand(uri, userId, projection, where, sort);
            return new QueryCommand(uri, userId, projection, where, sort, showType);
        }

        private void parseBindValue(ContentValues values) {
@@ -367,6 +373,14 @@ public class Content {
            }
        }

        private boolean argumentValueRequiredForBoolean(String argument) {
            String value = mTokenizer.nextArg();
            if (TextUtils.isEmpty(value) || value.startsWith(ARGUMENT_PREFIX)) {
                throw new IllegalArgumentException("No value for argument: " + argument);
            }
            return value.equals("true");
        }

        private String argumentValueRequired(String argument) {
            String value = mTokenizer.nextArg();
            if (TextUtils.isEmpty(value) || value.startsWith(ARGUMENT_PREFIX)) {
@@ -539,12 +553,15 @@ public class Content {
    private static class QueryCommand extends DeleteCommand {
        final String[] mProjection;
        final String mSortOrder;
        final boolean mShowType;

        public QueryCommand(
                Uri uri, int userId, String[] projection, String where, String sortOrder) {
                Uri uri, int userId, String[] projection, String where, String sortOrder,
                boolean showType) {
            super(uri, userId, where);
            mProjection = projection;
            mSortOrder = sortOrder;
            mShowType = showType;
        }

        @Override
@@ -590,6 +607,7 @@ public class Content {
                                    break;
                            }
                            builder.append(columnName).append("=").append(columnValue);
                            if (mShowType) builder.append(", type=").append(type);
                        }
                        System.out.println(builder);
                    } while (cursor.moveToNext());