package kr.co.icontrols.wallpadoutmoderun;
import com.artncore.WallPadDataMgr.WallpadDeviceSet;
import com.artncore.WallPadDataMgr.WallpadStatusData;
import com.artncore.wallpadapi.WallPadAPI;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import com.util.LogUtil;
import kr.co.icontrols.wallpadoutmoderun.declare.Common;
import kr.co.icontrols.wallpadoutmoderun.declare.ID;
import kr.co.icontrols.wallpadoutmoderun.screen.PopupInputPw;
import kr.co.icontrols.wallpadsupport.WpadActivity;
import kr.co.icontrols.wallpadsupport.WpadScale;
/**
* @description
* 월패드 [우리집제어] 팝업화면 관리자 이다.
*/
public class PopupActivity extends WpadActivity
{
private final String TAG = "PopupActivity";
private final boolean DEBUG_LOG_ON = true;
private void DebugLogOutput(String s) { if(DEBUG_LOG_ON) Log.d(TAG, s); }
// =================================================================================================
// [[ Declaration ]] region
// =================================================================================================
private static Context mContext = null;
public static Context getContext() { return mContext; }
private WallPadAPI wpapi = null;
private int OPERATION_MODE = Common.MODE;
// Layout
private RelativeLayout AllLayout;
// CurrentScreenId & mCurrentStyle
private int mCurrentScreenId = -1;
private int mCurrentStyle = -1;
private int mTempCurrentScreenId = -1;
// CreateComplete
private static boolean mCreateComplete = false;
protected static boolean getCreateComplete() { return mCreateComplete; }
protected static void setCreateComplete(boolean set) { mCreateComplete = set; }
// Screen
private PopupInputPw mPopupInputPw = null;
private WallpadStatusData wdb = null;
public WallpadStatusData GetDBObj()
{
return wdb;
}
/**
* Static Variables 초기화
* onCreate 도입부, onDestroy 종료시 삽입한다.
*/
private void setStaticVariablesInit()
{
mContext = null;
mCreateComplete = false;
if(AllLayout != null)
{
AllLayout.removeAllViews();
AllLayout = null;
}
}
// =================================================================================================
// [[ Activity Life Cycle ]] region
// =================================================================================================
/**
* Activity Constructors
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.i(TAG, "-------------------------------------------------------------");
Log.i(TAG, "------------------ [START - PopupActivity] ------------------");
Log.i(TAG, "-------------------------------------------------------------");
super.onCreate(savedInstanceState);
// 1. 변수 초기화
DebugLogOutput("[Create] - Step1 : Data Create");
setStaticVariablesInit();
mContext = this;
// 2. 자동 종료 셋팅
DebugLogOutput("[Create] - Step2 : Auto Finish Setting");
// 3. API 호출
DebugLogOutput("[Create] - Step3 : API Call");
if(OPERATION_MODE == Common.MODE_NOMAL)
{
Log.d(TAG, "OperationMode = MODE_NOMAL");
wpapi = new WallPadAPI(this);
}
else if(OPERATION_MODE == Common.MODE_BUSINESS_SAMPLE)
{
Log.d(TAG, "OperationMode = MODE_BUSINESS_SAMPLE");
}
try { wdb = new WallpadStatusData(this); }
catch (RuntimeException re) {
LogUtil.errorLogInfo("", TAG, re);
}
catch (Exception e) { Log.e(TAG, "[ExceptionError] - onCreate->WallpadStatusData Create : " + e); }
// 4. Layout registration
DebugLogOutput("[Create] - Step4 : Layout registration");
// 4-1. mainLayout
AllLayout = new RelativeLayout(this);
AllLayout.setId(ID.popup.layout.ALL);
super.setAllLayoutRegistration(AllLayout);
// 5. Start ScreenId Data Get
Intent intent = getIntent();
int ScreenId = intent.getIntExtra("ScreenId", -1);
Log.d(TAG, "ScreenId = " + ScreenId);
if(ScreenId < 0)
{
Log.e(TAG, "[Error] ScreenId Out of range (" + ScreenId + ")");
finish();
}
else
{
// 6. First Show Layout Find
mCreateComplete = true;
// if(OPERATION_MODE == Common.MODE_NOMAL)
// {
// mTempCurrentScreenId = ScreenId;
// //mWallPadApiCheckHandler.sendEmptyMessage(0);
// }
// else
{
if(setChangeScreen(ScreenId, false) == false)
{
Log.e(TAG, "[Error] setChangeScreen false...");
finish();
}
}
}
Log.i(TAG, "End onCreate -------------------------------------------------");
}
/**
* (임시코드) WallPadAPI 초기생성시 Connect 에 시간이 걸리며,
* WallPadAPI 초기화후 바로 Get_GasValveController 등을 사용하여 API 를 가져올수 없기에,
* 본 딜레이 코드를 임시로 적용한다.
*/
// @SuppressLint("HandlerLeak")
// protected Handler mWallPadApiCheckHandler = new Handler()
// {
// @SuppressLint("HandlerLeak")
// @Override
// public void handleMessage(Message msg)
// {
// if(msg.what == 0)
// {
// if(wpapi != null)
// {
// if(wpapi.Check_Connect()) mWallPadApiCheckHandler.sendEmptyMessageDelayed(1, 10);
// else mWallPadApiCheckHandler.sendEmptyMessageDelayed(0, 10);
// }
// }
// else if(msg.what == 1)
// {
// setChangeScreen(mTempCurrentScreenId, false);
// }
// }
// };
/**
* onDestroy
*/
@Override
protected void onDestroy()
{
setChangeScreen(mCurrentScreenId, true);
if(wdb != null) wdb.closeDB();
setStaticVariablesInit();
MainActivity.setPopupActivityRun(false);
mCreateComplete = false;
super.onDestroy();
Log.i(TAG, "*************************************************************");
Log.i(TAG, "****************** [END - PopupActivity] ******************");
Log.i(TAG, "*************************************************************");
}
/**
* onResume
*/
@Override
protected void onResume() {
super.onResume();
if(wpapi != null) wpapi.Resume();
Log.d(TAG, "@@@@@@@@ [onResume] @@@@@@@@");
}
/**
* onPause
*/
@Override
protected void onPause() {
super.onPause();
if(wpapi != null) wpapi.Pause();
Log.d(TAG, "@@@@@@@@ [onPause] @@@@@@@@");
}
/* ================================================================================================
* [BroadcastReceiver] Section
================================================================================================ */
/**
* BroadcastReceiver.onReceive for [WpadActivity]
*/
@Override
protected void onBrReceive(Intent intent)
{
super.onBrReceive(intent);
Log.d(TAG, "Receive alert BR " + intent.getAction());
}
/* ================================================================================================
* [Touch & Key Event] Section
================================================================================================ */
/**
* TouchEvent for [WpadActivity]
*/
@Override
protected void onTouchEvent(View v, MotionEvent event) {
super.onTouchEvent(v, event);
}
/**
* H/W Key Event
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
DebugLogOutput("onKeyDown - " + " / keyCode : " + keyCode + " / Action : " + event.getAction());
return super.onKeyDown(keyCode, event);
}
/* ================================================================================================
* [Screen] Section
================================================================================================ */
/**
* Screen 에서 Screen 변경을 요청할 경우 호출된다.
* Override 사용하여 Screen 변경을 처리한다.
*
* @param nScreenId - 변경할 Screen ID
* @return (boolean) true : 성공, false : 실패
*/
@Override
protected boolean onChangeScreen(int nScreenId) {
super.onChangeScreen(nScreenId);
return setChangeScreen(nScreenId, false);
}
/**
* Screen 등록자
* ScreenLayout 에 현재 Screen 을 삭제하고, 새로운 Screen 을 등록시킨다.
*
* @param nScreenId - 변경할 Screen ID
* @param OnlyRemove - 현재 Screen 을 삭제만 할 경우 (onDestroy 에서 사용)
* @return (boolean) true : 성공, false : 실패
*/
private boolean setChangeScreen(int nScreenId, boolean OnlyRemove)
{
DebugLogOutput("setChangeLayout - " + "CurrentScreenId : " + mCurrentScreenId
+ " / NextLayoutId : " + nScreenId + " / OnlyRemove : " + OnlyRemove);
if(getCreateComplete() == false)
{
Log.w(TAG, "[Waring] setChangeLayout - CreateComplete is false !!!");
return false;
}
//boolean remove = false;
if(mCurrentScreenId > 0)
{
//remove = true;
AllLayout.removeAllViews();
super.setCurrentScreen(null);
}
if(OnlyRemove) return true;
// 1. 새로운 Layout 표시
mCurrentScreenId = nScreenId;
int Style = Common.POPUP_STYLE_NOMAL;
int FinishTime = Common.FINISH_TIME_POPUP_NORMAL_SEC;
boolean FinshOutSide = true;
try
{
switch(nScreenId)
{
case ID.screen.InputPwPopupScreen:
mPopupInputPw = new PopupInputPw(mContext, AllLayout);
super.setCurrentScreen(mPopupInputPw);
break;
default:
Log.e(TAG, "[Error] setChangeLayout - OnlyRemove Input ID Not found!!!");
return false;
}
}
catch (RuntimeException re) {
LogUtil.errorLogInfo("", TAG, re);
mCurrentScreenId = -1;
return false;
}
catch (Exception e)
{
Log.e(TAG, "[ExceptionError] - setChangeScreen->Screen Create : " + e);
mCurrentScreenId = -1;
return false;
}
this.setFinishOnTouchOutside(FinshOutSide);
// 2. Style 변경시 WindowReSize
if(mCurrentStyle != Style)
{
int ScreenWidth = WpadScale.getWidth(Common.getPopupScreenSize_W(Style));
int ScreenHigh = WpadScale.getHeight(Common.getPopupScreenSize_H(Style));
if( (ScreenWidth <= 0) || (ScreenHigh <= 0) )
{
Log.e(TAG, "[Error] Style UnDefined !!! (" + Style + ")");
return false;
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ScreenWidth, ScreenHigh);
AllLayout.setLayoutParams(params);
setContentView(AllLayout);
super.WindowReSize(ScreenWidth, ScreenHigh, false);
mCurrentStyle = Style;
}
// 3. FinishTimer Setting
if(Common.FINISH_TIME_ENABLE)
super.setFinishTimerCreate(FinishTime);
mCurrentScreenId = nScreenId;
return true;
}
// 화면 매뉴얼
private static int mCurrentManualScreenId = -1;
private static boolean ManualActivityRun = false;
public static boolean getManualActivityRun() {
return ManualActivityRun;
}
public static void setManualActivityRun(boolean run) {
ManualActivityRun = run;
if (!ManualActivityRun) resetCurrentManualScreenID();
}
public boolean StartManual(int screenId) {
Log.d(TAG, "[StartManual] ScreenId [" + screenId +"], mCurrentManualScreenId [" + mCurrentManualScreenId + "]" );
if (getManualActivityRun()) {
mCurrentManualScreenId = screenId;
return ((WpadActivity)(ManualActivity.getContext())).ChangeScreen(screenId);
} else {
if (mCurrentManualScreenId == screenId) return false;
mCurrentManualScreenId = screenId;
setManualActivityRun(true);
Intent intent = new Intent(mContext, ManualActivity.class);
intent.putExtra("ScreenId", screenId);
/*if(mCurrentScreenId ==ID.screen.HeatingV2PopupScreen )
{
intent.putExtra("HeatPopInfo", heatingV2PopupScreen.HeatPopManual_Str);
}
else if(mCurrentScreenId ==ID.screen.LightEtcEachPopupRoomScreen )
{
intent.putExtra("EtcMultiPopInfo", lightEtcEachPopupRoomScreen.MultiLightCount);
}*/
overridePendingTransition(0, 0);
startActivityForResult(intent, 0);
return true;
}
}
public static void resetCurrentManualScreenID() {
mCurrentManualScreenId = -1;
}
public boolean GetManualUse() {
WallpadDeviceSet devSet = new WallpadDeviceSet(this.getContext());
String[] DBinfo = devSet.GetSettingData("매뉴얼");
devSet.closeDB();
if (DBinfo != null) {
if (DBinfo[1].indexOf("사용함") > 0) {
Log.d(TAG, "[GetManualUse] Use Manual");
return true;
} else {
Log.d(TAG, "[GetManualUse] Not Use Manual");
return false;
}
} else {
Log.w(TAG, "[GetManualUse] DBInfo is null");
return false;
}
}
public static boolean getManualUse() {
WallpadDeviceSet devSet = new WallpadDeviceSet(getContext());
String[] DBinfo = devSet.GetSettingData("매뉴얼");
devSet.closeDB();
if (DBinfo != null) {
if (DBinfo[1].indexOf("사용함") > 0) {
Log.d("PopupActivity", "[getManualUse] Use Manual");
return true;
} else {
Log.d("PopupActivity", "[getManualUse] Not Use Manual");
return false;
}
} else {
Log.w("PopupActivity", "[getManualUse] DBInfo is null");
return false;
}
}
int nHANDLER_SCREEN_FINISH = 100;
protected Handler PopupScreenHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == nHANDLER_SCREEN_FINISH) {
finishPopup();
}
}
};
public void sendHandlerMsgDelayed(int what, int arg1, int arg2, long delay) {
try {
Log.d(TAG, "[sendHandlerMsgDelayed] what [" + what + "], arg1 [" + arg1 + "], arg2 [" + arg2 + "], delay [" + delay + "]");
Message HandleMsg = PopupScreenHandler.obtainMessage();
HandleMsg.what = what;
HandleMsg.arg1 = arg1;
HandleMsg.arg2 = arg2;
PopupScreenHandler.sendMessageDelayed(HandleMsg, delay);
} catch (RuntimeException re) {
LogUtil.errorLogInfo("", TAG, re);
}
catch (Exception e) {
Log.e(TAG, "[Exception] sendHandlerMsgDelayed(msg)");
//e.printStackTrace();
LogUtil.errorLogInfo("", TAG, e);
}
}
private void finishPopup() {
try {
super.finish();
} catch (RuntimeException re) {
LogUtil.errorLogInfo("", TAG, re);
}
catch (Exception e) {
Log.e(TAG, "[Exception] finishPopup()");
//e.printStackTrace();
LogUtil.errorLogInfo("", TAG, e);
}
}
}