Browse Source

[메인][New] 위치 탐색에서 gps는 사용안함, stop/start 시 locationManager 조절

hyodong.min 6 years ago
parent
commit
ba9a0bca25

+ 59 - 64
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/main/MainModel.java

@@ -23,8 +23,10 @@ import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 
 import io.reactivex.Completable;
+import io.reactivex.Single;
 import io.reactivex.android.schedulers.AndroidSchedulers;
 import io.reactivex.disposables.Disposable;
+import io.reactivex.schedulers.Schedulers;
 import kr.co.zumo.app.lifeplus.bean.api.LifeplusAPIBean;
 import kr.co.zumo.app.lifeplus.bean.api.LifeplusData;
 import kr.co.zumo.app.lifeplus.bean.api.LoginResultBean;
@@ -102,11 +104,13 @@ public class MainModel extends Model {
      */
     BucketListModelHelper.getInstance().dispose();
     BookmarkModelHelper.getInstance().dispose();
+
+    watchLocation();
   }
 
   @Override
   protected void stopInternal() {
-
+    stopLocation();
   }
 
   @Override
@@ -329,18 +333,27 @@ public class MainModel extends Model {
 
   private void loadWeatherWithLocationInternal() {
     isRequestedLocation = false;
-    String areaCode = WeatherBean.getDefaultAreaCode();
 
-    if (latitude != 0 && longitude != 0) {
-      String address = getAddress(latitude, longitude);
-      Log.e("APP#  MainModel | loadWeatherWithLocationInternal", "| -> " + address);
+    Disposable d = Single.fromCallable(() -> {
+      String areaCode = WeatherBean.getDefaultAreaCode();
+
+      if (latitude != 0 && longitude != 0) {
+        String address = getAddress(latitude, longitude);
+        Log.e("APP#  MainModel | loadWeatherWithLocationInternal", "| -> " + address);
 
-      if (null != address && "none".equals(address) == false) {
-        areaCode = WeatherBean.getAreaCode(address);
+        if (null != address && "none".equals(address) == false) {
+          areaCode = WeatherBean.getAreaCode(address);
+        }
       }
-    }
+      return areaCode;
+
+    })
+      .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
+      .subscribe(code -> {
+        loadWeatherInternal(code);
+      }, Throwable::printStackTrace);
+
 
-    loadWeatherInternal(areaCode);
   }
 
   /**
@@ -349,7 +362,8 @@ public class MainModel extends Model {
    * @return
    */
   public boolean isLocationServiceEnabled() {
-    return SuperModel.getInstance().getPreferences().isLocationServiceEnabled();
+    // 비회원은 항상 허용
+    return SuperModel.getInstance().getPreferences().isLocationServiceEnabled() || isJoined() == false;
   }
 
   public void setLocationServiceEnabled(boolean isEnabled) {
@@ -387,7 +401,7 @@ public class MainModel extends Model {
   private LocationManager locationManager;
   private LocationListener locationNetworkListener;
   //  private LocationListener locationGpsListener;
-  private boolean isGPSEnabled;
+//  private boolean isGPSEnabled;
   private boolean isNetworkEnabled;
   private double latitude;
   private double longitude;
@@ -396,34 +410,48 @@ public class MainModel extends Model {
 
   private void prepareLocation() {
     locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
-    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
+//    isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
 
-    Log.e("APP#  MainModel | prepareLocation", "|" + "gps: " + isGPSEnabled + ", network: " + isNetworkEnabled);
+    Log.e("APP#  MainModel | prepareLocation", "|" + /*"gps: " + isGPSEnabled + */", network: " + isNetworkEnabled);
+  }
+
+  private void stopLocation() {
+    Log.w("APP# MainModel | stopLocation", "|" + " <-------- stop locating...");
+    if (null != locationManager) {
+      locationManager.removeUpdates(locationNetworkListener);
+    }
+  }
+
+  @SuppressLint("MissingPermission")
+  private void watchLocation() {
+    Log.w("APP# MainModel | stopLocation", "|" + " --------> start locating...");
+    if (null != locationManager && hasLocationPermissions()) {
+      locationManager.removeUpdates(locationNetworkListener);
+//      locationManager.removeUpdates(locationGpsListener);
+      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 100, locationNetworkListener);
+//      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 100, locationNetworkListener);
+    }
   }
 
   @SuppressLint("MissingPermission")
   private void getLocation() {
     Location location = null;
 
-    Log.e("APP#  MainModel | getLocation", "|" + "gps: " + isGPSEnabled + ", network: " + isNetworkEnabled);
+    Log.e("APP#  MainModel | getLocation", "|" + /*"gps: " + isGPSEnabled + */", network: " + isNetworkEnabled);
 
-    if (null != locationManager) {
-      locationManager.removeUpdates(locationNetworkListener);
-//      locationManager.removeUpdates(locationGpsListener);
-      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 100, locationNetworkListener);
-      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 100, locationNetworkListener);
+    if (null != locationManager && hasLocationPermissions()) {
 
       location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
       if (null != location) {
         Log.w("APP#  MainModel | getLocation", "| via network: " + location.getLatitude() + ", " + location.getLongitude());
       }
-      else {
-        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
-        if (null != location) {
-          Log.w("APP#  MainModel | getLocation", "| via gps: " + location.getLongitude() + ", " + location.getLatitude());
-        }
-      }
+//      else {
+//        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+//        if (null != location) {
+//          Log.w("APP#  MainModel | getLocation", "| via gps: " + location.getLongitude() + ", " + location.getLatitude());
+//        }
+//      }
     }
 
     if (null == location) {
@@ -447,7 +475,6 @@ public class MainModel extends Model {
       longitude = location.getLongitude();
       loadWeatherWithLocationInternal();
     }
-
   }
 
   private String getAddress(double latitude, double longitude) {
@@ -457,7 +484,7 @@ public class MainModel extends Model {
 
     try {
       addressList = geocoder.getFromLocation(latitude, longitude, 1);
-      if (addressList != null && addressList.size() > 0) {
+      if (null != addressList && addressList.size() > 0) {
         Address area = addressList.get(0);
         nowAddress = area.getAdminArea();   // area.getAddressLine(0);
         Log.i("APP# MainModel | getAddress", "|" + " address: " + nowAddress);
@@ -470,7 +497,7 @@ public class MainModel extends Model {
   }
 
   public boolean canDeviceLocating() {
-    return isNetworkEnabled && isGPSEnabled;
+    return isNetworkEnabled /*&& isGPSEnabled*/;
   }
 
   public boolean hasLocationPermissions() {
@@ -580,9 +607,9 @@ public class MainModel extends Model {
         if (NETWORK.equals(s)) {
           isNetworkEnabled = true;
         }
-        else if (GPS.equals(s)) {
+        /*else if (GPS.equals(s)) {
           isGPSEnabled = true;
-        }
+        }*/
         Log.w("APP# MainModel | onProviderEnabled", "|" + s);
       }
 
@@ -591,43 +618,11 @@ public class MainModel extends Model {
         if (NETWORK.equals(s)) {
           isNetworkEnabled = false;
         }
-        else if (GPS.equals(s)) {
+        /*else if (GPS.equals(s)) {
           isGPSEnabled = false;
-        }
+        }*/
         Log.w("APP# MainModel | onProviderDisabled", "|" + s);
       }
     };
-
-//    locationGpsListener = new LocationListener() {
-//      @Override
-//      public void onLocationChanged(Location location) {
-//        if (isRequestedLocation) {
-//          isRequestedLocation = false;
-//          latitude = location.getLatitude();
-//          longitude = location.getLongitude();
-//
-//          Log.e("APP#  MainModel | onLocationChanged", "| gps: " + latitude + ", " + longitude);
-//
-//          loadWeatherWithLocationInternal();
-//        }
-//      }
-//
-//      @Override
-//      public void onStatusChanged(String s, int i, Bundle bundle) {
-//        Log.w("APP# MainModel | onStatusChanged", "| gps: " + s);
-//      }
-//
-//      @Override
-//      public void onProviderEnabled(String s) {
-//        isGPSEnabled = true;
-//        Log.w("APP# MainModel | onProviderEnabled", "| gps: " + s);
-//      }
-//
-//      @Override
-//      public void onProviderDisabled(String s) {
-//        isGPSEnabled = false;
-//        Log.w("APP# MainModel | onProviderDisabled", "| gps: " + s);
-//      }
-//    };
   }
 }