From a16c28139940bc15f4209dd013baaef1a26da514 Mon Sep 17 00:00:00 2001 From: Dewmal Date: Mon, 23 Mar 2020 19:26:13 +0530 Subject: [PATCH] Fix location issues --- .../ceylon/selftrackingapp/MainActivity.kt | 55 +++++++++++-------- assets/lang/ta.json | 4 +- lib/page/screen/case_details_screen.dart | 7 ++- lib/page/screen/dashboard_screen.dart | 6 ++ 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/android/app/src/main/kotlin/app/ceylon/selftrackingapp/MainActivity.kt b/android/app/src/main/kotlin/app/ceylon/selftrackingapp/MainActivity.kt index b268769..35666b3 100644 --- a/android/app/src/main/kotlin/app/ceylon/selftrackingapp/MainActivity.kt +++ b/android/app/src/main/kotlin/app/ceylon/selftrackingapp/MainActivity.kt @@ -27,37 +27,20 @@ class MainActivity : FlutterActivity() { private lateinit var locationDao: LocationDao; + var isLocationServiceRunning = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) GeneratedPluginRegistrant.registerWith(this.flutterEngine!!); locationDao = LocationDatabase.getInstance(this).locationDao() - val permissionAccessCoarseLocationApproved = ActivityCompat - .checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == - PackageManager.PERMISSION_GRANTED - - if (permissionAccessCoarseLocationApproved) { - // App has permission to access location in the foreground. Start your - // foreground service that has a foreground service type of "location". - } else { - // Make a request for foreground-only location access. - ActivityCompat.requestPermissions(this, - arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), - LOCATION_UPDATE_REQUEST_CODE - ) - } - Intent(this, LocationTrackingService::class.java).also { intent -> - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - startForegroundService(intent) - }else{ - startService(intent); - } -// startService(intent); - } + + MethodChannel(flutterEngine!!.dartExecutor, CHANNEL).setMethodCallHandler { call, result -> + + if (call.method == "getLocation") { val handler = Handler() @@ -73,12 +56,38 @@ class MainActivity : FlutterActivity() { jsonLocation.put("title", location.dateString) locaitons.add(jsonLocation.toString(10)) } - + handler.post { result.success(locaitons); } } + } else if (call.method == "requestLocationPermission") { + val permissionAccessCoarseLocationApproved = ActivityCompat + .checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == + PackageManager.PERMISSION_GRANTED + + if (permissionAccessCoarseLocationApproved) { + result.success("PERMISSION_GRANTED"); + } else { + // Make a request for foreground-only location access. + ActivityCompat.requestPermissions(this, + arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), + LOCATION_UPDATE_REQUEST_CODE + ) + result.success("PERMISSION_GRANTED"); + } + } else if (call.method == "openLocationService") { + if (!isLocationServiceRunning) + Intent(this, LocationTrackingService::class.java).also { intent -> + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + startForegroundService(intent) + } else { + startService(intent); + } + isLocationServiceRunning = true + } + result.success("LOCATION_SERVICE_RUNNING"); } } diff --git a/assets/lang/ta.json b/assets/lang/ta.json index 5ca9169..8268f16 100644 --- a/assets/lang/ta.json +++ b/assets/lang/ta.json @@ -16,8 +16,8 @@ "dashboard_safe_track_tab_text": "நோயாளியின் பயணம்", "dashboard_case_list_tab_text": "சம்பவங்கள்", "dashboard_contact_tab_text": "தொடர்பு கொள்க", - "dashboard_register_tab_text": " பதிவு செய்க", - "user_register_bar_title_text": " பதிவு செய்க", + "dashboard_register_tab_text": "பதிவு செய்க", + "user_register_bar_title_text": "பதிவு செய்க", "user_register_screen_title": " பதிவு செய்க", "user_register_screen_subtitle": "உறுதிப்படுத்தப்பட்ட சம்பவத்திற்கு அருகில் இருந்தீர்களா?", "user_register_screen_name": "பெயர்", diff --git a/lib/page/screen/case_details_screen.dart b/lib/page/screen/case_details_screen.dart index dfb72d0..238481e 100644 --- a/lib/page/screen/case_details_screen.dart +++ b/lib/page/screen/case_details_screen.dart @@ -30,7 +30,12 @@ class CaseDetailScreenState extends State { @override void initState() { super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) { + _channel.invokeMethod('requestLocationPermission').then((res) { + _channel.invokeMethod('openLocationService').then((res) {}); + }); + _locationTimer = Timer.periodic(Duration(minutes: 5), (timer) async { print("POLLING the locations"); List newEntries = await getLocationUpdate(); @@ -116,7 +121,7 @@ class CaseDetailScreenState extends State { zoom: 12, ), myLocationButtonEnabled: true, - myLocationEnabled: true, +// myLocationEnabled: true, onMapCreated: (GoogleMapController controller) { _controller.complete(controller); }, diff --git a/lib/page/screen/dashboard_screen.dart b/lib/page/screen/dashboard_screen.dart index c70bd39..a324b48 100644 --- a/lib/page/screen/dashboard_screen.dart +++ b/lib/page/screen/dashboard_screen.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; import 'package:selftrackingapp/app_localizations.dart'; import 'package:selftrackingapp/models/message_type.dart'; @@ -28,6 +29,7 @@ class DashboardScreen extends StatefulWidget { } class _DashboardScreenState extends State { + static const MethodChannel _channel = MethodChannel('location'); RemoteConfig config; PageController _pageController; @@ -53,6 +55,10 @@ class _DashboardScreenState extends State { _timer = Timer.periodic(Duration(minutes: 15), (timer) { updateDashboard(); }); + + _channel.invokeMethod('requestLocationPermission').then((res) { + _channel.invokeMethod('openLocationService').then((res) {}); + }); } Future fetchArticles() async {