diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-06-23 21:29:13 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-06-23 21:29:13 -0700 |
commit | df669fcd9dbdd16ed2591c7b39b242e9c293fbdd (patch) | |
tree | 35d58c556e1f15a416620058ea1af1e92f39a227 /app/src | |
parent | 2782d5b9da67ac759ca35e02a7945358316f8d69 (diff) | |
download | etbsa-traccar-manager-android-df669fcd9dbdd16ed2591c7b39b242e9c293fbdd.tar.gz etbsa-traccar-manager-android-df669fcd9dbdd16ed2591c7b39b242e9c293fbdd.tar.bz2 etbsa-traccar-manager-android-df669fcd9dbdd16ed2591c7b39b242e9c293fbdd.zip |
Implement location support
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/AndroidManifest.xml | 1 | ||||
-rw-r--r-- | app/src/main/java/org/traccar/manager/MainActivity.java | 15 | ||||
-rw-r--r-- | app/src/main/java/org/traccar/manager/MainFragment.java | 49 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
4 files changed, 65 insertions, 1 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 41cbaf6..7931b2b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ package="org.traccar.manager"> <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" diff --git a/app/src/main/java/org/traccar/manager/MainActivity.java b/app/src/main/java/org/traccar/manager/MainActivity.java index 863ac79..b23e89f 100644 --- a/app/src/main/java/org/traccar/manager/MainActivity.java +++ b/app/src/main/java/org/traccar/manager/MainActivity.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2021 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,13 @@ */ package org.traccar.manager; +import android.app.Fragment; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; + +import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @@ -40,4 +45,12 @@ public class MainActivity extends AppCompatActivity { } } + @RequiresApi(api = Build.VERSION_CODES.M) + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + Fragment fragment = getFragmentManager().findFragmentById(android.R.id.content); + if (fragment != null) { + fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + } } diff --git a/app/src/main/java/org/traccar/manager/MainFragment.java b/app/src/main/java/org/traccar/manager/MainFragment.java index 78bfee1..1e98b17 100644 --- a/app/src/main/java/org/traccar/manager/MainFragment.java +++ b/app/src/main/java/org/traccar/manager/MainFragment.java @@ -15,6 +15,7 @@ */ package org.traccar.manager; +import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ActivityNotFoundException; @@ -23,11 +24,13 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; +import android.webkit.GeolocationPermissions; import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; @@ -35,7 +38,11 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewFragment; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; public class MainFragment extends WebViewFragment { @@ -44,6 +51,7 @@ public class MainFragment extends WebViewFragment { public final static String EVENT_TOKEN = "eventToken"; public final static String KEY_TOKEN = "keyToken"; + private static final int REQUEST_PERMISSIONS_LOCATION = 1; private final static int REQUEST_FILE_CHOOSER = 1; private LocalBroadcastManager broadcastManager; @@ -129,8 +137,49 @@ public class MainFragment extends WebViewFragment { } } + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (requestCode == REQUEST_PERMISSIONS_LOCATION) { + boolean granted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; + if (geolocationCallback != null) { + geolocationCallback.invoke(geolocationRequestOrigin, granted, false); + geolocationRequestOrigin = null; + geolocationCallback = null; + } + } + } + + private String geolocationRequestOrigin; + private GeolocationPermissions.Callback geolocationCallback; + private final WebChromeClient webChromeClient = new WebChromeClient() { + @Override + public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { + geolocationRequestOrigin = null; + geolocationCallback = null; + if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) { + new AlertDialog.Builder(getActivity()) + .setMessage(R.string.permission_location_rationale) + .setNeutralButton(android.R.string.ok, (dialog, which) -> { + geolocationRequestOrigin = origin; + geolocationCallback = callback; + ActivityCompat.requestPermissions( + getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_PERMISSIONS_LOCATION); + }) + .show(); + } else { + geolocationRequestOrigin = origin; + geolocationCallback = callback; + ActivityCompat.requestPermissions( + getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_PERMISSIONS_LOCATION); + } + } else { + callback.invoke(origin, true, false); + } + } + // Android 4.1+ protected void openFileChooser(ValueCallback<Uri> uploadMessage, String acceptType, String capture) { openFileChooser(uploadMessage); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2ae7947..feea781 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,4 +6,5 @@ <string name="error_connection">Server connection failed</string> <string name="file_browser">File Browser</string> <string name="notification_channel">Default</string> + <string name="permission_location_rationale">App requires location permission to show current location on the map</string> </resources> |