Skip to content

Commit

Permalink
add twitter login and logout
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel163 committed Jan 8, 2017
1 parent e0ca49c commit 6751dd2
Show file tree
Hide file tree
Showing 15 changed files with 347 additions and 5 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
apply plugin: 'com.android.application'

repositories {
maven { url 'https://maven.fabric.io/public' }
}

buildscript {
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
Expand All @@ -26,5 +40,6 @@ dependencies {
compile project(':instagram')
compile project(':facebook')
compile project(':vk')
compile project(':twitter')
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/ebr163/socialauth/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void onCreate() {
return;
}
refWatcher = LeakCanary.install(this);


}

@Override
Expand Down
30 changes: 29 additions & 1 deletion app/src/main/java/com/ebr163/socialauth/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.ebr163.socialauth.google.GooglePlusProfile;
import com.ebr163.socialauth.instagram.InstagramClient;
import com.ebr163.socialauth.instagram.model.InstagramProfile;
import com.ebr163.socialauth.twitter.TwitterClient;
import com.ebr163.socialauth.twitter.TwitterProfile;
import com.ebr163.socialauth.vk.VkClient;
import com.ebr163.socialauth.vk.model.VkProfile;
import com.facebook.FacebookException;
Expand All @@ -22,12 +24,14 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
VkClient.VkProfileLoadedListener, VkClient.VkLogOutListener,
FacebookClient.FacebookProfileLoadedListener, FacebookClient.FacebookLogOutListener,
GooglePlusClient.GooglePlusProfileLoadedListener, GooglePlusClient.GooglePlusLogOutListener,
InstagramClient.InstagramProfileLoadedListener, InstagramClient.InstagramLogOutListener {
InstagramClient.InstagramProfileLoadedListener, InstagramClient.InstagramLogOutListener,
TwitterClient.TwitterProfileLoadedListener, TwitterClient.TwitterLogOutListener {

GooglePlusClient googlePlusClient;
InstagramClient instagramClient;
FacebookClient facebookClient;
VkClient vkClient;
TwitterClient twitterClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -48,6 +52,10 @@ protected void onCreate(Bundle savedInstanceState) {
vkClient = new VkClient(this);
vkClient.setVkLogOutListener(this);
vkClient.setVkProfileLoadedListener(this);

twitterClient = new TwitterClient(this);
twitterClient.setTwitterProfileLoadedListener(this);
twitterClient.setTwitterLogOutListener(this);
}

@Override
Expand Down Expand Up @@ -77,6 +85,12 @@ public void onClick(View view) {
case R.id.vk_logout:
vkClient.logOut();
break;
case R.id.twitter:
twitterClient.getProfile();
break;
case R.id.twitter_logout:
twitterClient.logOut();
break;
default:
break;
}
Expand All @@ -89,6 +103,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
instagramClient.onActivityResult(requestCode, resultCode, data);
facebookClient.onActivityResult(requestCode, resultCode, data);
vkClient.onActivityResult(requestCode, resultCode, data);
twitterClient.onActivityResult(requestCode, resultCode, data);
}

private void toNextScreen() {
Expand Down Expand Up @@ -154,6 +169,19 @@ public void onInstagramProfileLoaded(InstagramProfile instagramProfile) {

@Override
public void onErrorInstagramProfileLoaded(Exception exception) {
}

@Override
public void onLogOutTwitter() {
Toast.makeText(MainActivity.this, "twitter logout", Toast.LENGTH_SHORT).show();
}

@Override
public void onTwitterProfileLoaded(TwitterProfile twitterProfile) {
Toast.makeText(MainActivity.this, "twitter", Toast.LENGTH_SHORT).show();
}

@Override
public void onErrorTwitterProfileLoaded(Exception exception) {
}
}
28 changes: 28 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,32 @@
android:textColor="@android:color/white" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/twitter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:onClick="onClick"
android:text="twitter"
android:textColor="@android:color/white" />

<Button
android:id="@+id/twitter_logout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:onClick="onClick"
android:text="twitter Logout"
android:textColor="@android:color/white" />
</LinearLayout>

</LinearLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@

<string name="instagram_redirect_uri">https://oauth.vk.com/blank.html</string>
<string name="instagram_app_id">bc0cfc8d49664f9eb3f920acef8c73e1</string>

<string name="facebook_app_id">360833260945108</string>

<string name="twitter_consumer_key">GpiqBsOZiIwApJVcPQ72mgESz</string>
<string name="twitter_consumer_secret">UoiX7PzvZlVHUZR92nLijuS08rIw50Z5WtWYYNqD95eyH78XWW</string>
</resources>
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include ':app'
include ':vk'
include ':instagram'
//include ':twitter'
include ':twitter'
include ':facebook'
include ':google'
11 changes: 8 additions & 3 deletions twitter/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ebr163.socialauth.twitter">

<application android:allowBackup="true" android:label="@string/app_name"
android:supportsRtl="true">
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">
<activity android:name=".TwitterAuthActivity" />
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.ebr163.socialauth.twitter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;

public class TwitterAuthActivity extends AppCompatActivity {

public static final String KEY_ACCESS_TOKEN = "access_token";
public static final String CODE_EXCEPTION = "code_exception";

private TwitterAuthClient twitterClient;
private boolean wasShown;

public static void authorize(Fragment fragment, int requestCode) {
fragment.startActivityForResult(
getIntent(fragment.getActivity()),
requestCode
);
fragment.getActivity().overridePendingTransition(0, 0);
}

public static void authorize(Activity activity, int requestCode) {
activity.startActivityForResult(
getIntent(activity),
requestCode
);
activity.overridePendingTransition(0, 0);
}

private static Intent getIntent(Context context) {
return new Intent(context, TwitterAuthActivity.class);
}

@Override
protected void onStart() {
super.onStart();

if (!wasShown) {
wasShown = true;
twitterClient = new TwitterAuthClient();
twitterClient.authorize(this,
new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
Intent resultIntent = new Intent();
resultIntent.putExtra(KEY_ACCESS_TOKEN, result.data.getAuthToken().toString());
setResult(Activity.RESULT_OK, resultIntent);
finish();
}

@Override
public void failure(TwitterException e) {
Intent resultIntent = new Intent();
resultIntent.putExtra(CODE_EXCEPTION, e.getMessage());
setResult(RESULT_CANCELED, resultIntent);
finish();
}
}
);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
twitterClient.onActivityResult(requestCode, resultCode, data);
}

@Override
public void finish() {
super.finish();
overridePendingTransition(0, 0);
}
}
Loading

0 comments on commit 6751dd2

Please sign in to comment.