• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 @Inject constructor(@ApplicationContext private val context: Context) :
37     HeadlineGenerator {
getTextHeadlinenull38     override fun getTextHeadline(text: CharSequence): String {
39         return context.getString(
40             getTemplateResource(text, R.string.sharing_link, R.string.sharing_text)
41         )
42     }
43 
getAlbumHeadlinenull44     override fun getAlbumHeadline(): String {
45         return context.getString(R.string.sharing_album)
46     }
47 
getImagesWithTextHeadlinenull48     override fun getImagesWithTextHeadline(text: CharSequence, count: Int): String {
49         return getPluralString(
50             getTemplateResource(
51                 text,
52                 R.string.sharing_images_with_link,
53                 R.string.sharing_images_with_text,
54             ),
55             count,
56         )
57     }
58 
getVideosWithTextHeadlinenull59     override fun getVideosWithTextHeadline(text: CharSequence, count: Int): String {
60         return getPluralString(
61             getTemplateResource(
62                 text,
63                 R.string.sharing_videos_with_link,
64                 R.string.sharing_videos_with_text,
65             ),
66             count,
67         )
68     }
69 
getFilesWithTextHeadlinenull70     override fun getFilesWithTextHeadline(text: CharSequence, count: Int): String {
71         return getPluralString(
72             getTemplateResource(
73                 text,
74                 R.string.sharing_files_with_link,
75                 R.string.sharing_files_with_text,
76             ),
77             count,
78         )
79     }
80 
getImagesHeadlinenull81     override fun getImagesHeadline(count: Int): String {
82         return getPluralString(R.string.sharing_images, count)
83     }
84 
getVideosHeadlinenull85     override fun getVideosHeadline(count: Int): String {
86         return getPluralString(R.string.sharing_videos, count)
87     }
88 
getFilesHeadlinenull89     override fun getFilesHeadline(count: Int): String {
90         return getPluralString(R.string.sharing_files, count)
91     }
92 
getNotItemsSelectedHeadlinenull93     override fun getNotItemsSelectedHeadline(): String =
94         context.getString(R.string.select_items_to_share)
95 
96     override fun getCopyButtonContentDescription(sharedText: CharSequence): String {
97         return context.getString(
98             getTemplateResource(sharedText, R.string.copy_link, R.string.copy_text)
99         )
100     }
101 
getPluralStringnull102     private fun getPluralString(@StringRes templateResource: Int, count: Int): String {
103         return PluralsMessageFormatter.format(
104             context.resources,
105             mapOf(PLURALS_COUNT to count),
106             templateResource,
107         )
108     }
109 
110     @StringRes
getTemplateResourcenull111     private fun getTemplateResource(
112         text: CharSequence,
113         @StringRes linkResource: Int,
114         @StringRes nonLinkResource: Int,
115     ): Int {
116         return if (text.toString().isHttpUri()) linkResource else nonLinkResource
117     }
118 }
119 
120 @Module
121 @InstallIn(SingletonComponent::class)
122 interface HeadlineGeneratorModule {
bindnull123     @Binds fun bind(impl: HeadlineGeneratorImpl): HeadlineGenerator
124 }
125