1 /*
<lambda>null2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.avatarpicker.domain
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.graphics.Bitmap
22 import android.graphics.BitmapFactory
23 import android.net.Uri
24 import android.provider.MediaStore
25 import androidx.core.content.FileProvider
26 import com.android.avatarpicker.R
27 import com.android.avatarpicker.data.entity.ResourceEntity
28 import com.android.avatarpicker.ui.details.items.ResourceViewModel
29 import com.android.avatarpicker.ui.details.items.UriTypedItem
30 import java.io.File
31 import java.io.IOException
32 
33 
34 private const val CROP_ACTION = "com.android.camera.action.CROP"
35 private const val FILE_PROVIDER = ".tempprovider"
36 private const val DEFAULT_ICON_TINT_COLOR = "default_icon_tint_color"
37 
38 // Item types definition
39 const val CAMERA = 1
40 const val PHOTO_PICKER = 2
41 const val DEFAULT_ICON = 3
42 const val ILLUSTRATION = 4
43 fun ResourceEntity.toViewModel(entityType: Int) =
44     ResourceViewModel(entityType, drawableId, descriptionId, colorInt)
45 
46 fun Context.getTempPNG(name: String): File {
47     val imagesDir = File(filesDir, getString(R.string.result_file_name))
48     if (!imagesDir.exists()) imagesDir.mkdir()
49     return File(imagesDir, "${name}.png")
50 }
51 
52 @Throws(IOException::class)
saveBitmapnull53 fun File.saveBitmap(bmp: Bitmap) = outputStream().use { stream ->
54     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream)
55     stream.flush()
56     stream.close()
57 }
58 
Contextnull59 fun Context.getFileUri(file: File): Uri {
60     val authority = applicationContext.packageName + FILE_PROVIDER
61     return FileProvider.getUriForFile(applicationContext, authority, file)
62 }
63 
<lambda>null64 fun getCropIntent(contentUri: Uri, sizeInPixels: Int) = Intent(CROP_ACTION).apply {
65     setDataAndType(contentUri, "image/*")
66     addFlags(
67         Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
68     )
69     putExtra(MediaStore.EXTRA_OUTPUT, contentUri)
70 
71     putExtra("crop", true)
72     putExtra("scale", true)
73     putExtra("scaleUpIfNeeded", true)
74     putExtra("aspectX", 1)
75     putExtra("aspectY", 1)
76     putExtra("outputX", sizeInPixels)
77     putExtra("outputY", sizeInPixels)
78 }
79 
toIntentnull80 fun ResourceViewModel.toIntent(context: Context): Intent {
81 
82     val intent = Intent()
83     when (typeId) {
84         DEFAULT_ICON -> this.color?.let { intent.putExtra(DEFAULT_ICON_TINT_COLOR, it) }
85         ILLUSTRATION -> context.apply {
86             BitmapFactory.decodeResource(resources, drawableId)?.let { bitmap ->
87                 val resultFile = getTempPNG(getString(R.string.result_file_name))
88                 val contentUri = getFileUri(resultFile)
89                 resultFile.saveBitmap(bitmap)
90                 intent.setData(contentUri)
91             }
92         }
93     }
94     return intent
95 }
96 
<lambda>null97 fun UriTypedItem.toIntent() = Intent().apply { data = uri }
98