package com.gc.android.market.api;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.gc.android.market.api.model.Market.AppsRequest;
import com.gc.android.market.api.model.Market.AppsResponse;
import com.gc.android.market.api.model.Market.CategoriesRequest;
import com.gc.android.market.api.model.Market.CategoriesResponse;
import com.gc.android.market.api.model.Market.CommentsRequest;
import com.gc.android.market.api.model.Market.CommentsResponse;
import com.gc.android.market.api.model.Market.GetImageRequest;
import com.gc.android.market.api.model.Market.GetImageResponse;
import com.gc.android.market.api.model.Market.Request;
import com.gc.android.market.api.model.Market.Request.RequestGroup;
import com.gc.android.market.api.model.Market.RequestContext;
import com.gc.android.market.api.model.Market.Response;
import com.gc.android.market.api.model.Market.Response.ResponseGroup;
import com.gc.android.market.api.model.Market.ResponseContext;
import com.gc.android.market.api.model.Market.GetAssetRequest;
import com.gc.android.market.api.model.Market.GetAssetResponse;;
/**
* MarketSession session = new MarketSession();
* session.login(login,password, androidId);
* For asyncronous calls use append, callback and flush
* session.append(xxx,yyy);
* session.append(xxx,yyy);
* ...
* session.flush();
* For syncronous call, use the specific method
*/
public class MarketSession {
public static interface Callback {
public void onResult(ResponseContext context, T response);
}
/*
* SERVICE : Service required to the market.
* Default value: android. This service must be used to query info to the Market
* androidsecure: This service must be used to download apps
* sierra (checkout): This service must be used for checkout (at moment unused)
*/
public String SERVICE = "android";
private static final String URL_LOGIN = "https://www.google.com/accounts/ClientLogin";
public static final String ACCOUNT_TYPE_GOOGLE = "GOOGLE";
public static final String ACCOUNT_TYPE_HOSTED = "HOSTED";
public static final String ACCOUNT_TYPE_HOSTED_OR_GOOGLE = "HOSTED_OR_GOOGLE";
public static final int PROTOCOL_VERSION = 2;
Request.Builder request = Request.newBuilder();
RequestContext.Builder context = RequestContext.newBuilder();
public RequestContext.Builder getContext() {
return context;
}
List> callbacks = new Vector>();
String authSubToken = null;
public String getAuthSubToken() {
return authSubToken;
}
/*
* Login must set isSecure to false for list and download
*/
public MarketSession(Boolean isSecure) {
if (isSecure)
SERVICE = "androidsecure";
else
SERVICE = "android";
context.setIsSecure(false);
context.setVersion(2009011);
setLocale(Locale.getDefault());
context.setDeviceAndSdkVersion("passion:9");
setOperatorTMobile();
}
public void setLocale(Locale locale) {
context.setUserLanguage(locale.getLanguage().toLowerCase());
context.setUserCountry(locale.getCountry().toLowerCase());
}
public void setOperator(String alpha, String numeric) {
setOperator(alpha, alpha, numeric, numeric);
}
public void setOperatorTMobile() {
setOperator("T-Mobile", "310260");
}
public void setOperatorSFR() {
setOperator("F SFR", "20810");
}
public void setOperatorO2() {
setOperator("o2 - de", "26207");
}
public void setOperatorSimyo() {
setOperator("E-Plus", "simyo", "26203", "26203");
}
public void setOperatorSunrise() {
setOperator("sunrise", "22802");
}
/**
* http://www.2030.tk/wiki/Android_market_switch
*/
public void setOperator(String alpha, String simAlpha, String numeric, String simNumeric) {
context.setOperatorAlpha(alpha);
context.setSimOperatorAlpha(simAlpha);
context.setOperatorNumeric(numeric);
context.setSimOperatorNumeric(simNumeric);
}
public void setAuthSubToken(String authSubToken) {
context.setAuthSubToken(authSubToken);
this.authSubToken = authSubToken;
}
public void setIsSecure(Boolean isSecure) {
context.setIsSecure(isSecure);
}
public void setAndroidId(String androidId) {
context.setAndroidId(androidId);
}
public void login(String email, String password, String androidId) {
this.login(email, password, androidId, ACCOUNT_TYPE_HOSTED_OR_GOOGLE);
}
public void login(String email, String password, String androidId,
String accountType) {
//Android ID must an unique identifier associated to the account
//used in in the login
setAndroidId(androidId);
Map params = new LinkedHashMap();
params.put("Email", email);
params.put("Passwd", password);
params.put("service", SERVICE);
// params.put("source", source);
params.put("accountType", accountType);
// Login at Google.com
try {
String data = Tools.postUrl(URL_LOGIN, params);
StringTokenizer st = new StringTokenizer(data, "nr=");
String authKey = null;
while (st.hasMoreTokens()) {
if (st.nextToken().equalsIgnoreCase("Auth")) {
authKey = st.nextToken();
break;
}
}
if(authKey == null)
throw new RuntimeException("authKey not found in "+ data);
setAuthSubToken(authKey);
} catch(Tools.HttpException httpEx) {
if(httpEx.getErrorCode() != 403)
throw httpEx;
String data = httpEx.getErrorData();
StringTokenizer st = new StringTokenizer(data, "nr=");
String googleErrorCode = null;
while (st.hasMoreTokens()) {
if (st.nextToken().equalsIgnoreCase("Error")) {
googleErrorCode = st.nextToken();
break;
}
}
if(googleErrorCode == null)
throw httpEx;
throw new LoginException(googleErrorCode);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public List
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net