├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── checkstyle.xml ├── diff └── handleShow25vs26.png ├── findbugs.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── checkstyle.gradle ├── gradle-mvn-push.gradle ├── gradle.properties ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── me │ └── drakeet │ └── support │ └── toast │ ├── BadTokenListener.java │ ├── SafeToastContext.java │ └── ToastCompat.java ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── me │ │ └── drakeet │ │ └── toast │ │ └── sample │ │ └── MainActivityTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── me │ │ └── drakeet │ │ └── toast │ │ └── sample │ │ └── MainActivity.java │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.ap_ 3 | 4 | # files for the dex VM 5 | *.dex 6 | 7 | # Java class files 8 | *.class 9 | 10 | # generated files 11 | bin/ 12 | gen/ 13 | 14 | # Local configuration file (sdk path, etc) 15 | local.properties 16 | 17 | # Proguard folder generated by Eclipse 18 | proguard/ 19 | 20 | # Ignore gradle files 21 | .gradle/ 22 | build/ 23 | 24 | # Eclipse project files 25 | .classpath 26 | .project 27 | .settings/ 28 | 29 | # Intellij project files 30 | *.iml 31 | *.ipr 32 | *.iws 33 | .idea/ 34 | 35 | # Mac system files 36 | .DS_Store 37 | 38 | *.keystore -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | jdk: oraclejdk8 3 | 4 | env: 5 | global: 6 | - ANDROID_BUILD_TOOLS_VERSION=27.0.3 7 | - ANDROID_ABI=armeabi-v7a 8 | 9 | android: 10 | components: 11 | - tools 12 | - build-tools-$ANDROID_BUILD_TOOLS_VERSION 13 | - android-22 14 | - android-27 15 | - sys-img-armeabi-v7a-android-22 16 | - extra-android-m2repository 17 | - extra-android-support 18 | - extra 19 | licenses: 20 | - android-sdk-license-.+ 21 | 22 | before_install: 23 | - mkdir "$ANDROID_HOME/licenses" || true 24 | - echo "d56f5187479451eabf01fb78af6dfcb131a6481e" > "$ANDROID_HOME/licenses/android-sdk-license" 25 | 26 | before_script: 27 | - chmod +x gradlew 28 | - echo no | android create avd --force -n test -t android-22 --abi $ANDROID_ABI 29 | - emulator -avd test -no-window & 30 | - android-wait-for-emulator 31 | - adb shell input keyevent 82 & 32 | 33 | script: 34 | - ./gradlew clean connectedAndroidTest 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ToastCompat 2 | 3 | An Android library to hook and fix Toast **BadTokenException** 4 | 5 | [![Build Status](https://travis-ci.org/drakeet/ToastCompat.svg)](https://travis-ci.org/drakeet/ToastCompat) 6 | ![maven-central](https://img.shields.io/maven-central/v/me.drakeet.support/toastcompat.svg) 7 | 8 | ### Usage 9 | 10 | ```groovy 11 | implementation 'me.drakeet.support:toastcompat:1.1.0' 12 | ``` 13 | 14 | ```java 15 | ToastCompat.makeText(context, "hello world!", Toast.LENGTH_SHORT).show(); 16 | ``` 17 | 18 | Or with `BadTokenListener#onBadTokenCaught(@NonNull Toast toast)`: 19 | 20 | ```java 21 | ToastCompat.makeText(this, "hello", Toast.LENGTH_SHORT) 22 | .setBadTokenListener(toast -> { 23 | ... 24 | }).show(); 25 | ``` 26 | 27 | Goodbye, BadTokenException. 28 | 29 | ### Why 30 | 31 | From API 25, Android added a new param `IBinder windowToken` for `Toast#handleShow()`, and It brought an exception. 32 | As Android said on API 26: 33 | 34 | ```java 35 | // Since the notification manager service cancels the token right 36 | // after it notifies us to cancel the toast there is an inherent 37 | // race and we may attempt to add a window after the token has been 38 | // invalidated. Let us hedge against that. 39 | ``` 40 | 41 | So they try-catch the `mWM.addView(mView, mParams)` on API 26. **However, API 25 is still at risk. Our applications will continue to produce such an exception, and can not capture it**: 42 | 43 | ```java 44 | Fatal Exception: android.view.WindowManager$BadTokenException: 45 | Unable to add window -- token android.os.BinderProxy@1c4411f is not valid; is your activity running? 46 | at android.view.ViewRootImpl.setView(ViewRootImpl.java:679) 47 | at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) 48 | at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94) 49 | at android.widget.Toast$TN.handleShow(Toast.java:459) 50 | at android.widget.Toast$TN$2.handleMessage(Toast.java:342) 51 | at android.os.Handler.dispatchMessage(Handler.java:102) 52 | at android.os.Looper.loop(Looper.java:154) 53 | at android.app.ActivityThread.main(ActivityThread.java:6236) 54 | at java.lang.reflect.Method.invoke(Method.java) 55 | at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) 56 | at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 57 | ``` 58 | 59 | This exception occurs regardless of whether the `Context` you passed to `Toast` is an `Activity` or `ApplicationContext` or `Service`. And you can not `try-catch` it. 60 | 61 | See the detail diff of **Android Toast sources**: 62 | 63 | ![handleShow25vs26.png](diff/handleShow25vs26.png) 64 | 65 | ### How 66 | 67 | So I created this library, and replace the base Context to a `SafeToastContext`, it will hook the `WindowManagerWrapper.addView(view, params)` method and fix the exception. 68 | 69 | 70 | License 71 | ------- 72 | 73 | Copyright 2017 drakeet. 74 | 75 | Licensed under the Apache License, Version 2.0 (the "License"); 76 | you may not use this file except in compliance with the License. 77 | You may obtain a copy of the License at 78 | 79 | http://www.apache.org/licenses/LICENSE-2.0 80 | 81 | Unless required by applicable law or agreed to in writing, software 82 | distributed under the License is distributed on an "AS IS" BASIS, 83 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 84 | See the License for the specific language governing permissions and 85 | limitations under the License. 86 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | 3 | repositories { 4 | google() 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:3.1.1' 9 | } 10 | } 11 | 12 | allprojects { 13 | repositories { 14 | google() 15 | jcenter() 16 | } 17 | } 18 | 19 | task clean(type: Delete) { 20 | delete rootProject.buildDir 21 | } 22 | -------------------------------------------------------------------------------- /checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 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 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /diff/handleShow25vs26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/diff/handleShow25vs26.png -------------------------------------------------------------------------------- /findbugs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Dec 08 20:35:40 CST 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-4.7-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 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'jacoco' 3 | apply from: 'gradle-mvn-push.gradle' 4 | apply from: 'checkstyle.gradle' 5 | 6 | 7 | android { 8 | compileSdkVersion gradle.compileSdkVersion 9 | 10 | defaultConfig { 11 | minSdkVersion gradle.minSdkVersion 12 | targetSdkVersion gradle.targetSdkVersion 13 | versionCode gradle.versionCode 14 | versionName gradle.versionName 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | testImplementation 'junit:junit:4.12' 26 | implementation 'com.android.support:support-annotations:' + gradle.supportLibraryVersion 27 | } 28 | -------------------------------------------------------------------------------- /library/checkstyle.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 drakeet. https://github.com/drakeet 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 | apply plugin: 'checkstyle' 17 | 18 | checkstyle { 19 | toolVersion = 8.2 20 | } 21 | 22 | task checkStyle(type: Checkstyle) { 23 | description = 'Runs checkStyle inspection against ToastCompat sourcesets.' 24 | group = 'Code Style' 25 | configFile = rootProject.file('checkstyle.xml') 26 | ignoreFailures = false 27 | showViolations = true 28 | classpath = files() 29 | source = 'src/main/java' 30 | } 31 | 32 | // check code style after project evaluation 33 | afterEvaluate { project -> getTasksByName('checkStyle', false)[0].execute() 34 | } 35 | -------------------------------------------------------------------------------- /library/gradle-mvn-push.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Chris Banes 3 | * Copyright 2016 drakeet 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | apply plugin: 'maven' 19 | apply plugin: 'signing' 20 | 21 | def isReleaseBuild() { 22 | return gradle.versionName.contains("SNAPSHOT") == false 23 | } 24 | 25 | def getReleaseRepositoryUrl() { 26 | return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL : "https://oss.sonatype.org/service/local/staging/deploy/maven2/" 27 | } 28 | 29 | def getSnapshotRepositoryUrl() { 30 | return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL : "https://oss.sonatype.org/content/repositories/snapshots/" 31 | } 32 | 33 | def getRepositoryUsername() { 34 | return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "" 35 | } 36 | 37 | def getRepositoryPassword() { 38 | return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "" 39 | } 40 | 41 | afterEvaluate { project -> 42 | uploadArchives { 43 | repositories { 44 | mavenDeployer { 45 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 46 | 47 | pom.groupId = GROUP 48 | pom.artifactId = POM_ARTIFACT_ID 49 | pom.version = gradle.versionName 50 | 51 | repository(url: getReleaseRepositoryUrl()) { 52 | authentication(userName: getRepositoryUsername(), 53 | password: getRepositoryPassword()) 54 | } 55 | snapshotRepository(url: getSnapshotRepositoryUrl()) { 56 | authentication(userName: getRepositoryUsername(), 57 | password: getRepositoryPassword()) 58 | } 59 | 60 | pom.project { 61 | name POM_NAME 62 | packaging POM_PACKAGING 63 | description POM_DESCRIPTION 64 | url POM_URL 65 | 66 | scm { 67 | url POM_SCM_URL 68 | connection POM_SCM_CONNECTION 69 | developerConnection POM_SCM_DEV_CONNECTION 70 | } 71 | 72 | licenses { 73 | license { 74 | name POM_LICENCE_NAME 75 | url POM_LICENCE_URL 76 | distribution POM_LICENCE_DIST 77 | } 78 | } 79 | 80 | developers { 81 | developer { 82 | id POM_DEVELOPER_ID 83 | name POM_DEVELOPER_NAME 84 | } 85 | } 86 | } 87 | } 88 | } 89 | } 90 | 91 | signing { 92 | required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") } 93 | sign configurations.archives 94 | } 95 | 96 | android.libraryVariants.all { variant -> 97 | task("${variant.name}Javadoc", type: Javadoc) { 98 | description "Generates Javadoc for $variant.name." 99 | source = variant.javaCompile.source 100 | classpath = files(variant.javaCompile.classpath.files, 101 | project.android.getBootClasspath()) 102 | exclude '**/BuildConfig.java' 103 | exclude '**/R.java' 104 | } 105 | } 106 | 107 | task androidJavadocs(type: Javadoc) { 108 | source = 'src/main/java,src/main/aidl' 109 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 110 | } 111 | 112 | task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { 113 | classifier = 'javadoc' 114 | from androidJavadocs.destinationDir 115 | } 116 | 117 | task androidSourcesJar(type: Jar) { 118 | classifier = 'sources' 119 | from android.sourceSets.main.java.sourceFiles 120 | } 121 | 122 | artifacts { 123 | archives androidSourcesJar 124 | archives androidJavadocsJar 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /library/gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2017 drakeet. https://github.com/drakeet 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 | POM_NAME=ToastCompat 17 | POM_ARTIFACT_ID=toastcompat 18 | POM_PACKAGING=aar 19 | GROUP=me.drakeet.support 20 | POM_DESCRIPTION=An Android library to fix Toast BadTokenException 21 | POM_URL=https://github.com/drakeet/ToastCompat 22 | POM_SCM_URL=https://github.com/drakeet/ToastCompat 23 | POM_SCM_CONNECTION=scm:git@github.com:drakeet/ToastCompat.git 24 | POM_SCM_DEV_CONNECTION=scm:git@github.com:drakeet/ToastCompat.git 25 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 26 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 27 | POM_LICENCE_DIST=repo 28 | POM_DEVELOPER_ID=drakeet 29 | POM_DEVELOPER_NAME=drakeet 30 | POM_DEVELOPER_URL=https://github.com/drakeet 31 | -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/library/proguard-rules.pro -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /library/src/main/java/me/drakeet/support/toast/BadTokenListener.java: -------------------------------------------------------------------------------- 1 | package me.drakeet.support.toast; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.widget.Toast; 5 | 6 | /** 7 | * @author drakeet 8 | */ 9 | public interface BadTokenListener { 10 | 11 | void onBadTokenCaught(@NonNull Toast toast); 12 | } 13 | -------------------------------------------------------------------------------- /library/src/main/java/me/drakeet/support/toast/SafeToastContext.java: -------------------------------------------------------------------------------- 1 | package me.drakeet.support.toast; 2 | 3 | import android.content.Context; 4 | import android.content.ContextWrapper; 5 | import android.support.annotation.NonNull; 6 | import android.support.annotation.Nullable; 7 | import android.util.Log; 8 | import android.view.Display; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.view.WindowManager; 12 | import android.widget.Toast; 13 | 14 | /** 15 | * @author drakeet 16 | */ 17 | final class SafeToastContext extends ContextWrapper { 18 | 19 | private @NonNull Toast toast; 20 | 21 | private @Nullable BadTokenListener badTokenListener; 22 | 23 | 24 | SafeToastContext(@NonNull Context base, @NonNull Toast toast) { 25 | super(base); 26 | this.toast = toast; 27 | } 28 | 29 | 30 | @Override 31 | public Context getApplicationContext() { 32 | return new ApplicationContextWrapper(getBaseContext().getApplicationContext()); 33 | } 34 | 35 | 36 | public void setBadTokenListener(@NonNull BadTokenListener badTokenListener) { 37 | this.badTokenListener = badTokenListener; 38 | } 39 | 40 | 41 | private final class ApplicationContextWrapper extends ContextWrapper { 42 | 43 | private ApplicationContextWrapper(@NonNull Context base) { 44 | super(base); 45 | } 46 | 47 | 48 | @Override 49 | public Object getSystemService(@NonNull String name) { 50 | if (Context.WINDOW_SERVICE.equals(name)) { 51 | // noinspection ConstantConditions 52 | return new WindowManagerWrapper((WindowManager) getBaseContext().getSystemService(name)); 53 | } 54 | return super.getSystemService(name); 55 | } 56 | } 57 | 58 | 59 | private final class WindowManagerWrapper implements WindowManager { 60 | 61 | private static final String TAG = "WindowManagerWrapper"; 62 | private final @NonNull WindowManager base; 63 | 64 | 65 | private WindowManagerWrapper(@NonNull WindowManager base) { 66 | this.base = base; 67 | } 68 | 69 | 70 | @Override 71 | public Display getDefaultDisplay() { 72 | return base.getDefaultDisplay(); 73 | } 74 | 75 | 76 | @Override 77 | public void removeViewImmediate(View view) { 78 | base.removeViewImmediate(view); 79 | } 80 | 81 | 82 | @Override 83 | public void addView(View view, ViewGroup.LayoutParams params) { 84 | try { 85 | Log.d(TAG, "WindowManager's addView(view, params) has been hooked."); 86 | base.addView(view, params); 87 | } catch (BadTokenException e) { 88 | Log.i(TAG, e.getMessage()); 89 | if (badTokenListener != null) { 90 | badTokenListener.onBadTokenCaught(toast); 91 | } 92 | } catch (Throwable throwable) { 93 | Log.e(TAG, "[addView]", throwable); 94 | } 95 | } 96 | 97 | 98 | @Override 99 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { 100 | base.updateViewLayout(view, params); 101 | } 102 | 103 | 104 | @Override 105 | public void removeView(View view) { 106 | base.removeView(view); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /library/src/main/java/me/drakeet/support/toast/ToastCompat.java: -------------------------------------------------------------------------------- 1 | package me.drakeet.support.toast; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Activity; 5 | import android.app.Application; 6 | import android.content.Context; 7 | import android.content.res.Resources; 8 | import android.os.Build; 9 | import android.support.annotation.NonNull; 10 | import android.support.annotation.StringRes; 11 | import android.view.View; 12 | import android.widget.Toast; 13 | import java.lang.reflect.Field; 14 | 15 | /** 16 | * @author drakeet 17 | */ 18 | public final class ToastCompat extends Toast { 19 | 20 | private final @NonNull Toast toast; 21 | 22 | 23 | /** 24 | * Construct an empty Toast object. You must call {@link #setView} before you 25 | * can call {@link #show}. 26 | * 27 | * @param context The context to use. Usually your {@link Application} 28 | * or {@link Activity} object. 29 | * @param base The base toast 30 | */ 31 | private ToastCompat(Context context, @NonNull Toast base) { 32 | super(context); 33 | this.toast = base; 34 | } 35 | 36 | 37 | /** 38 | * Make a standard toast that just contains a text view. 39 | * 40 | * @param context The context to use. Usually your {@link android.app.Application} 41 | * or {@link android.app.Activity} object. 42 | * @param text The text to show. Can be formatted text. 43 | * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or 44 | * {@link #LENGTH_LONG} 45 | */ 46 | public static ToastCompat makeText(Context context, CharSequence text, int duration) { 47 | // We cannot pass the SafeToastContext to Toast.makeText() because 48 | // the View will unwrap the base context and we are in vain. 49 | @SuppressLint("ShowToast") 50 | Toast toast = Toast.makeText(context, text, duration); 51 | setContextCompat(toast.getView(), new SafeToastContext(context, toast)); 52 | return new ToastCompat(context, toast); 53 | } 54 | 55 | 56 | /** 57 | * Make a standard toast that just contains a text view with the text from a resource. 58 | * 59 | * @param context The context to use. Usually your {@link android.app.Application} 60 | * or {@link android.app.Activity} object. 61 | * @param resId The resource id of the string resource to use. Can be formatted text. 62 | * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or 63 | * {@link #LENGTH_LONG} 64 | * @throws Resources.NotFoundException if the resource can't be found. 65 | */ 66 | public static Toast makeText(Context context, @StringRes int resId, int duration) 67 | throws Resources.NotFoundException { 68 | return makeText(context, context.getResources().getText(resId), duration); 69 | } 70 | 71 | 72 | public @NonNull ToastCompat setBadTokenListener(@NonNull BadTokenListener listener) { 73 | final Context context = getView().getContext(); 74 | if (context instanceof SafeToastContext) { 75 | ((SafeToastContext) context).setBadTokenListener(listener); 76 | } 77 | return this; 78 | } 79 | 80 | 81 | @Override 82 | public void show() { 83 | toast.show(); 84 | } 85 | 86 | 87 | @Override 88 | public void setDuration(int duration) { 89 | toast.setDuration(duration); 90 | } 91 | 92 | 93 | @Override 94 | public void setGravity(int gravity, int xOffset, int yOffset) { 95 | toast.setGravity(gravity, xOffset, yOffset); 96 | } 97 | 98 | 99 | @Override 100 | public void setMargin(float horizontalMargin, float verticalMargin) { 101 | toast.setMargin(horizontalMargin, verticalMargin); 102 | } 103 | 104 | 105 | @Override 106 | public void setText(int resId) { 107 | toast.setText(resId); 108 | } 109 | 110 | 111 | @Override 112 | public void setText(CharSequence s) { 113 | toast.setText(s); 114 | } 115 | 116 | 117 | @Override 118 | public void setView(View view) { 119 | toast.setView(view); 120 | setContextCompat(view, new SafeToastContext(view.getContext(), this)); 121 | } 122 | 123 | 124 | @Override 125 | public float getHorizontalMargin() { 126 | return toast.getHorizontalMargin(); 127 | } 128 | 129 | 130 | @Override 131 | public float getVerticalMargin() { 132 | return toast.getVerticalMargin(); 133 | } 134 | 135 | 136 | @Override 137 | public int getDuration() { 138 | return toast.getDuration(); 139 | } 140 | 141 | 142 | @Override 143 | public int getGravity() { 144 | return toast.getGravity(); 145 | } 146 | 147 | 148 | @Override 149 | public int getXOffset() { 150 | return toast.getXOffset(); 151 | } 152 | 153 | 154 | @Override 155 | public int getYOffset() { 156 | return toast.getYOffset(); 157 | } 158 | 159 | 160 | @Override 161 | public View getView() { 162 | return toast.getView(); 163 | } 164 | 165 | 166 | public @NonNull Toast getBaseToast() { 167 | return toast; 168 | } 169 | 170 | 171 | private static void setContextCompat(@NonNull View view, @NonNull Context context) { 172 | if (Build.VERSION.SDK_INT == 25) { 173 | try { 174 | Field field = View.class.getDeclaredField("mContext"); 175 | field.setAccessible(true); 176 | field.set(view, context); 177 | } catch (Throwable throwable) { 178 | throwable.printStackTrace(); 179 | } 180 | } 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion gradle.compileSdkVersion 5 | 6 | defaultConfig { 7 | applicationId "me.drakeet.toast.sample" 8 | minSdkVersion gradle.minSdkVersion 9 | targetSdkVersion gradle.targetSdkVersion 10 | versionCode gradle.versionCode 11 | versionName gradle.versionName 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | 22 | compileOptions { 23 | sourceCompatibility JavaVersion.VERSION_1_8 24 | targetCompatibility JavaVersion.VERSION_1_8 25 | } 26 | } 27 | 28 | 29 | dependencies { 30 | implementation project(':library') 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | implementation 'com.android.support:appcompat-v7:' + gradle.supportLibraryVersion 33 | testImplementation 'junit:junit:4.12' 34 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 35 | androidTestImplementation 'com.android.support.test:rules:1.0.2' 36 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 37 | } 38 | 39 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/me/drakeet/toast/sample/MainActivityTest.java: -------------------------------------------------------------------------------- 1 | package me.drakeet.toast.sample; 2 | 3 | import android.support.test.rule.ActivityTestRule; 4 | import android.support.test.runner.AndroidJUnit4; 5 | import android.test.suitebuilder.annotation.LargeTest; 6 | import org.junit.Rule; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static android.support.test.espresso.Espresso.onView; 11 | import static android.support.test.espresso.assertion.ViewAssertions.matches; 12 | import static android.support.test.espresso.matcher.RootMatchers.withDecorView; 13 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 14 | import static android.support.test.espresso.matcher.ViewMatchers.withText; 15 | import static org.hamcrest.Matchers.is; 16 | import static org.hamcrest.Matchers.not; 17 | 18 | @LargeTest 19 | @RunWith(AndroidJUnit4.class) 20 | public class MainActivityTest { 21 | 22 | @Rule 23 | public ActivityTestRule rule = new ActivityTestRule<>(MainActivity.class); 24 | 25 | 26 | @Test 27 | public void shouldShowToastWithHello() { 28 | onView(withText("hello")) 29 | .inRoot(withDecorView(not(is(rule.getActivity().getWindow().getDecorView())))) 30 | .check(matches(isDisplayed())); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/src/main/java/me/drakeet/toast/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package me.drakeet.toast.sample; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.util.Log; 6 | import android.widget.Toast; 7 | import me.drakeet.support.toast.ToastCompat; 8 | 9 | public class MainActivity extends AppCompatActivity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | ToastCompat.makeText(this, "hello", Toast.LENGTH_SHORT) 16 | .setBadTokenListener(toast -> { 17 | Log.e("failed toast", "hello"); 18 | }).show(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 8 | 13 | 14 | 20 | 23 | 26 | 27 | 28 | 29 | 35 | 36 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PureWriter/ToastCompat/79c629c2ca8b2d7803f6dc4a458971b5fa19e7f7/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ToastCompat 3 | 4 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 drakeet. https://github.com/drakeet 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 | include ':sample' 18 | include ':library' 19 | 20 | gradle.ext.supportLibraryVersion = '27.1.1' 21 | gradle.ext.versionCode = 5 22 | gradle.ext.versionName = "1.1.0" 23 | gradle.ext.compileSdkVersion = 27 24 | gradle.ext.minSdkVersion = 14 25 | gradle.ext.targetSdkVersion = 27 26 | --------------------------------------------------------------------------------