Created
August 18, 2020 10:54
-
-
Save TychoWerner/5499c4367fc196a55a3b7ad67dc1cf90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.tychowerner.drivingvolume | |
| import android.Manifest | |
| import android.content.pm.PackageManager | |
| import android.location.Location | |
| import android.media.AudioManager | |
| import android.os.Bundle | |
| import android.util.Log | |
| import android.widget.Button | |
| import android.widget.Toast | |
| import androidx.appcompat.app.AppCompatActivity | |
| import androidx.core.app.ActivityCompat | |
| import com.google.android.gms.common.api.ResolvableApiException | |
| import com.google.android.gms.location.* | |
| import com.google.android.gms.tasks.RuntimeExecutionException | |
| import com.google.android.gms.tasks.Task | |
| import kotlinx.android.synthetic.main.activity_main.* | |
| class MainActivity : AppCompatActivity() { | |
| lateinit var currentLocation: Location | |
| lateinit var fusedLocationProviderClient: FusedLocationProviderClient | |
| private val REQUEST_LOCATION = 1 | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this) | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val audioManager: AudioManager = getSystemService(AUDIO_SERVICE) as AudioManager | |
| val currentVolume = audioManager.getStreamVolume(9) | |
| val up = findViewById<Button>(R.id.upButton) | |
| val down = findViewById<Button>(R.id.downButton) | |
| val location = findViewById<Button>(R.id.location) | |
| // set on-click listener | |
| up.setOnClickListener { | |
| val currentVolume = getCurrentVolume() | |
| val volumeToSet = currentVolume + 1; | |
| Toast.makeText(this@MainActivity, "Current volume: $currentVolume", Toast.LENGTH_SHORT) | |
| .show() | |
| audioManager.setStreamVolume(9, volumeToSet, 1) | |
| updateCurrentVolume() | |
| } | |
| down.setOnClickListener { | |
| val currentVolume = getCurrentVolume() | |
| val volumeToSet = currentVolume - 1; | |
| Toast.makeText(this@MainActivity, "Current volume: $currentVolume", Toast.LENGTH_SHORT) | |
| .show() | |
| audioManager.setStreamVolume(9, volumeToSet, 1) | |
| updateCurrentVolume() | |
| } | |
| location.setOnClickListener { | |
| fetchLocation() | |
| } | |
| } | |
| override fun onRequestPermissionsResult( | |
| requestCode: Int, | |
| permissions: Array<out String>, | |
| grantResults: IntArray | |
| ) { | |
| if (requestCode == REQUEST_LOCATION) { | |
| if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
| fetchLocation() | |
| } else { | |
| Toast.makeText( | |
| this, | |
| "Location permission is required to locate you!", | |
| Toast.LENGTH_LONG | |
| ).show() | |
| } | |
| } | |
| } | |
| private fun fetchLocation() { | |
| if (ActivityCompat.checkSelfPermission( | |
| this, | |
| Manifest.permission.ACCESS_FINE_LOCATION | |
| ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( | |
| this, Manifest.permission.ACCESS_COARSE_LOCATION | |
| ) != PackageManager.PERMISSION_GRANTED | |
| ) { | |
| ActivityCompat.requestPermissions( | |
| this, | |
| arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), | |
| REQUEST_LOCATION | |
| ) | |
| } else { | |
| val task: Task<Location> = fusedLocationProviderClient.lastLocation | |
| task.addOnSuccessListener { | |
| if (it != null) { | |
| currentLocation = it | |
| } else { | |
| val REQUEST_CHECK_STATE = 12300 // any suitable ID | |
| val builder = LocationSettingsRequest.Builder() | |
| .addLocationRequest(reqSetting) | |
| val client = LocationServices.getSettingsClient(this) | |
| client.checkLocationSettings(builder.build()).addOnCompleteListener { task -> | |
| try { | |
| val state: LocationSettingsStates = task.result!!.locationSettingsStates | |
| Log.d("salam", task.result!!.toString()) | |
| Log.e( | |
| "LOG", "LocationSettings: \n" + | |
| " GPS present: ${state.isGpsPresent} \n" + | |
| " GPS usable: ${state.isGpsUsable} \n" + | |
| " Location present: " + | |
| "${state.isLocationPresent} \n" + | |
| " Location usable: " + | |
| "${state.isLocationUsable} \n" + | |
| " Network Location present: " + | |
| "${state.isNetworkLocationPresent} \n" + | |
| " Network Location usable: " + | |
| "${state.isNetworkLocationUsable} \n" | |
| ) | |
| } catch (e: RuntimeExecutionException) { | |
| Log.d("salam", "hei") | |
| if (e.cause is ResolvableApiException) | |
| (e.cause as ResolvableApiException).startResolutionForResult( | |
| this, | |
| REQUEST_CHECK_STATE | |
| ) | |
| } | |
| } | |
| val locationUpdates = object : LocationCallback() { | |
| override fun onLocationResult(lr: LocationResult) { | |
| Log.e("salam", lr.toString()) | |
| Log.e("salam", "Newest Location: " + lr.locations.last()) | |
| // do something with the new location... | |
| Toast.makeText(this@MainActivity, "Lat: ${lr.locations.last().latitude}", Toast.LENGTH_SHORT) | |
| .show() | |
| } | |
| } | |
| fusedLocationProviderClient.requestLocationUpdates( | |
| reqSetting, | |
| locationUpdates, | |
| null /* Looper */ | |
| ) | |
| fusedLocationProviderClient.removeLocationUpdates(locationUpdates) | |
| } | |
| } | |
| } | |
| } | |
| private val reqSetting = LocationRequest.create().apply { | |
| fastestInterval = 10000 | |
| interval = 10000 | |
| priority = LocationRequest.PRIORITY_HIGH_ACCURACY | |
| smallestDisplacement = 1.0f | |
| } | |
| private fun getCurrentVolume(): Int { | |
| val audioManager: AudioManager = getSystemService(AUDIO_SERVICE) as AudioManager | |
| return audioManager.getStreamVolume(9); | |
| } | |
| private fun updateCurrentVolume() { | |
| CurrentVolume.setText("Current volume: ${getCurrentVolume()}") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment