Leon há 1 ano atrás
pai
commit
fd32b0f180

+ 141 - 71
android/app/src/main/java/com/hola/HolaModule.java

@@ -9,15 +9,23 @@ import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.location.Location;
+import android.location.LocationListener;
 import android.location.LocationManager;
 import android.net.Uri;
 import android.os.Build;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
 import android.os.SystemClock;
 import android.provider.Settings;
 import android.util.Log;
+import android.widget.Toast;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.core.app.ActivityCompat;
 
 import com.facebook.react.bridge.Callback;
 import com.facebook.react.bridge.Promise;
@@ -39,6 +47,12 @@ public class HolaModule extends ReactContextBaseJavaModule {
     private static final String CHANNEL_NAME = "Daily Reminder";
     private static final String CHANNEL_DESCRIPTION = "Daily reminder notification ";
 
+    private Location gpsLocation;
+    private Location networkLocation;
+    private Promise locationPromise;
+    private Handler handler;
+    private Runnable runnable;
+
 
     public HolaModule(@Nullable ReactApplicationContext reactContext) {
         super(reactContext);
@@ -80,8 +94,6 @@ public class HolaModule extends ReactContextBaseJavaModule {
         Activity currentActivity = getCurrentActivity();
         Context context = getReactApplicationContext();
         Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
-//        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
-//        intent.setData(uri);
         currentActivity.startActivity(intent);
     }
 
@@ -115,9 +127,6 @@ public class HolaModule extends ReactContextBaseJavaModule {
 
     @ReactMethod
     public void addLocalPush(String jsonData){
-
-
-
         if (getCurrentActivity()==null){
             return;
         }
@@ -187,81 +196,51 @@ public class HolaModule extends ReactContextBaseJavaModule {
         }
     }
 
+    public static long toNearest1000(long number) {
+        return ((number + 500) / 1000) * 1000;
+    }
+
     @ReactMethod
-    public void addLocalPush2(String jsonData){
-        if (getCurrentActivity()==null){
+    public void rnPageLoaded(){
+        GlobalVariable.rnLoaded = true;
+    }
+
+    @ReactMethod
+    public void getLocation(Promise promise){
+        Context context = getReactApplicationContext();
+        if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
             return;
         }
-        try {
-            JSONArray jsonArray = new JSONArray(jsonData);
-            for (int i = 0; i < jsonArray.length(); i++) {
-                JSONObject jsonObject = jsonArray.getJSONObject(i);
-                String mode = jsonObject.getString("mode");
-                String title = jsonObject.getString("title");
-                String body = jsonObject.getString("body");
-                String strId = jsonObject.getString("id");
-                String categoryId = jsonObject.getString("category_id");
-
-                createNotificationChannel(getCurrentActivity(),categoryId);
-                Intent intent = new Intent(getCurrentActivity(), AlarmReceiver.class);
-                intent.putExtra("title", title);
-                intent.putExtra("message", body);
-                intent.putExtra("channel", categoryId);
-                intent.putExtra("id",strId);
-                intent.putExtra("categoryId",categoryId);
-                intent.putExtra("msg_id",i);
-                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-
-                PendingIntent pendingIntent = PendingIntent.getBroadcast(
-                        getCurrentActivity(), i, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
-
-                AlarmManager alarmManager = (AlarmManager) getCurrentActivity().getSystemService(Context.ALARM_SERVICE);
-
-                AlarmManager.AlarmClockInfo info = alarmManager.getNextAlarmClock();
-                long t = info.getTriggerTime();
-                PendingIntent pt = info.getShowIntent();
-                String aa = info.toString();
-
-                if (mode.equals("ONCE")){
-                    long leftMillis = toNearest1000(jsonObject.getLong("once")-System.currentTimeMillis());
-                    if (leftMillis>0){
-                        alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+leftMillis,pendingIntent);
-                    }
-                }
-                else {
-                    JSONObject recurring = jsonObject.getJSONObject("recurring");
-                    String time = recurring.getString("time");
-                    String[] parts = time.split(":");
-                    Calendar calendar = Calendar.getInstance();
-                    calendar.setTimeInMillis(System.currentTimeMillis());
-                    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parts[0]));
-                    calendar.set(Calendar.MINUTE, Integer.parseInt(parts[1]));
-                    calendar.set(Calendar.SECOND,Integer.parseInt(parts[2]));
-
-                    // 如果当前时间已经超过 设定时间,则设置为明天
-                    if (calendar.getTimeInMillis() <= System.currentTimeMillis()) {
-                        calendar.add(Calendar.DAY_OF_YEAR, 1);
-                    }
+        locationPromise = promise;
+        //step1
+        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+        //step2
+        boolean hasGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
+        boolean hasNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
+        //step3
+
+        if (hasGPS) {
+            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0f,
+                    gpsListener);
+        }
 
-                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
-                        AlarmManager.INTERVAL_DAY, pendingIntent);
-                    Log.i("set alarm status:",time);
+        if (hasNetwork){
+            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0f,
+                    networkListener);
+        }
 
-                }
 
+        handler = new Handler(Looper.getMainLooper());
+        runnable = new Runnable() {
+            @Override
+            public void run() {
+                locationPromise.reject("error","timeout");
+                stopLocation();
             }
-        } catch (JSONException e) {
-            e.printStackTrace();
-        }
-    }
+        };
+        handler.postDelayed(runnable,300000);
 
-    public static long toNearest1000(long number) {
-        return ((number + 500) / 1000) * 1000;
-    }
 
-    @ReactMethod
-    public void rnPageLoaded(){
-        GlobalVariable.rnLoaded = true;
     }
 
     private void createNotificationChannel(Context context,String channelId) {
@@ -273,4 +252,95 @@ public class HolaModule extends ReactContextBaseJavaModule {
             notificationManager.createNotificationChannel(channel);
         }
     }
+
+    private void stopLocation(){
+        Context context = getReactApplicationContext();
+        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+        locationManager.removeUpdates(gpsListener);
+        locationManager.removeUpdates(networkListener);
+
+        if (handler!=null){
+            handler.removeCallbacks(runnable);
+        }
+    }
+
+    LocationListener gpsListener = new LocationListener() {
+
+        @Override
+        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
+            // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void onProviderEnabled(String arg0) {
+            // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void onProviderDisabled(String arg0) {
+            // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void onLocationChanged(Location arg0) {
+            // TODO Auto-generated method stub
+            // 更新当前经纬度
+            gpsLocation = arg0;
+
+            JSONObject data = new JSONObject();
+            try {
+                data.put("latitude", gpsLocation.getLatitude());
+                data.put("longitude", gpsLocation.getLongitude());
+            } catch (JSONException e) {
+                throw new RuntimeException(e);
+            }
+            locationPromise.resolve(data.toString());
+
+            stopLocation();
+        }
+    };
+
+    LocationListener networkListener = new LocationListener() {
+
+        @Override
+        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
+            // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void onProviderEnabled(String arg0) {
+            // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void onProviderDisabled(String arg0) {
+            // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void onLocationChanged(Location arg0) {
+            // TODO Auto-generated method stub
+            // 更新当前经纬度
+            networkLocation = arg0;
+
+            JSONObject data = new JSONObject();
+            try {
+                data.put("latitude", networkLocation.getLatitude());
+                data.put("longitude", networkLocation.getLongitude());
+            } catch (JSONException e) {
+                throw new RuntimeException(e);
+            }
+            locationPromise.resolve(data.toString());
+
+            stopLocation();
+        }
+
+
+    };
 }

+ 0 - 7
android/app/src/main/java/com/hola/MainActivity.java

@@ -81,13 +81,6 @@ public class MainActivity extends ReactActivity {
 
   private void demo2() {
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
-      // TODO: Consider calling
-      //    ActivityCompat#requestPermissions
-      // here to request the missing permissions, and then overriding
-      //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
-      //                                          int[] grantResults)
-      // to handle the case where the user grants the permission. See the documentation
-      // for ActivityCompat#requestPermissions for more details.
       return;
     }
     //step1