Skip to content

Instantly share code, notes, and snippets.

View molidev8's full-sized avatar

Miguel Olivera molidev8

View GitHub Profile
@molidev8
molidev8 / splash_theme.xml
Last active May 30, 2022 14:39
A sample of the XML configuration to implement an animated splash screen with the core-splashscreen API on Android.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.AppSplash" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/white</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/icon_animated</item>
<item name="windowSplashScreenAnimationDuration">1000</item>
<item name="postSplashScreenTheme">@style/Theme.SplashAPISample</item>
<item name="android:windowSplashScreenBrandingImage">@drawable/branding</item>
<!-- Status bar and Nav bar configs -->
@molidev8
molidev8 / AndroidManifest.xml
Created May 30, 2022 14:35
An AndroidManifest with the core-splashscreen configured
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.molidev.splashapisample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
@molidev8
molidev8 / build.gradle
Created June 4, 2022 14:22
Gradle dependencies for Glide and OkHttp3
depedencies {
implementation 'com.github.bumptech.glide:glide:4.13.2'
kapt 'com.github.bumptech.glide:compiler:4.13.2'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation ("com.github.bumptech.glide:okhttp3-integration:4.0.0") {
exclude group: 'glide-parent'
}
}
@molidev8
molidev8 / MyGlideModule.kt
Created June 4, 2022 14:25
A GlideModule to use OkHttp for network requests
private const val SIZE_IN_BYTES: Long = 1024 * 1024 * 10
@GlideModule
class MyGlideModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
val client = OkHttpClient.Builder()
.cache(Cache(context.cacheDir, SIZE_IN_BYTES))
.build()
val factory = OkHttpUrlLoader.Factory(client)
@molidev8
molidev8 / someGlideUsage.kt
Created June 4, 2022 14:30
How to skip Glilde caching
Glide
.with(context)
.load("someURL")
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(someView)
@molidev8
molidev8 / advertiser.kt
Created June 9, 2022 14:05
A function to act as a publisher in Nearby Share
fun startAdvertising() {
val advertisingOptions =
AdvertisingOptions.Builder().setStrategy(Strategy.P2P_POINT_TO_POINT).build()
client.startAdvertising(
android.os.Build.MODEL, SERVICE_ID, ConnectingProcessCallback(), advertisingOptions
)
.addOnSuccessListener {
Log.d(GENERAL, "advertising...")
}
.addOnFailureListener {
@molidev8
molidev8 / discoverer.kt
Created June 9, 2022 14:07
A function to act as a subscriber in Nearby Share
fun startDiscovering() {
val discoveryOptions =
DiscoveryOptions.Builder().setStrategy(Strategy.P2P_POINT_TO_POINT).build()
client
.startDiscovery(SERVICE_ID, object : EndpointDiscoveryCallback() {
override fun onEndpointFound(endPointId: String, info: DiscoveredEndpointInfo) {
client.requestConnection(android.os.Build.MODEL, endPointId, ConnectingProcessCallback())
.addOnSuccessListener {
}
@molidev8
molidev8 / pairing.kt
Created June 9, 2022 14:14
A callback to pair two devices with Nearby Share
private inner class ConnectingProcessCallback : ConnectionLifecycleCallback() {
override fun onConnectionInitiated(endPointId: String, info: ConnectionInfo) {
MaterialAlertDialogBuilder(context)
.setTitle(Strings.get(R.string.accept_connection, info.endpointName))
.setMessage(Strings.get(R.string.confirm_code, info.authenticationDigits))
.setPositiveButton(Strings.get(R.string.accept)) { _: DialogInterface, _: Int ->
Nearby.getConnectionsClient(context)
.acceptConnection(endPointId, DataReceivedCallback())
}
.setNegativeButton(Strings.get(R.string.cancel)) { _: DialogInterface, _: Int ->
@molidev8
molidev8 / receiver.kt
Created June 9, 2022 14:18
A callback to receive information with Android Nearby
private inner class DataReceivedCallback : PayloadCallback() {
private val incomingFilePayloads = SimpleArrayMap<Long, Payload>()
private val completedFilePayloads = SimpleArrayMap<Long, Payload>()
private var filePayloadFilename: String = ""
override fun onPayloadReceived(endPointId: String, payload: Payload) {
when (payload.type) {
Payload.Type.BYTES -> {
val gson = Gson()
val recipe =
@molidev8
molidev8 / timerService.kt
Created June 11, 2022 16:49
A barebones foreground service
class TimerService : Service() {
private val binder: Binder = TimerBinder()
override fun onBind(p0: Intent?): IBinder = binder
inner class TimerBinder : Binder() {
val service: TimerService
get() = this@TimerService
}