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 @file:JvmName("NotificationContentDescription")
18 
19 package com.android.systemui.statusbar.notification
20 
21 import android.app.Notification
22 import android.content.Context
23 import android.text.TextUtils
24 import androidx.annotation.MainThread
25 import com.android.systemui.res.R
26 
27 /** Returns accessibility content description for a given notification. */
28 @MainThread
contentDescForNotificationnull29 fun contentDescForNotification(c: Context, n: Notification): CharSequence {
30     val appName = n.loadHeaderAppName(c) ?: ""
31     val title = n.extras?.getCharSequence(Notification.EXTRA_TITLE)
32     val text = n.extras?.getCharSequence(Notification.EXTRA_TEXT)
33     val ticker = n.tickerText
34 
35     // Some apps just put the app name into the title
36     val titleOrText = if (TextUtils.equals(title, appName)) text else title
37     val desc =
38         if (!TextUtils.isEmpty(titleOrText)) titleOrText
39         else if (!TextUtils.isEmpty(ticker)) ticker else ""
40     return c.getString(R.string.accessibility_desc_notification_icon, appName, desc)
41 }
42