├── .gitignore ├── .travis.yml ├── LICENSE ├── NOTICE ├── README.md ├── asyncquery ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── madrapps │ └── asyncquery │ └── AsyncQueryHandler.java ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── madrapps │ │ └── asyncqueryhandler │ │ ├── MainActivity.java │ │ ├── database │ │ ├── DatabaseHandler.java │ │ └── DatabaseHelper.java │ │ └── provider │ │ ├── Contract.java │ │ └── DataProvider.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | sudo: false 3 | install: true 4 | 5 | jdk: oraclejdk8 6 | android: 7 | components: 8 | - tools 9 | - platform-tools 10 | 11 | # The BuildTools version used by your project 12 | - build-tools-25.0.2 13 | 14 | # The SDK version used to compile your project 15 | - android-25 16 | 17 | # Specify at least one system image, 18 | # if you need to run emulator(s) during your tests 19 | - sys-img-armeabi-v7a-android-21 20 | 21 | env: 22 | global: 23 | # install timeout in minutes (2 minutes by default) 24 | - ADB_INSTALL_TIMEOUT=8 25 | 26 | before_install: 27 | - mkdir "$ANDROID_HOME/licenses" || true 28 | - echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license" 29 | - echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license" 30 | 31 | # Emulator Management: Create, Start and Wait 32 | before_script: 33 | - echo no | android create avd --force -n test -t android-21 --abi armeabi-v7a 34 | - emulator -avd test -no-skin -no-audio -no-window & 35 | - android-wait-for-emulator 36 | - adb shell input keyevent 82 & 37 | 38 | script: 39 | - android list target 40 | - ./gradlew connectedAndroidTest 41 | 42 | cache: 43 | directories: 44 | - '$HOME/.m2/repository' 45 | - '$HOME/.gradle' 46 | - '.gradle' -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | 190 | Copyright (C) 2007 The Android Open Source Project 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Android Code 2 | Copyright 2005-2008 The Android Open Source Project 3 | 4 | This product includes software developed as part of 5 | The Android Open Source Project (http://source.android.com). 6 | 7 | ========================================================================= 8 | == NOTICE file corresponding to the section 4 d of == 9 | == the Apache License, Version 2.0, == 10 | == in this case for Apache Commons code. == 11 | ========================================================================= 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AsyncQuery 2 | [![Build Status](https://travis-ci.org/Madrapps/AsyncQuery.svg?branch=master)](https://travis-ci.org/Madrapps/AsyncQuery) 3 | [ ![Download](https://api.bintray.com/packages/madrapps/maven/com.github.madrapps%3Aasyncquery/images/download.svg) ](https://bintray.com/madrapps/maven/com.github.madrapps%3Aasyncquery/_latestVersion) 4 | 5 | Improved AsyncQueryHandler that handles BulkInsert operation 6 | 7 | **Note:** This is literally a copy paste of the `AsyncQueryHandler` in Android and then modified that to support Bulk Insert operation. All credits goes to the Android Open Source team for writing the original `AsyncQueryHandler`. 8 | 9 | Download 10 | ----- 11 | 12 | ```gradle 13 | repositories { 14 | jcenter() // or mavenCentral() 15 | } 16 | 17 | dependencies { 18 | compile 'com.github.madrapps:asyncquery:1.0.1' 19 | } 20 | ``` 21 | 22 | Usage 23 | ----- 24 | You would use this the same way as you would use `AsyncQueryHandler` except you can now use `startBulkInsert()` 25 | 26 | ```java 27 | final DatabaseHandler handler = new DatabaseHandler(getContentResolver()); 28 | final Uri uri = new Uri.Builder().scheme(SCHEME).authority(AUTHORITY).appendEncodedPath(ORGANIZATION).build(); 29 | handler.startBulkInsert(1, null, uri, getContentValues()); 30 | ``` 31 | 32 | ```java 33 | public class DatabaseHandler extends AsyncQueryHandler { 34 | 35 | public DatabaseHandler(ContentResolver cr) { 36 | super(cr); 37 | } 38 | 39 | @Override 40 | protected void onBulkInsertComplete(int token, Object cookie, int result) { 41 | super.onBulkInsertComplete(token, cookie, result); 42 | Log.d("DatabaseHandler", "Bulk Insert Done"); 43 | } 44 | } 45 | ``` 46 | 47 | License 48 | ----- 49 | 50 | AsyncQuery by [Madrapps](http://madrapps.github.io/) is licensed under a [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0) by Android Open Source Platform. 51 | -------------------------------------------------------------------------------- /asyncquery/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /asyncquery/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | apply plugin: "com.jfrog.bintray" 4 | 5 | def projectVersion = "1.0.0" 6 | def projectGroupId = "com.github.madrapps" 7 | def siteUrl = 'https://github.com/Madrapps/AsyncQuery' 8 | def gitUrl = 'https://github.com/Madrapps/AsyncQuery.git' 9 | def fullName = "com.github.madrapps:asyncquery" 10 | 11 | version = projectVersion 12 | group = projectGroupId 13 | 14 | 15 | android { 16 | compileSdkVersion 25 17 | buildToolsVersion "25.0.2" 18 | 19 | defaultConfig { 20 | minSdkVersion 15 21 | targetSdkVersion 25 22 | versionCode 1 23 | versionName "1.0" 24 | 25 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 26 | 27 | } 28 | buildTypes { 29 | release { 30 | minifyEnabled false 31 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 32 | } 33 | } 34 | } 35 | 36 | dependencies { 37 | compile fileTree(dir: 'libs', include: ['*.jar']) 38 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 39 | exclude group: 'com.android.support', module: 'support-annotations' 40 | }) 41 | compile 'com.android.support:appcompat-v7:25.3.1' 42 | testCompile 'junit:junit:4.12' 43 | } 44 | 45 | 46 | install { 47 | repositories.mavenInstaller { 48 | pom { 49 | project { 50 | packaging 'aar' 51 | 52 | groupId projectGroupId 53 | 54 | name fullName 55 | description = 'Improved AsyncQueryHandler that handles BulkInsert operation Edit' 56 | url siteUrl 57 | 58 | licenses { 59 | license { 60 | name 'The Apache Software License, Version 2.0' 61 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 62 | } 63 | } 64 | developers { 65 | developer { 66 | id 'instrap' 67 | name 'Madrapps' 68 | email 'madrasappfactory@gmail.com' 69 | } 70 | } 71 | scm { 72 | connection gitUrl 73 | developerConnection gitUrl 74 | url siteUrl 75 | } 76 | } 77 | } 78 | } 79 | } 80 | 81 | task sourcesJar(type: Jar) { 82 | from android.sourceSets.main.java.srcDirs 83 | classifier = 'sources' 84 | } 85 | 86 | task javadoc(type: Javadoc) { 87 | source = android.sourceSets.main.java.srcDirs 88 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 89 | } 90 | 91 | task javadocJar(type: Jar, dependsOn: javadoc) { 92 | classifier = 'javadoc' 93 | from javadoc.destinationDir 94 | } 95 | 96 | artifacts { 97 | archives javadocJar 98 | archives sourcesJar 99 | } 100 | 101 | bintray { 102 | user = System.getenv('bintray_user') 103 | key = System.getenv('bintray_apikey') 104 | 105 | configurations = ['archives'] 106 | pkg { 107 | repo = "maven" 108 | name = fullName 109 | userOrg = "madrapps" 110 | websiteUrl = siteUrl 111 | vcsUrl = gitUrl 112 | licenses = ["Apache-2.0"] 113 | publish = true 114 | version { 115 | gpg { 116 | sign = true 117 | } 118 | } 119 | } 120 | } 121 | 122 | repositories { 123 | mavenCentral() 124 | maven { 125 | url "https://maven.google.com" 126 | } 127 | } -------------------------------------------------------------------------------- /asyncquery/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/tsaravana/Me/Programming/SDK/Android/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /asyncquery/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /asyncquery/src/main/java/com/madrapps/asyncquery/AsyncQueryHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.madrapps.asyncquery; 18 | 19 | import android.content.ContentResolver; 20 | import android.content.ContentValues; 21 | import android.database.Cursor; 22 | import android.net.Uri; 23 | import android.os.Handler; 24 | import android.os.HandlerThread; 25 | import android.os.Looper; 26 | import android.os.Message; 27 | import android.util.Log; 28 | 29 | import java.lang.ref.WeakReference; 30 | 31 | /** 32 | * A helper class to help make handling asynchronous {@link ContentResolver} 33 | * queries easier. 34 | */ 35 | public abstract class AsyncQueryHandler extends Handler { 36 | private static final String TAG = "AsyncQuery"; 37 | private static final boolean localLOGV = false; 38 | 39 | private static final int EVENT_ARG_QUERY = 1; 40 | private static final int EVENT_ARG_INSERT = 2; 41 | private static final int EVENT_ARG_UPDATE = 3; 42 | private static final int EVENT_ARG_DELETE = 4; 43 | private static final int EVENT_ARG_BULK_INSERT = 5; 44 | private static Looper sLooper = null; 45 | private final WeakReference mResolver; 46 | private Handler mWorkerThreadHandler; 47 | 48 | public AsyncQueryHandler(ContentResolver cr) { 49 | super(); 50 | mResolver = new WeakReference<>(cr); 51 | synchronized (AsyncQueryHandler.class) { 52 | if (sLooper == null) { 53 | HandlerThread thread = new HandlerThread("AsyncQueryWorker"); 54 | thread.start(); 55 | 56 | sLooper = thread.getLooper(); 57 | } 58 | } 59 | mWorkerThreadHandler = createHandler(sLooper); 60 | } 61 | 62 | protected Handler createHandler(Looper looper) { 63 | return new WorkerHandler(looper); 64 | } 65 | 66 | /** 67 | * This method begins an asynchronous query. When the query is done 68 | * {@link #onQueryComplete} is called. 69 | * 70 | * @param token A token passed into {@link #onQueryComplete} to identify 71 | * the query. 72 | * @param cookie An object that gets passed into {@link #onQueryComplete} 73 | * @param uri The URI, using the content:// scheme, for the content to 74 | * retrieve. 75 | * @param projection A list of which columns to return. Passing null will 76 | * return all columns, which is discouraged to prevent reading data 77 | * from storage that isn't going to be used. 78 | * @param selection A filter declaring which rows to return, formatted as an 79 | * SQL WHERE clause (excluding the WHERE itself). Passing null will 80 | * return all rows for the given URI. 81 | * @param selectionArgs You may include ?s in selection, which will be 82 | * replaced by the values from selectionArgs, in the order that they 83 | * appear in the selection. The values will be bound as Strings. 84 | * @param orderBy How to order the rows, formatted as an SQL ORDER BY 85 | * clause (excluding the ORDER BY itself). Passing null will use the 86 | * default sort order, which may be unordered. 87 | */ 88 | public void startQuery(int token, Object cookie, Uri uri, 89 | String[] projection, String selection, String[] selectionArgs, 90 | String orderBy) { 91 | // Use the token as what so cancelOperations works properly 92 | Message msg = mWorkerThreadHandler.obtainMessage(token); 93 | msg.arg1 = EVENT_ARG_QUERY; 94 | 95 | WorkerArgs args = new WorkerArgs(); 96 | args.handler = this; 97 | args.uri = uri; 98 | args.projection = projection; 99 | args.selection = selection; 100 | args.selectionArgs = selectionArgs; 101 | args.orderBy = orderBy; 102 | args.cookie = cookie; 103 | msg.obj = args; 104 | 105 | mWorkerThreadHandler.sendMessage(msg); 106 | } 107 | 108 | /** 109 | * Attempts to cancel operation that has not already started. Note that 110 | * there is no guarantee that the operation will be canceled. They still may 111 | * result in a call to on[Query/Insert/Update/Delete]Complete after this 112 | * call has completed. 113 | * 114 | * @param token The token representing the operation to be canceled. 115 | * If multiple operations have the same token they will all be canceled. 116 | */ 117 | public final void cancelOperation(int token) { 118 | mWorkerThreadHandler.removeMessages(token); 119 | } 120 | 121 | /** 122 | * This method begins an asynchronous insert. When the insert operation is 123 | * done {@link #onInsertComplete} is called. 124 | * 125 | * @param token A token passed into {@link #onInsertComplete} to identify 126 | * the insert operation. 127 | * @param cookie An object that gets passed into {@link #onInsertComplete} 128 | * @param uri the Uri passed to the insert operation. 129 | * @param initialValues the ContentValues parameter passed to the insert operation. 130 | */ 131 | public final void startInsert(int token, Object cookie, Uri uri, 132 | ContentValues initialValues) { 133 | // Use the token as what so cancelOperations works properly 134 | Message msg = mWorkerThreadHandler.obtainMessage(token); 135 | msg.arg1 = EVENT_ARG_INSERT; 136 | 137 | WorkerArgs args = new WorkerArgs(); 138 | args.handler = this; 139 | args.uri = uri; 140 | args.cookie = cookie; 141 | args.values = initialValues; 142 | msg.obj = args; 143 | 144 | mWorkerThreadHandler.sendMessage(msg); 145 | } 146 | 147 | /** 148 | * This method begins an asynchronous bulk insert. When the insert operation is 149 | * done {@link #onBulkInsertComplete} is called. 150 | * 151 | * @param token A token passed into {@link #onBulkInsertComplete} to identify 152 | * the insert operation. 153 | * @param cookie An object that gets passed into {@link #onBulkInsertComplete} 154 | * @param uri the Uri passed to the insert operation. 155 | * @param initialValues the ContentValues array parameter passed to the insert operation. 156 | */ 157 | public final void startBulkInsert(int token, Object cookie, Uri uri, 158 | ContentValues[] initialValues) { 159 | // Use the token as what so cancelOperations works properly 160 | Message msg = mWorkerThreadHandler.obtainMessage(token); 161 | msg.arg1 = EVENT_ARG_BULK_INSERT; 162 | 163 | WorkerArgs args = new WorkerArgs(); 164 | args.handler = this; 165 | args.uri = uri; 166 | args.cookie = cookie; 167 | args.valuesArray = initialValues; 168 | msg.obj = args; 169 | 170 | mWorkerThreadHandler.sendMessage(msg); 171 | } 172 | 173 | /** 174 | * This method begins an asynchronous update. When the update operation is 175 | * done {@link #onUpdateComplete} is called. 176 | * 177 | * @param token A token passed into {@link #onUpdateComplete} to identify 178 | * the update operation. 179 | * @param cookie An object that gets passed into {@link #onUpdateComplete} 180 | * @param uri the Uri passed to the update operation. 181 | * @param values the ContentValues parameter passed to the update operation. 182 | */ 183 | public final void startUpdate(int token, Object cookie, Uri uri, 184 | ContentValues values, String selection, String[] selectionArgs) { 185 | // Use the token as what so cancelOperations works properly 186 | Message msg = mWorkerThreadHandler.obtainMessage(token); 187 | msg.arg1 = EVENT_ARG_UPDATE; 188 | 189 | WorkerArgs args = new WorkerArgs(); 190 | args.handler = this; 191 | args.uri = uri; 192 | args.cookie = cookie; 193 | args.values = values; 194 | args.selection = selection; 195 | args.selectionArgs = selectionArgs; 196 | msg.obj = args; 197 | 198 | mWorkerThreadHandler.sendMessage(msg); 199 | } 200 | 201 | /** 202 | * This method begins an asynchronous delete. When the delete operation is 203 | * done {@link #onDeleteComplete} is called. 204 | * 205 | * @param token A token passed into {@link #onDeleteComplete} to identify 206 | * the delete operation. 207 | * @param cookie An object that gets passed into {@link #onDeleteComplete} 208 | * @param uri the Uri passed to the delete operation. 209 | * @param selection the where clause. 210 | */ 211 | public final void startDelete(int token, Object cookie, Uri uri, 212 | String selection, String[] selectionArgs) { 213 | // Use the token as what so cancelOperations works properly 214 | Message msg = mWorkerThreadHandler.obtainMessage(token); 215 | msg.arg1 = EVENT_ARG_DELETE; 216 | 217 | WorkerArgs args = new WorkerArgs(); 218 | args.handler = this; 219 | args.uri = uri; 220 | args.cookie = cookie; 221 | args.selection = selection; 222 | args.selectionArgs = selectionArgs; 223 | msg.obj = args; 224 | 225 | mWorkerThreadHandler.sendMessage(msg); 226 | } 227 | 228 | /** 229 | * Called when an asynchronous query is completed. 230 | * 231 | * @param token the token to identify the query, passed in from 232 | * {@link #startQuery}. 233 | * @param cookie the cookie object passed in from {@link #startQuery}. 234 | * @param cursor The cursor holding the results from the query. 235 | */ 236 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { 237 | // Empty 238 | } 239 | 240 | /** 241 | * Called when an asynchronous insert is completed. 242 | * 243 | * @param token the token to identify the query, passed in from 244 | * {@link #startInsert}. 245 | * @param cookie the cookie object that's passed in from 246 | * {@link #startInsert}. 247 | * @param uri the uri returned from the insert operation. 248 | */ 249 | protected void onInsertComplete(int token, Object cookie, Uri uri) { 250 | // Empty 251 | } 252 | 253 | /** 254 | * Called when an asynchronous insert is completed. 255 | * 256 | * @param token the token to identify the query, passed in from 257 | * {@link #startInsert}. 258 | * @param cookie the cookie object that's passed in from 259 | * {@link #startInsert}. 260 | * @param result the result returned from the bulk insert operation 261 | */ 262 | protected void onBulkInsertComplete(int token, Object cookie, int result) { 263 | // Empty 264 | } 265 | 266 | /** 267 | * Called when an asynchronous update is completed. 268 | * 269 | * @param token the token to identify the query, passed in from 270 | * {@link #startUpdate}. 271 | * @param cookie the cookie object that's passed in from 272 | * {@link #startUpdate}. 273 | * @param result the result returned from the update operation 274 | */ 275 | protected void onUpdateComplete(int token, Object cookie, int result) { 276 | // Empty 277 | } 278 | 279 | /** 280 | * Called when an asynchronous delete is completed. 281 | * 282 | * @param token the token to identify the query, passed in from 283 | * {@link #startDelete}. 284 | * @param cookie the cookie object that's passed in from 285 | * {@link #startDelete}. 286 | * @param result the result returned from the delete operation 287 | */ 288 | protected void onDeleteComplete(int token, Object cookie, int result) { 289 | // Empty 290 | } 291 | 292 | @Override 293 | public void handleMessage(Message msg) { 294 | WorkerArgs args = (WorkerArgs) msg.obj; 295 | 296 | if (localLOGV) { 297 | Log.d(TAG, "AsyncQueryHandler.handleMessage: msg.what=" + msg.what 298 | + ", msg.arg1=" + msg.arg1); 299 | } 300 | 301 | int token = msg.what; 302 | int event = msg.arg1; 303 | 304 | // pass token back to caller on each callback. 305 | switch (event) { 306 | case EVENT_ARG_QUERY: 307 | onQueryComplete(token, args.cookie, (Cursor) args.result); 308 | break; 309 | 310 | case EVENT_ARG_INSERT: 311 | onInsertComplete(token, args.cookie, (Uri) args.result); 312 | break; 313 | 314 | case EVENT_ARG_BULK_INSERT: 315 | onBulkInsertComplete(token, args.cookie, (Integer) args.result); 316 | break; 317 | 318 | case EVENT_ARG_UPDATE: 319 | onUpdateComplete(token, args.cookie, (Integer) args.result); 320 | break; 321 | 322 | case EVENT_ARG_DELETE: 323 | onDeleteComplete(token, args.cookie, (Integer) args.result); 324 | break; 325 | } 326 | } 327 | 328 | protected static final class WorkerArgs { 329 | public Uri uri; 330 | public Handler handler; 331 | public String[] projection; 332 | public String selection; 333 | public String[] selectionArgs; 334 | public String orderBy; 335 | public Object result; 336 | public Object cookie; 337 | public ContentValues values; 338 | public ContentValues[] valuesArray; 339 | } 340 | 341 | protected class WorkerHandler extends Handler { 342 | public WorkerHandler(Looper looper) { 343 | super(looper); 344 | } 345 | 346 | @Override 347 | public void handleMessage(Message msg) { 348 | final ContentResolver resolver = mResolver.get(); 349 | if (resolver == null) return; 350 | 351 | WorkerArgs args = (WorkerArgs) msg.obj; 352 | 353 | int token = msg.what; 354 | int event = msg.arg1; 355 | 356 | switch (event) { 357 | case EVENT_ARG_QUERY: 358 | Cursor cursor; 359 | try { 360 | cursor = resolver.query(args.uri, args.projection, 361 | args.selection, args.selectionArgs, 362 | args.orderBy); 363 | // Calling getCount() causes the cursor window to be filled, 364 | // which will make the first access on the main thread a lot faster. 365 | if (cursor != null) { 366 | cursor.getCount(); 367 | } 368 | } catch (Exception e) { 369 | Log.w(TAG, "Exception thrown during handling EVENT_ARG_QUERY", e); 370 | cursor = null; 371 | } 372 | 373 | args.result = cursor; 374 | break; 375 | 376 | case EVENT_ARG_INSERT: 377 | args.result = resolver.insert(args.uri, args.values); 378 | break; 379 | 380 | case EVENT_ARG_BULK_INSERT: 381 | args.result = resolver.bulkInsert(args.uri, args.valuesArray); 382 | break; 383 | 384 | case EVENT_ARG_UPDATE: 385 | args.result = resolver.update(args.uri, args.values, args.selection, 386 | args.selectionArgs); 387 | break; 388 | 389 | case EVENT_ARG_DELETE: 390 | args.result = resolver.delete(args.uri, args.selection, args.selectionArgs); 391 | break; 392 | } 393 | 394 | // passing the original token value back to the caller 395 | // on top of the event values in arg1. 396 | Message reply = args.handler.obtainMessage(token); 397 | reply.obj = args; 398 | reply.arg1 = msg.arg1; 399 | 400 | if (localLOGV) { 401 | Log.d(TAG, "WorkerHandler.handleMsg: msg.arg1=" + msg.arg1 402 | + ", reply.what=" + reply.what); 403 | } 404 | 405 | reply.sendToTarget(); 406 | } 407 | } 408 | } 409 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | maven { 7 | url "https://maven.google.com" 8 | } 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:2.3.2' 12 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 13 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | jcenter() 20 | maven { 21 | url "https://maven.google.com" 22 | } 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Madrapps/AsyncQuery/e82f7558ba2500498260db62e3ce43566c3365dd/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 16 14:58:48 IST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.madrapps.asyncqueryhandler" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | testCompile 'junit:junit:4.12' 29 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 30 | compile project(':asyncquery') 31 | } 32 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/tsaravana/Me/Programming/SDK/Android/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /sample/src/main/java/com/madrapps/asyncqueryhandler/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.madrapps.asyncqueryhandler; 2 | 3 | import android.content.ContentValues; 4 | import android.net.Uri; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.Log; 8 | import android.view.View; 9 | 10 | import com.madrapps.asyncqueryhandler.database.DatabaseHandler; 11 | 12 | import static com.madrapps.asyncqueryhandler.database.DatabaseHelper.Organization.COL_AGE; 13 | import static com.madrapps.asyncqueryhandler.database.DatabaseHelper.Organization.COL_NAME; 14 | import static com.madrapps.asyncqueryhandler.provider.Contract.AUTHORITY; 15 | import static com.madrapps.asyncqueryhandler.provider.Contract.ORGANIZATION; 16 | import static com.madrapps.asyncqueryhandler.provider.Contract.SCHEME; 17 | 18 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | 25 | initialize(); 26 | } 27 | 28 | private void initialize() { 29 | findViewById(R.id.btnInsert).setOnClickListener(this); 30 | findViewById(R.id.btnBulkInsert).setOnClickListener(this); 31 | } 32 | 33 | @Override 34 | public void onClick(View v) { 35 | switch (v.getId()) { 36 | case R.id.btnInsert: 37 | insertIntoTable(); 38 | break; 39 | case R.id.btnBulkInsert: 40 | bulkInsertIntoTable(); 41 | break; 42 | } 43 | } 44 | 45 | private void bulkInsertIntoTable() { 46 | Log.d("MainActivity", "Bulk Insert Started"); 47 | final DatabaseHandler handler = new DatabaseHandler(getContentResolver()); 48 | final Uri uri = new Uri.Builder().scheme(SCHEME).authority(AUTHORITY).appendEncodedPath(ORGANIZATION).build(); 49 | 50 | handler.startBulkInsert(1, null, uri, getContentValues()); 51 | 52 | Log.d("MainActivity", "Bulk Insert Ended"); 53 | } 54 | 55 | private void insertIntoTable() { 56 | Log.d("MainActivity", "Insert Started"); 57 | final DatabaseHandler handler = new DatabaseHandler(getContentResolver()); 58 | final Uri uri = new Uri.Builder().scheme(SCHEME).authority(AUTHORITY).appendEncodedPath(ORGANIZATION).build(); 59 | 60 | for (ContentValues initialValues : getContentValues()) { 61 | handler.startInsert(1, null, uri, initialValues); 62 | } 63 | } 64 | 65 | private ContentValues[] getContentValues() { 66 | final int size = 2000; 67 | final ContentValues[] values = new ContentValues[size]; 68 | for (int i = 0; i < size; i++) { 69 | final ContentValues contentValues = new ContentValues(); 70 | contentValues.put(COL_NAME, "John" + String.valueOf(i)); 71 | contentValues.put(COL_AGE, i + 20); 72 | values[i] = contentValues; 73 | } 74 | return values; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /sample/src/main/java/com/madrapps/asyncqueryhandler/database/DatabaseHandler.java: -------------------------------------------------------------------------------- 1 | package com.madrapps.asyncqueryhandler.database; 2 | 3 | import android.content.ContentResolver; 4 | import android.net.Uri; 5 | import android.util.Log; 6 | 7 | import com.madrapps.asyncquery.AsyncQueryHandler; 8 | 9 | public class DatabaseHandler extends AsyncQueryHandler { 10 | 11 | public DatabaseHandler(ContentResolver cr) { 12 | super(cr); 13 | } 14 | 15 | @Override 16 | protected void onInsertComplete(int token, Object cookie, Uri uri) { 17 | super.onInsertComplete(token, cookie, uri); 18 | Log.d("DatabaseHandler", "Insert Done"); 19 | } 20 | 21 | @Override 22 | protected void onBulkInsertComplete(int token, Object cookie, int result) { 23 | super.onBulkInsertComplete(token, cookie, result); 24 | Log.d("DatabaseHandler", "Bulk Insert Done"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/java/com/madrapps/asyncqueryhandler/database/DatabaseHelper.java: -------------------------------------------------------------------------------- 1 | package com.madrapps.asyncqueryhandler.database; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteOpenHelper; 6 | 7 | import java.util.Locale; 8 | 9 | import static com.madrapps.asyncqueryhandler.database.DatabaseHelper.Organization.COL_AGE; 10 | import static com.madrapps.asyncqueryhandler.database.DatabaseHelper.Organization.COL_NAME; 11 | import static com.madrapps.asyncqueryhandler.database.DatabaseHelper.Organization.TABLE_NAME; 12 | 13 | public class DatabaseHelper extends SQLiteOpenHelper { 14 | 15 | public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 16 | super(context, name, factory, version); 17 | } 18 | 19 | @Override 20 | public void onCreate(SQLiteDatabase db) { 21 | db.execSQL(String.format(Locale.US, "CREATE TABLE %s (_id INTEGER PRIMARY KEY AUTOINCREMENT, %s text not null, %s INTEGER not null)", TABLE_NAME, COL_NAME, COL_AGE)); 22 | } 23 | 24 | @Override 25 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 26 | 27 | } 28 | 29 | public class Organization { 30 | public static final String TABLE_NAME = "organization"; 31 | public static final String COL_NAME = "name"; 32 | public static final String COL_AGE = "age"; 33 | 34 | private Organization() {/* Do not allow instantiation */} 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /sample/src/main/java/com/madrapps/asyncqueryhandler/provider/Contract.java: -------------------------------------------------------------------------------- 1 | package com.madrapps.asyncqueryhandler.provider; 2 | 3 | public class Contract { 4 | public static final String SCHEME = "content"; 5 | public static final String AUTHORITY = "com.madrapps.provider"; 6 | public static final String ORGANIZATION = "organization"; 7 | 8 | private Contract() {/* Do not allow instantiation */} 9 | } 10 | -------------------------------------------------------------------------------- /sample/src/main/java/com/madrapps/asyncqueryhandler/provider/DataProvider.java: -------------------------------------------------------------------------------- 1 | package com.madrapps.asyncqueryhandler.provider; 2 | 3 | import android.content.ContentProvider; 4 | import android.content.ContentValues; 5 | import android.content.UriMatcher; 6 | import android.database.Cursor; 7 | import android.database.sqlite.SQLiteDatabase; 8 | import android.net.Uri; 9 | import android.support.annotation.NonNull; 10 | import android.support.annotation.Nullable; 11 | import android.util.Log; 12 | 13 | import com.madrapps.asyncqueryhandler.database.DatabaseHelper; 14 | 15 | import static com.madrapps.asyncqueryhandler.database.DatabaseHelper.Organization.TABLE_NAME; 16 | import static com.madrapps.asyncqueryhandler.provider.Contract.AUTHORITY; 17 | import static com.madrapps.asyncqueryhandler.provider.Contract.ORGANIZATION; 18 | 19 | public class DataProvider extends ContentProvider { 20 | 21 | private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 22 | private static final int ORGANIZATION_URI = 1; 23 | 24 | static { 25 | uriMatcher.addURI(AUTHORITY, ORGANIZATION, ORGANIZATION_URI); 26 | } 27 | 28 | private SQLiteDatabase database; 29 | private DatabaseHelper sqlLiteHelper; 30 | 31 | @Override 32 | public boolean onCreate() { 33 | sqlLiteHelper = new DatabaseHelper(this.getContext(), "sample.db", null, 1); 34 | return true; 35 | } 36 | 37 | @Nullable 38 | @Override 39 | public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { 40 | throw new UnsupportedOperationException(); 41 | } 42 | 43 | @Nullable 44 | @Override 45 | public String getType(@NonNull Uri uri) { 46 | throw new UnsupportedOperationException(); 47 | } 48 | 49 | @Nullable 50 | @Override 51 | public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { 52 | Log.d("DataProvider", "Insert: " + uri); 53 | final int match = uriMatcher.match(uri); 54 | if (match == ORGANIZATION_URI) { 55 | getDatabase().beginTransaction(); 56 | getDatabase().insert(TABLE_NAME, null, values); 57 | getDatabase().setTransactionSuccessful(); 58 | getDatabase().endTransaction(); 59 | } 60 | return null; 61 | } 62 | 63 | @Override 64 | public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { 65 | throw new UnsupportedOperationException(); 66 | } 67 | 68 | @Override 69 | public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { 70 | throw new UnsupportedOperationException(); 71 | } 72 | 73 | @Override 74 | public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) { 75 | Log.d("DataProvider", "Insert: " + uri); 76 | final int match = uriMatcher.match(uri); 77 | if (match == ORGANIZATION_URI) { 78 | return insertInBulk(getDatabase(), TABLE_NAME, values); 79 | } 80 | return 0; 81 | } 82 | 83 | private int insertInBulk(SQLiteDatabase database, String tableName, ContentValues[] values) { 84 | database.beginTransaction(); 85 | 86 | for (ContentValues value : values) { 87 | database.insertOrThrow(tableName, null, value); 88 | } 89 | 90 | database.setTransactionSuccessful(); 91 | database.endTransaction(); 92 | return values.length; 93 | } 94 | 95 | @NonNull 96 | private SQLiteDatabase getDatabase() { 97 | if (database == null) { 98 | database = sqlLiteHelper.getWritableDatabase(); 99 | } 100 | return database; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 |