1 /*
2  * Copyright (C) 2019 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.car.developeroptions.applications;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.widget.TextView;
23 
24 import com.android.car.developeroptions.R;
25 import com.android.car.developeroptions.accessibility.ListDialogPreference;
26 
27 public class AppDomainsPreference extends ListDialogPreference {
28     private int mNumEntries;
29 
AppDomainsPreference(Context context, AttributeSet attrs)30     public AppDomainsPreference(Context context, AttributeSet attrs) {
31         super(context, attrs);
32 
33         setDialogLayoutResource(R.layout.app_domains_dialog);
34         setListItemLayoutResource(R.layout.app_domains_item);
35     }
36 
37     @Override
setTitles(CharSequence[] titles)38     public void setTitles(CharSequence[] titles) {
39         mNumEntries = (titles != null) ? titles.length : 0;
40         super.setTitles(titles);
41     }
42 
43     @Override
getSummary()44     public CharSequence getSummary() {
45         final Context context = getContext();
46         if (mNumEntries == 0) {
47             return context.getString(R.string.domain_urls_summary_none);
48         }
49 
50         // The superclass summary is the text of the first entry in the list
51         final CharSequence summary = super.getSummary();
52         final int whichVersion = (mNumEntries == 1)
53                 ? R.string.domain_urls_summary_one
54                 : R.string.domain_urls_summary_some;
55         return context.getString(whichVersion, summary);
56     }
57 
58     @Override
onBindListItem(View view, int index)59     protected void onBindListItem(View view, int index) {
60         final CharSequence title = getTitleAt(index);
61         if (title != null) {
62             final TextView domainName = (TextView) view.findViewById(R.id.domain_name);
63             domainName.setText(title);
64         }
65     }
66 }
67