123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- package com.jyc.threegames.controller;
- import android.content.Intent;
- import android.os.Environment;
- import android.text.TextUtils;
- import android.util.Log;
- import com.google.gson.Gson;
- import com.google.gson.JsonParser;
- import com.jyc.threegames.App;
- import com.jyc.threegames.bean.ControllerMessage;
- import com.jyc.threegames.bean.request.ReqLogin;
- import com.jyc.threegames.bean.result.ResGameConfig;
- import com.jyc.threegames.bean.result.ResLogin;
- import com.jyc.threegames.bean.result.ResUserConfig;
- import com.jyc.threegames.net.ResData;
- import com.jyc.threegames.net.RetrofitHelper;
- import com.jyc.threegames.net.api.LoginService;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.Calendar;
- import java.util.List;
- import io.reactivex.Observable;
- import io.reactivex.ObservableSource;
- import io.reactivex.functions.Consumer;
- import io.reactivex.functions.Function;
- public class LoginController extends BaseController {
- private static LoginController mInstance;
- private LoginController(){}
- private ResLogin mLoginInfo;
- private static final String PATH = "/ThreeGames/";
- private static final String FILE_NAME = "loginInfo";
- public static LoginController getInstance(){
- if(mInstance == null){
- synchronized (LoginController.class){
- if(mInstance == null)
- mInstance = new LoginController();
- }
- }
- return mInstance;
- }
- private static LoginService mService;
- protected static LoginService getLoginService(){
- if (mService == null){
- synchronized (LoginController.class){
- if (mService == null)
- mService = RetrofitHelper.getDefaultRetrofit(App.SERVER_ADDRESS).create(LoginService.class);
- }
- }
- return mService;
- }
- public boolean restoreLoginInfoFromSDCardFile(){
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- try {
- File targetFile = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + PATH + FILE_NAME);
- if (targetFile.exists() && targetFile.isFile()){
- FileInputStream inputStream = new FileInputStream(targetFile);
- BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
- StringBuilder sb = new StringBuilder("");
- String line;
- while ((line = br.readLine()) != null){
- sb.append(line);
- }
- br.close();
- inputStream.close();
- mLoginInfo = new Gson().fromJson(sb.toString(), ResLogin.class);
- return true;
- }else
- return false;
- }catch (Exception e){
- e.printStackTrace();
- Log.e("three_games", "read file error: " + e.getMessage());
- }
- }
- return false;
- }
- public Observable<ControllerMessage<Boolean>> canLogout(){
- return UserConfigController.getInstance().getUserConfig(mLoginInfo.info.id)
- .map(new Function<ControllerMessage<ResUserConfig>, ControllerMessage<Boolean>>() {
- @Override
- public ControllerMessage<Boolean> apply(ControllerMessage<ResUserConfig> message) throws Exception {
- return ControllerMessage.getSimpleMessage(message.isSuccess(), message.getMessage(), message.isSuccess() && message.getObject() != null && message.getObject().loginOut == ResUserConfig.CAN_LOGOUT);
- }
- });
- }
- private List<ResGameConfig> mGameConfigTempData;
- public Observable<ControllerMessage<ResLogin>> login(String userName, String passWord){
- return getLoginService().login(userName, passWord)
- .concatMap(new Function<ResData<ResLogin>, ObservableSource<ResData<ResLogin>>>() {
- @Override
- public ObservableSource<ResData<ResLogin>> apply(ResData<ResLogin> data) throws Exception {
- if (data.isRequestSuccess() && data.data != null && data.data.info != null && !TextUtils.isEmpty(data.data.token)){
- mLoginInfo = data.data;
- return getLoginService().getGameConfigs(data.data.info.id).
- map(new Function<ResData<List<ResGameConfig>>, ResData<ResLogin>>() {
- @Override
- public ResData<ResLogin> apply(ResData<List<ResGameConfig>> listData) throws Exception {
- if (listData.isRequestSuccess() && listData.data != null) {
- mGameConfigTempData = listData.data;
- }else {
- data.code = ResData.CODE_ERROR;
- data.message = listData.message;
- data.data = null;
- }
- return data;
- }
- });
- }else
- return Observable.just(data);
- }
- })
- .doOnNext(new Consumer<ResData<ResLogin>>() {
- @Override
- public void accept(ResData<ResLogin> data) throws Exception {
- if (data.isRequestSuccess() && data.data != null && data.data.info != null && !TextUtils.isEmpty(data.data.token)){
- mLoginInfo = data.data;
- mLoginInfo.gameConfigs = mGameConfigTempData;
- writeLoginInfoToSDCard();
- }
- }
- })
- .takeLast(1)
- .map(new SimpleDataHandleFunction<ResData<ResLogin>, ControllerMessage<ResLogin>>());
- }
- public void logout(){
- this.mLoginInfo = null;
- deleteLoginInfoOnSDCard();
- }
- public boolean inTheTestPeriod(){
- if (this.mLoginInfo == null || this.mLoginInfo.gameConfigs == null || this.mLoginInfo.gameConfigs.size() == 0)
- return false;
- for (ResGameConfig item : this.mLoginInfo.gameConfigs){
- String startTime = item.startTime;
- Calendar startCalendar = Calendar.getInstance();
- startCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startTime.split(":")[0]));
- startCalendar.set(Calendar.MINUTE, Integer.parseInt(startTime.split(":")[1]));
- String endTime = item.endTime;
- Calendar endCalendar = Calendar.getInstance();
- endCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endTime.split(":")[0]));
- endCalendar.set(Calendar.MINUTE, Integer.parseInt(endTime.split(":")[1]));
- Calendar nowCalendar = Calendar.getInstance();
- if (nowCalendar.getTime().getTime() >= startCalendar.getTime().getTime() && nowCalendar.getTime().getTime() <= endCalendar.getTime().getTime())
- return true;
- }
- return false;
- }
- private void deleteLoginInfoOnSDCard(){
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- try {
- File targetFile = new File(Environment.getExternalStorageDirectory() + PATH + FILE_NAME);
- if (targetFile.exists())
- targetFile.delete();
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- private void writeLoginInfoToSDCard(){
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- try {
- File sdCardDir = Environment.getExternalStorageDirectory();
- File dir = new File(sdCardDir.getCanonicalPath() + PATH);
- if (!dir.exists()) {
- if (dir.mkdirs()){
- continueCreateFile(sdCardDir);
- }else
- Log.e("three_games", "create sdcard file path error");
- }else
- continueCreateFile(sdCardDir);
- } catch (Exception e){
- e.printStackTrace();
- Log.e("three_games", "create sdcard file exception: " + e.getMessage());
- }
- }
- }
- private void continueCreateFile(File sdCardDir) throws Exception{
- File targetFile = new File(sdCardDir.getCanonicalPath() + PATH + FILE_NAME);
- targetFile.delete();
- if (targetFile.createNewFile()){
- String content = new Gson().toJson(mLoginInfo);
- FileOutputStream outStream = new FileOutputStream(targetFile);
- outStream.write(content.getBytes());
- outStream.close();
- }else
- Log.e("three_games", "create sdcard file error");
- }
- public ResLogin getCurrentLoginInfo(){
- return mLoginInfo;
- }
- public boolean isCurrentUserAdmin(){
- if (mLoginInfo != null){
- return mLoginInfo.isAdmin();
- }
- return false;
- }
- public String getToken(){
- if (mLoginInfo != null && !TextUtils.isEmpty(mLoginInfo.token))
- return mLoginInfo.token;
- return "";
- }
- }
|