1 /* 2 * Copyright (C) 2022 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.systemui.common.shared.model 18 19 import android.annotation.StringRes 20 import android.content.Context 21 22 /** 23 * Models a content description, that can either be already [loaded][ContentDescription.Loaded] or 24 * be a [reference][ContentDescription.Resource] to a resource. 25 */ 26 sealed class ContentDescription { 27 data class Loaded( 28 val description: String?, 29 ) : ContentDescription() 30 31 data class Resource( 32 @StringRes val res: Int, 33 ) : ContentDescription() 34 35 companion object { 36 /** 37 * Returns the loaded content description string, or null if we don't have one. 38 * 39 * Prefer [com.android.systemui.common.ui.binder.ContentDescriptionViewBinder.bind] over 40 * this method. This should only be used for testing or concatenation purposes. 41 */ ContentDescriptionnull42 fun ContentDescription?.loadContentDescription(context: Context): String? { 43 return when (this) { 44 null -> null 45 is Loaded -> this.description 46 is Resource -> context.getString(this.res) 47 } 48 } 49 } 50 } 51