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> canLogout(){ return UserConfigController.getInstance().getUserConfig(mLoginInfo.info.id) .map(new Function, ControllerMessage>() { @Override public ControllerMessage apply(ControllerMessage message) throws Exception { return ControllerMessage.getSimpleMessage(message.isSuccess(), message.getMessage(), message.isSuccess() && message.getObject() != null && message.getObject().loginOut == ResUserConfig.CAN_LOGOUT); } }); } private List mGameConfigTempData; public Observable> login(String userName, String passWord){ return getLoginService().login(userName, passWord) .concatMap(new Function, ObservableSource>>() { @Override public ObservableSource> apply(ResData 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>() { @Override public ResData apply(ResData> 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>() { @Override public void accept(ResData 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, ControllerMessage>()); } 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 ""; } }