• 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.annotation.StringRes
20 import android.content.Context
21 import com.android.intentresolver.R
22 import android.util.PluralsMessageFormatter
23 
24 private const val PLURALS_COUNT = "count"
25 
26 /**
27  * HeadlineGenerator generates the text to show at the top of the sharesheet as a brief
28  * description of the content being shared.
29  */
30 class HeadlineGeneratorImpl(private val context: Context) : HeadlineGenerator {
getTextHeadlinenull31     override fun getTextHeadline(text: CharSequence): String {
32         return context.getString(
33             getTemplateResource(text, R.string.sharing_link, R.string.sharing_text))
34     }
35 
getImagesWithTextHeadlinenull36     override fun getImagesWithTextHeadline(text: CharSequence, count: Int): String {
37         return getPluralString(getTemplateResource(
38             text, R.string.sharing_images_with_link, R.string.sharing_images_with_text), count)
39     }
40 
getVideosWithTextHeadlinenull41     override fun getVideosWithTextHeadline(text: CharSequence, count: Int): String {
42         return getPluralString(getTemplateResource(
43             text, R.string.sharing_videos_with_link, R.string.sharing_videos_with_text), count)
44     }
45 
getFilesWithTextHeadlinenull46     override fun getFilesWithTextHeadline(text: CharSequence, count: Int): String {
47         return getPluralString(getTemplateResource(
48             text, R.string.sharing_files_with_link, R.string.sharing_files_with_text), count)
49     }
50 
getImagesHeadlinenull51     override fun getImagesHeadline(count: Int): String {
52         return getPluralString(R.string.sharing_images, count)
53     }
54 
getVideosHeadlinenull55     override fun getVideosHeadline(count: Int): String {
56         return getPluralString(R.string.sharing_videos, count)
57     }
58 
getFilesHeadlinenull59     override fun getFilesHeadline(count: Int): String {
60         return getPluralString(R.string.sharing_files, count)
61     }
62 
getPluralStringnull63     private fun getPluralString(@StringRes templateResource: Int, count: Int): String {
64         return PluralsMessageFormatter.format(
65             context.resources,
66             mapOf(PLURALS_COUNT to count),
67             templateResource
68         )
69     }
70 
71     @StringRes
getTemplateResourcenull72     private fun getTemplateResource(
73         text: CharSequence, @StringRes linkResource: Int, @StringRes nonLinkResource: Int
74     ): Int {
75         return if (text.toString().isHttpUri()) linkResource else nonLinkResource
76     }
77 }
78