/*
* Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.partynetwork.iparty.app.common;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.renn.rennsdk.AccessToken;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.tencent.weibo.sdk.android.component.sso.WeiboToken;
/**
* 该类定义了微博授权时所需要的参数。
*
* @author SINA
* @since 2013-10-07
*/
public class AccessTokenKeeper {
private static final String PREFERENCES_NAME = "com_weibo_sdk_android";
private static final String SINA_KEY_UID = "sina_uid";
private static final String SINA_KEY_ACCESS_TOKEN = "sina_access_token";
private static final String SINA_KEY_EXPIRES_IN = "sina_expires_in";
private static final String QQ_KEY_UID = "qq_uid";
private static final String QQ_KEY_ACCESS_TOKEN = "qq_access_token";
private static final String QQ_KEY_EXPIRES_IN = "qq_expires_in";
private static final String RENREN_KEY_UID = "renren_uid";
private static final String RENREN_KEY_ACCESS_TOKEN = "renren_access_token";
private static final String RENREN_KEY_EXPIRES_IN = "renren_expires_in";
/**
* 保存 sinaToken 对象到 SharedPreferences。
*
* @param context
* 应用程序上下文环境
* @param token
* Token 对象
*/
public static void writeSinaAccessToken(Context context,
Oauth2AccessToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(SINA_KEY_UID, token.getUid());
editor.putString(SINA_KEY_ACCESS_TOKEN, token.getToken());
editor.putLong(SINA_KEY_EXPIRES_IN, token.getExpiresTime());
editor.commit();
}
/**
* 保存 QQToken 对象到 SharedPreferences。
*
* @param context
* 应用程序上下文环境
* @param token
* Token 对象
*/
public static void writeQQAccessToken(Context context, WeiboToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(QQ_KEY_UID, token.openID);
editor.putString(QQ_KEY_ACCESS_TOKEN, token.accessToken);
editor.putLong(QQ_KEY_EXPIRES_IN, token.expiresIn);
editor.commit();
}
/**
* 保存 QQToken 对象到 SharedPreferences。
*
* @param context
* 应用程序上下文环境
* @param token
* Token 对象
*/
public static void writeRenRenAccessToken(Context context, AccessToken token) {
if (null == context || null == token) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_APPEND);
Editor editor = pref.edit();
editor.putString(RENREN_KEY_ACCESS_TOKEN, token.accessToken);
editor.putLong(RENREN_KEY_EXPIRES_IN, token.expiresIn);
editor.commit();
}
/**
* 从 SharedPreferences 读取 SinaToken 信息。
*
* @param context
* 应用程序上下文环境
*
* @return 返回 Token 对象
*/
public static Oauth2AccessToken readSinaAccessToken(Context context) {
if (null == context) {
return null;
}
Oauth2AccessToken token = new Oauth2AccessToken();
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_APPEND);
token.setUid(pref.getString(SINA_KEY_UID, ""));
token.setToken(pref.getString(SINA_KEY_ACCESS_TOKEN, ""));
token.setExpiresTime(pref.getLong(SINA_KEY_EXPIRES_IN, 0));
return token;
}
/**
* 从 SharedPreferences 读取 QQToken 信息。
*
* @param context
* 应用程序上下文环境
*
* @return 返回 Token 对象
*/
public static WeiboToken readQQAccessToken(Context context) {
if (null == context) {
return null;
}
WeiboToken token = new WeiboToken();
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_APPEND);
token.openID = pref.getString(QQ_KEY_UID, "");
token.accessToken = pref.getString(QQ_KEY_ACCESS_TOKEN, "");
token.expiresIn = pref.getLong(QQ_KEY_EXPIRES_IN, 0);
return token;
}
/**
* 从 SharedPreferences 读取 QQToken 信息。
*
* @param context
* 应用程序上下文环境
*
* @return 返回 Token 对象
*/
public static AccessToken readRenRenAccessToken(Context context) {
if (null == context) {
return null;
}
AccessToken token = new AccessToken();
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_APPEND);
token.accessToken = pref.getString(RENREN_KEY_ACCESS_TOKEN, "");
token.expiresIn = pref.getLong(RENREN_KEY_EXPIRES_IN, 0);
return token;
}
/**
* 清空 SharedPreferences 中 Token信息。
*
* @param context
* 应用程序上下文环境
*/
public static void clear(Context context) {
if (null == context) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME,
Context.MODE_APPEND);
Editor editor = pref.edit();
editor.clear();
editor.commit();
}
}