|
|
|
|
@@ -3,6 +3,7 @@ package net.irext.webapi;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import net.irext.webapi.model.*;
|
|
|
|
|
@@ -38,6 +39,11 @@ public class WebAPIs {
|
|
|
|
|
private static final String DEFAULT_APP = "/irext-server";
|
|
|
|
|
private static String URL_PREFIX = DEFAULT_ADDRESS + DEFAULT_APP;
|
|
|
|
|
|
|
|
|
|
// download bin from OSS
|
|
|
|
|
private static final String IR_BIN_FILE_PREFIX = "irda_";
|
|
|
|
|
private static final String IR_BIN_FILE_SUFFIX = ".bin";
|
|
|
|
|
private static final String IR_BIN_DOWNLOAD_PREFIX = "http://irext-debug.oss-cn-hangzhou.aliyuncs.com/";
|
|
|
|
|
|
|
|
|
|
private static final String SERVICE_SIGN_IN = "/app/app_login";
|
|
|
|
|
private static final String SERVICE_LIST_CATEGORIES = "/indexing/list_categories";
|
|
|
|
|
private static final String SERVICE_LIST_BRANDS = "/indexing/list_brands";
|
|
|
|
|
@@ -48,7 +54,7 @@ public class WebAPIs {
|
|
|
|
|
private static final String SERVICE_DOWNLOAD_BIN = "/operation/download_bin";
|
|
|
|
|
private static final String SERVICE_ONLINE_DECODE = "/operation/decode";
|
|
|
|
|
|
|
|
|
|
private int adminId;
|
|
|
|
|
private int id;
|
|
|
|
|
private String token;
|
|
|
|
|
|
|
|
|
|
private OkHttpClient mHttpClient;
|
|
|
|
|
@@ -72,6 +78,16 @@ public class WebAPIs {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private InputStream getFileByteStreamByURL(String url) throws IOException {
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(url)
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
Response response = new OkHttpClient().newCall(request).execute();
|
|
|
|
|
return response.body().byteStream();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String postToServer(String url, String json) throws IOException {
|
|
|
|
|
MediaType JSON
|
|
|
|
|
= MediaType.parse("application/json; charset=utf-8");
|
|
|
|
|
@@ -123,14 +139,17 @@ public class WebAPIs {
|
|
|
|
|
appSignInRequest.setAndroidSignature(signature);
|
|
|
|
|
String bodyJson = appSignInRequest.toJson();
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Android APP example sign-in request data : " + bodyJson);
|
|
|
|
|
String response = postToServer(signInURL, bodyJson);
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Android APP example sign-in response data : " + response);
|
|
|
|
|
LoginResponse loginResponse = new Gson().fromJson(response, LoginResponse.class);
|
|
|
|
|
if (loginResponse.getStatus().getCode() == Constants.ERROR_CODE_SUCCESS) {
|
|
|
|
|
UserApp admin = loginResponse.getEntity();
|
|
|
|
|
if (0 != admin.getId() && null != admin.getToken()) {
|
|
|
|
|
adminId = admin.getId();
|
|
|
|
|
token = admin.getToken();
|
|
|
|
|
signInCallback.onSignInSuccess(admin);
|
|
|
|
|
UserApp userApp = loginResponse.getEntity();
|
|
|
|
|
if (0 != userApp.getId() && null != userApp.getToken()) {
|
|
|
|
|
id = userApp.getId();
|
|
|
|
|
token = userApp.getToken();
|
|
|
|
|
signInCallback.onSignInSuccess(userApp);
|
|
|
|
|
} else {
|
|
|
|
|
signInCallback.onSignInFailed();
|
|
|
|
|
}
|
|
|
|
|
@@ -145,7 +164,7 @@ public class WebAPIs {
|
|
|
|
|
public void listCategories(int from, int count, ListCategoriesCallback listCategoriesCallback) {
|
|
|
|
|
String listCategoriesURL = URL_PREFIX + SERVICE_LIST_CATEGORIES;
|
|
|
|
|
ListCategoriesRequest listCategoriesRequest = new ListCategoriesRequest();
|
|
|
|
|
listCategoriesRequest.setAdminId(adminId);
|
|
|
|
|
listCategoriesRequest.setId(id);
|
|
|
|
|
listCategoriesRequest.setToken(token);
|
|
|
|
|
listCategoriesRequest.setFrom(from);
|
|
|
|
|
listCategoriesRequest.setCount(count);
|
|
|
|
|
@@ -171,7 +190,7 @@ public class WebAPIs {
|
|
|
|
|
ListBrandsCallback listBrandsCallback) {
|
|
|
|
|
String listBrandsURL = URL_PREFIX + SERVICE_LIST_BRANDS;
|
|
|
|
|
ListBrandsRequest listBrandsRequest = new ListBrandsRequest();
|
|
|
|
|
listBrandsRequest.setAdminId(adminId);
|
|
|
|
|
listBrandsRequest.setId(id);
|
|
|
|
|
listBrandsRequest.setToken(token);
|
|
|
|
|
listBrandsRequest.setCategoryId(categoryId);
|
|
|
|
|
listBrandsRequest.setFrom(from);
|
|
|
|
|
@@ -197,7 +216,7 @@ public class WebAPIs {
|
|
|
|
|
public void listProvinces(ListProvincesCallback listProvincesCallback) {
|
|
|
|
|
String listProvincesURL = URL_PREFIX + SERVICE_LIST_PROVINCES;
|
|
|
|
|
ListCitiesRequest listCitiesRequest = new ListCitiesRequest();
|
|
|
|
|
listCitiesRequest.setAdminId(adminId);
|
|
|
|
|
listCitiesRequest.setId(id);
|
|
|
|
|
listCitiesRequest.setToken(token);
|
|
|
|
|
String bodyJson = listCitiesRequest.toJson();
|
|
|
|
|
|
|
|
|
|
@@ -220,7 +239,7 @@ public class WebAPIs {
|
|
|
|
|
public void listCities(String prefix, ListCitiesCallback listCitiesCallback) {
|
|
|
|
|
String listCitiesURL = URL_PREFIX + SERVICE_LIST_CITIES;
|
|
|
|
|
ListCitiesRequest listCitiesRequest = new ListCitiesRequest();
|
|
|
|
|
listCitiesRequest.setAdminId(adminId);
|
|
|
|
|
listCitiesRequest.setId(id);
|
|
|
|
|
listCitiesRequest.setToken(token);
|
|
|
|
|
listCitiesRequest.setProvincePrefix(prefix);
|
|
|
|
|
String bodyJson = listCitiesRequest.toJson();
|
|
|
|
|
@@ -245,7 +264,7 @@ public class WebAPIs {
|
|
|
|
|
ListOperatersCallback listOperatersCallback) {
|
|
|
|
|
String listOperatorsURL = URL_PREFIX + SERVICE_LIST_OPERATORS;
|
|
|
|
|
ListOperatorsRequest listOperatorsRequest = new ListOperatorsRequest();
|
|
|
|
|
listOperatorsRequest.setAdminId(adminId);
|
|
|
|
|
listOperatorsRequest.setId(id);
|
|
|
|
|
listOperatorsRequest.setToken(token);
|
|
|
|
|
listOperatorsRequest.setCityCode(cityCode);
|
|
|
|
|
listOperatorsRequest.setFrom(0);
|
|
|
|
|
@@ -275,7 +294,7 @@ public class WebAPIs {
|
|
|
|
|
ListIndexesCallback onListIndexCallback) {
|
|
|
|
|
String listIndexesURL = URL_PREFIX + SERVICE_LIST_INDEXES;
|
|
|
|
|
ListIndexesRequest listIndexesRequest = new ListIndexesRequest();
|
|
|
|
|
listIndexesRequest.setAdminId(adminId);
|
|
|
|
|
listIndexesRequest.setId(id);
|
|
|
|
|
listIndexesRequest.setToken(token);
|
|
|
|
|
listIndexesRequest.setCategoryId(categoryId);
|
|
|
|
|
listIndexesRequest.setBrandId(brandId);
|
|
|
|
|
@@ -304,9 +323,10 @@ public class WebAPIs {
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
public void downloadBin(String remoteMap, int indexId,
|
|
|
|
|
DownloadBinCallback downloadBinCallback) {
|
|
|
|
|
String downloadURL = URL_PREFIX + SERVICE_DOWNLOAD_BIN;
|
|
|
|
|
String fileName = IR_BIN_FILE_PREFIX + remoteMap + IR_BIN_FILE_SUFFIX;
|
|
|
|
|
String downloadURL = IR_BIN_DOWNLOAD_PREFIX + fileName;
|
|
|
|
|
DownloadBinaryRequest downloadBinaryRequest = new DownloadBinaryRequest();
|
|
|
|
|
downloadBinaryRequest.setAdminId(adminId);
|
|
|
|
|
downloadBinaryRequest.setId(id);
|
|
|
|
|
downloadBinaryRequest.setToken(token);
|
|
|
|
|
downloadBinaryRequest.setIndexId(indexId);
|
|
|
|
|
|
|
|
|
|
@@ -314,7 +334,7 @@ public class WebAPIs {
|
|
|
|
|
|
|
|
|
|
if (null != bodyJson) {
|
|
|
|
|
try {
|
|
|
|
|
InputStream binStream = postToServerForOctets(downloadURL, bodyJson);
|
|
|
|
|
InputStream binStream = getFileByteStreamByURL(downloadURL);
|
|
|
|
|
|
|
|
|
|
if (null != binStream) {
|
|
|
|
|
downloadBinCallback.onDownloadBinSuccess(binStream);
|
|
|
|
|
@@ -333,7 +353,7 @@ public class WebAPIs {
|
|
|
|
|
public int[] decodeIR(int indexId) {
|
|
|
|
|
String decodeURL = URL_PREFIX + SERVICE_ONLINE_DECODE;
|
|
|
|
|
DecodeRequest decodeRequest = new DecodeRequest();
|
|
|
|
|
decodeRequest.setAdminId(adminId);
|
|
|
|
|
decodeRequest.setId(id);
|
|
|
|
|
decodeRequest.setToken(token);
|
|
|
|
|
decodeRequest.setIndexId(indexId);
|
|
|
|
|
|
|
|
|
|
|