package com.lh.fly.ui.activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lh.fly.App;
import com.lh.fly.R;
import com.lh.fly.api.AccessTokenKeeper;
import com.lh.fly.api.Constants;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;
import java.text.SimpleDateFormat;
public class LoginActivity extends BaseActivity {
/**
* 注意:SsoHandler 仅当 SDK 支持 SSO 时有效
*/
private SsoHandler mSsoHandler;
private AuthInfo mAuthInfo;
private TextView tvLoginTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Oauth2AccessToken token = AccessTokenKeeper.readAccessToken(getApplicationContext());
Log.i(TAG, "Oauth2AccessToken " + token.getExpiresTime() + " " + System.currentTimeMillis());
// 未过期 就跳转Home
if (token.getExpiresTime() > System.currentTimeMillis()) {
goHome();
} else {
setContentView(R.layout.activity_login);
init();
}
}
protected void init() {
// 设置Logo 1/4 处
tvLoginTitle = (TextView) findViewById(R.id.tv_login_title);
tvLoginTitle.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvLoginTitle.getLayoutParams();
layoutParams.setMargins(0, Math.round(App.SCREEN_HEIGHT / 4f), 0, 0);
tvLoginTitle.setLayoutParams(layoutParams);
}
});
// 快速授权时,请不要传入 SCOPE,否则可能会授权不成功
mAuthInfo = new AuthInfo(this, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE);
mSsoHandler = new SsoHandler(LoginActivity.this, mAuthInfo);
// SSO 授权, 仅Web
findViewById(R.id.btn_login).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
mSsoHandler.authorizeWeb(new AuthListener());
}
});
}
/**
* 微博认证授权回调类。 1. SSO 授权时,需要在 {@link #onActivityResult} 中调用
* {@link SsoHandler#authorizeCallBack} 后, 该回调才会被执行。 2. 非 SSO
* 授权时,当授权结束后,该回调就会被执行。 当授权成功后,请保存该 access_token、expires_in、uid 等信息到
* SharedPreferences 中。
*/
class AuthListener implements WeiboAuthListener {
@Override
public void onComplete(Bundle values) {
// 获取 Token View,并让提示 View 的内容可滚动(小屏幕可能显示不全)
// 从 Bundle 中解析 Token
mAccessToken = Oauth2AccessToken.parseAccessToken(values);
if (mAccessToken.isSessionValid()) {
// 保存 Token 到 SharedPreferences
AccessTokenKeeper.writeAccessToken(getApplicationContext(), mAccessToken);
goHome();
} else {
// 以下几种情况,您会收到 Code:
// 1. 当您未在平台上注册的应用程序的包名与签名时;
// 2. 当您注册的应用程序包名与签名不正确时;
// 3. 当您在平台上注册的包名和签名与您当前测试的应用的包名和签名不匹配时。
String code = values.getString("code");
String message = getString(R.string.weibosdk_demo_toast_auth_failed);
if (!TextUtils.isEmpty(code)) {
message = message + "\nObtained the code: " + code;
}
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancel() {
Toast.makeText(
getApplicationContext(),
R.string.weibosdk_demo_toast_auth_canceled,
Toast.LENGTH_LONG).show();
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(
getApplicationContext(),
"Auth exception : " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
/**
* 封装了 "access_token","expires_in","refresh_token",并提供了他们的管理功能
*/
private Oauth2AccessToken mAccessToken;
private void goHome() {
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
finish();
}
}