mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Added FINE_LOCATION permission checks. Added location update and save logic.
This commit is contained in:
1 parent
ad3900a17f
commit
f2e3d99a85
6 files changed
+91
-16
No files matched your search
+4
-1
@@ -54,11 +54,14 @@ dependencies {
|
||||
|
||||
// retrofit
|
||||
implementation "com.squareup.retrofit2:retrofit:$retrofit"
|
||||
implementation"com.squareup.retrofit2:converter-gson:$retrofit"
|
||||
implementation "com.squareup.retrofit2:converter-gson:$retrofit"
|
||||
|
||||
// predict4java
|
||||
implementation "com.github.davidmoten:predict4java:$predict4java"
|
||||
|
||||
// location
|
||||
implementation "com.google.android.gms:play-services-location:$google_play"
|
||||
|
||||
// testing
|
||||
testImplementation "junit:junit:$junit"
|
||||
androidTestImplementation "androidx.test:runner:$runner"
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.rtbishop.lookingsat">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="false"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.rtbishop.lookingsat
|
||||
|
||||
import android.Manifest
|
||||
import android.content.SharedPreferences
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.location.Location
|
||||
import android.os.Bundle
|
||||
import android.os.CountDownTimer
|
||||
import android.view.Menu
|
||||
@@ -11,6 +15,9 @@ import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.widget.Toolbar
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.edit
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import androidx.navigation.findNavController
|
||||
@@ -18,27 +25,38 @@ import androidx.navigation.ui.AppBarConfiguration
|
||||
import androidx.navigation.ui.navigateUp
|
||||
import androidx.navigation.ui.setupActionBarWithNavController
|
||||
import androidx.navigation.ui.setupWithNavController
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.google.android.gms.location.FusedLocationProviderClient
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import com.google.android.material.navigation.NavigationView
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
const val LOCATION_REQ = 101
|
||||
const val LOCATION_PERM = Manifest.permission.ACCESS_FINE_LOCATION
|
||||
const val GRANTED = PackageManager.PERMISSION_GRANTED
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var appBarConfig: AppBarConfiguration
|
||||
private lateinit var mainViewModel: MainViewModel
|
||||
private lateinit var viewModel: MainViewModel
|
||||
private lateinit var timerLayout: ConstraintLayout
|
||||
private lateinit var timeToAos: TextView
|
||||
private lateinit var fusedLocationClient: FusedLocationProviderClient
|
||||
private lateinit var preferences: SharedPreferences
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
setupViews()
|
||||
setupComponents()
|
||||
setupNavigation()
|
||||
setupTimer()
|
||||
}
|
||||
|
||||
private fun setupViews() {
|
||||
mainViewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
|
||||
private fun setupComponents() {
|
||||
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
|
||||
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
timerLayout = findViewById(R.id.timer_layout)
|
||||
timeToAos = findViewById(R.id.time_to_aos)
|
||||
}
|
||||
@@ -99,6 +117,47 @@ class MainActivity : AppCompatActivity() {
|
||||
timer.start()
|
||||
}
|
||||
|
||||
private fun updateLocation() {
|
||||
fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
|
||||
val currentLat = location?.latitude
|
||||
val currentLon = location?.longitude
|
||||
val currentHeight = location?.altitude
|
||||
|
||||
preferences.edit {
|
||||
putString("LATITUDE", currentLat.toString())
|
||||
putString("LONGITUDE", currentLon.toString())
|
||||
putString("HEIGHT", currentHeight.toString())
|
||||
apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkPermissions() {
|
||||
if (ContextCompat.checkSelfPermission(this, LOCATION_PERM) != GRANTED) {
|
||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, LOCATION_PERM)) {
|
||||
Toast.makeText(this, "Missing permissions", Toast.LENGTH_LONG).show()
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(this, arrayOf(LOCATION_PERM), LOCATION_REQ)
|
||||
}
|
||||
} else {
|
||||
updateLocation()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(
|
||||
requestCode: Int,
|
||||
permissions: Array<out String>,
|
||||
grantResults: IntArray
|
||||
) {
|
||||
when (requestCode) {
|
||||
LOCATION_REQ -> {
|
||||
if (grantResults.isNotEmpty() && grantResults[0] == GRANTED) {
|
||||
updateLocation()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSupportNavigateUp(): Boolean {
|
||||
val navController = findNavController(R.id.nav_host)
|
||||
return navController.navigateUp(appBarConfig) || super.onSupportNavigateUp()
|
||||
@@ -111,11 +170,8 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.fetch_trans -> Toast.makeText(
|
||||
this,
|
||||
"Fetching transmitters",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
R.id.update_location -> checkPermissions()
|
||||
R.id.fetch_trans -> Toast.makeText(this, "Fetch transmit", Toast.LENGTH_SHORT).show()
|
||||
R.id.save_to_db -> Toast.makeText(this, "Saving to DB", Toast.LENGTH_SHORT).show()
|
||||
R.id.load_from_db -> Toast.makeText(this, "Loading from DB", Toast.LENGTH_SHORT).show()
|
||||
R.id.fetch_tle -> Toast.makeText(this, "Fetching TLE", Toast.LENGTH_SHORT).show()
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:id="@+id/update_location"
|
||||
android:title="@string/menu_update_location" />
|
||||
<item
|
||||
android:id="@+id/fetch_trans"
|
||||
android:title="Fetch Transmitters" />
|
||||
android:title="@string/menu_fetch_transmitters" />
|
||||
<item
|
||||
android:id="@+id/save_to_db"
|
||||
android:title="Save to DB" />
|
||||
android:title="@string/menu_save_to_db" />
|
||||
<item
|
||||
android:id="@+id/load_from_db"
|
||||
android:title="Load from DB" />
|
||||
android:title="@string/menu_load_from_db" />
|
||||
<item
|
||||
android:id="@+id/fetch_tle"
|
||||
android:title="Fetch TLE" />
|
||||
android:title="@string/menu_fetch_tle" />
|
||||
<item
|
||||
android:id="@+id/load_tle"
|
||||
android:title="Load TLE" />
|
||||
android:title="@string/menu_load_tle" />
|
||||
<item
|
||||
android:id="@+id/action_exit"
|
||||
android:title="Exit" />
|
||||
android:title="@string/menu_exit" />
|
||||
</menu>
|
||||
@@ -22,6 +22,14 @@
|
||||
<string name="height_def">64</string>
|
||||
<string name="height_summary">Ground station height in meters AMSL</string>
|
||||
|
||||
<string name="menu_update_location">Update location</string>
|
||||
<string name="menu_fetch_transmitters">Fetch Transmitters</string>
|
||||
<string name="menu_save_to_db">Save to DB</string>
|
||||
<string name="menu_load_from_db">Load from DB</string>
|
||||
<string name="menu_fetch_tle">Fetch TLE</string>
|
||||
<string name="menu_load_tle">Load TLE</string>
|
||||
<string name="menu_exit">Exit</string>
|
||||
|
||||
<string name="next_aos_pattern">Next AOS in %02d:%02d:%02d</string>
|
||||
<string name="map_content">Nasa Map</string>
|
||||
<string name="placeholder">Placeholder</string>
|
||||
|
||||
@@ -41,6 +41,8 @@ ext {
|
||||
|
||||
predict4java = '1.3.1'
|
||||
|
||||
google_play = '17.0.0'
|
||||
|
||||
junit = "4.12"
|
||||
runner = '1.1.1'
|
||||
espresso = '3.2.0'
|
||||
|
||||
Reference in new issue
Block a user