Browse Source

[공통][New] 네트워크 상태 모니터링
- NetworkReceiver 를 통한 모니터링 -> SuperModel -> Presenter 로 전달된다.

hyodong.min 7 years ago
parent
commit
93c2b0ba57

+ 1 - 1
app/build.gradle

@@ -20,7 +20,7 @@ android {
     compileSdkVersion 27
     defaultConfig {
         applicationId "kr.co.zumo.app"
-        minSdkVersion 19
+        minSdkVersion 19    // 4.4
         targetSdkVersion 27
         versionCode 149
         versionName "4.0.0"

+ 17 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/activity/ActivityBase.java

@@ -3,6 +3,7 @@
  */
 package kr.co.zumo.app.lifeplus.activity;
 
+import android.content.Context;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.v7.app.AppCompatActivity;
@@ -17,6 +18,7 @@ import kr.co.zumo.app.lifeplus.manager.ActionBarManager;
 import kr.co.zumo.app.lifeplus.model.BaseSharedPreferences;
 import kr.co.zumo.app.lifeplus.model.LifeplusPreferences;
 import kr.co.zumo.app.lifeplus.model.SuperModel;
+import kr.co.zumo.app.lifeplus.network.NetworkReceiver;
 import kr.co.zumo.app.lifeplus.network.NetworkStatus;
 import kr.co.zumo.app.lifeplus.supervisor.ScreenChangerHelper;
 import kr.co.zumo.app.lifeplus.util.AppUtil;
@@ -35,8 +37,10 @@ import kr.co.zumo.app.lifeplus.view.presenter.Presenter;
  */
 public abstract class ActivityBase extends AppCompatActivity {
 
+  private NetworkReceiver networkReceiver;
   IAppSetting appSetting;
 
+
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
@@ -50,7 +54,10 @@ public abstract class ActivityBase extends AppCompatActivity {
     ScreenChangerHelper.getInstance().setContainerId(R.id.container);
     ActionBarManager.getInstance().setActivityInfo(this);
 
-    SuperModel.getInstance().init(new LifeplusPreferences(new BaseSharedPreferences(PreferenceManager.getDefaultSharedPreferences(App.getInstance().getContext()))));
+    Context context = App.getInstance().getContext();
+    SuperModel.getInstance().init(new LifeplusPreferences(new BaseSharedPreferences(PreferenceManager.getDefaultSharedPreferences(context))));
+
+    networkReceiver = new NetworkReceiver(context, SuperModel.getInstance());
 
     /**
      * 네트워크 연결 확인
@@ -76,6 +83,15 @@ public abstract class ActivityBase extends AppCompatActivity {
     appSetting.startFragment();
   }
 
+  @Override
+  protected void onDestroy() {
+    super.onDestroy();
+
+    SuperModel.getInstance().dispose();
+    networkReceiver.dispose();
+
+  }
+
   /**
    * 액티비티에 해당하는 AppSetting 을 반환한다.
    *

+ 19 - 3
app/src/main/java/kr/co/zumo/app/lifeplus/model/SuperModel.java

@@ -5,6 +5,7 @@ package kr.co.zumo.app.lifeplus.model;
 
 import android.util.Log;
 
+import kr.co.zumo.app.lifeplus.network.INetworkReceiverListener;
 import kr.co.zumo.app.lifeplus.view.presenter.Presenter;
 
 /**
@@ -17,7 +18,7 @@ import kr.co.zumo.app.lifeplus.view.presenter.Presenter;
  * @history 민효동   [2018. 9. 5.]   [최초 작성]
  * @since 2018. 9. 5.
  */
-public final class SuperModel {
+public final class SuperModel implements INetworkReceiverListener {
   private static final SuperModel ourInstance = new SuperModel();
 
   public static SuperModel getInstance() {
@@ -25,6 +26,7 @@ public final class SuperModel {
   }
 
   private LifeplusPreferences preferences;
+  private Presenter presenter;
 
   private SuperModel() {
   }
@@ -33,8 +35,6 @@ public final class SuperModel {
     this.preferences = preferences;
   }
 
-  private Presenter presenter;
-
   public Presenter getPresenter() {
     return presenter;
   }
@@ -52,4 +52,20 @@ public final class SuperModel {
   public LifeplusPreferences getPreferences() {
     return preferences;
   }
+
+  /**
+   * dispose
+   */
+  public void dispose() {
+  }
+
+  /***********************************
+   * listener
+   ***********************************/
+  @Override
+  public void onChangedConnection(boolean isConnected, int type) {
+    if (null != presenter) {
+      presenter.onNetworkStatusChanged(isConnected, type);
+    }
+  }
 }

+ 24 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/network/INetworkReceiverListener.java

@@ -0,0 +1,24 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.network;
+
+/**
+ * INetworkReceiverListener
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 9. 18.]   [최초 작성]
+ * @since 2018. 9. 18.
+ */
+public interface INetworkReceiverListener {
+  /**
+   * 네트워크 상태가 변경되었을 때 이벤트를 전달한다.
+   *
+   * @param isConnected true|false
+   * @param type        disconnected = NetworkStatus.DISCONNECTED
+   */
+  void onChangedConnection(boolean isConnected, int type);
+}

+ 68 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/network/NetworkReceiver.java

@@ -0,0 +1,68 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.network;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.util.Log;
+
+/**
+ * NetworkReceiver
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 9. 18.]   [최초 작성]
+ * @since 2018. 9. 18.
+ */
+public class NetworkReceiver extends BroadcastReceiver {
+
+  private Context context;
+  private INetworkReceiverListener listener;
+
+  public NetworkReceiver(Context context, INetworkReceiverListener listener) {
+
+    this.context = context;
+    this.listener = listener;
+
+    this.context.registerReceiver(this, getIntentFilter());
+
+  }
+
+  private IntentFilter getIntentFilter() {
+    return new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
+  }
+
+  @Override
+  public void onReceive(Context context, Intent intent) {
+    String action = intent.getAction();
+
+    if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
+      NetworkStatus networkStatus = new NetworkStatus();
+
+      boolean isConnected = networkStatus.isConnected();
+
+      Log.i("APP# NetworkReceiver | onReceive", "| networkStatus.isConnected(): " + isConnected);
+
+      if (null != listener) {
+        listener.onChangedConnection(isConnected, networkStatus.getConnectedType());
+      }
+    }
+  }
+
+  /**
+   * dispose
+   */
+  public void dispose() {
+    if (null != context) {
+      context.unregisterReceiver(this);
+      context = null;
+    }
+  }
+
+}

+ 4 - 2
app/src/main/java/kr/co/zumo/app/lifeplus/network/NetworkStatus.java

@@ -25,6 +25,8 @@ public class NetworkStatus {
   public static final String TYPE_WIMAX = "WIMAX";
   public static final String TYPE_MOBILE = "MOBILE";
 
+  public static final int DISCONNECTED = -1;
+
   NetworkInfo networkInfo;
 
   public NetworkStatus() {
@@ -50,12 +52,12 @@ public class NetworkStatus {
 
   /**
    * 연결된 네트워크의 종류를 반환한다.
-   * 연결되지 않았다면 -1 반환
+   * 연결되지 않았다면 DISCONNECTED(-1) 반환
    *
    * @return ConnectivityManager.TYPE_WIFI ...
    */
   public int getConnectedType() {
-    int type = -1;
+    int type = DISCONNECTED;
 
     try {
       if (null != networkInfo) {

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/presenter/MainPresenter.java

@@ -40,7 +40,7 @@ public class MainPresenter extends Presenter {
   }
 
   @Override
-  protected void onEvent(int eventId, int intValue, @Nullable String stringValue) {
+  public void performOnEvent(int eventId, int intValue, @Nullable String stringValue) {
     switch (eventId) {
       case Event.LOGIN:
         exitTo(ScreenID.LOGIN);

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/presenter/NetworkDisconnectedPresenter.java

@@ -39,7 +39,7 @@ public class NetworkDisconnectedPresenter extends Presenter {
   }
 
   @Override
-  protected void onEvent(int eventId, int intValue, @Nullable String stringValue) {
+  public void performOnEvent(int eventId, int intValue, @Nullable String stringValue) {
     switch (eventId) {
       case Event.LOGIN:
         exitTo(ScreenID.LOGIN);

+ 23 - 7
app/src/main/java/kr/co/zumo/app/lifeplus/view/presenter/Presenter.java

@@ -4,9 +4,10 @@
 package kr.co.zumo.app.lifeplus.view.presenter;
 
 import android.support.annotation.Nullable;
+import android.util.Log;
 
-import kr.co.zumo.app.lifeplus.view.IExit;
 import kr.co.zumo.app.lifeplus.supervisor.ScreenChanger;
+import kr.co.zumo.app.lifeplus.view.IExit;
 
 /**
  * 모든 Presenter 들이 상속받는 Presenter base
@@ -24,8 +25,8 @@ public abstract class Presenter implements IExit {
 
   protected ScreenChanger screenChanger;
 
-  public static final int INT_NONE = -232748271;  /// onEvent 로 intValue 가 전달되지 않으면 맵핑되는 값
-  public static final String STRING_NONE = null;  /// onEvent 로 stringValue 가 전달되지 않으면 맵핑되는 값
+  public static final int INT_NONE = -232748271;  /// performOnEvent 로 intValue 가 전달되지 않으면 맵핑되는 값
+  public static final String STRING_NONE = "";  /// performOnEvent 로 stringValue 가 전달되지 않으면 맵핑되는 값
 
   public Presenter(ScreenChanger screenChanger) {
     this.screenChanger = screenChanger;
@@ -41,6 +42,8 @@ public abstract class Presenter implements IExit {
    */
   public abstract void dispose();
 
+  protected abstract void performOnEvent(int eventId, int intValue, @Nullable String stringValue);
+
   /**
    * View 로 부터 전달되는 이벤트 처리
    *
@@ -48,7 +51,9 @@ public abstract class Presenter implements IExit {
    * @param intValue    int 데이터
    * @param stringValue string 데이터
    */
-  protected abstract void onEvent(int eventId, int intValue, @Nullable String stringValue);
+  public void onEvent(int eventId, int intValue, @Nullable String stringValue) {
+    performOnEvent(eventId, intValue, stringValue);
+  }
 
   /**
    * View 로 부터 전달되는 이벤트 처리
@@ -57,7 +62,7 @@ public abstract class Presenter implements IExit {
    * @param intValue int 데이터
    */
   public void onEvent(int eventId, int intValue) {
-    onEvent(eventId, intValue, STRING_NONE);
+    performOnEvent(eventId, intValue, STRING_NONE);
   }
 
   /**
@@ -67,7 +72,7 @@ public abstract class Presenter implements IExit {
    * @param stringValue string 데이터
    */
   public void onEvent(int eventId, String stringValue) {
-    onEvent(eventId, INT_NONE, stringValue);
+    performOnEvent(eventId, INT_NONE, stringValue);
   }
 
   /**
@@ -76,7 +81,18 @@ public abstract class Presenter implements IExit {
    * @param eventId Event.CLICK
    */
   public void onEvent(int eventId) {
-    onEvent(eventId, INT_NONE, STRING_NONE);
+    performOnEvent(eventId, INT_NONE, STRING_NONE);
+  }
+
+  /**
+   * 네트워크 상태 변경 시 SuperModel 으로 부터 이벤트가 전달된다.
+   *
+   * @param isConnected true|false
+   * @param connectType disconnected = NetworkStatus.DISCONNECTED
+   */
+  public void onNetworkStatusChanged(boolean isConnected, int connectType) {
+    //todo 네트워크 연결 해제 시 처리 필요;
+    Log.w("APP# Presenter | onNetworkStatusChanged", "|" + "isConnected: " + isConnected + ", type: " + connectType);
   }
 
   /**

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/presenter/login/LoginPresenter.java

@@ -41,7 +41,7 @@ public class LoginPresenter extends Presenter {
   }
 
   @Override
-  protected void onEvent(int eventId, int intValue, @Nullable String stringValue) {
+  public void performOnEvent(int eventId, int intValue, @Nullable String stringValue) {
     switch (eventId) {
       case Event.BACK:
         exitTo(ScreenID.MAIN);

+ 4 - 4
app/src/main/java/kr/co/zumo/app/lifeplus/view/presenter/signup/SignUpPresenter.java

@@ -59,16 +59,16 @@ public class SignUpPresenter extends Presenter {
   }
 
   @Override
-  protected void onEvent(int eventId, int intValue, @Nullable String stringValue) {
+  public void performOnEvent(int eventId, int intValue, @Nullable String stringValue) {
     switch (eventId) {
       case Event.BACK:
         exitTo(ScreenID.MAIN);
         break;
       case Event.CHECK:
-        Log.e("APP#  SignUpPresenter | onEvent", "|" + "check");
+        Log.e("APP#  SignUpPresenter | performOnEvent", "|" + "check");
         break;
       case Event.UNCHECK:
-        Log.e("APP#  SignUpPresenter | onEvent", "|" + "uncheck");
+        Log.e("APP#  SignUpPresenter | performOnEvent", "|" + "uncheck");
         break;
       case Event.CLICK:
         break;
@@ -87,7 +87,7 @@ public class SignUpPresenter extends Presenter {
          * - 실패 시 알림 후 재시도 준비
          */
         step.validate();
-        Log.i("APP# SignUpPresenter | onEvent", "|" + "step.isValidated(): " + step.isValidated());
+        Log.i("APP# SignUpPresenter | performOnEvent", "|" + "step.isValidated(): " + step.isValidated());
         if (step.isValidated()) {
           exitTo(ScreenID.MAIN);
         }