Skip to content

Commit

Permalink
Fix location issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dewmal committed Mar 23, 2020
1 parent 195b27d commit a16c281
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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");
}
}

Expand Down
4 changes: 2 additions & 2 deletions assets/lang/ta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "பெயர்",
Expand Down
7 changes: 6 additions & 1 deletion lib/page/screen/case_details_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ class CaseDetailScreenState extends State<CaseDetailScreen> {
@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<Location> newEntries = await getLocationUpdate();
Expand Down Expand Up @@ -116,7 +121,7 @@ class CaseDetailScreenState extends State<CaseDetailScreen> {
zoom: 12,
),
myLocationButtonEnabled: true,
myLocationEnabled: true,
// myLocationEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
Expand Down
6 changes: 6 additions & 0 deletions lib/page/screen/dashboard_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,6 +29,7 @@ class DashboardScreen extends StatefulWidget {
}

class _DashboardScreenState extends State<DashboardScreen> {
static const MethodChannel _channel = MethodChannel('location');
RemoteConfig config;
PageController _pageController;

Expand All @@ -53,6 +55,10 @@ class _DashboardScreenState extends State<DashboardScreen> {
_timer = Timer.periodic(Duration(minutes: 15), (timer) {
updateDashboard();
});

_channel.invokeMethod('requestLocationPermission').then((res) {
_channel.invokeMethod('openLocationService').then((res) {});
});
}

Future<void> fetchArticles() async {
Expand Down

0 comments on commit a16c281

Please sign in to comment.