updated Android example using new webapi

This commit is contained in:
2017-07-02 10:01:23 +08:00
parent ff4ce90915
commit ced8791cb7
45 changed files with 3113 additions and 57 deletions

View File

@@ -4,8 +4,10 @@ import android.util.Log;
import com.activeandroid.ActiveAndroid;
import net.irext.webapi.WebAPICallbacks;
import net.irext.webapi.WebAPIs;
import net.irext.webapi.model.UserApp;
import net.irext.webapi.WebAPICallbacks.SignInCallback;
/**
* Filename: IRApplication.java
@@ -29,6 +31,23 @@ public class IRApplication extends com.activeandroid.app.Application {
private UserApp mUserApp;
private SignInCallback mSignInCallback = new SignInCallback() {
@Override
public void onSignInSuccess(UserApp admin) {
mUserApp = admin;
}
@Override
public void onSignInFailed() {
Log.w(TAG, "sign in failed");
}
@Override
public void onSignInError() {
Log.e(TAG, "sign in error");
}
};
public UserApp getAdmin() {
return mUserApp;
}
@@ -44,7 +63,7 @@ public class IRApplication extends com.activeandroid.app.Application {
new Thread() {
@Override
public void run() {
mUserApp = mWeAPIs.signIn(IRApplication.this);
mWeAPIs.signIn(IRApplication.this, mSignInCallback);
if (null != mUserApp) {
Log.d(TAG, "signIn response : " + mUserApp.getId() + ", " + mUserApp.getToken());
}

View File

@@ -15,6 +15,7 @@ import net.irext.ircontrol.ui.activity.CreateActivity;
import net.irext.ircontrol.ui.adapter.BrandAdapter;
import net.irext.ircontrol.ui.widget.PullToRefreshListView;
import net.irext.ircontrol.utils.MessageUtil;
import net.irext.webapi.WebAPICallbacks.ListBrandsCallback;
import net.irext.webapi.model.Brand;
import java.lang.ref.WeakReference;
@@ -46,6 +47,27 @@ public class BrandFragment extends BaseCreateFragment {
private MsgHandler mMsgHandler;
private IRApplication mApp;
private ListBrandsCallback mListBrandsCallback = new ListBrandsCallback() {
@Override
public void onListBrandsSuccess(List<Brand> brands) {
mBrands = brands;
if (null == mBrands) {
mBrands = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_BRAND_LIST);
}
@Override
public void onListBrandsFailed() {
Log.w(TAG, "list brands failed");
}
@Override
public void onListBrandsError() {
Log.e(TAG, "list brands error");
}
};
public BrandFragment() {
}
@@ -53,12 +75,9 @@ public class BrandFragment extends BaseCreateFragment {
new Thread() {
@Override
public void run() {
mBrands = mApp.mWeAPIs
.listBrands(mParent.getCurrentCategory().getId(), 0, 20);
if (null == mBrands) {
mBrands = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_BRAND_LIST);
mApp.mWeAPIs
.listBrands(mParent.getCurrentCategory().getId(), 0, 20,
mListBrandsCallback);
}
}.start();
}

View File

@@ -17,6 +17,7 @@ import net.irext.ircontrol.ui.widget.PullToRefreshListView;
import net.irext.ircontrol.utils.MessageUtil;
import net.irext.decodesdk.utils.Constants;
import net.irext.webapi.model.Category;
import net.irext.webapi.WebAPICallbacks.ListCategoriesCallback;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
@@ -46,6 +47,27 @@ public class CategoryFragment extends BaseCreateFragment {
private MsgHandler mMsgHandler;
private IRApplication mApp;
private ListCategoriesCallback mListCategoriesCallback = new ListCategoriesCallback() {
@Override
public void onListCategoriesSuccess(List<Category> categories) {
mCategories = categories;
if (null == mCategories) {
mCategories = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_CATEGORY_LIST);
}
@Override
public void onListCategoriesFailed() {
Log.w(TAG, "list categories failed");
}
@Override
public void onListCategoriesError() {
Log.e(TAG, "list categories error");
}
};
public CategoryFragment() {
}
@@ -53,11 +75,7 @@ public class CategoryFragment extends BaseCreateFragment {
new Thread() {
@Override
public void run() {
mCategories = mApp.mWeAPIs.listCategories(0, 20);
if (null == mCategories) {
mCategories = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_CATEGORY_LIST);
mApp.mWeAPIs.listCategories(0, 20, mListCategoriesCallback);
}
}.start();
}

View File

@@ -16,6 +16,11 @@ import net.irext.ircontrol.ui.activity.CreateActivity;
import net.irext.ircontrol.ui.adapter.CityAdapter;
import net.irext.ircontrol.ui.adapter.OperatorAdapter;
import net.irext.ircontrol.utils.MessageUtil;
import net.irext.webapi.WebAPICallbacks.ListProvincesCallback;
import net.irext.webapi.WebAPICallbacks.ListCitiesCallback;
import net.irext.webapi.WebAPICallbacks.ListOperatersCallback;
import net.irext.webapi.model.City;
import net.irext.webapi.model.StbOperator;
@@ -57,6 +62,70 @@ public class CityFragment extends BaseCreateFragment {
private MsgHandler mMsgHandler;
private IRApplication mApp;
private ListProvincesCallback mListProvincesCallback = new ListProvincesCallback() {
@Override
public void onListProvincesSuccess(List<City> provinces) {
mProvinces = provinces;
if (null == mProvinces) {
mProvinces = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_PROVINCE_LIST);
}
@Override
public void onListProvincesFailed() {
Log.w(TAG, "list provinces failed");
}
@Override
public void onListProvincesError() {
Log.e(TAG, "list provinces error");
}
};
private ListCitiesCallback mListCitiesCallback = new ListCitiesCallback() {
@Override
public void onListCitiesSuccess(List<City> cities) {
mCities = cities;
if (null == mCities) {
mCities = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_CITY_LIST);
}
@Override
public void onListCitiesFailed() {
Log.w(TAG, "list cities failed");
}
@Override
public void onListCitiesError() {
Log.w(TAG, "list cities error");
}
};
private ListOperatersCallback mListOperatorCallback = new ListOperatersCallback() {
@Override
public void onListOperatorsSuccess(List<StbOperator> operators) {
mOperators = operators;
if (null == mOperators) {
mOperators = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_OPERATOR_LIST);
}
@Override
public void onListOperatorsFailed() {
Log.w(TAG, "list operators failed");
}
@Override
public void onListOperatorsError() {
Log.e(TAG, "list operators error");
}
};
private City mCurrentProvince;
private int mListLevel = LEVEL_PROVINCE;
@@ -70,13 +139,8 @@ public class CityFragment extends BaseCreateFragment {
new Thread() {
@Override
public void run() {
mProvinces = mApp.mWeAPIs
.listProvinces();
if (null == mProvinces) {
mProvinces = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_PROVINCE_LIST);
mApp.mWeAPIs
.listProvinces(mListProvincesCallback);
}
}.start();
} else {
@@ -89,13 +153,8 @@ public class CityFragment extends BaseCreateFragment {
new Thread() {
@Override
public void run() {
mCities = mApp.mWeAPIs
.listCities(prefix);
if (null == mCities) {
mCities = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_CITY_LIST);
mApp.mWeAPIs
.listCities(prefix, mListCitiesCallback);
}
}.start();
}
@@ -105,13 +164,8 @@ public class CityFragment extends BaseCreateFragment {
new Thread() {
@Override
public void run() {
mOperators = mApp.mWeAPIs
.listOperators(cityCode);
if (null == mOperators) {
mOperators = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_OPERATOR_LIST);
mApp.mWeAPIs
.listOperators(cityCode, mListOperatorCallback);
}
}.start();
}

View File

@@ -17,6 +17,8 @@ import net.irext.ircontrol.R;
import net.irext.ircontrol.ui.adapter.IndexAdapter;
import net.irext.ircontrol.utils.FileUtils;
import net.irext.ircontrol.utils.MessageUtil;
import net.irext.webapi.WebAPICallbacks.ListIndexesCallback;
import net.irext.webapi.WebAPICallbacks.DownloadBinCallback;
import net.irext.webapi.model.Brand;
import net.irext.webapi.model.Category;
import net.irext.webapi.model.City;
@@ -45,7 +47,8 @@ public class IndexFragment extends BaseCreateFragment {
private static final int CMD_REFRESH_INDEX_LIST = 0;
private static final int CMD_DOWNLOAD_BIN_FILE = 1;
private static final int CMD_SAVE_REMOTE_CONTROL = 2;
private static final int CMD_BIN_FILE_DOWNLOADED = 2;
private static final int CMD_SAVE_REMOTE_CONTROL = 3;
private PullToRefreshListView mIndexList;
@@ -61,11 +64,54 @@ public class IndexFragment extends BaseCreateFragment {
private String mBrandName = "";
private String mOperatorName = "";
private InputStream mBinStream;
private IndexAdapter mIndexAdapter;
private MsgHandler mMsgHandler;
private IRApplication mApp;
private ListIndexesCallback mListIndexesCallback = new ListIndexesCallback() {
@Override
public void onListIndexesSuccess(List<RemoteIndex> indexes) {
mIndexes = indexes;
if (null == mIndexes) {
mIndexes = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_INDEX_LIST);
}
@Override
public void onListIndexesFailed() {
Log.w(TAG, "list indexes failed");
}
@Override
public void onListIndexesError() {
Log.e(TAG, "list indexes error");
}
};
private DownloadBinCallback mDownloadBinCallback = new DownloadBinCallback() {
@Override
public void onDownloadBinSuccess(InputStream inputStream) {
Log.d(TAG, "binary file download successfully");
mBinStream = inputStream;
MessageUtil.postMessage(mMsgHandler, CMD_BIN_FILE_DOWNLOADED);
}
@Override
public void onDownloadBinFailed() {
Log.w(TAG, "download bin file failed");
}
@Override
public void onDownloadBinError() {
Log.w(TAG, "download bin file error");
}
};
public IndexFragment() {
}
@@ -74,12 +120,8 @@ public class IndexFragment extends BaseCreateFragment {
new Thread() {
@Override
public void run() {
mIndexes = mApp.mWeAPIs.listRemoteIndexes(mParent.getCurrentCategory().getId(),
mBrandId, mCityCode, mOperatorId);
if (null == mIndexes) {
mIndexes = new ArrayList<>();
}
MessageUtil.postMessage(mMsgHandler, CMD_REFRESH_INDEX_LIST);
mApp.mWeAPIs.listRemoteIndexes(mParent.getCurrentCategory().getId(),
mBrandId, mCityCode, mOperatorId, mListIndexesCallback);
}
}.start();
}
@@ -91,21 +133,7 @@ public class IndexFragment extends BaseCreateFragment {
try {
String remoteMap = mCurrentIndex.getRemoteMap();
int indexId = mCurrentIndex.getId();
InputStream in = mApp.mWeAPIs.downloadBin(remoteMap, indexId);
if (createDirectory()) {
File binFile = new File(FileUtils.BIN_PATH +
FileUtils.FILE_NAME_PREFIX + mCurrentIndex.getRemoteMap() +
FileUtils.FILE_NAME_EXT);
FileUtils.write(binFile, in);
} else {
Log.w(TAG, "no directory to contain bin file");
}
if (null != in) {
MessageUtil.postMessage(mMsgHandler, CMD_SAVE_REMOTE_CONTROL);
} else {
Log.e(TAG, "bin file download failed");
}
mApp.mWeAPIs.downloadBin(remoteMap, indexId, mDownloadBinCallback);
} catch (Exception e) {
e.printStackTrace();
}
@@ -121,6 +149,29 @@ public class IndexFragment extends BaseCreateFragment {
return file.mkdirs();
}
private void saveBinFile() {
new Thread() {
@Override
public void run() {
if (createDirectory()) {
File binFile = new File(FileUtils.BIN_PATH +
FileUtils.FILE_NAME_PREFIX + mCurrentIndex.getRemoteMap() +
FileUtils.FILE_NAME_EXT);
boolean ret = FileUtils.write(binFile, mBinStream);
Log.d(TAG, "write bin file succeeded : " + ret);
} else {
Log.w(TAG, "no directory to contain bin file");
}
if (null != mBinStream) {
MessageUtil.postMessage(mMsgHandler, CMD_SAVE_REMOTE_CONTROL);
} else {
Log.e(TAG, "bin file download failed");
}
}
}.start();
}
private void saveRemoteControl() {
// TODO update brand and operator name i18n
RemoteControl remoteControl = new RemoteControl();
@@ -138,6 +189,8 @@ public class IndexFragment extends BaseCreateFragment {
remoteControl.setSubCategory(mCurrentIndex.getSubCate());
long id = RemoteControl.createRemoteControl(remoteControl);
Log.d(TAG, "save remote control : " + id);
mParent.finish();
}
@@ -226,6 +279,10 @@ public class IndexFragment extends BaseCreateFragment {
indexFragment.downloadBinFile();
break;
case CMD_BIN_FILE_DOWNLOADED:
indexFragment.saveBinFile();
break;
case CMD_SAVE_REMOTE_CONTROL:
indexFragment.saveRemoteControl();