Skip to content

Commit

Permalink
Merge pull request Almoullim#13 from 0xecute/master
Browse files Browse the repository at this point in the history
Remove notification and fix context
  • Loading branch information
Almoullim committed Nov 5, 2019
2 parents 1417f4f + a4cbf78 commit 21a57d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
class BackgroundLocationPlugin() : MethodCallHandler, PluginRegistry.RequestPermissionsResultListener {


private lateinit var activity: Activity
private lateinit var registrar: Registrar
private lateinit var channel: MethodChannel
private var myReceiver: MyReceiver? = null
private var mService: LocationUpdatesService? = null
Expand All @@ -34,27 +34,27 @@ class BackgroundLocationPlugin() : MethodCallHandler, PluginRegistry.RequestPerm
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "almoullim.com/background_location")
channel.setMethodCallHandler(BackgroundLocationPlugin(registrar.activity(), channel))
channel.setMethodCallHandler(BackgroundLocationPlugin(registrar, channel))
}

private const val TAG = "com.almoullim.Log.Tag"
private const val REQUEST_PERMISSIONS_REQUEST_CODE = 34
}


constructor(activity: Activity, channel: MethodChannel) : this() {
this.activity = activity
constructor(registrar: Registrar, channel: MethodChannel) : this() {
this.registrar = registrar
this.channel = channel


myReceiver = MyReceiver()

if (Utils.requestingLocationUpdates(activity)) {
if (Utils.requestingLocationUpdates(registrar.activeContext())) {
if (!checkPermissions()) {
requestPermissions()
}
}
LocalBroadcastManager.getInstance(activity).registerReceiver(myReceiver!!,
LocalBroadcastManager.getInstance(registrar.activeContext()).registerReceiver(myReceiver!!,
IntentFilter(LocationUpdatesService.ACTION_BROADCAST))
}

Expand All @@ -68,19 +68,19 @@ class BackgroundLocationPlugin() : MethodCallHandler, PluginRegistry.RequestPerm

mService?.removeLocationUpdates()

LocalBroadcastManager.getInstance(activity).unregisterReceiver(myReceiver!!)
LocalBroadcastManager.getInstance(registrar.activeContext()).unregisterReceiver(myReceiver!!)

if (mBound) {
activity.unbindService(mServiceConnection)
registrar.activeContext().unbindService(mServiceConnection)
mBound = false
}

}
call.method == "start_location_service" -> {
LocalBroadcastManager.getInstance(activity).registerReceiver(myReceiver!!,
LocalBroadcastManager.getInstance(registrar.activeContext()).registerReceiver(myReceiver!!,
IntentFilter(LocationUpdatesService.ACTION_BROADCAST))
if (!mBound) {
activity.bindService(Intent(activity, LocationUpdatesService::class.java), mServiceConnection, Context.BIND_AUTO_CREATE)
registrar.activeContext().bindService(Intent(registrar.activeContext(), LocationUpdatesService::class.java), mServiceConnection, Context.BIND_AUTO_CREATE)
}
/*
if (mService != null) {
Expand Down Expand Up @@ -127,7 +127,7 @@ class BackgroundLocationPlugin() : MethodCallHandler, PluginRegistry.RequestPerm
when {
grantResults!!.isEmpty() -> Log.i(TAG, "User interaction was cancelled.")
grantResults[0] == PackageManager.PERMISSION_GRANTED -> mService!!.requestLocationUpdates()
else -> Toast.makeText(activity, R.string.permission_denied_explanation, Toast.LENGTH_LONG).show()
else -> Toast.makeText(registrar.activeContext(), R.string.permission_denied_explanation, Toast.LENGTH_LONG).show()
}
}
return true
Expand All @@ -151,21 +151,21 @@ class BackgroundLocationPlugin() : MethodCallHandler, PluginRegistry.RequestPerm
}

private fun checkPermissions(): Boolean {
return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(activity,
return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(registrar.activeContext(),
Manifest.permission.ACCESS_FINE_LOCATION)
}

private fun requestPermissions() {
val shouldProvideRationale = ActivityCompat.shouldShowRequestPermissionRationale(activity,
val shouldProvideRationale = ActivityCompat.shouldShowRequestPermissionRationale(registrar.activity(),
Manifest.permission.ACCESS_FINE_LOCATION)
if (shouldProvideRationale) {

Log.i(TAG, "Displaying permission rationale to provide additional context.")
Toast.makeText(activity, R.string.permission_rationale, Toast.LENGTH_LONG).show()
Toast.makeText(registrar.activeContext(), R.string.permission_rationale, Toast.LENGTH_LONG).show()

} else {
Log.i(TAG, "Requesting permission")
ActivityCompat.requestPermissions(activity,
ActivityCompat.requestPermissions(registrar.activity(),
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_PERMISSIONS_REQUEST_CODE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class LocationUpdatesService : Service() {
mNotificationManager!!.createNotificationChannel(mChannel)
}

startForeground(NOTIFICATION_ID, notification)
//startForeground(NOTIFICATION_ID, notification)


broadcastReceiver = object : BroadcastReceiver() {
Expand Down Expand Up @@ -138,7 +138,7 @@ class LocationUpdatesService : Service() {
Utils.setRequestingLocationUpdates(this, false)
mNotificationManager!!.cancel(NOTIFICATION_ID)
stopSelf()
stopForeground(true)
//stopForeground(true)
} catch (unlikely: SecurityException) {
Utils.setRequestingLocationUpdates(this, true)
}
Expand Down

0 comments on commit 21a57d9

Please sign in to comment.