|
|
@@ -0,0 +1,246 @@
|
|
|
+package com.ntels.onecable.common.handler;
|
|
|
+
|
|
|
+import com.ntels.onecable.R;
|
|
|
+import com.ntels.onecable.common.Constants;
|
|
|
+import com.ntels.onecable.common.util.Util;
|
|
|
+
|
|
|
+import android.app.Activity;
|
|
|
+import android.os.Handler;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+import static com.ntels.onecable.common.handler.BackPressCloseHandler.BACK_PRESS_VALID_TIME;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by jinochoi on 2017. 3. 31..
|
|
|
+ */
|
|
|
+
|
|
|
+public class CallWithDelayHandler {
|
|
|
+
|
|
|
+ private long mBackKeyPressedTime = 0;
|
|
|
+ private Activity mActivity;
|
|
|
+ private Toast mToast;
|
|
|
+ private Util mUtil = new Util();
|
|
|
+
|
|
|
+ public CallWithDelayHandler(Activity activity) {
|
|
|
+ this.mActivity = activity;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void callWithDelay() {
|
|
|
+ if (System.currentTimeMillis() > mBackKeyPressedTime + BACK_PRESS_VALID_TIME) {
|
|
|
+ mBackKeyPressedTime = System.currentTimeMillis();
|
|
|
+ showGuide();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (System.currentTimeMillis() <= mBackKeyPressedTime + BACK_PRESS_VALID_TIME) {
|
|
|
+ mActivity.finish();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callFinishWithDelay(String isMethodName) {
|
|
|
+
|
|
|
+ callWithDelayMethod(isMethodName, true, "finish", Constants.HANDLER_DELAY_MILLIS);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callFinishWithDelay(String isMethodName, Boolean equalValue) {
|
|
|
+
|
|
|
+ callWithDelayMethod(isMethodName, equalValue, "finish", Constants.HANDLER_DELAY_MILLIS);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void callWithDelay(final String callMethodName) {
|
|
|
+
|
|
|
+ callWithDelayMethod("", true, callMethodName, Constants.HANDLER_DELAY_MILLIS);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelay(final String callMethodName, int delayTime) {
|
|
|
+
|
|
|
+ callWithDelayMethod("", true, callMethodName, delayTime);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelay(String isMethodName, String callMethodName) {
|
|
|
+
|
|
|
+ callWithDelayMethod(isMethodName, true, callMethodName, Constants.HANDLER_DELAY_MILLIS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelay(String isMethodName, Boolean equalValue, String callMethodName) {
|
|
|
+
|
|
|
+ callWithDelayMethod(isMethodName, equalValue, callMethodName, Constants.HANDLER_DELAY_MILLIS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelayMethod(final String isMethodName, final Boolean equalValue, final String callMethodName, final int delayTime) {
|
|
|
+ new Handler().postDelayed(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ Boolean isProc = false;
|
|
|
+
|
|
|
+ if (!isMethodName.equals("")) {
|
|
|
+ Method isMethod;
|
|
|
+ try {
|
|
|
+ isMethod = mActivity.getClass().getMethod(isMethodName);
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ isMethod = mActivity.getClass().getDeclaredMethod(isMethodName);
|
|
|
+ isMethod.setAccessible(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ Object returnValue = isMethod.invoke(mActivity);
|
|
|
+ if (returnValue == equalValue) {
|
|
|
+ isProc = true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ isProc = true;
|
|
|
+ }
|
|
|
+ if (isProc) {
|
|
|
+ if (callMethodName.equals("finish")) {
|
|
|
+ mActivity.finish();
|
|
|
+ } else {
|
|
|
+ Method method;
|
|
|
+ try {
|
|
|
+ method = mActivity.getClass().getMethod(callMethodName);
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ method = mActivity.getClass().getDeclaredMethod(callMethodName);
|
|
|
+ method.setAccessible(true);
|
|
|
+ }
|
|
|
+ method.invoke(mActivity);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ callWithDelayMethod(isMethodName, equalValue, callMethodName, delayTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, delayTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callFinishWithDelayField(String isFieldName) {
|
|
|
+
|
|
|
+ callWithDelayField(isFieldName, true, "finish", Constants.HANDLER_DELAY_MILLIS);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callFinishWithDelayField(String isFieldName, Boolean equalValue) {
|
|
|
+
|
|
|
+ callWithDelayField(isFieldName, equalValue, "finish", Constants.HANDLER_DELAY_MILLIS);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelayField(final String callMethodName) {
|
|
|
+
|
|
|
+ callWithDelayField("", true, callMethodName, Constants.HANDLER_DELAY_MILLIS);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelayField(final String callMethodName, int delayTime) {
|
|
|
+
|
|
|
+ callWithDelayField("", true, callMethodName, delayTime);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelayField(String isFieldName, String callMethodName) {
|
|
|
+
|
|
|
+ callWithDelayField(isFieldName, true, callMethodName, Constants.HANDLER_DELAY_MILLIS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelayField(String isFieldName, String callMethodName, int delayTime) {
|
|
|
+
|
|
|
+ callWithDelayField(isFieldName, true, callMethodName, delayTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelayField(String isFieldName, Boolean equalValue, String callMethodName) {
|
|
|
+
|
|
|
+ callWithDelayField(isFieldName, equalValue, callMethodName, Constants.HANDLER_DELAY_MILLIS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void callWithDelayField(final String isFieldName, final Boolean equalValue, final String callMethodName, final int delayTime) {
|
|
|
+ new Handler().postDelayed(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ Boolean isProc = false;
|
|
|
+
|
|
|
+ Field isField;
|
|
|
+ try {
|
|
|
+ isField = mActivity.getClass().getField(isFieldName);
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ isField = mActivity.getClass().getDeclaredField(isFieldName);
|
|
|
+ isField.setAccessible(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ Object returnValue = isField.get(mActivity);
|
|
|
+ if (returnValue == equalValue) {
|
|
|
+ isProc = true;
|
|
|
+ }
|
|
|
+ if (isProc) {
|
|
|
+ if (callMethodName.equals("finish")) {
|
|
|
+ mActivity.finish();
|
|
|
+ } else {
|
|
|
+ Method method;
|
|
|
+ try {
|
|
|
+ method = mActivity.getClass().getMethod(callMethodName);
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ method = mActivity.getClass().getDeclaredMethod(callMethodName);
|
|
|
+ method.setAccessible(true);
|
|
|
+ }
|
|
|
+ method.invoke(mActivity);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ callWithDelayField(isFieldName, equalValue, callMethodName, delayTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, delayTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean doesReturnVoid(Method method) {
|
|
|
+ Boolean isReturn = false;
|
|
|
+ if (void.class.equals(method.getReturnType())) {
|
|
|
+ isReturn = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return isReturn;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showGuide() {
|
|
|
+ mToast = mUtil.setShowToast(mActivity, mUtil.getString(mActivity, R.string.MSG823), Toast.LENGTH_SHORT);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void runMethod(String methodName) {
|
|
|
+ try {
|
|
|
+ Method method = mActivity.getClass().getMethod(methodName);
|
|
|
+ method.invoke(mActivity);
|
|
|
+ } catch (NoSuchMethodException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|