├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── hdasync-sample ├── .gitignore ├── .idea │ ├── .name │ ├── compiler.xml │ ├── copyright │ │ └── profiles_settings.xml │ ├── libraries │ │ └── gen.xml │ ├── misc.xml │ ├── modules.xml │ ├── vcs.xml │ └── workspace.xml ├── AndroidManifest.xml ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── hdasync │ │ └── sample │ │ ├── IOtherAsyncCallback.java │ │ ├── OtherAsyncAction.java │ │ ├── SampleActivity.java │ │ ├── SampleApplication.java │ │ ├── SecondActivity.java │ │ └── ThirdActivity.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── layout │ └── hdasync_sample_activity.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-v11 │ └── styles.xml │ ├── values-v14 │ └── styles.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── hdasync ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── hdasync │ │ ├── AsyncAction.java │ │ ├── AsyncActionGroup.java │ │ ├── AsyncCallable.java │ │ ├── AsyncCountDownAction.java │ │ ├── AsyncCountDownResult.java │ │ ├── AsyncResult.java │ │ ├── BaseAction.java │ │ ├── BaseResult.java │ │ ├── HandlerThreadFactory.java │ │ └── HdAsync.java │ └── res │ └── values │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | .gradletasknamecache 20 | 21 | # Local configuration file (sdk path, etc) 22 | local.properties 23 | 24 | # Proguard folder generated by Eclipse 25 | proguard/ 26 | 27 | # Log Files 28 | *.log 29 | 30 | .DS_Store 31 | 32 | #intellij files 33 | .idea/ 34 | 35 | out/ 36 | *.iml 37 | 38 | gradle.properties 39 | -------------------------------------------------------------------------------- /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 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HdAsync 2 | 3 | **An android asynchronous operation library** 4 | 5 | `HdAsync` is an android library to help asynchronous operation easily. 6 | 7 | * support each step of asynchronous operation flow bind to handler thread looper or thread pool. 8 | * support `then`, `both` and `delay` operation. 9 | * `append` can add other async operation to the current flow. 10 | * simple and clean 11 | 12 | 13 | ## For example 14 | 15 | #### Initialize a synchronous Activity 16 | 17 | ``` Java 18 | public class SampleActivity extends Activity { 19 | @Override 20 | protected void onCreate(final Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | 23 | HdAsync.with(this) 24 | .then(new HdAsyncAction(getMainLooper()) { 25 | @Override 26 | public HdAsyncResult call(Object args) { 27 | beforeInitAtMainThread(savedInstanceState); 28 | return doNext(true); 29 | } 30 | }) 31 | .both(2, new HdAsyncCountDownAction(getMainLooper()) { 32 | @Override 33 | public HdAsyncCountDownResult call(Object args) { 34 | initAtMainThread(); 35 | return doNextByCountDown(true); 36 | } 37 | }, new HdAsyncCountDownAction(backgroundPool) { 38 | @Override 39 | public HdAsyncCountDownResult call(Object args) { 40 | initAtBackgroundThread(); 41 | return doNextByCountDown(true); 42 | } 43 | }) 44 | .then(new HdAsyncAction(getMainLooper()) { 45 | @Override 46 | public HdAsyncResult call(Object args) { 47 | afterInitAtMainThread(); 48 | isInitFinish = true; 49 | return doNext(true); 50 | } 51 | }) 52 | .call(); 53 | } 54 | } 55 | 56 | ``` 57 | 58 | #### Async get data from db and render 59 | ``` java 60 | HdAsync asyncGetDataFromDb() { 61 | return HdAsync.with(this) 62 | .then(new HdAsyncAction(backgroundPool) { 63 | @Override 64 | public HdAsyncResult call(Object args) { 65 | Data data = getDataFromDb(); 66 | return doNext(true, data); 67 | } 68 | }); 69 | } 70 | 71 | void render() { 72 | HdAsync.with(this) 73 | .append(asyncGetDataFromDb()) 74 | .then(new HdAsyncAction(Looper.getMainLooper()) { 75 | @Override 76 | public HdAsyncResult call(Object args) { 77 | if ((Data) args) { 78 | onRender(data); 79 | } 80 | return doNext(false); 81 | } 82 | }) 83 | .call(); 84 | } 85 | ``` 86 | -------------------------------------------------------------------------------- /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 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.3.1' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true 19 | 20 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 21 11:34:03 PDT 2015 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-2.8-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 | -------------------------------------------------------------------------------- /hdasync-sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /hdasync-sample/.idea/.name: -------------------------------------------------------------------------------- 1 | hdasync-sample -------------------------------------------------------------------------------- /hdasync-sample/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 23 | -------------------------------------------------------------------------------- /hdasync-sample/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /hdasync-sample/.idea/libraries/gen.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /hdasync-sample/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | -------------------------------------------------------------------------------- /hdasync-sample/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /hdasync-sample/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /hdasync-sample/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 36 | 37 | 38 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 71 | 72 | 75 | 76 | 77 | 94 | 95 | 96 | 97 | true 98 | 99 | 100 | 105 | 106 | 107 | 108 | 109 | 110 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 147 | 148 | 151 | 152 | 153 | 154 | 157 | 158 | 161 | 162 | 163 | 164 | 167 | 168 | 171 | 172 | 175 | 176 | 179 | 180 | 181 | 182 | 185 | 186 | 189 | 190 | 191 | 192 | 195 | 196 | 199 | 200 | 203 | 204 | 205 | 206 | 209 | 210 | 213 | 214 | 217 | 218 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 252 | 253 | 254 | 272 | 273 | 274 | 293 | 294 | 300 | 301 | 302 | 315 | 316 | 317 | 334 | 335 | 356 | 369 | 370 | 379 | 383 | 384 | 385 | 392 | 395 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 482 | 489 | 490 | 491 | 492 | 493 | 494 | true 495 | 496 | 497 | 498 | 499 | 500 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 1429152743482 530 | 537 | 538 | 539 | 540 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 615 | 618 | 619 | 620 | 622 | 623 | 624 | 626 | 627 | 628 | 629 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 710 | 711 | 712 | 713 | 714 | 715 | Android 716 | 717 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 733 | 734 | 735 | 736 | 737 | 738 | Android API 21 Platform 739 | 740 | 745 | 746 | 747 | 748 | 749 | 750 | Android|hdasync 751 | 752 | 757 | 758 | 759 | 760 | 761 | 762 | Android API 21 Platform 763 | 764 | 769 | 770 | 771 | 772 | 773 | 774 | gen 775 | 776 | 781 | 782 | 783 | 784 | 785 | 786 | -------------------------------------------------------------------------------- /hdasync-sample/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 10 | 11 | 17 | 18 | 19 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /hdasync-sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "ccom.hdasync.sample" 9 | minSdkVersion 9 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | 15 | compileOptions { 16 | sourceCompatibility JavaVersion.VERSION_1_7 17 | targetCompatibility JavaVersion.VERSION_1_7 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | lintOptions { 27 | abortOnError false 28 | } 29 | } 30 | 31 | dependencies { 32 | compile fileTree(dir: 'libs', include: ['*.jar']) 33 | compile project(':hdasync') 34 | 35 | debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' 36 | releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' 37 | 38 | compile 'com.jakewharton:butterknife:7.0.1' 39 | } 40 | -------------------------------------------------------------------------------- /hdasync-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 C:/Android/android-sdk/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 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/java/com/hdasync/sample/IOtherAsyncCallback.java: -------------------------------------------------------------------------------- 1 | package com.hdasync.sample; 2 | 3 | /** 4 | * Created by scott on 15/4/15. 5 | */ 6 | interface IOtherAsyncCallback { 7 | void onSuccess(); 8 | } 9 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/java/com/hdasync/sample/OtherAsyncAction.java: -------------------------------------------------------------------------------- 1 | package com.hdasync.sample; 2 | 3 | import android.os.Looper; 4 | import android.util.Log; 5 | import com.hdasync.AsyncCallable; 6 | import com.hdasync.HdAsync; 7 | import com.hdasync.AsyncAction; 8 | import com.hdasync.AsyncResult; 9 | 10 | /** 11 | * Created by scott on 15/4/15. 12 | */ 13 | public class OtherAsyncAction { 14 | 15 | public static final String TAG = "OtherAsyncAction"; 16 | 17 | public AsyncCallable test(final IOtherAsyncCallback otherAsyncCallback, Looper backgroundLooper) { 18 | return OtherAsyncAction.createAsyncCallable(this, otherAsyncCallback, backgroundLooper); 19 | } 20 | 21 | public static AsyncCallable createAsyncCallable(OtherAsyncAction host, final IOtherAsyncCallback otherAsyncCallback, Looper backgroundLooper) { 22 | return HdAsync.with(host) 23 | .then(new AsyncAction(backgroundLooper) { 24 | @Override 25 | public AsyncResult call(Object args) { 26 | Log.d(HdAsync.TAG, "OtherAsyncAction start"); 27 | 28 | if (getHost() != null) { 29 | Log.d(HdAsync.TAG, ((OtherAsyncAction) getHost()).TAG); 30 | } 31 | 32 | try { 33 | Thread.sleep(2000); 34 | } catch (InterruptedException e) { 35 | e.printStackTrace(); 36 | } 37 | otherAsyncCallback.onSuccess(); 38 | return doNext(true); 39 | } 40 | }); 41 | 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/java/com/hdasync/sample/SampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.hdasync.sample; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.graphics.Color; 6 | import android.os.Bundle; 7 | import android.os.Looper; 8 | import android.util.Log; 9 | import android.view.View; 10 | import android.widget.TextView; 11 | import butterknife.Bind; 12 | import butterknife.ButterKnife; 13 | import butterknife.OnClick; 14 | import com.hdasync.AsyncAction; 15 | import com.hdasync.AsyncCountDownAction; 16 | import com.hdasync.AsyncCountDownResult; 17 | import com.hdasync.AsyncResult; 18 | import com.hdasync.AsyncCallable; 19 | import com.hdasync.HandlerThreadFactory; 20 | import com.hdasync.HdAsync; 21 | 22 | import java.lang.ref.WeakReference; 23 | import java.util.concurrent.ExecutorService; 24 | import java.util.concurrent.Executors; 25 | 26 | /** 27 | * Created by scott on 15/3/26. 28 | */ 29 | public class SampleActivity extends Activity { 30 | 31 | @Bind(R.id.container) View container; 32 | @Bind(R.id.test) TextView testBtn; 33 | 34 | boolean isInitFinish = false; 35 | 36 | volatile HdAsync hdAsync; 37 | volatile AsyncCallable asyncCallable; 38 | 39 | 40 | static Looper backgroundLooper = HandlerThreadFactory.getLooper(HandlerThreadFactory.BackGroundThread); 41 | 42 | static ExecutorService backgroundPool = Executors.newFixedThreadPool(4); 43 | 44 | @Override 45 | protected void onCreate(final Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | 48 | asyncCallable = HdAsync.with(this) 49 | .then(new AsyncAction(getMainLooper()) { 50 | @Override 51 | public AsyncResult call(Object args) { 52 | beforeInitAtMainThread(savedInstanceState); 53 | return doNext(true); 54 | } 55 | }) 56 | .both(2, new AsyncCountDownAction(getMainLooper()) { 57 | @Override 58 | public AsyncCountDownResult call(Object args) { 59 | initAtMainThread(); 60 | return doNextByCountDown(true); 61 | } 62 | }, new AsyncCountDownAction(backgroundLooper) { 63 | @Override 64 | public AsyncCountDownResult call(Object args) { 65 | initAtBackgroundThread(); 66 | return doNextByCountDown(true); 67 | } 68 | }) 69 | .then(new AsyncAction(getMainLooper()) { 70 | @Override 71 | public AsyncResult call(Object args) { 72 | afterInitAtMainThread(); 73 | isInitFinish = true; 74 | return doNext(true); 75 | } 76 | }) 77 | .call(); 78 | } 79 | 80 | @Override 81 | protected void onResume() { 82 | super.onResume(); 83 | 84 | asyncCallable.then(new AsyncAction(Looper.getMainLooper()) { 85 | @Override 86 | public AsyncResult call(Object args) { 87 | resumeAtMainThread(); 88 | return doNext(true); 89 | } 90 | }).call(); 91 | 92 | } 93 | 94 | @Override 95 | protected void onDestroy() { 96 | super.onDestroy(); 97 | 98 | if (hdAsync != null) { 99 | asyncCallable.yield(); 100 | asyncCallable.destroy(); 101 | } 102 | } 103 | 104 | 105 | protected void beforeInitAtMainThread(Bundle savedInstanceState) { 106 | 107 | } 108 | 109 | protected void initAtMainThread() { 110 | initView(); 111 | } 112 | 113 | 114 | protected void initAtBackgroundThread() { 115 | initDatas(); 116 | } 117 | 118 | 119 | protected void afterInitAtMainThread() { 120 | //refresh 121 | } 122 | 123 | protected void activityResultAtMainThread(int requestCode, int resultCode, Intent data) { 124 | //onActivityResult 125 | } 126 | 127 | protected void resumeAtMainThread() { 128 | 129 | } 130 | 131 | 132 | private void initView() { 133 | setContentView(R.layout.hdasync_sample_activity); 134 | ButterKnife.bind(this); 135 | 136 | container.setBackgroundColor(Color.parseColor("#00B0FF")); 137 | testBtn.setBackgroundColor(Color.parseColor("#FF1744")); 138 | } 139 | 140 | 141 | private void initDatas() { 142 | } 143 | 144 | @OnClick(R.id.test) 145 | public void test() { 146 | asyncCallable = createTestAsyncCallable(this).call(); 147 | } 148 | 149 | @OnClick(R.id.container) 150 | public void onContainerClick() { 151 | Intent intent = new Intent(); 152 | intent.setClass(SampleActivity.this, SecondActivity.class); 153 | startActivity(intent); 154 | } 155 | 156 | public static AsyncCallable createTestAsyncCallable(SampleActivity host) { 157 | return HdAsync.with(host) 158 | .then(new AsyncAction(backgroundPool) { 159 | @Override 160 | public AsyncResult call(Object args) { 161 | Log.d(HdAsync.TAG, "1"); 162 | try { 163 | Thread.sleep(2000); 164 | } catch (InterruptedException e) { 165 | e.printStackTrace(); 166 | } 167 | 168 | return doNext(true, true); 169 | } 170 | }) 171 | .then(new AsyncAction(Looper.getMainLooper()) { 172 | @Override 173 | public AsyncResult call(Object args) { 174 | Log.d(HdAsync.TAG, "2"); 175 | if ((Boolean) args) { 176 | SampleActivity activity = (SampleActivity) getHost(); 177 | if (activity != null) { 178 | activity.container.setBackgroundColor(Color.TRANSPARENT); 179 | } 180 | } 181 | return doNext(true, false); 182 | } 183 | }) 184 | .then(new AsyncAction(Looper.getMainLooper()) { 185 | @Override 186 | public AsyncResult call(Object args) { 187 | Log.d(HdAsync.TAG, "3"); 188 | try { 189 | Thread.sleep(2000); 190 | } catch (InterruptedException e) { 191 | e.printStackTrace(); 192 | } 193 | return doNext(true, false); 194 | } 195 | }) 196 | .then(new AsyncAction(Looper.getMainLooper()) { 197 | @Override 198 | public AsyncResult call(Object args) { 199 | Log.d(HdAsync.TAG, "4"); 200 | if (!(Boolean) args) { 201 | SampleActivity activity = (SampleActivity) getHost(); 202 | if (activity != null) { 203 | activity.container.setBackgroundColor(Color.parseColor("#4CAF50")); 204 | } 205 | } 206 | return doNext(true); 207 | } 208 | }) 209 | .then(new AsyncAction(backgroundPool) { 210 | @Override 211 | public AsyncResult call(final Object args) { 212 | Log.d(HdAsync.TAG, "5"); 213 | 214 | SampleActivity activity = (SampleActivity) getHost(); 215 | if (activity != null && getCallable() != null) { 216 | activity.testWithOtherAsyncFunc(new OtherAsyncCallback(getCallable())); 217 | getCallable().yield(); 218 | } 219 | 220 | return doNext(false); 221 | } 222 | }) 223 | .both(new AsyncAction(Looper.getMainLooper()) { 224 | @Override 225 | public AsyncResult call(Object args) { 226 | Log.d(HdAsync.TAG, "6"); 227 | 228 | SampleActivity activity = (SampleActivity) getHost(); 229 | if (activity != null) { 230 | activity.testBtn.setText("" + args); 231 | } 232 | return doNext(false); 233 | } 234 | }, new AsyncAction(backgroundLooper) { 235 | @Override 236 | public AsyncResult call(Object args) { 237 | Log.d(HdAsync.TAG, "7"); 238 | 239 | return doNext(true); 240 | } 241 | }) 242 | .delay(new AsyncAction(Looper.getMainLooper()) { 243 | @Override 244 | public AsyncResult call(Object args) { 245 | Log.d(HdAsync.TAG, "8"); 246 | 247 | SampleActivity activity = (SampleActivity) getHost(); 248 | if (activity != null) { 249 | activity.testBtn.setText("finish"); 250 | } 251 | return doNext(false); 252 | } 253 | }, 1200); 254 | 255 | } 256 | 257 | public void testWithOtherAsyncFunc(final IOtherAsyncCallback otherAsyncCallback) { 258 | OtherAsyncAction otherAsyncAction = new OtherAsyncAction(); 259 | AsyncCallable otherAsyncCallbale = OtherAsyncAction.createAsyncCallable(otherAsyncAction, otherAsyncCallback, backgroundLooper); 260 | 261 | HdAsync.with(this) 262 | .then(new AsyncAction(backgroundLooper) { 263 | @Override 264 | public AsyncResult call(Object args) { 265 | Log.d(HdAsync.TAG, "testWithOtherAsyncFunc start"); 266 | 267 | try { 268 | Thread.sleep(2000); 269 | } catch (InterruptedException e) { 270 | e.printStackTrace(); 271 | } 272 | return doNext(true); 273 | } 274 | }) 275 | .append(otherAsyncCallbale) 276 | .call(); 277 | } 278 | 279 | 280 | static class OtherAsyncCallback implements IOtherAsyncCallback { 281 | WeakReference weakReference; 282 | 283 | public OtherAsyncCallback(AsyncCallable asyncCallable) { 284 | this.weakReference = new WeakReference<>(asyncCallable); 285 | } 286 | 287 | @Override 288 | public void onSuccess() { 289 | Log.d(HdAsync.TAG, "OtherAsyncCallback onSuccess1"); 290 | 291 | 292 | AsyncCallable asyncCallable = weakReference.get(); 293 | if (asyncCallable != null) { 294 | Log.d(HdAsync.TAG, "OtherAsyncCallback onSuccess2"); 295 | asyncCallable.resume(10); 296 | } 297 | } 298 | } 299 | 300 | 301 | } 302 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/java/com/hdasync/sample/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.hdasync.sample; 2 | 3 | import android.app.Application; 4 | import com.squareup.leakcanary.LeakCanary; 5 | 6 | /** 7 | * Created by scott on 16/1/6. 8 | */ 9 | public class SampleApplication extends Application { 10 | 11 | @Override 12 | public void onCreate() { 13 | super.onCreate(); 14 | LeakCanary.install(this); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/java/com/hdasync/sample/SecondActivity.java: -------------------------------------------------------------------------------- 1 | package com.hdasync.sample; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | /** 10 | * Created by scott on 16/1/6. 11 | */ 12 | public class SecondActivity extends Activity { 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | 18 | View v = new View(this); 19 | v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 20 | 21 | setContentView(v); 22 | 23 | v.setOnClickListener(onClickListener); 24 | } 25 | 26 | View.OnClickListener onClickListener = new View.OnClickListener() { 27 | @Override 28 | public void onClick(View v) { 29 | Intent intent = new Intent(); 30 | intent.setClass(SecondActivity.this, ThirdActivity.class); 31 | startActivity(intent); 32 | } 33 | }; 34 | } 35 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/java/com/hdasync/sample/ThirdActivity.java: -------------------------------------------------------------------------------- 1 | package com.hdasync.sample; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | 8 | /** 9 | * Created by scott on 16/1/6. 10 | */ 11 | public class ThirdActivity extends Activity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | 17 | View v = new View(this); 18 | v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 19 | 20 | setContentView(v); 21 | 22 | v.setOnClickListener(onClickListener); 23 | } 24 | 25 | View.OnClickListener onClickListener = new View.OnClickListener() { 26 | @Override 27 | public void onClick(View v) { 28 | 29 | } 30 | }; 31 | } 32 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/hdasync-sample/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/hdasync-sample/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/hdasync-sample/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/layout/hdasync_sample_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | 12 | 20 | 21 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/hdasync-sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/hdasync-sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/hdasync-sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/supercocoa/HdAsync/57d7b7383d6fb909f0574872abfe7c70a04b3679/hdasync-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | HdAsyncSample 4 | 5 | First 6 | Second 7 | Third 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /hdasync-sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /hdasync/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /hdasync/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | minSdkVersion 9 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | 14 | compileOptions { 15 | sourceCompatibility JavaVersion.VERSION_1_7 16 | targetCompatibility JavaVersion.VERSION_1_7 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | lintOptions { 26 | abortOnError false 27 | } 28 | } 29 | 30 | dependencies { 31 | compile fileTree(dir: 'libs', include: ['*.jar']) 32 | } 33 | -------------------------------------------------------------------------------- /hdasync/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 C:/Android/android-sdk/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 | -------------------------------------------------------------------------------- /hdasync/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/AsyncAction.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | import android.os.Looper; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | 7 | /** 8 | * Created by scott on 15/3/26. 9 | */ 10 | public abstract class AsyncAction extends BaseAction { 11 | 12 | 13 | public AsyncAction(Looper looper) { 14 | super(looper); 15 | } 16 | 17 | public AsyncAction(ExecutorService pool) { 18 | super(pool); 19 | } 20 | 21 | @Override 22 | public abstract AsyncResult call(Object args); 23 | 24 | public AsyncResult doNext(boolean needNext, Object value) { 25 | return new AsyncResult(needNext, value); 26 | } 27 | 28 | public AsyncResult doNext(boolean needNext) { 29 | return doNext(needNext, null); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/AsyncActionGroup.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | import java.util.LinkedList; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | /** 7 | * Created by scott on 15/3/26. 8 | */ 9 | public class AsyncActionGroup { 10 | 11 | public class ActionArray { 12 | BaseAction[] array; 13 | } 14 | 15 | private LinkedList actionList; 16 | private AtomicInteger actionCount; 17 | 18 | public AsyncActionGroup() { 19 | this.actionList = new LinkedList(); 20 | this.actionCount = new AtomicInteger(0); 21 | } 22 | 23 | 24 | public ActionArray poll() { 25 | if (!actionList.isEmpty()) { 26 | return actionList.poll(); 27 | } 28 | return null; 29 | } 30 | 31 | 32 | public void clear() { 33 | if (actionList != null) { 34 | actionList.clear(); 35 | } 36 | } 37 | 38 | public boolean allActionFinish() { 39 | boolean ret = false; 40 | if (actionCount.get() <= 0) { 41 | ret = true; 42 | } 43 | return ret; 44 | } 45 | 46 | public void finishOneAction() { 47 | actionCount.decrementAndGet(); 48 | } 49 | 50 | protected void then(final AsyncAction action) { 51 | actionCount.incrementAndGet(); 52 | 53 | ActionArray actionArray = new ActionArray(); 54 | actionArray.array = new AsyncAction[1]; 55 | actionArray.array[0] = action; 56 | 57 | actionList.add(actionArray); 58 | } 59 | 60 | protected void delay(final AsyncAction action, long delay) { 61 | actionCount.incrementAndGet(); 62 | action.delay = delay; 63 | 64 | ActionArray actionArray = new ActionArray(); 65 | actionArray.array = new AsyncAction[1]; 66 | actionArray.array[0] = action; 67 | 68 | actionList.add(actionArray); 69 | } 70 | 71 | protected void both(AsyncAction... actions) { 72 | ActionArray actionArray = new ActionArray(); 73 | actionArray.array = actions; 74 | 75 | actionCount.addAndGet(actions.length); 76 | actionList.add(actionArray); 77 | } 78 | 79 | protected void both(AsyncCountDownAction... actions) { 80 | ActionArray actionArray = new ActionArray(); 81 | actionArray.array = actions; 82 | 83 | actionCount.addAndGet(actions.length); 84 | actionList.add(actionArray); 85 | } 86 | 87 | protected void append(AsyncActionGroup group) { 88 | if (group != null) { 89 | actionCount.addAndGet(group.actionCount.get()); 90 | actionList.addAll(group.actionList); 91 | } 92 | } 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/AsyncCallable.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | import android.os.Handler; 4 | import android.os.Message; 5 | 6 | import java.util.concurrent.Executors; 7 | import java.util.concurrent.ScheduledExecutorService; 8 | import java.util.concurrent.TimeUnit; 9 | import java.util.concurrent.atomic.AtomicInteger; 10 | 11 | /** 12 | * Created by scott on 16/1/6. 13 | */ 14 | public class AsyncCallable { 15 | public static final String TAG = "AsyncCallable"; 16 | 17 | private AsyncActionGroup actionGroup; 18 | private Object host; 19 | 20 | private boolean isCalling = false; 21 | private boolean isYield = false; 22 | private boolean isDone = false; 23 | 24 | private static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); 25 | 26 | 27 | protected AsyncCallable(Object host) { 28 | this.host = host; 29 | actionGroup = new AsyncActionGroup(); 30 | } 31 | 32 | 33 | public synchronized AsyncCallable call() { 34 | isYield = false; 35 | if (!isCalling) { 36 | isCalling = true; 37 | executeActionWithoutLock(null, true); 38 | } 39 | 40 | return this; 41 | } 42 | 43 | public synchronized AsyncCallable call(Object args) { 44 | isYield = false; 45 | if (!isCalling) { 46 | isCalling = true; 47 | executeActionWithoutLock(args, true); 48 | } 49 | return this; 50 | } 51 | 52 | public AsyncCallable resume() { 53 | call(); 54 | return this; 55 | } 56 | 57 | public AsyncCallable resume(Object args) { 58 | call(args); 59 | return this; 60 | } 61 | 62 | public synchronized void yield() { 63 | isYield = true; 64 | isCalling = false; 65 | } 66 | 67 | public boolean isDone() { 68 | return isDone; 69 | } 70 | 71 | public synchronized void destroy() { 72 | destroyWithoutLock(); 73 | } 74 | 75 | public void destroyWithoutLock() { 76 | if (actionGroup != null) { 77 | actionGroup.clear(); 78 | } 79 | } 80 | 81 | public synchronized AsyncCallable then(final AsyncAction action) { 82 | if (action != null) { 83 | action.setHost(host); 84 | actionGroup.then(action); 85 | } 86 | 87 | return this; 88 | } 89 | 90 | public synchronized AsyncCallable delay(final AsyncAction action, long delay) { 91 | if (action != null) { 92 | action.setHost(host); 93 | actionGroup.delay(action, delay); 94 | } 95 | return this; 96 | } 97 | 98 | public synchronized AsyncCallable both(AsyncAction... actions) { 99 | if (actions != null) { 100 | for (AsyncAction action : actions) { 101 | action.setHost(host); 102 | } 103 | actionGroup.both(actions); 104 | } 105 | return this; 106 | } 107 | 108 | public synchronized AsyncCallable both(int countDownNum, AsyncCountDownAction... actions) { 109 | if (actions != null) { 110 | AtomicInteger atomicCountDownNum = new AtomicInteger(countDownNum); 111 | for (AsyncCountDownAction action : actions) { 112 | action.setHost(host); 113 | action.setCountDownNum(atomicCountDownNum); 114 | } 115 | } 116 | actionGroup.both(actions); 117 | return this; 118 | } 119 | 120 | public synchronized AsyncCallable append(AsyncCallable other) { 121 | if (other != null) { 122 | actionGroup.append(other.actionGroup); 123 | } 124 | return this; 125 | } 126 | 127 | 128 | static class Data { 129 | BaseAction action; 130 | Object args; 131 | } 132 | 133 | private synchronized void executeAction(Object args, boolean needNext) { 134 | executeActionWithoutLock(args, needNext); 135 | } 136 | 137 | private void executeActionWithoutLock(final Object args, boolean needNext) { 138 | if (actionGroup != null && !actionGroup.allActionFinish()) { 139 | 140 | if (!needNext || isYield) { 141 | return; 142 | } 143 | 144 | AsyncActionGroup.ActionArray actionArray = actionGroup.poll(); 145 | 146 | if (actionArray == null) { 147 | return; 148 | } 149 | 150 | BaseAction[] actions = actionArray.array; 151 | 152 | for (final BaseAction action : actions) { 153 | 154 | if (action.looper == null && action.pool == null) { 155 | continue; 156 | } 157 | 158 | action.setCallable(this); // setHdAsync in runtime 159 | 160 | final Data data = new Data(); 161 | data.action = action; 162 | data.args = args; 163 | 164 | if (action.looper != null) { 165 | executeByLooper(data); 166 | } else if (action.pool != null) { 167 | executeByPool(data); 168 | } 169 | } 170 | 171 | } else { 172 | isCalling = false; 173 | isDone = true; 174 | destroyWithoutLock(); 175 | } 176 | } 177 | 178 | private void executeByLooper(Data data) { 179 | Handler handler = new Handler(data.action.looper, 180 | new Handler.Callback() { 181 | @Override 182 | public boolean handleMessage(Message msg) { 183 | if (!(msg.obj instanceof Data)) { 184 | return false; 185 | } 186 | 187 | Data data = (Data) msg.obj; 188 | onActionExecute(data); 189 | return false; 190 | } 191 | }); 192 | 193 | Message msg = Message.obtain(); 194 | 195 | msg.obj = data; 196 | handler.sendMessageDelayed(msg, data.action.delay); 197 | } 198 | 199 | private void executeByPool(final Data data) { 200 | if (data.action.delay == 0) { 201 | submitToPool(data); 202 | } else { 203 | scheduleToPool(data); 204 | } 205 | } 206 | 207 | private void submitToPool(final Data data) { 208 | data.action.pool.submit(new Runnable() { 209 | @Override 210 | public void run() { 211 | onActionExecute(data); 212 | } 213 | }); 214 | } 215 | 216 | private void scheduleToPool(final Data data) { 217 | scheduledExecutorService.schedule(new Runnable() { 218 | @Override 219 | public void run() { 220 | submitToPool(data); 221 | } 222 | }, data.action.delay, TimeUnit.MILLISECONDS); 223 | } 224 | 225 | private void onActionExecute(Data data) { 226 | BaseResult result = data.action.call(data.args); 227 | actionGroup.finishOneAction(); 228 | if (result != null) { 229 | executeAction(result.value, result.needNext); 230 | } 231 | } 232 | 233 | } 234 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/AsyncCountDownAction.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | import android.os.Looper; 4 | 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | /** 9 | * Created by scott on 15/5/2. 10 | */ 11 | public abstract class AsyncCountDownAction extends BaseAction { 12 | 13 | private AtomicInteger countDownNum; 14 | 15 | public AsyncCountDownAction(Looper looper) { 16 | super(looper); 17 | } 18 | 19 | public AsyncCountDownAction(ExecutorService pool) { 20 | super(pool); 21 | } 22 | 23 | 24 | @Override 25 | public abstract AsyncCountDownResult call(Object args); 26 | 27 | public void setCountDownNum(AtomicInteger countDownNum) { 28 | this.countDownNum = countDownNum; 29 | } 30 | 31 | public AsyncCountDownResult doNextByCountDown(boolean needNext, Object value) { 32 | 33 | if (!needNext || countDownNum == null) { 34 | return new AsyncCountDownResult(false, value); 35 | } 36 | 37 | if (countDownNum.decrementAndGet() == 0) { 38 | return new AsyncCountDownResult(true, value); 39 | } else { 40 | return new AsyncCountDownResult(false, value); 41 | } 42 | } 43 | 44 | public AsyncCountDownResult doNextByCountDown(boolean needNext) { 45 | return doNextByCountDown(needNext, null); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/AsyncCountDownResult.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | /** 4 | * Created by scott on 15/5/2. 5 | */ 6 | public class AsyncCountDownResult extends BaseResult { 7 | 8 | protected AsyncCountDownResult(boolean needNext, Object value) { 9 | super(needNext, value); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/AsyncResult.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | /** 4 | * Created by scott on 15/4/12. 5 | */ 6 | public class AsyncResult extends BaseResult { 7 | 8 | protected AsyncResult(boolean needNext, Object value) { 9 | super(needNext, value); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/BaseAction.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | import android.os.Looper; 4 | 5 | import java.lang.ref.WeakReference; 6 | import java.util.concurrent.ExecutorService; 7 | 8 | /** 9 | * Created by scott on 15/5/2. 10 | */ 11 | abstract class BaseAction { 12 | protected Looper looper; 13 | 14 | protected ExecutorService pool; 15 | 16 | protected long delay = 0; 17 | 18 | private WeakReference weakHdAsync; 19 | private WeakReference weakCallable; 20 | 21 | private WeakReference weakHost; 22 | 23 | public BaseAction(Looper looper) { 24 | this.looper = looper; 25 | this.pool = null; 26 | } 27 | 28 | public BaseAction(ExecutorService pool) { 29 | this.pool = pool; 30 | this.looper = null; 31 | } 32 | 33 | public abstract BaseResult call(Object args); 34 | 35 | 36 | public void setHdAsync(HdAsync hdasync) { 37 | weakHdAsync = new WeakReference(hdasync); 38 | } 39 | 40 | public void setCallable(AsyncCallable asyncCallable) { 41 | weakCallable = new WeakReference(asyncCallable); 42 | } 43 | 44 | public HdAsync getHdAsync() { 45 | if (weakHdAsync != null) { 46 | return weakHdAsync.get(); 47 | } 48 | return null; 49 | } 50 | 51 | public AsyncCallable getCallable() { 52 | if (weakCallable != null) { 53 | return weakCallable.get(); 54 | } 55 | return null; 56 | } 57 | 58 | public void setHost(Object host) { 59 | weakHost = new WeakReference(host); 60 | } 61 | 62 | public Object getHost() { 63 | if (weakHost != null) { 64 | return weakHost.get(); 65 | } 66 | return null; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/BaseResult.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | /** 4 | * Created by scott on 15/5/2. 5 | */ 6 | class BaseResult { 7 | protected Object value; 8 | protected boolean needNext = false; 9 | 10 | protected BaseResult(boolean needNext, Object value) { 11 | this.needNext = needNext; 12 | this.value = value; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/HandlerThreadFactory.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | import android.os.HandlerThread; 4 | import android.os.Looper; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * Created by scott on 15/5/21. 11 | */ 12 | public class HandlerThreadFactory { 13 | 14 | public static final String ActivityInitThread = "ActivityInit_HandlerThread"; 15 | 16 | public static final String BackGroundThread = "BackGround_HandlerThread"; 17 | 18 | public static final String DBThread = "DB_HandlerThread"; 19 | 20 | public static final String IOThread = "IO_HandlerThread"; 21 | 22 | public static final String NetThread = "Net_HandlerThread"; 23 | 24 | private static Map handlerThreadMap = new HashMap(); 25 | 26 | public static HandlerThread getThread(String type) { 27 | HandlerThread handlerThread = handlerThreadMap.get(type); 28 | if (null == handlerThread) { 29 | handlerThread = new HandlerThread(type, getPriority(type)); 30 | handlerThread.start(); 31 | handlerThreadMap.put(type, handlerThread); 32 | 33 | } else { 34 | if (!handlerThread.isAlive()) { 35 | handlerThread.start(); 36 | } 37 | } 38 | return handlerThread; 39 | } 40 | 41 | public static HandlerThread getThread(String type, boolean isDaemon) { 42 | HandlerThread handlerThread = handlerThreadMap.get(type); 43 | if (null == handlerThread) { 44 | handlerThread = new HandlerThread(type, getPriority(type)); 45 | handlerThread.setDaemon(isDaemon); 46 | handlerThread.start(); 47 | handlerThreadMap.put(type, handlerThread); 48 | 49 | } else { 50 | if (!handlerThread.isAlive()) { 51 | handlerThread.start(); 52 | } 53 | } 54 | return handlerThread; 55 | } 56 | 57 | public static Looper getLooper(String type) { 58 | return getThread(type).getLooper(); 59 | } 60 | 61 | 62 | private static int getPriority(String type) { 63 | 64 | if (ActivityInitThread.equalsIgnoreCase(type)) { 65 | 66 | return android.os.Process.THREAD_PRIORITY_DEFAULT; 67 | 68 | } else if (BackGroundThread.equalsIgnoreCase(type)) { 69 | 70 | return android.os.Process.THREAD_PRIORITY_BACKGROUND; 71 | 72 | } else if (DBThread.equalsIgnoreCase(type)) { 73 | 74 | return android.os.Process.THREAD_PRIORITY_BACKGROUND; 75 | 76 | } else if (IOThread.equalsIgnoreCase(type)) { 77 | 78 | return android.os.Process.THREAD_PRIORITY_BACKGROUND; 79 | 80 | } else if (NetThread.equalsIgnoreCase(type)) { 81 | 82 | return android.os.Process.THREAD_PRIORITY_BACKGROUND; 83 | 84 | } else { 85 | return android.os.Process.THREAD_PRIORITY_BACKGROUND; 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /hdasync/src/main/java/com/hdasync/HdAsync.java: -------------------------------------------------------------------------------- 1 | package com.hdasync; 2 | 3 | /** 4 | * Created by scott on 15/3/26. 5 | */ 6 | public class HdAsync { 7 | 8 | public static final String TAG = "HdAsync"; 9 | 10 | 11 | public static AsyncCallable with(Object host) { 12 | return new AsyncCallable(host); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /hdasync/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | hdasync 3 | 4 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':hdasync-sample', ':hdasync', ':hdasync' 2 | --------------------------------------------------------------------------------