├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── PATENTS ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── build.gradle └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── parse │ │ │ └── twitter │ │ │ ├── AsyncCallback.java │ │ │ ├── OAuth1FlowDialog.java │ │ │ ├── OAuth1FlowException.java │ │ │ ├── ParseTwitterUtils.java │ │ │ ├── Twitter.java │ │ │ └── TwitterController.java │ └── res │ │ └── layout │ │ └── parse_twitter_dialog_login.xml │ └── test │ └── java │ └── com │ └── parse │ └── twitter │ ├── ParseTwitterUtilsTest.java │ └── TwitterControllerTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Android Studio 19 | .idea 20 | *.iml 21 | *.ipr 22 | *.iws 23 | classes 24 | gen-external-apklibs 25 | 26 | # Gradle 27 | .gradle 28 | build 29 | 30 | # Other 31 | .metadata 32 | */bin/* 33 | */gen/* 34 | testData 35 | testCache 36 | 37 | # Jacoco 38 | jacoco.exec 39 | 40 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | branches: 2 | only: 3 | - master 4 | - /^v4-\d+\.\d+\.\d+$/ # regex 5 | 6 | language: android 7 | 8 | jdk: 9 | - openjdk8 10 | 11 | before_install: 12 | - pip install --user codecov 13 | - mkdir "$ANDROID_HOME/licenses" || true 14 | - echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_HOME/licenses/android-sdk-license" 15 | 16 | script: 17 | - ./gradlew clean testDebugUnitTest jacocoTestReport --info 18 | 19 | after_success: 20 | - ./gradlew coveralls 21 | - codecov 22 | 23 | cache: 24 | directories: 25 | - $HOME/.gradle 26 | - $HOME/.m2/repository 27 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at codeofconduct@parseplatform.org. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD License 2 | 3 | For Parse Twitter Utils for Android software 4 | 5 | Copyright (c) 2015-present, Parse, LLC. All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without modification, 8 | are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | * Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | * Neither the name Parse nor the names of its contributors may be used to 18 | endorse or promote products derived from this software without specific 19 | prior written permission. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | ----- 33 | 34 | As of April 5, 2017, Parse, LLC has transferred this code to the parse-community organization, and will no longer be contributing to or distributing this code. 35 | -------------------------------------------------------------------------------- /PATENTS: -------------------------------------------------------------------------------- 1 | Additional Grant of Patent Rights Version 2 2 | 3 | "Software" means the Parse Twitter Utils for Android software distributed by Parse, LLC. 4 | 5 | Parse, LLC. ("Parse") hereby grants to each recipient of the Software 6 | ("you") a perpetual, worldwide, royalty-free, non-exclusive, irrevocable 7 | (subject to the termination provision below) license under any Necessary 8 | Claims, to make, have made, use, sell, offer to sell, import, and otherwise 9 | transfer the Software. For avoidance of doubt, no license is granted under 10 | Parse’s rights in any patent claims that are infringed by (i) modifications 11 | to the Software made by you or any third party or (ii) the Software in 12 | combination with any software or other technology. 13 | 14 | The license granted hereunder will terminate, automatically and without notice, 15 | if you (or any of your subsidiaries, corporate affiliates or agents) initiate 16 | directly or indirectly, or take a direct financial interest in, any Patent 17 | Assertion: (i) against Parse or any of its subsidiaries or corporate 18 | affiliates, (ii) against any party if such Patent Assertion arises in whole or 19 | in part from any software, technology, product or service of Parse or any of 20 | its subsidiaries or corporate affiliates, or (iii) against any party relating 21 | to the Software. Notwithstanding the foregoing, if Parse or any of its 22 | subsidiaries or corporate affiliates files a lawsuit alleging patent 23 | infringement against you in the first instance, and you respond by filing a 24 | patent infringement counterclaim in that lawsuit against that party that is 25 | unrelated to the Software, the license granted hereunder will not terminate 26 | under section (i) of this paragraph due to such counterclaim. 27 | 28 | A "Necessary Claim" is a claim of a patent owned by Parse that is 29 | necessarily infringed by the Software standing alone. 30 | 31 | A "Patent Assertion" is any lawsuit or other action alleging direct, indirect, 32 | or contributory infringement or inducement to infringe any patent, including a 33 | cross-claim or counterclaim. 34 | 35 | ----- 36 | 37 | As of April 5, 2017, Parse, LLC has transferred this code to the parse-community organization, and will no longer be contributing to or distributing this code. 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [ARCHIVE] Parse Twitter Utils for Android 2 | ## ParseTwitterUtils has moved to [Parse-SDK-Android](https://github.com/parse-community/Parse-SDK-Android) 3 | 4 | [![Build Status][build-status-svg]][build-status-link] 5 | [![License][license-svg]][license-link] 6 | [![](https://jitpack.io/v/parse-community/ParseTwitterUtils-Android.svg)](https://jitpack.io/#parse-community/ParseTwitterUtils-Android) 7 | [![Join The Conversation](https://img.shields.io/discourse/https/community.parseplatform.org/topics.svg)](https://community.parseplatform.org/c/parse-server) 8 | [![Backers on Open Collective](https://opencollective.com/parse-server/backers/badge.svg)](#backers) 9 | [![Sponsors on Open Collective](https://opencollective.com/parse-server/sponsors/badge.svg)](#sponsors) 10 | ![Twitter Follow](https://img.shields.io/twitter/follow/ParsePlatform.svg?label=Follow%20us%20on%20Twitter&style=social) 11 | 12 | A utility library to authenticate `ParseUser`s with Twitter. 13 | 14 | ## Dependency 15 | 16 | Add this in your root `build.gradle` file (**not** your module `build.gradle` file): 17 | 18 | ```gradle 19 | allprojects { 20 | repositories { 21 | ... 22 | maven { url "https://jitpack.io" } 23 | } 24 | } 25 | ``` 26 | 27 | Then, add the library to your project `build.gradle` 28 | ```gradle 29 | dependencies { 30 | implementation 'com.github.parse-community:ParseTwitterUtils-Android:latest.version.here' 31 | } 32 | ``` 33 | 34 | ## Usage 35 | Extensive docs can be found in the [guide][guide]. The basic steps are: 36 | ```java 37 | // in Application.onCreate(); or somewhere similar 38 | ParseTwitterUtils.initialize("YOUR CONSUMER KEY", "YOUR CONSUMER SECRET"); 39 | ``` 40 | Then later, when your user taps the login button: 41 | ```java 42 | ParseTwitterUtils.logIn(this, new LogInCallback() { 43 | @Override 44 | public void done(ParseUser user, ParseException err) { 45 | if (user == null) { 46 | Log.d("MyApp", "Uh oh. The user cancelled the Twitter login."); 47 | } else if (user.isNew()) { 48 | Log.d("MyApp", "User signed up and logged in through Twitter!"); 49 | } else { 50 | Log.d("MyApp", "User logged in through Twitter!"); 51 | } 52 | } 53 | }); 54 | ``` 55 | 56 | ## How Do I Contribute? 57 | We want to make contributing to this project as easy and transparent as possible. Please refer to the [Contribution Guidelines](https://github.com/parse-community/Parse-SDK-Android/blob/master/CONTRIBUTING.md). 58 | 59 | ## License 60 | Copyright (c) 2015-present, Parse, LLC. 61 | All rights reserved. 62 | 63 | This source code is licensed under the BSD-style license found in the 64 | LICENSE file in the root directory of this source tree. An additional grant 65 | of patent rights can be found in the PATENTS file in the same directory. 66 | 67 | As of April 5, 2017, Parse, LLC has transferred this code to the parse-community organization, and will no longer be contributing to or distributing this code. 68 | 69 | [guide]: https://docs.parseplatform.org/android/guide/#twitter-users 70 | 71 | [build-status-svg]: https://travis-ci.org/parse-community/ParseTwitterUtils-Android.svg?branch=master 72 | [build-status-link]: https://travis-ci.org/parse-community/ParseTwitterUtils-Android 73 | 74 | [license-svg]: https://img.shields.io/badge/license-BSD-lightgrey.svg 75 | [license-link]: https://github.com/parse-community/ParseTwitterUtils-Android/blob/master/LICENSE 76 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | google() 4 | jcenter() 5 | } 6 | dependencies { 7 | classpath "com.android.tools.build:gradle:3.5.2" 8 | classpath "org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.3" 9 | classpath "com.github.dcendents:android-maven-gradle-plugin:2.1" 10 | } 11 | } 12 | 13 | plugins { 14 | id "com.github.ben-manes.versions" version "0.27.0" 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | maven { url "https://jitpack.io" } 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/parse-community/ParseTwitterUtils-Android/41ad7e57bdabda8e57827e9beec22a40e38440ce/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin, switch paths to Windows format before running java 129 | if $cygwin ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=$((i+1)) 158 | done 159 | case $i in 160 | (0) set -- ;; 161 | (1) set -- "$args0" ;; 162 | (2) set -- "$args0" "$args1" ;; 163 | (3) set -- "$args0" "$args1" "$args2" ;; 164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=$(save "$@") 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 185 | cd "$(dirname "$0")" 186 | fi 187 | 188 | exec "$JAVACMD" "$@" 189 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem http://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: "com.android.library" 2 | 3 | android { 4 | compileSdkVersion 29 5 | 6 | defaultConfig { 7 | minSdkVersion 14 8 | targetSdkVersion 29 9 | versionCode 1 10 | versionName "1.0" 11 | } 12 | } 13 | 14 | dependencies { 15 | 16 | api "androidx.appcompat:appcompat:1.1.0" 17 | 18 | api "oauth.signpost:signpost-core:1.2.1.2" 19 | 20 | api "se.akerfeldt:okhttp-signpost:1.1.0" 21 | 22 | api "com.github.parse-community.Parse-SDK-Android:parse:1.22.1" 23 | 24 | testImplementation "junit:junit:4.12" 25 | testImplementation "org.mockito:mockito-core:1.10.19" 26 | } 27 | 28 | //region Code Coverage 29 | 30 | apply plugin: "jacoco" 31 | 32 | jacoco { 33 | toolVersion "0.7.1.201405082137" 34 | } 35 | 36 | task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") { 37 | group = "Reporting" 38 | description = "Generate Jacoco coverage reports" 39 | 40 | classDirectories = fileTree( 41 | dir: "${buildDir}/intermediates/classes/debug", 42 | excludes: ['**/R.class', 43 | '**/R$*.class', 44 | '**/*$ViewInjector*.*', 45 | '**/BuildConfig.*', 46 | '**/Manifest*.*', 47 | '**/gdata/**', 48 | '**/oauth/**', 49 | '**/signpost/**'] 50 | ) 51 | 52 | sourceDirectories = files("${buildDir.parent}/src/main/java") 53 | additionalSourceDirs = files([ 54 | "${buildDir}/generated/source/buildConfig/debug", 55 | "${buildDir}/generated/source/r/debug" 56 | ]) 57 | executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec") 58 | 59 | reports { 60 | xml.enabled = true 61 | html.enabled = true 62 | } 63 | } 64 | 65 | //endregion 66 | 67 | //region Coveralls 68 | 69 | apply plugin: "com.github.kt3k.coveralls" 70 | 71 | coveralls.jacocoReportPath = "${buildDir}/reports/jacoco/jacocoTestReport/jacocoTestReport.xml" 72 | 73 | //endregion 74 | 75 | apply from: "https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.1.0/gradle-android-javadocs.gradle" 76 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | -------------------------------------------------------------------------------- /library/src/main/java/com/parse/twitter/AsyncCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | public interface AsyncCallback { 12 | void onSuccess(Object result); 13 | 14 | void onCancel(); 15 | 16 | void onFailure(Throwable error); 17 | } 18 | -------------------------------------------------------------------------------- /library/src/main/java/com/parse/twitter/OAuth1FlowDialog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | import android.annotation.SuppressLint; 12 | import android.content.Context; 13 | import android.content.DialogInterface; 14 | import android.content.Intent; 15 | import android.graphics.Bitmap; 16 | import android.net.Uri; 17 | import android.os.Bundle; 18 | import androidx.appcompat.app.AppCompatDialog; 19 | import android.view.View; 20 | import android.view.Window; 21 | import android.webkit.WebView; 22 | import android.webkit.WebViewClient; 23 | import android.widget.ProgressBar; 24 | 25 | /** 26 | * For internal use. 27 | */ 28 | class OAuth1FlowDialog extends AppCompatDialog { 29 | 30 | private final String callbackUrl; 31 | private final String requestUrl; 32 | private final String serviceUrlIdentifier; 33 | private final FlowResultHandler handler; 34 | 35 | private WebView webView; 36 | private ProgressBar progress; 37 | 38 | OAuth1FlowDialog(Context context, String requestUrl, String callbackUrl, String serviceUrlIdentifier, FlowResultHandler resultHandler) { 39 | super(context); 40 | this.requestUrl = requestUrl; 41 | this.callbackUrl = callbackUrl; 42 | this.serviceUrlIdentifier = serviceUrlIdentifier; 43 | this.handler = resultHandler; 44 | this.setOnCancelListener(new OnCancelListener() { 45 | @Override 46 | public void onCancel(DialogInterface dialog) { 47 | handler.onCancel(); 48 | } 49 | }); 50 | } 51 | 52 | @SuppressLint("SetJavaScriptEnabled") 53 | @Override 54 | protected void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | requestWindowFeature(Window.FEATURE_NO_TITLE); 57 | setContentView(R.layout.parse_twitter_dialog_login); 58 | webView = findViewById(R.id.webView); 59 | progress = findViewById(R.id.progress); 60 | 61 | webView.setVerticalScrollBarEnabled(false); 62 | webView.setHorizontalScrollBarEnabled(false); 63 | webView.setWebViewClient(new OAuth1WebViewClient()); 64 | webView.getSettings().setJavaScriptEnabled(true); 65 | webView.loadUrl(requestUrl); 66 | } 67 | 68 | public interface FlowResultHandler { 69 | /** 70 | * Called when the user cancels the dialog. 71 | */ 72 | void onCancel(); 73 | 74 | /** 75 | * Called when the dialog's web view receives an error. 76 | */ 77 | void onError(int errorCode, String description, String failingUrl); 78 | 79 | /** 80 | * Called when the dialog portion of the flow is complete. 81 | * 82 | * @param callbackUrl The final URL called back (including any query string appended 83 | * by the server). 84 | */ 85 | void onComplete(String callbackUrl); 86 | } 87 | 88 | private class OAuth1WebViewClient extends WebViewClient { 89 | @Override 90 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 91 | if (url.startsWith(callbackUrl)) { 92 | OAuth1FlowDialog.this.dismiss(); 93 | handler.onComplete(url); 94 | return true; 95 | } else if (url.contains(serviceUrlIdentifier)) { 96 | return false; 97 | } 98 | // launch non-service URLs in a full browser 99 | getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 100 | return true; 101 | } 102 | 103 | @Override 104 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 105 | super.onReceivedError(view, errorCode, description, failingUrl); 106 | OAuth1FlowDialog.this.dismiss(); 107 | handler.onError(errorCode, description, failingUrl); 108 | } 109 | 110 | @Override 111 | public void onPageStarted(WebView view, String url, Bitmap favicon) { 112 | super.onPageStarted(view, url, favicon); 113 | progress.setVisibility(View.VISIBLE); 114 | } 115 | 116 | @Override 117 | public void onPageFinished(WebView view, String url) { 118 | super.onPageFinished(view, url); 119 | progress.setVisibility(View.GONE); 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /library/src/main/java/com/parse/twitter/OAuth1FlowException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | /** 12 | * OAuth Flow exception 13 | */ 14 | public class OAuth1FlowException extends Exception { 15 | private static final long serialVersionUID = 4272662026279290823L; 16 | private final int errorCode; 17 | private final String description; 18 | private final String failingUrl; 19 | 20 | public OAuth1FlowException(int errorCode, String description, String failingUrl) { 21 | super(String.format("OAuth Flow Error %d: Url: %s Description: %s", errorCode, failingUrl, 22 | description)); 23 | this.errorCode = errorCode; 24 | this.description = description; 25 | this.failingUrl = failingUrl; 26 | } 27 | 28 | public int getErrorCode() { 29 | return errorCode; 30 | } 31 | 32 | public String getDescription() { 33 | return description; 34 | } 35 | 36 | public String getFailingUrl() { 37 | return failingUrl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /library/src/main/java/com/parse/twitter/ParseTwitterUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | import android.content.Context; 12 | 13 | import com.parse.AuthenticationCallback; 14 | import com.parse.LogInCallback; 15 | import com.parse.ParseException; 16 | import com.parse.ParseUser; 17 | import com.parse.SaveCallback; 18 | 19 | import java.util.Map; 20 | import java.util.concurrent.CancellationException; 21 | 22 | import bolts.AggregateException; 23 | import bolts.Continuation; 24 | import bolts.Task; 25 | 26 | /** 27 | * Provides a set of utilities for using Parse with Twitter. 28 | */ 29 | public final class ParseTwitterUtils { 30 | private static final String AUTH_TYPE = "twitter"; 31 | 32 | private static final String CALLBACK_URL = "twittersdk://"; 33 | 34 | private static final Object lock = new Object(); 35 | static boolean isInitialized; 36 | static TwitterController controller; 37 | static ParseUserDelegate userDelegate = new ParseUserDelegateImpl(); 38 | 39 | private static TwitterController getTwitterController() { 40 | synchronized (lock) { 41 | if (controller == null) { 42 | controller = new TwitterController(); 43 | } 44 | return controller; 45 | } 46 | } 47 | 48 | /** 49 | * Gets the shared {@link Twitter} singleton that Parse is using. 50 | * 51 | * @return {@link Twitter} instance. 52 | */ 53 | public static Twitter getTwitter() { 54 | return getTwitterController().getTwitter(); 55 | } 56 | 57 | /** 58 | * Initializes Twitter for use with Parse. This method must be invoked prior to calling 59 | * {@link #link(ParseUser, Context, SaveCallback)} and {@link #logIn(Context, LogInCallback)}. 60 | * 61 | * @param consumerKey Your Twitter consumer key. 62 | * @param consumerSecret Your Twitter consumer secret. 63 | */ 64 | public static void initialize(String consumerKey, String consumerSecret) { 65 | initialize(consumerKey, consumerSecret, CALLBACK_URL); 66 | } 67 | 68 | /** 69 | * Initializes Twitter for use with Parse. This method must be invoked prior to calling 70 | * {@link #link(ParseUser, Context, SaveCallback)} and {@link #logIn(Context, LogInCallback)}. 71 | * 72 | * @param consumerKey Your Twitter consumer key. 73 | * @param consumerSecret Your Twitter consumer secret. 74 | * @param callbackUrl the callback url 75 | */ 76 | public static void initialize(String consumerKey, String consumerSecret, String callbackUrl) { 77 | synchronized (lock) { 78 | if (isInitialized) { 79 | return; 80 | } 81 | 82 | if (controller == null) { 83 | Twitter twitter = new Twitter(consumerKey, consumerSecret, callbackUrl); 84 | controller = new TwitterController(twitter); 85 | } else { 86 | controller.initialize(consumerKey, consumerSecret); 87 | } 88 | 89 | userDelegate.registerAuthenticationCallback(AUTH_TYPE, new AuthenticationCallback() { 90 | @Override 91 | public boolean onRestore(Map authData) { 92 | try { 93 | getTwitterController().setAuthData(authData); 94 | return true; 95 | } catch (Exception e) { 96 | return false; 97 | } 98 | } 99 | }); 100 | 101 | isInitialized = true; 102 | } 103 | } 104 | 105 | private static void checkInitialization() { 106 | if (!isInitialized) { 107 | throw new IllegalStateException( 108 | "You must call ParseTwitterUtils.initialize() before using ParseTwitterUtils"); 109 | } 110 | } 111 | 112 | /** 113 | * @return {@code true} if the user is linked to a Twitter account. 114 | */ 115 | public static boolean isLinked(ParseUser user) { 116 | return user.isLinked(AUTH_TYPE); 117 | } 118 | 119 | /** 120 | * Links a ParseUser to a Twitter account, allowing you to use Twitter for authentication, and 121 | * providing access to Twitter data for the user. A dialog will be shown to the user for Twitter 122 | * authentication. 123 | * 124 | * @param user The user to link to a Twitter account. 125 | * @param context An Android context from which the login dialog can be launched. 126 | * @return A Task that will be resolved when linking is completed. 127 | */ 128 | public static Task linkInBackground(Context context, final ParseUser user) { 129 | checkInitialization(); 130 | return getTwitterController().authenticateAsync(context).onSuccessTask(new Continuation, Task>() { 131 | @Override 132 | public Task then(Task> task) { 133 | return user.linkWithInBackground(AUTH_TYPE, task.getResult()); 134 | } 135 | }); 136 | } 137 | 138 | /** 139 | * @deprecated Please use {@link ParseTwitterUtils#linkInBackground(Context, ParseUser)} instead. 140 | */ 141 | @Deprecated 142 | public static void link(ParseUser user, Context context) { 143 | link(user, context, null); 144 | } 145 | 146 | /** 147 | * Links a ParseUser to a Twitter account, allowing you to use Twitter for authentication, and 148 | * providing access to Twitter data for the user. A dialog will be shown to the user for Twitter 149 | * authentication. 150 | * 151 | * @param user The user to link to a Twitter account. 152 | * @param context An Android context from which the login dialog can be launched. 153 | * @param callback Callback for notifying the calling application when the Twitter authentication has 154 | * completed, failed, or been canceled. 155 | * @see #linkInBackground(Context, ParseUser) 156 | */ 157 | public static void link(ParseUser user, Context context, SaveCallback callback) { 158 | callbackOnMainThreadAsync(linkInBackground(context, user), callback, true); 159 | } 160 | 161 | /** 162 | * Links a ParseUser to a Twitter account, allowing you to use Twitter for authentication, and 163 | * providing access to Twitter data for the user. This method allows you to handle getting the 164 | * auth tokens for your users, rather than delegating to the provided dialog log-in. 165 | * 166 | * @param user The user to link to a Twitter account. 167 | * @param twitterId The user's Twitter ID. 168 | * @param screenName The user's Twitter screen name. 169 | * @param authToken The auth token for the session. 170 | * @param authTokenSecret The auth token secret for the session. 171 | * @return A Task that will be resolved when linking is completed. 172 | */ 173 | public static Task linkInBackground(ParseUser user, String twitterId, String screenName, 174 | String authToken, String authTokenSecret) { 175 | checkInitialization(); 176 | Map authData = getTwitterController().getAuthData( 177 | twitterId, 178 | screenName, 179 | authToken, 180 | authTokenSecret); 181 | return user.linkWithInBackground(AUTH_TYPE, authData); 182 | } 183 | 184 | /** 185 | * @deprecated Please use {@link ParseTwitterUtils#linkInBackground(ParseUser, String, String, 186 | * String, String)} instead. 187 | */ 188 | @Deprecated 189 | public static void link(ParseUser user, String twitterId, String screenName, String authToken, 190 | String authTokenSecret) { 191 | link(user, twitterId, screenName, authToken, authTokenSecret, null); 192 | } 193 | 194 | /** 195 | * Links a ParseUser to a Twitter account, allowing you to use Twitter for authentication, and 196 | * providing access to Twitter data for the user. This method allows you to handle getting the 197 | * auth tokens for your users, rather than delegating to the provided dialog log-in. 198 | * 199 | * @param user The user to link to a Twitter account. 200 | * @param twitterId The user's Twitter ID. 201 | * @param screenName The user's Twitter screen name. 202 | * @param authToken The auth token for the session. 203 | * @param authTokenSecret The auth token secret for the session. 204 | * @param callback Callback for notifying that the authentication data has been saved to the ParseUser. 205 | * @see #linkInBackground(ParseUser, String, String, String, String) 206 | */ 207 | public static void link(ParseUser user, String twitterId, String screenName, String authToken, 208 | String authTokenSecret, SaveCallback callback) { 209 | callbackOnMainThreadAsync( 210 | linkInBackground(user, twitterId, screenName, authToken, authTokenSecret), 211 | callback, 212 | false 213 | ); 214 | } 215 | 216 | /** 217 | * Logs in a ParseUser using Twitter for authentication. If a user for the given Twitter 218 | * credentials does not already exist, a new user will be created. This method allows you to 219 | * handle getting the auth tokens for your users, rather than delegating to the provided dialog 220 | * log-in. 221 | * 222 | * @param twitterId The user's Twitter ID. 223 | * @param screenName The user's Twitter screen name. 224 | * @param authToken The auth token for the session. 225 | * @param authTokenSecret The auth token secret for the session. 226 | * @return A Task that will be resolved when logging in is completed. 227 | */ 228 | public static Task logInInBackground(String twitterId, String screenName, 229 | String authToken, String authTokenSecret) { 230 | checkInitialization(); 231 | Map authData = getTwitterController().getAuthData( 232 | twitterId, 233 | screenName, 234 | authToken, 235 | authTokenSecret); 236 | return userDelegate.logInWithInBackground(AUTH_TYPE, authData); 237 | } 238 | 239 | /** 240 | * Logs in a ParseUser using Twitter for authentication. If a user for the given Twitter 241 | * credentials does not already exist, a new user will be created. This method allows you to 242 | * handle getting the auth tokens for your users, rather than delegating to the provided dialog 243 | * log-in. 244 | * 245 | * @param twitterId The user's Twitter ID. 246 | * @param screenName The user's Twitter screen name. 247 | * @param authToken The auth token for the session. 248 | * @param authTokenSecret The auth token secret for the session. 249 | * @param callback Callback for notifying that the authentication data has been saved to the ParseUser. 250 | * @see #logInInBackground(String, String, String, String) 251 | */ 252 | public static void logIn(String twitterId, String screenName, String authToken, 253 | String authTokenSecret, LogInCallback callback) { 254 | callbackOnMainThreadAsync( 255 | logInInBackground(twitterId, screenName, authToken, authTokenSecret), 256 | callback, 257 | false 258 | ); 259 | } 260 | 261 | /** 262 | * Logs in a ParseUser using Twitter for authentication. If a user for the given Twitter 263 | * credentials does not already exist, a new user will be created. A dialog will be shown to the 264 | * user for Twitter authentication. 265 | * 266 | * @param context An Android context from which the login dialog can be launched. 267 | * @return A Task that will be resolved when logging in is completed. 268 | */ 269 | public static Task logInInBackground(Context context) { 270 | checkInitialization(); 271 | return getTwitterController().authenticateAsync(context).onSuccessTask(new Continuation, Task>() { 272 | @Override 273 | public Task then(Task> task) { 274 | return userDelegate.logInWithInBackground(AUTH_TYPE, task.getResult()); 275 | } 276 | }); 277 | } 278 | 279 | /** 280 | * Logs in a ParseUser using Twitter for authentication. If a user for the given Twitter 281 | * credentials does not already exist, a new user will be created. A dialog will be shown to the 282 | * user for Twitter authentication. 283 | * 284 | * @param context An Android context from which the login dialog can be launched. 285 | * @param callback Callback for notifying the calling application when the Twitter authentication has 286 | * completed, failed, or been canceled. 287 | * @see #logInInBackground(android.content.Context) 288 | */ 289 | public static void logIn(Context context, LogInCallback callback) { 290 | callbackOnMainThreadAsync(logInInBackground(context), callback, true); 291 | } 292 | 293 | /** 294 | * Unlinks a user from a Twitter account. Unlinking a user will save the user's data. 295 | * 296 | * @param user The user to unlink from a Facebook account. 297 | */ 298 | public static void unlink(ParseUser user) throws ParseException { 299 | wait(unlinkInBackground(user)); 300 | } 301 | 302 | /** 303 | * Unlinks a user from a Twitter account in the background. Unlinking a user will save the user's 304 | * data. 305 | * 306 | * @param user The user to unlink from a Facebook account. 307 | * @return A Task that will be resolved when unlinking is completed. 308 | */ 309 | public static Task unlinkInBackground(ParseUser user) { 310 | checkInitialization(); 311 | return user.unlinkFromInBackground(AUTH_TYPE); 312 | } 313 | 314 | /** 315 | * Unlinks a user from a Twitter account in the background. Unlinking a user will save the user's 316 | * data. 317 | * 318 | * @param user The user to unlink from a Facebook account. 319 | * @param callback Callback for notifying when unlinking is complete. 320 | * @see #unlinkInBackground(ParseUser) 321 | */ 322 | public static void unlinkInBackground(ParseUser user, SaveCallback callback) { 323 | callbackOnMainThreadAsync(unlinkInBackground(user), callback, false); 324 | } 325 | 326 | //region TaskUtils 327 | 328 | /** 329 | * Converts a task execution into a synchronous action. 330 | */ 331 | //TODO (grantland): Task.cs actually throws an AggregateException if the task was cancelled with 332 | // TaskCancellationException as an inner exception or an AggregateException with the original 333 | // exception as an inner exception if task.isFaulted(). 334 | // https://msdn.microsoft.com/en-us/library/dd235635(v=vs.110).aspx 335 | /* package */ 336 | static T wait(Task task) throws ParseException { 337 | try { 338 | task.waitForCompletion(); 339 | if (task.isFaulted()) { 340 | Exception error = task.getError(); 341 | if (error instanceof ParseException) { 342 | throw (ParseException) error; 343 | } 344 | if (error instanceof AggregateException) { 345 | throw new ParseException(error); 346 | } 347 | if (error instanceof RuntimeException) { 348 | throw (RuntimeException) error; 349 | } 350 | throw new RuntimeException(error); 351 | } else if (task.isCancelled()) { 352 | throw new RuntimeException(new CancellationException()); 353 | } 354 | return task.getResult(); 355 | } catch (InterruptedException e) { 356 | throw new RuntimeException(e); 357 | } 358 | } 359 | 360 | /** 361 | * Calls the callback after a task completes on the main thread, returning a Task that completes 362 | * with the same result as the input task after the callback has been run. 363 | */ 364 | private static Task callbackOnMainThreadAsync( 365 | Task task, LogInCallback callback, boolean reportCancellation) { 366 | return callbackOnMainThreadInternalAsync(task, callback, reportCancellation); 367 | } 368 | 369 | /** 370 | * Calls the callback after a task completes on the main thread, returning a Task that completes 371 | * with the same result as the input task after the callback has been run. 372 | */ 373 | private static Task callbackOnMainThreadAsync( 374 | Task task, SaveCallback callback, boolean reportCancellation) { 375 | return callbackOnMainThreadInternalAsync(task, callback, reportCancellation); 376 | } 377 | 378 | /** 379 | * Calls the callback after a task completes on the main thread, returning a Task that completes 380 | * with the same result as the input task after the callback has been run. If reportCancellation 381 | * is false, the callback will not be called if the task was cancelled. 382 | */ 383 | private static Task callbackOnMainThreadInternalAsync( 384 | Task task, final Object callback, final boolean reportCancellation) { 385 | if (callback == null) { 386 | return task; 387 | } 388 | final Task.TaskCompletionSource tcs = Task.create(); 389 | task.continueWith(new Continuation() { 390 | @Override 391 | public Void then(final Task task) throws Exception { 392 | if (task.isCancelled() && !reportCancellation) { 393 | tcs.setCancelled(); 394 | return null; 395 | } 396 | Task.UI_THREAD_EXECUTOR.execute(new Runnable() { 397 | @Override 398 | public void run() { 399 | try { 400 | Exception error = task.getError(); 401 | if (error != null && !(error instanceof ParseException)) { 402 | error = new ParseException(error); 403 | } 404 | if (callback instanceof SaveCallback) { 405 | ((SaveCallback) callback).done((ParseException) error); 406 | } else if (callback instanceof LogInCallback) { 407 | ((LogInCallback) callback).done( 408 | (ParseUser) task.getResult(), (ParseException) error); 409 | } 410 | } finally { 411 | if (task.isCancelled()) { 412 | tcs.setCancelled(); 413 | } else if (task.isFaulted()) { 414 | tcs.setError(task.getError()); 415 | } else { 416 | tcs.setResult(task.getResult()); 417 | } 418 | } 419 | } 420 | }); 421 | return null; 422 | } 423 | }); 424 | return tcs.getTask(); 425 | } 426 | 427 | //endregion 428 | 429 | private ParseTwitterUtils() { 430 | // do nothing 431 | } 432 | 433 | interface ParseUserDelegate { 434 | void registerAuthenticationCallback(String authType, AuthenticationCallback callback); 435 | 436 | Task logInWithInBackground(String authType, Map authData); 437 | } 438 | 439 | private static class ParseUserDelegateImpl implements ParseUserDelegate { 440 | @Override 441 | public void registerAuthenticationCallback(String authType, AuthenticationCallback callback) { 442 | ParseUser.registerAuthenticationCallback(authType, callback); 443 | } 444 | 445 | @Override 446 | public Task logInWithInBackground(String authType, Map authData) { 447 | return ParseUser.logInWithInBackground(authType, authData); 448 | } 449 | } 450 | } 451 | -------------------------------------------------------------------------------- /library/src/main/java/com/parse/twitter/Twitter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | import android.content.Context; 12 | import android.net.Uri; 13 | import android.os.AsyncTask; 14 | import android.webkit.CookieSyncManager; 15 | 16 | import com.parse.twitter.OAuth1FlowDialog.FlowResultHandler; 17 | 18 | import oauth.signpost.http.HttpParameters; 19 | import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer; 20 | import se.akerfeldt.okhttp.signpost.OkHttpOAuthProvider; 21 | 22 | /** 23 | * Provides access to Twitter info as it relates to Parse 24 | */ 25 | public class Twitter { 26 | 27 | private static final String REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"; 28 | private static final String AUTHORIZE_URL = "https://api.twitter.com/oauth/authenticate"; 29 | private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"; 30 | 31 | private static final String VERIFIER_PARAM = "oauth_verifier"; 32 | private static final String USER_ID_PARAM = "user_id"; 33 | private static final String SCREEN_NAME_PARAM = "screen_name"; 34 | 35 | // App configuration for enabling authentication. 36 | private String consumerKey; 37 | private String consumerSecret; 38 | private String callbackUrl; 39 | 40 | // User information. 41 | private String authToken; 42 | private String authTokenSecret; 43 | private String userId; 44 | private String screenName; 45 | 46 | Twitter(String consumerKey, String consumerSecret, String callbackUrl) { 47 | this.consumerKey = consumerKey; 48 | this.consumerSecret = consumerSecret; 49 | this.callbackUrl = callbackUrl; 50 | } 51 | 52 | public String getConsumerKey() { 53 | return consumerKey; 54 | } 55 | 56 | public Twitter setConsumerKey(String consumerKey) { 57 | this.consumerKey = consumerKey; 58 | return this; 59 | } 60 | 61 | public String getConsumerSecret() { 62 | return consumerSecret; 63 | } 64 | 65 | public Twitter setConsumerSecret(String consumerSecret) { 66 | this.consumerSecret = consumerSecret; 67 | return this; 68 | } 69 | 70 | public String getAuthToken() { 71 | return authToken; 72 | } 73 | 74 | public void setAuthToken(String authToken) { 75 | this.authToken = authToken; 76 | } 77 | 78 | public String getAuthTokenSecret() { 79 | return authTokenSecret; 80 | } 81 | 82 | public void setAuthTokenSecret(String authTokenSecret) { 83 | this.authTokenSecret = authTokenSecret; 84 | } 85 | 86 | public String getUserId() { 87 | return userId; 88 | } 89 | 90 | public void setUserId(String userId) { 91 | this.userId = userId; 92 | } 93 | 94 | public String getScreenName() { 95 | return screenName; 96 | } 97 | 98 | public void setScreenName(String screenName) { 99 | this.screenName = screenName; 100 | } 101 | 102 | public void authorize(final Context context, final AsyncCallback callback) { 103 | if (getConsumerKey() == null || getConsumerKey().length() == 0 || getConsumerSecret() == null 104 | || getConsumerSecret().length() == 0) { 105 | throw new IllegalStateException( 106 | "Twitter must be initialized with a consumer key and secret before authorization."); 107 | } 108 | 109 | final OkHttpOAuthProvider provider = new OkHttpOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_URL); 110 | final OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(getConsumerKey(), getConsumerSecret()); 111 | 112 | AsyncTask task = new AsyncTask() { 113 | private Throwable error; 114 | 115 | @Override 116 | protected String doInBackground(Void... params) { 117 | try { 118 | return provider.retrieveRequestToken(consumer, callbackUrl); 119 | } catch (Throwable e) { 120 | error = e; 121 | } 122 | return null; 123 | } 124 | 125 | @Override 126 | protected void onPostExecute(String result) { 127 | super.onPostExecute(result); 128 | if (error != null) { 129 | callback.onFailure(error); 130 | return; 131 | } 132 | CookieSyncManager.createInstance(context); 133 | OAuth1FlowDialog dialog = new OAuth1FlowDialog(context, result, callbackUrl, 134 | "api.twitter", new FlowResultHandler() { 135 | 136 | @Override 137 | public void onError(int errorCode, String description, String failingUrl) { 138 | callback.onFailure(new OAuth1FlowException(errorCode, description, failingUrl)); 139 | } 140 | 141 | @Override 142 | public void onComplete(String callbackUrl) { 143 | CookieSyncManager.getInstance().sync(); 144 | Uri uri = Uri.parse(callbackUrl); 145 | final String verifier = uri.getQueryParameter(VERIFIER_PARAM); 146 | if (verifier == null) { 147 | callback.onCancel(); 148 | return; 149 | } 150 | AsyncTask getTokenTask = new AsyncTask() { 151 | private Throwable error; 152 | 153 | @Override 154 | protected HttpParameters doInBackground(Void... params) { 155 | try { 156 | provider.retrieveAccessToken(consumer, verifier); 157 | } catch (Throwable e) { 158 | error = e; 159 | } 160 | return provider.getResponseParameters(); 161 | } 162 | 163 | @Override 164 | protected void onPostExecute(HttpParameters result) { 165 | super.onPostExecute(result); 166 | if (error != null) { 167 | callback.onFailure(error); 168 | return; 169 | } 170 | try { 171 | setAuthToken(consumer.getToken()); 172 | setAuthTokenSecret(consumer.getTokenSecret()); 173 | setScreenName(result.getFirst(SCREEN_NAME_PARAM)); 174 | setUserId(result.getFirst(USER_ID_PARAM)); 175 | } catch (Throwable e) { 176 | callback.onFailure(e); 177 | return; 178 | } 179 | callback.onSuccess(Twitter.this); 180 | } 181 | }; 182 | getTokenTask.execute(); 183 | } 184 | 185 | @Override 186 | public void onCancel() { 187 | callback.onCancel(); 188 | } 189 | }); 190 | dialog.show(); 191 | } 192 | }; 193 | task.execute(); 194 | } 195 | 196 | } 197 | -------------------------------------------------------------------------------- /library/src/main/java/com/parse/twitter/TwitterController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | import android.content.Context; 12 | 13 | import com.parse.ParseException; 14 | 15 | import java.util.HashMap; 16 | import java.util.Map; 17 | 18 | import bolts.Task; 19 | 20 | class TwitterController { 21 | 22 | private static final String CONSUMER_KEY_KEY = "consumer_key"; 23 | private static final String CONSUMER_SECRET_KEY = "consumer_secret"; 24 | 25 | private static final String ID_KEY = "id"; 26 | private static final String SCREEN_NAME_KEY = "screen_name"; 27 | private static final String AUTH_TOKEN_KEY = "auth_token"; 28 | private static final String AUTH_TOKEN_SECRET_KEY = "auth_token_secret"; 29 | 30 | private final Twitter twitter; 31 | private Task>.TaskCompletionSource currentTcs; 32 | 33 | TwitterController() { 34 | this(new Twitter("", "", "")); 35 | } 36 | 37 | TwitterController(Twitter twitter) { 38 | this.twitter = twitter; 39 | } 40 | 41 | public void initialize(String consumerKey, String consumerSecret) { 42 | twitter.setConsumerKey(consumerKey).setConsumerSecret(consumerSecret); 43 | } 44 | 45 | public Twitter getTwitter() { 46 | return twitter; 47 | } 48 | 49 | public Task> authenticateAsync(Context context) { 50 | final Task>.TaskCompletionSource tcs = Task.create(); 51 | if (currentTcs != null) { 52 | handleCancel(currentTcs); 53 | } 54 | currentTcs = tcs; 55 | 56 | if (context == null) { 57 | throw new IllegalStateException( 58 | "Context must be non-null for Twitter authentication to proceed."); 59 | } 60 | twitter.authorize(context, new AsyncCallback() { 61 | @Override 62 | public void onCancel() { 63 | handleCancel(tcs); 64 | } 65 | 66 | @Override 67 | public void onFailure(Throwable error) { 68 | if (currentTcs != tcs) { 69 | return; 70 | } 71 | try { 72 | if (error instanceof Exception) { 73 | tcs.trySetError((Exception) error); 74 | } else { 75 | tcs.trySetError(new ParseException(error)); 76 | } 77 | } finally { 78 | currentTcs = null; 79 | } 80 | } 81 | 82 | @Override 83 | public void onSuccess(Object result) { 84 | if (currentTcs != tcs) { 85 | return; 86 | } 87 | try { 88 | Map authData = getAuthData( 89 | twitter.getUserId(), 90 | twitter.getScreenName(), 91 | twitter.getAuthToken(), 92 | twitter.getAuthTokenSecret()); 93 | tcs.trySetResult(authData); 94 | } finally { 95 | currentTcs = null; 96 | } 97 | } 98 | }); 99 | 100 | return tcs.getTask(); 101 | } 102 | 103 | private void handleCancel(Task>.TaskCompletionSource callback) { 104 | // Ensure that the operation being cancelled is actually the current 105 | // operation (so that if one had already been cancelled but still 106 | // invokes this method, it doesn't cancel the "real" current operation). 107 | if (currentTcs != callback || callback == null) { 108 | return; 109 | } 110 | try { 111 | callback.trySetCancelled(); 112 | } finally { 113 | currentTcs = null; 114 | } 115 | } 116 | 117 | public Map getAuthData( 118 | String userId, 119 | String screenName, 120 | String authToken, 121 | String authTokenSecret) { 122 | Map authData = new HashMap<>(); 123 | authData.put(CONSUMER_KEY_KEY, twitter.getConsumerKey()); 124 | authData.put(CONSUMER_SECRET_KEY, twitter.getConsumerSecret()); 125 | authData.put(ID_KEY, userId); 126 | authData.put(SCREEN_NAME_KEY, screenName); 127 | authData.put(AUTH_TOKEN_KEY, authToken); 128 | authData.put(AUTH_TOKEN_SECRET_KEY, authTokenSecret); 129 | return authData; 130 | } 131 | 132 | public void setAuthData(Map authData) { 133 | if (authData == null) { 134 | twitter.setAuthToken(null); 135 | twitter.setAuthTokenSecret(null); 136 | twitter.setScreenName(null); 137 | twitter.setUserId(null); 138 | // Consumer key/secret are set by the application. 139 | } else { 140 | twitter.setAuthToken(authData.get(AUTH_TOKEN_KEY)); 141 | twitter.setAuthTokenSecret(authData.get(AUTH_TOKEN_SECRET_KEY)); 142 | twitter.setUserId(authData.get(ID_KEY)); 143 | twitter.setScreenName(authData.get(SCREEN_NAME_KEY)); 144 | // Consumer key/secret are set by the application. 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /library/src/main/res/layout/parse_twitter_dialog_login.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 10 | 11 | 15 | 16 | 21 | 22 | -------------------------------------------------------------------------------- /library/src/test/java/com/parse/twitter/ParseTwitterUtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | import android.content.Context; 12 | 13 | import com.parse.AuthenticationCallback; 14 | import com.parse.ParseUser; 15 | 16 | import org.junit.After; 17 | import org.junit.Before; 18 | import org.junit.Test; 19 | import org.junit.runner.RunWith; 20 | import org.mockito.ArgumentCaptor; 21 | import org.mockito.Mock; 22 | import org.mockito.runners.MockitoJUnitRunner; 23 | 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | import bolts.Task; 28 | 29 | import static org.junit.Assert.assertFalse; 30 | import static org.junit.Assert.assertTrue; 31 | import static org.mockito.Matchers.any; 32 | import static org.mockito.Matchers.anyMapOf; 33 | import static org.mockito.Matchers.anyString; 34 | import static org.mockito.Matchers.eq; 35 | import static org.mockito.Mockito.doThrow; 36 | import static org.mockito.Mockito.mock; 37 | import static org.mockito.Mockito.verify; 38 | import static org.mockito.Mockito.verifyNoMoreInteractions; 39 | import static org.mockito.Mockito.when; 40 | 41 | @RunWith(MockitoJUnitRunner.class) 42 | public class ParseTwitterUtilsTest { 43 | 44 | @Mock 45 | private TwitterController controller; 46 | @Mock 47 | private ParseTwitterUtils.ParseUserDelegate userDelegate; 48 | 49 | @Before 50 | public void setUp() { 51 | ParseTwitterUtils.controller = controller; 52 | ParseTwitterUtils.userDelegate = userDelegate; 53 | } 54 | 55 | @After 56 | public void tearDown() { 57 | ParseTwitterUtils.isInitialized = false; 58 | ParseTwitterUtils.controller = null; 59 | ParseTwitterUtils.userDelegate = null; 60 | } 61 | 62 | @Test 63 | public void testInitialize() { 64 | ParseTwitterUtils.initialize("test_key", "test_secret"); 65 | assertTrue(ParseTwitterUtils.isInitialized); 66 | verify(controller).initialize("test_key", "test_secret"); 67 | verify(userDelegate) 68 | .registerAuthenticationCallback(eq("twitter"), any(AuthenticationCallback.class)); 69 | } 70 | 71 | //region testRestoreAuthentication 72 | 73 | @Test 74 | public void testRestoreAuthentication() { 75 | ParseTwitterUtils.initialize(null, null); 76 | ArgumentCaptor captor = 77 | ArgumentCaptor.forClass(AuthenticationCallback.class); 78 | verify(userDelegate).registerAuthenticationCallback(eq("twitter"), captor.capture()); 79 | 80 | AuthenticationCallback callback = captor.getValue(); 81 | Map authData = new HashMap<>(); 82 | assertTrue(callback.onRestore(authData)); 83 | verify(controller).setAuthData(authData); 84 | } 85 | 86 | @Test 87 | public void testRestoreAuthenticationFailure() { 88 | ParseTwitterUtils.initialize(null, null); 89 | ArgumentCaptor captor = 90 | ArgumentCaptor.forClass(AuthenticationCallback.class); 91 | verify(userDelegate).registerAuthenticationCallback(eq("twitter"), captor.capture()); 92 | 93 | doThrow(new RuntimeException()) 94 | .when(controller) 95 | .setAuthData(anyMapOf(String.class, String.class)); 96 | AuthenticationCallback callback = captor.getValue(); 97 | Map authData = new HashMap<>(); 98 | assertFalse(callback.onRestore(authData)); 99 | verify(controller).setAuthData(authData); 100 | } 101 | 102 | //endregion 103 | 104 | @Test 105 | public void testIsLinked() { 106 | ParseUser user = mock(ParseUser.class); 107 | when(user.isLinked(anyString())).thenReturn(true); 108 | 109 | assertTrue(ParseTwitterUtils.isLinked(user)); 110 | verify(user).isLinked("twitter"); 111 | } 112 | 113 | //region testLogIn 114 | 115 | @Test 116 | @SuppressWarnings("unchecked") 117 | public void testLogInWithToken() { 118 | ParseTwitterUtils.isInitialized = true; 119 | ParseUser user = mock(ParseUser.class); 120 | when(userDelegate.logInWithInBackground(anyString(), anyMapOf(String.class, String.class))) 121 | .thenReturn(Task.forResult(user)); 122 | 123 | String twitterId = "test_id"; 124 | String screenName = "test_screen_name"; 125 | String authToken = "test_token"; 126 | String authSecret = "test_secret"; 127 | Task task = ParseTwitterUtils.logInInBackground( 128 | twitterId, screenName, authToken, authSecret); 129 | verify(controller).getAuthData(twitterId, screenName, authToken, authSecret); 130 | verify(userDelegate).logInWithInBackground(eq("twitter"), anyMapOf(String.class, String.class)); 131 | assertTrue(task.isCompleted()); 132 | } 133 | 134 | @Test 135 | @SuppressWarnings("unchecked") 136 | public void testLogInWithContext() { 137 | ParseTwitterUtils.isInitialized = true; 138 | ParseUser user = mock(ParseUser.class); 139 | Map authData = new HashMap<>(); 140 | when(controller.authenticateAsync(any(Context.class))).thenReturn(Task.forResult(authData)); 141 | when(userDelegate.logInWithInBackground(anyString(), anyMapOf(String.class, String.class))) 142 | .thenReturn(Task.forResult(user)); 143 | 144 | Context context = mock(Context.class); 145 | Task task = ParseTwitterUtils.logInInBackground(context); 146 | verify(controller).authenticateAsync(context); 147 | verify(userDelegate).logInWithInBackground("twitter", authData); 148 | assertTrue(task.isCompleted()); 149 | } 150 | 151 | //endregion 152 | 153 | //region testLink 154 | 155 | @Test 156 | @SuppressWarnings("unchecked") 157 | public void testLinkWithToken() { 158 | ParseTwitterUtils.isInitialized = true; 159 | 160 | ParseUser user = mock(ParseUser.class); 161 | when(user.linkWithInBackground(anyString(), anyMapOf(String.class, String.class))) 162 | .thenReturn(Task.forResult(null)); 163 | String twitterId = "test_id"; 164 | String screenName = "test_screen_name"; 165 | String authToken = "test_token"; 166 | String authSecret = "test_secret"; 167 | Task task = ParseTwitterUtils.linkInBackground( 168 | user, twitterId, screenName, authToken, authSecret); 169 | verify(controller).getAuthData(twitterId, screenName, authToken, authSecret); 170 | verify(user).linkWithInBackground(eq("twitter"), anyMapOf(String.class, String.class)); 171 | assertTrue(task.isCompleted()); 172 | } 173 | 174 | @Test 175 | @SuppressWarnings("unchecked") 176 | public void testLinkWithContext() { 177 | ParseTwitterUtils.isInitialized = true; 178 | Map authData = new HashMap<>(); 179 | when(controller.authenticateAsync(any(Context.class))).thenReturn(Task.forResult(authData)); 180 | 181 | Context context = mock(Context.class); 182 | ParseUser user = mock(ParseUser.class); 183 | when(user.linkWithInBackground(anyString(), anyMapOf(String.class, String.class))) 184 | .thenReturn(Task.forResult(null)); 185 | Task task = ParseTwitterUtils.linkInBackground(context, user); 186 | verify(controller).authenticateAsync(context); 187 | verify(user).linkWithInBackground("twitter", authData); 188 | assertTrue(task.isCompleted()); 189 | } 190 | 191 | //endregion 192 | 193 | @Test 194 | public void testUnlink() { 195 | ParseTwitterUtils.isInitialized = true; 196 | ParseUser user = mock(ParseUser.class); 197 | when(user.unlinkFromInBackground(anyString())).thenReturn(Task.forResult(null)); 198 | Task task = ParseTwitterUtils.unlinkInBackground(user); 199 | verify(user).unlinkFromInBackground("twitter"); 200 | verifyNoMoreInteractions(user); 201 | assertTrue(task.isCompleted()); 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /library/src/test/java/com/parse/twitter/TwitterControllerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015-present, Parse, LLC. 3 | * All rights reserved. 4 | * 5 | * This source code is licensed under the BSD-style license found in the 6 | * LICENSE file in the root directory of this source tree. An additional grant 7 | * of patent rights can be found in the PATENTS file in the same directory. 8 | */ 9 | package com.parse.twitter; 10 | 11 | import android.content.Context; 12 | 13 | import org.junit.Before; 14 | import org.junit.Test; 15 | import org.junit.runner.RunWith; 16 | import org.mockito.ArgumentCaptor; 17 | import org.mockito.Mock; 18 | import org.mockito.runners.MockitoJUnitRunner; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import bolts.Task; 24 | 25 | import static org.junit.Assert.assertEquals; 26 | import static org.junit.Assert.assertSame; 27 | import static org.junit.Assert.assertTrue; 28 | import static org.mockito.Matchers.anyString; 29 | import static org.mockito.Matchers.eq; 30 | import static org.mockito.Mockito.mock; 31 | import static org.mockito.Mockito.verify; 32 | import static org.mockito.Mockito.verifyNoMoreInteractions; 33 | import static org.mockito.Mockito.when; 34 | 35 | @RunWith(MockitoJUnitRunner.class) 36 | public class TwitterControllerTest { 37 | 38 | @Mock 39 | private Twitter twitter; 40 | 41 | @Before 42 | public void setUp() { 43 | when(twitter.setConsumerKey(anyString())).thenReturn(twitter); 44 | when(twitter.setConsumerSecret(anyString())).thenReturn(twitter); 45 | } 46 | 47 | @Test 48 | public void testInitialize() { 49 | TwitterController controller = new TwitterController(twitter); 50 | 51 | controller.initialize("test_key", "test_secret"); 52 | verify(twitter).setConsumerKey("test_key"); 53 | verify(twitter).setConsumerSecret("test_secret"); 54 | } 55 | 56 | @Test 57 | public void testGetTwitter() { 58 | TwitterController controller = new TwitterController(twitter); 59 | assertSame(twitter, controller.getTwitter()); 60 | } 61 | 62 | //region testAuthenticateAsync 63 | 64 | @Test 65 | public void testAuthenticateAsync() { 66 | when(twitter.getConsumerKey()).thenReturn("test_key"); 67 | when(twitter.getConsumerSecret()).thenReturn("test_secret"); 68 | when(twitter.getUserId()).thenReturn("test_id"); 69 | when(twitter.getScreenName()).thenReturn("test_screen_name"); 70 | when(twitter.getAuthToken()).thenReturn("test_token"); 71 | when(twitter.getAuthTokenSecret()).thenReturn("test_secret"); 72 | TwitterController controller = new TwitterController(twitter); 73 | 74 | Context context = mock(Context.class); 75 | Task> task = controller.authenticateAsync(context); 76 | ArgumentCaptor captor = ArgumentCaptor.forClass(AsyncCallback.class); 77 | verify(twitter).authorize(eq(context), captor.capture()); 78 | AsyncCallback callback = captor.getValue(); 79 | callback.onSuccess(twitter); 80 | assertTrue(task.isCompleted()); 81 | Map authData = task.getResult(); 82 | assertEquals("test_key", authData.get("consumer_key")); 83 | assertEquals("test_secret", authData.get("consumer_secret")); 84 | assertEquals("test_id", authData.get("id")); 85 | assertEquals("test_screen_name", authData.get("screen_name")); 86 | assertEquals("test_token", authData.get("auth_token")); 87 | assertEquals("test_secret", authData.get("auth_token_secret")); 88 | } 89 | 90 | @Test 91 | public void testAuthenticateAsyncCancel() { 92 | TwitterController controller = new TwitterController(twitter); 93 | 94 | Context context = mock(Context.class); 95 | Task> task = controller.authenticateAsync(context); 96 | ArgumentCaptor captor = ArgumentCaptor.forClass(AsyncCallback.class); 97 | verify(twitter).authorize(eq(context), captor.capture()); 98 | AsyncCallback callback = captor.getValue(); 99 | callback.onCancel(); 100 | assertTrue(task.isCancelled()); 101 | } 102 | 103 | @Test 104 | public void testAuthenticateAsyncFailure() { 105 | TwitterController controller = new TwitterController(twitter); 106 | 107 | Context context = mock(Context.class); 108 | Task> task = controller.authenticateAsync(context); 109 | ArgumentCaptor captor = ArgumentCaptor.forClass(AsyncCallback.class); 110 | verify(twitter).authorize(eq(context), captor.capture()); 111 | AsyncCallback callback = captor.getValue(); 112 | callback.onFailure(new RuntimeException()); 113 | assertTrue(task.isFaulted()); 114 | } 115 | 116 | //endregion 117 | 118 | //region testSetAuthData 119 | 120 | @Test 121 | public void testSetAuthData() { 122 | TwitterController controller = new TwitterController(twitter); 123 | 124 | Map authData = new HashMap<>(); 125 | authData.put("id", "test_id"); 126 | authData.put("screen_name", "test_screen_name"); 127 | authData.put("auth_token", "test_token"); 128 | authData.put("auth_token_secret", "test_secret"); 129 | controller.setAuthData(authData); 130 | verify(twitter).setUserId("test_id"); 131 | verify(twitter).setScreenName("test_screen_name"); 132 | verify(twitter).setAuthToken("test_token"); 133 | verify(twitter).setAuthTokenSecret("test_secret"); 134 | verifyNoMoreInteractions(twitter); 135 | } 136 | 137 | @Test 138 | public void testSetAuthDataNull() { 139 | TwitterController controller = new TwitterController(twitter); 140 | 141 | controller.setAuthData(null); 142 | verify(twitter).setUserId(null); 143 | verify(twitter).setScreenName(null); 144 | verify(twitter).setAuthToken(null); 145 | verify(twitter).setAuthTokenSecret(null); 146 | verifyNoMoreInteractions(twitter); 147 | } 148 | 149 | //endregion 150 | } 151 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library' 2 | --------------------------------------------------------------------------------