|
| 1 | +package servicestack.net.androidchat; |
| 2 | + |
| 3 | +import android.animation.ObjectAnimator; |
| 4 | +import android.animation.ValueAnimator; |
| 5 | +import android.app.Activity; |
| 6 | +import android.content.Intent; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.support.v4.view.animation.FastOutLinearInInterpolator; |
| 9 | +import android.support.v7.app.AppCompatActivity; |
| 10 | +import android.support.v7.widget.Toolbar; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.Button; |
| 13 | +import android.widget.ProgressBar; |
| 14 | + |
| 15 | +import com.facebook.AccessToken; |
| 16 | +import com.facebook.CallbackManager; |
| 17 | +import com.facebook.FacebookCallback; |
| 18 | +import com.facebook.FacebookException; |
| 19 | +import com.facebook.login.LoginResult; |
| 20 | +import com.facebook.login.widget.LoginButton; |
| 21 | +import com.twitter.sdk.android.Twitter; |
| 22 | +import com.twitter.sdk.android.core.Callback; |
| 23 | +import com.twitter.sdk.android.core.Result; |
| 24 | +import com.twitter.sdk.android.core.TwitterAuthConfig; |
| 25 | +import com.twitter.sdk.android.core.TwitterException; |
| 26 | +import com.twitter.sdk.android.core.TwitterSession; |
| 27 | +import com.twitter.sdk.android.core.identity.TwitterLoginButton; |
| 28 | + |
| 29 | +import net.servicestack.android.AndroidServiceClient; |
| 30 | +import net.servicestack.client.Log; |
| 31 | + |
| 32 | +import io.fabric.sdk.android.Fabric; |
| 33 | + |
| 34 | +/** |
| 35 | + * This Login Page signs in using Facebook and Twitter's SDK Buttons |
| 36 | + */ |
| 37 | + |
| 38 | +public class LoginButtonsActivity extends AppCompatActivity { |
| 39 | + private ProgressBar progressBar; |
| 40 | + private ObjectAnimator animation; |
| 41 | + |
| 42 | + private TwitterLoginButton btnTwitterLogin; |
| 43 | + private LoginButton btnFacebookLogin; |
| 44 | + private CallbackManager facebookCallback; |
| 45 | + private Button btnGuestLogin; |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void onCreate(Bundle savedInstanceState) { |
| 49 | + super.onCreate(savedInstanceState); |
| 50 | + |
| 51 | + Fabric.with(this, new Twitter(new TwitterAuthConfig( |
| 52 | + getString(R.string.twitter_key), |
| 53 | + getString(R.string.twitter_secret)))); |
| 54 | + |
| 55 | + setContentView(R.layout.login_buttons); |
| 56 | + |
| 57 | + Toolbar toolbar = (Toolbar) findViewById(R.id.loginToolbar); |
| 58 | + setSupportActionBar(toolbar); |
| 59 | + getSupportActionBar().setHomeButtonEnabled(true); |
| 60 | + getSupportActionBar().setDisplayShowTitleEnabled(true); |
| 61 | + |
| 62 | + progressBar = (ProgressBar) findViewById(R.id.progressBar); |
| 63 | + |
| 64 | + animation = ObjectAnimator.ofInt(progressBar, "progress", 0, 500); // see this max value coming back here, we animale towards that value |
| 65 | + animation.setRepeatMode(ValueAnimator.REVERSE); |
| 66 | + animation.setRepeatCount(100); |
| 67 | + animation.setDuration(1500); |
| 68 | + animation.setInterpolator(new FastOutLinearInInterpolator()); |
| 69 | + |
| 70 | + LoginButtonsActivity activity = this; |
| 71 | + |
| 72 | + btnTwitterLogin = (TwitterLoginButton) findViewById(R.id.btnTwitterLogin); |
| 73 | + btnTwitterLogin.setCallback(new Callback<TwitterSession>() { |
| 74 | + @Override |
| 75 | + public void success(Result<TwitterSession> result) { |
| 76 | + startProgressBar(); |
| 77 | + |
| 78 | + TwitterSession session = result.data; |
| 79 | + App.get().getServiceClient().postAsync(new dtos.Authenticate() |
| 80 | + .setProvider("twitter") |
| 81 | + .setAccessToken(session.getAuthToken().token) |
| 82 | + .setAccessTokenSecret(session.getAuthToken().secret) |
| 83 | + .setRememberMe(true), |
| 84 | + r -> { |
| 85 | + Intent intent = new Intent(activity, MainActivity.class); |
| 86 | + startActivity(intent); |
| 87 | + stopProgressBar(); |
| 88 | + }, |
| 89 | + error -> { |
| 90 | + Log.e("TwitterAuthClient FAILED!", error); |
| 91 | + stopProgressBar(); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void failure(TwitterException exception) { |
| 97 | + Log.e(exception); |
| 98 | + stopProgressBar(); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + facebookCallback = CallbackManager.Factory.create(); |
| 103 | + btnFacebookLogin = (LoginButton) findViewById(R.id.btnFacebookLogin); |
| 104 | + btnFacebookLogin.setReadPermissions("email"); |
| 105 | + btnFacebookLogin.registerCallback(facebookCallback, new FacebookCallback<LoginResult>() { |
| 106 | + @Override |
| 107 | + public void onSuccess(LoginResult loginResult) { |
| 108 | + startProgressBar(); |
| 109 | + loginWithFacebook(loginResult.getAccessToken()); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void onCancel() { |
| 114 | + stopProgressBar(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onError(FacebookException exception) { |
| 119 | + Log.e(exception); |
| 120 | + stopProgressBar(); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + Button btnGuestLogin = (Button)findViewById(R.id.btnGuestLogin); |
| 125 | + btnGuestLogin.setOnClickListener(view -> { |
| 126 | + startProgressBar(); |
| 127 | + startGuestChatActivity(App.get().getServiceClient()); |
| 128 | + stopProgressBar(); |
| 129 | + }); |
| 130 | + |
| 131 | + //Login with facebook if already logged in |
| 132 | +// AccessToken accessToken = AccessToken.getCurrentAccessToken(); |
| 133 | +// if (accessToken != null){ |
| 134 | +// loginWithFacebook(accessToken); |
| 135 | +// } |
| 136 | + } |
| 137 | + |
| 138 | + private void loginWithFacebook(AccessToken accessToken){ |
| 139 | + LoginButtonsActivity activity = this; |
| 140 | + App.get().getServiceClient().postAsync(new dtos.Authenticate() |
| 141 | + .setProvider("facebook") |
| 142 | + .setAccessToken(accessToken.getToken()) |
| 143 | + .setRememberMe(true), |
| 144 | + r -> { |
| 145 | + Intent intent = new Intent(activity, MainActivity.class); |
| 146 | + startActivity(intent); |
| 147 | + stopProgressBar(); |
| 148 | + }, |
| 149 | + error -> { |
| 150 | + Log.e("Facebook LoginButton FAILED!", error); |
| 151 | + stopProgressBar(); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 157 | + super.onActivityResult(requestCode, resultCode, data); |
| 158 | + btnTwitterLogin.onActivityResult(requestCode, resultCode, data); |
| 159 | + facebookCallback.onActivityResult(requestCode, resultCode, data); |
| 160 | + } |
| 161 | + |
| 162 | + private void startProgressBar(){ |
| 163 | + runOnUiThread(() -> { |
| 164 | + progressBar.setVisibility(View.VISIBLE); |
| 165 | + animation.start(); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + private void stopProgressBar(){ |
| 170 | + runOnUiThread(() -> { |
| 171 | + progressBar.clearAnimation(); |
| 172 | + progressBar.setVisibility(View.INVISIBLE); |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + private void startGuestChatActivity(AndroidServiceClient client) |
| 177 | + { |
| 178 | + client.clearCookies(); |
| 179 | + Intent intent = new Intent(this, MainActivity.class); |
| 180 | + startActivity(intent); |
| 181 | + } |
| 182 | +} |
0 commit comments