{"action":"created","comment":{"url":"https://api.github.com/repos/novoda/download-manager/pulls/comments/34195029","id":34195029,"diff_hunk":"@@ -0,0 +1,244 @@\n+package com.novoda.downloadmanager.lib;\n+\n+import android.support.annotation.IntDef;\n+import android.support.annotation.NonNull;\n+\n+import java.lang.annotation.Retention;\n+import java.lang.annotation.RetentionPolicy;\n+import java.util.ArrayList;\n+import java.util.List;\n+\n+/**\n+ * Class used to query the batches table. Use {@link BatchQuery.Builder} in order to build it.\n+ */\n+public class BatchQuery {\n+    public static final BatchQuery ALL = new BatchQuery(null, null, null);\n+    private static final int LOW_END_FAILED_STATUS_CODE = 400;\n+    private static final int HIGH_END_FAILED_STATUS_CODE = 600;\n+\n+    private final String selection;\n+    private final String sortOrder;\n+    private final String[] selectionArguments;\n+\n+    BatchQuery(String selection, String sortOrder, String[] selectionArguments) {\n+        this.selection = selection;\n+        this.sortOrder = sortOrder;\n+        this.selectionArguments = selectionArguments;\n+    }\n+\n+    String getSelection() {\n+        return selection;\n+    }\n+\n+    String getSortOrder() {\n+        return sortOrder;\n+    }\n+\n+    String[] getSelectionArguments() {\n+        return selectionArguments;\n+    }\n+\n+    public static class Builder {\n+\n+        private final Criteria.Builder builder;\n+\n+        private static final String ORDER_BY_LIVENESS = String.format(\"CASE batch_status \"\n+                        + \"WHEN %1$d THEN 1 \"\n+                        + \"WHEN %2$d THEN 2 \"\n+                        + \"WHEN %3$d THEN 3 \"\n+                        + \"WHEN %4$d THEN 4 \"\n+                        + \"WHEN %5$d THEN 5 \"\n+                        + \"ELSE 2 \"\n+                        + \"END\",\n+                Downloads.Impl.STATUS_RUNNING,\n+                Downloads.Impl.STATUS_PENDING,\n+                Downloads.Impl.STATUS_PAUSED_BY_APP,\n+                Downloads.Impl.STATUS_BATCH_FAILED,\n+                Downloads.Impl.STATUS_SUCCESS\n+        );\n+\n+        private Criteria.Builder criteriaIdBuilder;\n+        private Criteria.Builder criteriaStatusBuilder;\n+\n+        public Builder() {\n+            builder = new Criteria.Builder();\n+        }\n+\n+        /**\n+         * Filter by batch id\n+         *\n+         * @param id id of the batch\n+         * @return {@link BatchQuery.Builder}\n+         */\n+        public Builder withId(long id) {\n+            this.criteriaIdBuilder = new Criteria.Builder();\n+            criteriaIdBuilder\n+                    .withSelection(Downloads.Impl.Batches._ID, Criteria.Wildcard.EQUALS)\n+                    .withArgument(String.valueOf(id));\n+            return this;\n+        }\n+\n+        /**\n+         * Sorting in ascending order by sort column\n+         *\n+         * @param sortColumn sort column\n+         * @return {@link BatchQuery.Builder}\n+         */\n+        public Builder withSortAscendingBy(String sortColumn) {\n+            builder.sortBy(sortColumn).ascending();\n+            return this;\n+        }\n+\n+        /**\n+         * Sorting in descending order by sort column\n+         *\n+         * @param sortColumn sort column\n+         * @return {@link BatchQuery.Builder}\n+         */\n+        public Builder withSortDescendingBy(String sortColumn) {\n+            builder.sortBy(sortColumn).descending();\n+            return this;\n+        }\n+\n+        /**\n+         * Sorts batches according to the 'liveness' of the download, i.e. in the order:\n+         * Downloading, queued, other, paused, failed, completed\n+         *\n+         * @return {@link BatchQuery.Builder}\n+         */\n+        public Builder withSortByLiveness() {\n+            builder.sortBy(ORDER_BY_LIVENESS);\n+            return this;\n+        }\n+\n+        /**\n+         * Filter by status\n+         *\n+         * @param statusFlags status flags that can be combined with \"|\"\n+         *                    one of {@link DownloadManager#STATUS_PAUSED},\n+         *                    {@link DownloadManager#STATUS_FAILED},\n+         *                    {@link DownloadManager#STATUS_PENDING},\n+         *                    {@link DownloadManager#STATUS_SUCCESSFUL},\n+         *                    {@link DownloadManager#STATUS_RUNNING}\n+         *                    <p/>\n+         *                    e.g. withStatusFilter(DownloadManager.STATUS_FAILED | DownloadManager.STATUS_PENDING)\n+         * @return {@link BatchQuery.Builder}\n+         */\n+        public Builder withStatusFilter(@Status int statusFlags) {\n+            this.criteriaStatusBuilder = new Criteria.Builder();\n+            List<Integer> statuses = buildStatusesFrom(statusFlags);","path":"library/src/main/java/com/novoda/downloadmanager/lib/BatchQuery.java","position":129,"original_position":129,"commit_id":"5417ebd2b1995c99de1503d5f90d813e0e889f4e","original_commit_id":"5417ebd2b1995c99de1503d5f90d813e0e889f4e","user":{"login":"friedger","id":1449049,"avatar_url":"https://avatars.githubusercontent.com/u/1449049?v=3","gravatar_id":"","url":"https://api.github.com/users/friedger","html_url":"https://github.com/friedger","followers_url":"https://api.github.com/users/friedger/followers","following_url":"https://api.github.com/users/friedger/following{/other_user}","gists_url":"https://api.github.com/users/friedger/gists{/gist_id}","starred_url":"https://api.github.com/users/friedger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/friedger/subscriptions","organizations_url":"https://api.github.com/users/friedger/orgs","repos_url":"https://api.github.com/users/friedger/repos","events_url":"https://api.github.com/users/friedger/events{/privacy}","received_events_url":"https://api.github.com/users/friedger/received_events","type":"User","site_admin":false},"body":"and then do something like builder.or(list of criteria)","created_at":"2015-07-08T20:34:16Z","updated_at":"2015-07-08T20:34:16Z","html_url":"https://github.com/novoda/download-manager/pull/79#discussion_r34195029","pull_request_url":"https://api.github.com/repos/novoda/download-manager/pulls/79","_links":{"self":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/comments/34195029"},"html":{"href":"https://github.com/novoda/download-manager/pull/79#discussion_r34195029"},"pull_request":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/79"}}},"pull_request":{"url":"https://api.github.com/repos/novoda/download-manager/pulls/79","id":39482481,"html_url":"https://github.com/novoda/download-manager/pull/79","diff_url":"https://github.com/novoda/download-manager/pull/79.diff","patch_url":"https://github.com/novoda/download-manager/pull/79.patch","issue_url":"https://api.github.com/repos/novoda/download-manager/issues/79","number":79,"state":"open","locked":false,"title":"Query for batches","user":{"login":"Electryc","id":913571,"avatar_url":"https://avatars.githubusercontent.com/u/913571?v=3","gravatar_id":"","url":"https://api.github.com/users/Electryc","html_url":"https://github.com/Electryc","followers_url":"https://api.github.com/users/Electryc/followers","following_url":"https://api.github.com/users/Electryc/following{/other_user}","gists_url":"https://api.github.com/users/Electryc/gists{/gist_id}","starred_url":"https://api.github.com/users/Electryc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Electryc/subscriptions","organizations_url":"https://api.github.com/users/Electryc/orgs","repos_url":"https://api.github.com/users/Electryc/repos","events_url":"https://api.github.com/users/Electryc/events{/privacy}","received_events_url":"https://api.github.com/users/Electryc/received_events","type":"User","site_admin":false},"body":"Introduces the ability to query for batches. The querying is done through `downloadManager.queryBatch(BatchQuery query)`. \r\n\r\n`BatchQuery` is the class with which you can sort/filter the query. \r\n\r\nThe following constants were exposed:\r\n- `DownloadManager.BATCH_CONTENT_URI` has been exposed in order clients to be able to watch the batches table. \r\n- `DownloadManager.COLUMN_BATCH_TOTAL_SIZE_BYTES`\r\n- `DownloadManager.COLUMN_BATCH_CURRENT_SIZE_BYTES`\r\n\r\nExample of usage (for querying batches that have failed or pending):\r\n```java\r\nBatchQuery query = new BatchQuery.Builder()\r\n        .withStatusFilter(\r\n                DownloadManager.STATUS_FAILED\r\n                        | DownloadManager.STATUS_PENDING)\r\n        .build();\r\nCursor batchesCursor = downloadManager.queryBatch(query);\r\n```\r\n\r\nThe extended demo was updated with an additional activity `ShowBatchesActivity` that reflects the new functionality. \r\n![query_batches](https://cloud.githubusercontent.com/assets/913571/8571955/9905583a-2581-11e5-80a2-99ee2eb33dc1.gif)","created_at":"2015-07-08T14:25:13Z","updated_at":"2015-07-08T20:34:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"371308e81536f74003a6f0fc349c719023c2f96d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/novoda/download-manager/pulls/79/commits","review_comments_url":"https://api.github.com/repos/novoda/download-manager/pulls/79/comments","review_comment_url":"https://api.github.com/repos/novoda/download-manager/pulls/comments{/number}","comments_url":"https://api.github.com/repos/novoda/download-manager/issues/79/comments","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/5417ebd2b1995c99de1503d5f90d813e0e889f4e","head":{"label":"novoda:query_for_batches","ref":"query_for_batches","sha":"5417ebd2b1995c99de1503d5f90d813e0e889f4e","user":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"repo":{"id":35211795,"name":"download-manager","full_name":"novoda/download-manager","owner":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/novoda/download-manager","description":"Download manager based on AOSP DM but allowing downloading to internal private storage","fork":false,"url":"https://api.github.com/repos/novoda/download-manager","forks_url":"https://api.github.com/repos/novoda/download-manager/forks","keys_url":"https://api.github.com/repos/novoda/download-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/novoda/download-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/novoda/download-manager/teams","hooks_url":"https://api.github.com/repos/novoda/download-manager/hooks","issue_events_url":"https://api.github.com/repos/novoda/download-manager/issues/events{/number}","events_url":"https://api.github.com/repos/novoda/download-manager/events","assignees_url":"https://api.github.com/repos/novoda/download-manager/assignees{/user}","branches_url":"https://api.github.com/repos/novoda/download-manager/branches{/branch}","tags_url":"https://api.github.com/repos/novoda/download-manager/tags","blobs_url":"https://api.github.com/repos/novoda/download-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/novoda/download-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/novoda/download-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/novoda/download-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/novoda/download-manager/languages","stargazers_url":"https://api.github.com/repos/novoda/download-manager/stargazers","contributors_url":"https://api.github.com/repos/novoda/download-manager/contributors","subscribers_url":"https://api.github.com/repos/novoda/download-manager/subscribers","subscription_url":"https://api.github.com/repos/novoda/download-manager/subscription","commits_url":"https://api.github.com/repos/novoda/download-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/novoda/download-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/novoda/download-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/novoda/download-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/novoda/download-manager/contents/{+path}","compare_url":"https://api.github.com/repos/novoda/download-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/novoda/download-manager/merges","archive_url":"https://api.github.com/repos/novoda/download-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/novoda/download-manager/downloads","issues_url":"https://api.github.com/repos/novoda/download-manager/issues{/number}","pulls_url":"https://api.github.com/repos/novoda/download-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/novoda/download-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/novoda/download-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/novoda/download-manager/labels{/name}","releases_url":"https://api.github.com/repos/novoda/download-manager/releases{/id}","created_at":"2015-05-07T09:41:52Z","updated_at":"2015-07-04T09:11:59Z","pushed_at":"2015-07-08T18:22:49Z","git_url":"git://github.com/novoda/download-manager.git","ssh_url":"git@github.com:novoda/download-manager.git","clone_url":"https://github.com/novoda/download-manager.git","svn_url":"https://github.com/novoda/download-manager","homepage":"","size":2865,"stargazers_count":48,"watchers_count":48,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":17,"forks":4,"open_issues":17,"watchers":48,"default_branch":"master"}},"base":{"label":"novoda:master","ref":"master","sha":"3de4e72878e602d5ca8a0547e2e3b93fa8ca9c7d","user":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"repo":{"id":35211795,"name":"download-manager","full_name":"novoda/download-manager","owner":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/novoda/download-manager","description":"Download manager based on AOSP DM but allowing downloading to internal private storage","fork":false,"url":"https://api.github.com/repos/novoda/download-manager","forks_url":"https://api.github.com/repos/novoda/download-manager/forks","keys_url":"https://api.github.com/repos/novoda/download-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/novoda/download-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/novoda/download-manager/teams","hooks_url":"https://api.github.com/repos/novoda/download-manager/hooks","issue_events_url":"https://api.github.com/repos/novoda/download-manager/issues/events{/number}","events_url":"https://api.github.com/repos/novoda/download-manager/events","assignees_url":"https://api.github.com/repos/novoda/download-manager/assignees{/user}","branches_url":"https://api.github.com/repos/novoda/download-manager/branches{/branch}","tags_url":"https://api.github.com/repos/novoda/download-manager/tags","blobs_url":"https://api.github.com/repos/novoda/download-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/novoda/download-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/novoda/download-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/novoda/download-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/novoda/download-manager/languages","stargazers_url":"https://api.github.com/repos/novoda/download-manager/stargazers","contributors_url":"https://api.github.com/repos/novoda/download-manager/contributors","subscribers_url":"https://api.github.com/repos/novoda/download-manager/subscribers","subscription_url":"https://api.github.com/repos/novoda/download-manager/subscription","commits_url":"https://api.github.com/repos/novoda/download-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/novoda/download-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/novoda/download-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/novoda/download-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/novoda/download-manager/contents/{+path}","compare_url":"https://api.github.com/repos/novoda/download-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/novoda/download-manager/merges","archive_url":"https://api.github.com/repos/novoda/download-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/novoda/download-manager/downloads","issues_url":"https://api.github.com/repos/novoda/download-manager/issues{/number}","pulls_url":"https://api.github.com/repos/novoda/download-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/novoda/download-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/novoda/download-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/novoda/download-manager/labels{/name}","releases_url":"https://api.github.com/repos/novoda/download-manager/releases{/id}","created_at":"2015-05-07T09:41:52Z","updated_at":"2015-07-04T09:11:59Z","pushed_at":"2015-07-08T18:22:49Z","git_url":"git://github.com/novoda/download-manager.git","ssh_url":"git@github.com:novoda/download-manager.git","clone_url":"https://github.com/novoda/download-manager.git","svn_url":"https://github.com/novoda/download-manager","homepage":"","size":2865,"stargazers_count":48,"watchers_count":48,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":17,"forks":4,"open_issues":17,"watchers":48,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/79"},"html":{"href":"https://github.com/novoda/download-manager/pull/79"},"issue":{"href":"https://api.github.com/repos/novoda/download-manager/issues/79"},"comments":{"href":"https://api.github.com/repos/novoda/download-manager/issues/79/comments"},"review_comments":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/79/comments"},"review_comment":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/79/commits"},"statuses":{"href":"https://api.github.com/repos/novoda/download-manager/statuses/5417ebd2b1995c99de1503d5f90d813e0e889f4e"}}}}