Skip to content

Commit

Permalink
Glimpse: Sync up with Twelve
Browse files Browse the repository at this point in the history
Change-Id: Ia3be23fa20da0a43da1e7211be3e15aea6660467
  • Loading branch information
SebaUbuntu committed Dec 30, 2024
1 parent cc93a0c commit e88669a
Show file tree
Hide file tree
Showing 101 changed files with 3,786 additions and 3,336 deletions.
5 changes: 5 additions & 0 deletions app/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ android_app {
"preinstalled-packages-org.lineageos.glimpse",
],

kotlin_plugins: [
"kotlin-parcelize-compiler-plugin",
],

static_libs: [
// DO NOT EDIT THIS SECTION MANUALLY
"androidx.activity_activity",
Expand Down Expand Up @@ -66,6 +70,7 @@ android_app {
"Glimpse_io.coil-kt.coil3_coil-video",
"Glimpse_io.github.panpf.zoomimage_zoomimage-core-android",
"Glimpse_io.github.panpf.zoomimage_zoomimage-view-coil",
"kotlin-parcelize-runtime",
"kotlin-stdlib",
],

Expand Down
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.lineageos.generatebp.models.Module
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.parcelize)
}

apply {
Expand All @@ -18,7 +19,7 @@ apply {

buildscript {
repositories {
maven("https://raw.githubusercontent.com/lineage-next/gradle-generatebp/v1.15/.m2")
maven("https://raw.githubusercontent.com/lineage-next/gradle-generatebp/v1.16/.m2")
}

dependencies {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/org/lineageos/glimpse/GlimpseApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import coil3.gif.AnimatedImageDecoder
import coil3.memory.MemoryCache
import coil3.video.VideoFrameDecoder
import com.google.android.material.color.DynamicColors
import kotlinx.coroutines.MainScope
import org.lineageos.glimpse.repository.MediaRepository
import org.lineageos.glimpse.ui.coil.ThumbnailMapper

class GlimpseApplication : Application(), SingletonImageLoader.Factory {
val mediaRepository by lazy { MediaRepository(applicationContext, MainScope()) }

override fun onCreate() {
super.onCreate()

Expand All @@ -26,6 +31,7 @@ class GlimpseApplication : Application(), SingletonImageLoader.Factory {
.components {
add(AnimatedImageDecoder.Factory())
add(VideoFrameDecoder.Factory())
add(ThumbnailMapper)
}
.memoryCache {
MemoryCache.Builder().maxSizePercent(context, 0.25).build()
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/lineageos/glimpse/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
* SPDX-FileCopyrightText: 2023 The LineageOS Project
* SPDX-FileCopyrightText: 2023-2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

package org.lineageos.glimpse

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat

class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Setup edge-to-edge
WindowCompat.setDecorFitsSystemWindows(window, false)
// Enable edge-to-edge
enableEdgeToEdge()
}
}
119 changes: 68 additions & 51 deletions app/src/main/java/org/lineageos/glimpse/PickerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,75 @@ package org.lineageos.glimpse
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.util.Consumer
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import com.google.android.material.appbar.AppBarLayout
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.shape.MaterialShapeDrawable
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.lineageos.glimpse.ext.updateMargin
import org.lineageos.glimpse.models.MediaType
import org.lineageos.glimpse.utils.PickerUtils
import org.lineageos.glimpse.utils.MimeUtils
import org.lineageos.glimpse.viewmodels.IntentsViewModel

class PickerActivity : AppCompatActivity(R.layout.activity_picker) {
// View models
private val intentsViewModel by viewModels<IntentsViewModel>()

// Views
private val appBarLayout by lazy { findViewById<AppBarLayout>(R.id.appBarLayout)!! }
private val contentView by lazy { findViewById<View>(android.R.id.content)!! }
private val toolbar by lazy { findViewById<MaterialToolbar>(R.id.toolbar)!! }

// Intents
private val intentListener = Consumer<Intent> { intentsViewModel.onIntent(it) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Setup edge-to-edge
WindowCompat.setDecorFitsSystemWindows(window, false)

appBarLayout.statusBarForeground = MaterialShapeDrawable.createWithElevationOverlay(this)
// Enable edge-to-edge
enableEdgeToEdge()

setSupportActionBar(toolbar)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}

ViewCompat.setOnApplyWindowInsetsListener(contentView) { _, windowInsets ->
// Insets
ViewCompat.setOnApplyWindowInsetsListener(toolbar) { _, windowInsets ->
val insets = windowInsets.getInsets(
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()
)

toolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> {
leftMargin = insets.left
rightMargin = insets.right
}
toolbar.updateMargin(
insets,
start = true,
end = true,
)

windowInsets
}

// Parse intent
if (intent.action !in supportedIntentActions) {
Toast.makeText(
this, R.string.intent_action_not_supported, Toast.LENGTH_SHORT
).show()
finish()
return
setSupportActionBar(toolbar)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setDisplayShowHomeEnabled(true)
}

val mimeType = PickerUtils.translateMimeType(intent) ?: run {
Toast.makeText(
this, R.string.intent_media_type_not_supported, Toast.LENGTH_SHORT
).show()
finish()
return
intentListener.accept(intent)
addOnNewIntentListener(intentListener)

lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
loadData()
}
}
}

val mediaType = MediaType.fromMimeType(mimeType)
override fun onDestroy() {
removeOnNewIntentListener(intentListener)

toolbar.setTitle(
when (mediaType) {
MediaType.IMAGE -> R.string.pick_a_photo
MediaType.VIDEO -> R.string.pick_a_video
else -> R.string.pick_a_media
}
)
super.onDestroy()
}

override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
Expand All @@ -94,11 +90,32 @@ class PickerActivity : AppCompatActivity(R.layout.activity_picker) {
}
}

companion object {
private val supportedIntentActions = listOf(
Intent.ACTION_GET_CONTENT,
Intent.ACTION_PICK,
Intent.ACTION_SET_WALLPAPER,
)
private suspend fun loadData() {
intentsViewModel.parsedIntent.collectLatest {
it?.handle { parsedIntent ->
when (parsedIntent) {
is IntentsViewModel.ParsedIntent.PickIntent -> {
val mediaType = parsedIntent.mimeType?.let { mimeType ->
MimeUtils.mimeTypeToMediaType(mimeType)
}

toolbar.setTitle(
when (mediaType) {
MediaType.IMAGE -> R.string.pick_a_photo
MediaType.VIDEO -> R.string.pick_a_video
else -> R.string.pick_a_media
}
)
}

else -> {
Toast.makeText(
this, R.string.intent_action_not_supported, Toast.LENGTH_SHORT
).show()
finish()
}
}
}
}
}
}
54 changes: 35 additions & 19 deletions app/src/main/java/org/lineageos/glimpse/SetWallpaperActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,63 @@
package org.lineageos.glimpse

import android.app.WallpaperManager
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.util.Consumer
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import coil3.load
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.lineageos.glimpse.ext.updateMargin

class SetWallpaperActivity : AppCompatActivity(R.layout.activity_set_wallpaper) {
// Views
private val contentView by lazy { findViewById<View>(android.R.id.content)!! }
private val wallpaperImageView by lazy { findViewById<ImageView>(R.id.wallpaperImageView)!! }
private val setWallpaperButton by lazy { findViewById<MaterialButton>(R.id.setWallpaperButton)!! }
private val setWallpaperButton by lazy { findViewById<MaterialButton>(R.id.setWallpaperButton) }
private val wallpaperImageView by lazy { findViewById<ImageView>(R.id.wallpaperImageView) }

// System services
private val wallpaperManager by lazy { getSystemService(WallpaperManager::class.java) }

// Intents
private val intentListener = Consumer<Intent> { handleIntent(it) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Setup edge-to-edge
WindowCompat.setDecorFitsSystemWindows(window, false)
// Enable edge-to-edge
enableEdgeToEdge()

ViewCompat.setOnApplyWindowInsetsListener(contentView) { _, windowInsets ->
// Insets
ViewCompat.setOnApplyWindowInsetsListener(setWallpaperButton) { _, windowInsets ->
val insets = windowInsets.getInsets(
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()
)

setWallpaperButton.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = insets.bottom
}
setWallpaperButton.updateMargin(
insets,
bottom = true,
)

windowInsets
}

intentListener.accept(intent)
addOnNewIntentListener(intentListener)
}

override fun onDestroy() {
removeOnNewIntentListener(intentListener)

super.onDestroy()
}

private fun handleIntent(intent: Intent) {
// Load wallpaper from intent
val wallpaperUri = intent.data ?: run {
Toast.makeText(this, R.string.intent_media_not_found, Toast.LENGTH_LONG).show()
Expand All @@ -72,14 +88,14 @@ class SetWallpaperActivity : AppCompatActivity(R.layout.activity_set_wallpaper)
return
}

wallpaperImageView.setImageURI(wallpaperUri)
wallpaperImageView.load(wallpaperUri)

// Set wallpaper
setWallpaperButton.setOnClickListener {
MaterialAlertDialogBuilder(this, R.style.Theme_Glimpse_SetWallpaperDialog)
.setTitle(R.string.set_wallpaper_dialog_title)
.setItems(R.array.set_wallpaper_items) { _, which ->
val flags = POSITION_TO_FLAG[which] ?: throw Exception("Invalid position")
val flags = positionToFlag[which]
setWallpaper(wallpaperUri, flags)
finish()
}.show()
Expand All @@ -93,10 +109,10 @@ class SetWallpaperActivity : AppCompatActivity(R.layout.activity_set_wallpaper)
}

companion object {
private val POSITION_TO_FLAG = mapOf(
0 to WallpaperManager.FLAG_SYSTEM,
1 to WallpaperManager.FLAG_LOCK,
2 to (WallpaperManager.FLAG_SYSTEM or WallpaperManager.FLAG_LOCK)
private val positionToFlag = arrayOf(
WallpaperManager.FLAG_SYSTEM,
WallpaperManager.FLAG_LOCK,
WallpaperManager.FLAG_SYSTEM or WallpaperManager.FLAG_LOCK,
)
}
}
Loading

0 comments on commit e88669a

Please sign in to comment.