-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b64a88
commit c4881e7
Showing
16 changed files
with
525 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package guillermo.lagos.svtl | ||
|
||
import android.app.Activity | ||
import android.app.Dialog | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.SharedPreferences | ||
import android.database.sqlite.SQLiteCantOpenDatabaseException | ||
import android.database.sqlite.SQLiteDatabase | ||
import android.os.Build | ||
import android.view.Window | ||
import guillermo.lagos.svtl.servicio.* | ||
import timber.log.Timber | ||
import java.io.BufferedInputStream | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipInputStream | ||
|
||
fun Activity.actionOnService(action: Actions) { | ||
if (getServiceState(this) == ServiceState.STOPPED && action == Actions.STOP) return | ||
Intent(this, EndlessService::class.java).also { | ||
it.action = action.name | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
Timber.e("Starting the service in >=26 Mode") | ||
startForegroundService(it) | ||
return | ||
} | ||
Timber.e("Starting the service in < 26 Mode") | ||
startService(it) | ||
} | ||
} | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package guillermo.lagos.svtl.servicio | ||
|
||
enum class Actions { | ||
START, | ||
STOP | ||
} |
164 changes: 164 additions & 0 deletions
164
app/src/main/java/guillermo/lagos/svtl/servicio/EndlessService.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package guillermo.lagos.svtl.servicio | ||
|
||
import android.app.* | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.graphics.Color | ||
import android.os.Build | ||
import android.os.IBinder | ||
import android.os.PowerManager | ||
import android.os.SystemClock | ||
import android.util.Log | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkInfo | ||
import androidx.work.WorkManager | ||
import androidx.work.workDataOf | ||
import guillermo.lagos.svtl.* | ||
import kotlinx.android.synthetic.main.activity_main.* | ||
import kotlinx.coroutines.* | ||
import timber.log.Timber | ||
|
||
|
||
class EndlessService : Service() { | ||
|
||
private var wakeLock: PowerManager.WakeLock? = null | ||
private var isServiceStarted = false | ||
|
||
override fun onBind(intent: Intent): IBinder? { | ||
Timber.e("Some component want to bind with the service") | ||
// We don't provide binding, so return null | ||
return null | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
Timber.e("onStartCommand executed with startId: $startId") | ||
if (intent != null) { | ||
val action = intent.action | ||
Timber.e("using an intent with action $action") | ||
when (action) { | ||
Actions.START.name -> startService() | ||
Actions.STOP.name -> stopService() | ||
else -> Timber.e("This should never happen. No action in the received intent") | ||
} | ||
} else { | ||
Timber.e( | ||
"with a null intent. It has been probably restarted by the system." | ||
) | ||
} | ||
// by returning this we make sure the service is restarted if the system kills the service | ||
return START_STICKY | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
Timber.e("The service has been created".toUpperCase()) | ||
val notification = createNotification() | ||
startForeground(1, notification) | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
Timber.e("The service has been destroyed".toUpperCase()) | ||
} | ||
|
||
override fun onTaskRemoved(rootIntent: Intent) { | ||
val restartServiceIntent = Intent(applicationContext, EndlessService::class.java).also { | ||
it.setPackage(packageName) | ||
}; | ||
val restartServicePendingIntent: PendingIntent = PendingIntent.getService(this, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); | ||
applicationContext.getSystemService(Context.ALARM_SERVICE); | ||
val alarmService: AlarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager; | ||
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent); | ||
} | ||
|
||
private fun startService() { | ||
if (isServiceStarted) return | ||
Timber.e("Starting the foreground service task") | ||
isServiceStarted = true | ||
setServiceState(this, ServiceState.STARTED) | ||
|
||
// we need this lock so our service gets not affected by Doze Mode | ||
wakeLock = | ||
(getSystemService(Context.POWER_SERVICE) as PowerManager).run { | ||
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "EndlessService::lock").apply { | ||
acquire() | ||
} | ||
} | ||
|
||
// we're starting a loop in a coroutine | ||
GlobalScope.launch(Dispatchers.IO) { | ||
while (isServiceStarted) { | ||
launch(Dispatchers.IO) { | ||
|
||
|
||
} | ||
delay(1 * 60 * 1000) | ||
} | ||
Timber.e("End of the loop for the service") | ||
} | ||
} | ||
|
||
private fun stopService() { | ||
Timber.e("Stopping the foreground service") | ||
try { | ||
wakeLock?.let { | ||
if (it.isHeld) { | ||
it.release() | ||
} | ||
} | ||
stopForeground(true) | ||
stopSelf() | ||
} catch (e: Exception) { | ||
Timber.e("Service stopped without being started: ${e.message}") | ||
} | ||
isServiceStarted = false | ||
setServiceState(this, ServiceState.STOPPED) | ||
} | ||
|
||
|
||
|
||
private fun createNotification(): Notification { | ||
val notificationChannelId = "ENDLESS SERVICE CHANNEL" | ||
|
||
// depending on the Android API that we're dealing with we will have | ||
// to use a specific method to create the notification | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
val channel = NotificationChannel( | ||
notificationChannelId, | ||
"Endless Service notifications channel", | ||
NotificationManager.IMPORTANCE_HIGH | ||
).let { | ||
it.description = "Endless Service channel" | ||
it.enableLights(true) | ||
it.lightColor = Color.RED | ||
it.enableVibration(true) | ||
it.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400) | ||
it | ||
} | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
|
||
val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent -> | ||
PendingIntent.getActivity(this, 0, notificationIntent, 0) | ||
} | ||
|
||
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder( | ||
this, | ||
notificationChannelId | ||
) else Notification.Builder(this) | ||
|
||
return builder | ||
.setContentTitle("Servidor en linea") | ||
/* .setContentText("This is your favorite endless service working")*/ | ||
.setContentIntent(pendingIntent) | ||
.setSmallIcon(R.mipmap.ic_launcher) | ||
/*.setTicker("Ticker text")*/ | ||
.setPriority(Notification.PRIORITY_HIGH) // for under android 26 compatibility | ||
.build() | ||
} | ||
|
||
|
||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/guillermo/lagos/svtl/servicio/ServiceState.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package guillermo.lagos.svtl.servicio | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import guillermo.lagos.svtl.getPreferences | ||
|
||
enum class ServiceState { | ||
STARTED, | ||
STOPPED, | ||
} | ||
|
||
private const val name = "SPYSERVICE_KEY" | ||
private const val key = "SPYSERVICE_STATE" | ||
|
||
fun setServiceState(context: Context, state: ServiceState) { | ||
val sharedPrefs = getPreferences(context) | ||
sharedPrefs.edit().let { | ||
it.putString(key, state.name) | ||
it.apply() | ||
} | ||
} | ||
|
||
fun getServiceState(context: Context): ServiceState { | ||
val sharedPrefs = getPreferences(context) | ||
val value = sharedPrefs.getString(key, ServiceState.STOPPED.name) | ||
return ServiceState.valueOf(value!!) | ||
} | ||
|
||
fun getPreferences(context: Context): SharedPreferences { | ||
return context.getSharedPreferences(name, 0) | ||
} | ||
|
Oops, something went wrong.