Skip to content

Commit

Permalink
Add download wallpaper screen and edit set wallpaper screen
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzsout committed May 2, 2023
1 parent fa89c39 commit bbb1860
Show file tree
Hide file tree
Showing 28 changed files with 413 additions and 82 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

<application
android:name=".core.WalliesApp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.oguzdogdu.wallies.databinding.FragmentCollectionsBinding
import com.oguzdogdu.wallies.presentation.main.MainActivity
import com.oguzdogdu.wallies.util.CheckConnection
import com.oguzdogdu.wallies.util.observe
import com.oguzdogdu.wallies.util.setUp
import com.oguzdogdu.wallies.util.setupRecyclerView
import com.oguzdogdu.wallies.util.showToast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
Expand All @@ -32,7 +32,7 @@ class CollectionsFragment :
override fun initViews() {
super.initViews()
binding.apply {
recyclerViewCollections.setUp(
recyclerViewCollections.setupRecyclerView(
layoutManager = GridLayoutManager(requireContext(), 2),
adapter = collectionListAdapter,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.navigation.fragment.navArgs
import coil.load
import coil.request.CachePolicy
import coil.transform.CircleCropTransformation
import com.oguzdogdu.data.model.maindto.Urls
import com.oguzdogdu.domain.model.favorites.FavoriteImages
import com.oguzdogdu.domain.model.singlephoto.Photo
import com.oguzdogdu.wallies.R
Expand Down Expand Up @@ -92,6 +93,13 @@ class DetailFragment : BaseFragment<FragmentDetailBinding>(FragmentDetailBinding
showProfileInfos(it.detail)
navigateToSetWallpaper(it.detail?.urls)
sharePhoto(it.detail)
navigateToDownloadWallpaper(
raw = it.detail?.rawQuality,
high = it.detail?.highQuality,
medium = it.detail?.mediumQuality,
low = it.detail?.lowQuality,
imageTitle = it.detail?.desc
)
}
}

Expand Down Expand Up @@ -126,6 +134,19 @@ class DetailFragment : BaseFragment<FragmentDetailBinding>(FragmentDetailBinding
}
}

private fun navigateToDownloadWallpaper(raw:String?,high:String?,medium:String?,low:String?,imageTitle:String?){
binding.buttonDownload.setOnClickListener {
val arguments = Bundle().apply {
putString("raw", raw)
putString("high", high)
putString("medium", medium)
putString("low", low)
putString("imageTitle", imageTitle)
}
navigate(R.id.toDownload, arguments)
}
}

private fun setItems(photo: Photo?) {
with(binding) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.oguzdogdu.wallies.presentation.downloadphoto

import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.navigation.fragment.navArgs
import com.oguzdogdu.wallies.R
import com.oguzdogdu.wallies.core.BaseBottomSheetDialogFragment
import com.oguzdogdu.wallies.databinding.FragmentDownloadPhotoBinding
import com.oguzdogdu.wallies.presentation.setwallpaper.SetWallpaperFragmentArgs
import com.oguzdogdu.wallies.util.downloadImage
import com.oguzdogdu.wallies.util.showToast
import java.io.File

class DownloadPhotoFragment : BaseBottomSheetDialogFragment<FragmentDownloadPhotoBinding>(FragmentDownloadPhotoBinding::inflate) {

private val args: DownloadPhotoFragmentArgs by navArgs()

override fun initListeners() {
super.initListeners()
binding.apply {
buttonRawQuality.setOnClickListener {
downloadImageFromWeb(args.raw)
}
buttonFullQuality.setOnClickListener {
downloadImageFromWeb(args.high)
}
buttonMediumQuality.setOnClickListener {
downloadImageFromWeb(args.medium)
}
buttonLowQuality.setOnClickListener {
downloadImageFromWeb(args.low)
}
}
}


private fun downloadImageFromWeb(url: String?) {
val directory : String = requireContext().getString(R.string.app_name)
val fileName = "${args.imageTitle}.jpg"
if (url != null) {
requireContext().downloadImage(url, directory, fileName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.oguzdogdu.wallies.presentation.favorites

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.oguzdogdu.wallies.R
Expand All @@ -11,12 +10,9 @@ import com.oguzdogdu.wallies.databinding.FragmentFavoritesBinding
import com.oguzdogdu.wallies.presentation.main.MainActivity
import com.oguzdogdu.wallies.util.hide
import com.oguzdogdu.wallies.util.observe
import com.oguzdogdu.wallies.util.observeInLifecycle
import com.oguzdogdu.wallies.util.setUp
import com.oguzdogdu.wallies.util.setupRecyclerView
import com.oguzdogdu.wallies.util.show
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

@AndroidEntryPoint
class FavoritesFragment :
Expand All @@ -29,7 +25,7 @@ class FavoritesFragment :
override fun initViews() {
super.initViews()
binding.apply {
recyclerViewFavorites.setUp(
recyclerViewFavorites.setupRecyclerView(
layoutManager = GridLayoutManager(requireContext(), 2),
adapter = favoritesListAdapter,
true, onScroll = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.oguzdogdu.wallies.presentation.main.MainActivity
import com.oguzdogdu.wallies.util.CheckConnection
import com.oguzdogdu.wallies.util.hide
import com.oguzdogdu.wallies.util.observe
import com.oguzdogdu.wallies.util.setUp
import com.oguzdogdu.wallies.util.setupRecyclerView
import com.oguzdogdu.wallies.util.show
import com.oguzdogdu.wallies.util.showToast
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -33,7 +33,7 @@ class LatestFragment : BaseFragment<FragmentLatestBinding>(FragmentLatestBinding
override fun initViews() {
super.initViews()
binding.apply {
recyclerViewWallpapers.setUp(
recyclerViewWallpapers.setupRecyclerView(
layoutManager = GridLayoutManager(requireContext(), 2),
adapter = latestWallpaperAdapter,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.oguzdogdu.wallies.presentation.main.MainActivity
import com.oguzdogdu.wallies.util.CheckConnection
import com.oguzdogdu.wallies.util.hide
import com.oguzdogdu.wallies.util.observe
import com.oguzdogdu.wallies.util.setUp
import com.oguzdogdu.wallies.util.setupRecyclerView
import com.oguzdogdu.wallies.util.show
import com.oguzdogdu.wallies.util.showToast
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -33,7 +33,7 @@ class PopularFragment : BaseFragment<FragmentPopularBinding>(FragmentPopularBind
override fun initViews() {
super.initViews()
binding.apply {
recyclerViewWallpapers.setUp(
recyclerViewWallpapers.setupRecyclerView(
layoutManager = GridLayoutManager(requireContext(), 2),
adapter = popularWallpaperAdapter,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.oguzdogdu.wallies.databinding.FragmentSearchBinding
import com.oguzdogdu.wallies.util.CheckConnection
import com.oguzdogdu.wallies.util.observe
import com.oguzdogdu.wallies.util.observeInLifecycle
import com.oguzdogdu.wallies.util.setUp
import com.oguzdogdu.wallies.util.setupRecyclerView
import com.oguzdogdu.wallies.util.showToast
import com.oguzdogdu.wallies.util.textChanges
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -37,7 +37,7 @@ class SearchFragment : BaseFragment<FragmentSearchBinding>(FragmentSearchBinding
override fun initViews() {
super.initViews()
binding.apply {
recyclerViewSearch.setUp(
recyclerViewSearch.setupRecyclerView(
layoutManager = GridLayoutManager(requireContext(), 2),
adapter = searchWallpaperAdapter,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package com.oguzdogdu.wallies.presentation.setwallpaper

import android.app.WallpaperManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Build
import android.util.Base64
import androidx.annotation.RequiresApi
import androidx.core.net.toUri
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.navArgs
import coil.Coil
import coil.executeBlocking
Expand All @@ -18,7 +23,13 @@ import com.oguzdogdu.wallies.R
import com.oguzdogdu.wallies.core.BaseBottomSheetDialogFragment
import com.oguzdogdu.wallies.databinding.FragmentSetWallpaperBinding
import com.oguzdogdu.wallies.util.showToast
import com.oguzdogdu.wallies.util.toBitmap
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.ByteArrayOutputStream
import java.net.HttpURLConnection
import java.net.URL


@AndroidEntryPoint
Expand All @@ -33,6 +44,7 @@ class SetWallpaperFragment :
setWallpaperOptions(args.imageUrl)
}


@RequiresApi(Build.VERSION_CODES.N)
private fun setWallpaperOptions(url: String?) {
binding.apply {
Expand All @@ -56,60 +68,39 @@ class SetWallpaperFragment :

@RequiresApi(Build.VERSION_CODES.N)
fun setWallpaperFromUrl(imageUrl: String?, isLockScreen: Boolean?) {
Glide.with(requireContext())
.asBitmap().load(imageUrl)
.listener(object : RequestListener<Bitmap> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: com.bumptech.glide.request.target.Target<Bitmap>?,
isFirstResource: Boolean
): Boolean {
return false
}

@RequiresApi(Build.VERSION_CODES.N)
override fun onResourceReady(
resource: Bitmap?,
model: Any?,
target: com.bumptech.glide.request.target.Target<Bitmap>?,
dataSource: DataSource?,
isFirstResource: Boolean,
): Boolean {
lifecycleScope.launch(Dispatchers.IO) {
val bitmap = imageUrl?.toBitmap()
val wallpaperManager = WallpaperManager.getInstance(requireContext())
/* TODO This field adjusts an image with a high size again according to the size of the phone.
val displayMetrics = DisplayMetrics()
val windowManager = context?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels
val height = displayMetrics.heightPixels
val bitmap = Bitmap.createScaledBitmap(resource!!, width, height, true)*/
try {

val wallpaperManager = WallpaperManager.getInstance(requireContext())
/* TODO This field adjusts an image with a high size again according to the size of the phone.
val displayMetrics = DisplayMetrics()
val windowManager = context?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.defaultDisplay.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels
val height = displayMetrics.heightPixels
val bitmap = Bitmap.createScaledBitmap(resource!!, width, height, true)*/
try {
resource.let {
when (isLockScreen) {
false -> wallpaperManager.setBitmap(
it,
null,
true,
WallpaperManager.FLAG_LOCK
)
when (isLockScreen) {
false -> wallpaperManager.setBitmap(
bitmap,
null,
true,
WallpaperManager.FLAG_LOCK
)

null -> wallpaperManager.setBitmap(it)
true -> wallpaperManager.setBitmap(
it,
null,
true,
WallpaperManager.FLAG_SYSTEM
)
}
}
null -> wallpaperManager.setBitmap(bitmap)
true -> wallpaperManager.setBitmap(
bitmap,
null,
true,
WallpaperManager.FLAG_SYSTEM
)

} catch (e: Exception) {
e.printStackTrace()
}
return false
}
}).submit()

} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/oguzdogdu/wallies/util/StringExtension.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.oguzdogdu.wallies.util

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.net.HttpURLConnection
import java.net.URL
import java.text.SimpleDateFormat
import java.util.*

Expand All @@ -27,6 +31,15 @@ fun String.formatDate(
}
}

fun String.toBitmap(): Bitmap {
val imageUrl = URL(this)
val connection = imageUrl.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val input = connection.inputStream
return BitmapFactory.decodeStream(input)
}

enum class DateFormats(val value: String) {
INPUT_DATE_FORMAT("yyyy-MM-dd'T'HH:mm:ss"),
OUTPUT_BASIC_DATE_FORMAT("MMMM yyyy"),
Expand Down
Loading

0 comments on commit bbb1860

Please sign in to comment.