1 /*
2  * 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.intentresolver.contentpreview
18 
19 import android.content.Context
20 import android.util.PluralsMessageFormatter
21 import androidx.annotation.StringRes
22 import com.android.intentresolver.R
23 import dagger.Binds
24 import dagger.Module
25 import dagger.hilt.InstallIn
26 import dagger.hilt.android.qualifiers.ApplicationContext
27 import dagger.hilt.components.SingletonComponent
28 import javax.inject.Inject
29 
30 private const val PLURALS_COUNT = "count"
31 
32 /**
33  * HeadlineGenerator generates the text to show at the top of the sharesheet as a brief description
34  * of the content being shared.
35  */
36 class HeadlineGeneratorImpl
37 @Inject
38 constructor(
39     @ApplicationContext private val context: Context,
40 ) : HeadlineGenerator {
getTextHeadlinenull41     override fun getTextHeadline(text: CharSequence): String {
42         return context.getString(
43             getTemplateResource(text, R.string.sharing_link, R.string.sharing_text)
44         )
45     }
46 
getAlbumHeadlinenull47     override fun getAlbumHeadline(): String {
48         return context.getString(R.string.sharing_album)
49     }
50 
getImagesWithTextHeadlinenull51     override fun getImagesWithTextHeadline(text: CharSequence, count: Int): String {
52         return getPluralString(
53             getTemplateResource(
54                 text,
55                 R.string.sharing_images_with_link,
56                 R.string.sharing_images_with_text
57             ),
58             count
59         )
60     }
61 
getVideosWithTextHeadlinenull62     override fun getVideosWithTextHeadline(text: CharSequence, count: Int): String {
63         return getPluralString(
64             getTemplateResource(
65                 text,
66                 R.string.sharing_videos_with_link,
67                 R.string.sharing_videos_with_text
68             ),
69             count
70         )
71     }
72 
getFilesWithTextHeadlinenull73     override fun getFilesWithTextHeadline(text: CharSequence, count: Int): String {
74         return getPluralString(
75             getTemplateResource(
76                 text,
77                 R.string.sharing_files_with_link,
78                 R.string.sharing_files_with_text
79             ),
80             count
81         )
82     }
83 
getImagesHeadlinenull84     override fun getImagesHeadline(count: Int): String {
85         return getPluralString(R.string.sharing_images, count)
86     }
87 
getVideosHeadlinenull88     override fun getVideosHeadline(count: Int): String {
89         return getPluralString(R.string.sharing_videos, count)
90     }
91 
getFilesHeadlinenull92     override fun getFilesHeadline(count: Int): String {
93         return getPluralString(R.string.sharing_files, count)
94     }
95 
getPluralStringnull96     private fun getPluralString(@StringRes templateResource: Int, count: Int): String {
97         return PluralsMessageFormatter.format(
98             context.resources,
99             mapOf(PLURALS_COUNT to count),
100             templateResource
101         )
102     }
103 
104     @StringRes
getTemplateResourcenull105     private fun getTemplateResource(
106         text: CharSequence,
107         @StringRes linkResource: Int,
108         @StringRes nonLinkResource: Int
109     ): Int {
110         return if (text.toString().isHttpUri()) linkResource else nonLinkResource
111     }
112 }
113 
114 @Module
115 @InstallIn(SingletonComponent::class)
116 interface HeadlineGeneratorModule {
bindnull117     @Binds fun bind(impl: HeadlineGeneratorImpl): HeadlineGenerator
118 }
119