1 /*
2  * Copyright (C) 2014 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.tv.settings.accessories;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.text.SpannableString;
23 import android.text.SpannableStringBuilder;
24 import android.text.TextUtils;
25 import android.text.style.DynamicDrawableSpan;
26 import android.util.TypedValue;
27 import android.view.View;
28 
29 import com.android.tv.settings.R;
30 import com.android.tv.settings.dialog.ProgressDialogFragment;
31 import com.android.tv.settings.widget.CenterAlignedDynamicDrawableSpan;
32 
33 /**
34  * Custom Content Fragment for the Bluetooth settings activity.
35  */
36 public class AddAccessoryContentFragment extends ProgressDialogFragment {
37 
38     private static final String BUTTON1_TOKEN = "button1_token";
39     private static final String BUTTON2_TOKEN = "button2_token";
40 
newInstance()41     public static AddAccessoryContentFragment newInstance() {
42         return new AddAccessoryContentFragment();
43     }
44 
45     @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)46     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
47         super.onViewCreated(view, savedInstanceState);
48         setTitle(R.string.accessories_add_title);
49         setIcon(R.drawable.ic_bluetooth_searching_128dp);
50         setSummary(R.string.accessories_add_bluetooth_inst);
51         updateExtraDescription();
52         if (getResources() != null) {
53             TypedValue lineSpacingMultiplier = new TypedValue();
54             getResources().getValue(
55                     R.dimen.description_with_inline_icon_line_spacing_multiplier,
56                     lineSpacingMultiplier,
57                     true);
58             setDescriptionLineSpacingMultiplier(lineSpacingMultiplier.getFloat());
59         }
60     }
61 
updateExtraDescription()62     private void updateExtraDescription() {
63         String extraDescription = getString(
64                 R.string.accessories_add_bluetooth_inst_extra, BUTTON1_TOKEN, BUTTON2_TOKEN);
65         if (TextUtils.isEmpty(extraDescription) || getActivity() == null) {
66             return;
67         }
68 
69         SpannableStringBuilder builder = new SpannableStringBuilder(extraDescription);
70         int indexOfBackButtonImage = extraDescription.indexOf(BUTTON1_TOKEN);
71         DynamicDrawableSpan imageSpan =
72                 new CenterAlignedDynamicDrawableSpan(
73                         getActivity(), R.drawable.pairing_button1_icon);
74         if (indexOfBackButtonImage != -1) {
75             builder.setSpan(
76                     imageSpan,
77                     indexOfBackButtonImage,
78                     indexOfBackButtonImage + BUTTON1_TOKEN.length(),
79                     SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
80         }
81 
82         int indexOfHomeButtonImage = extraDescription.indexOf(BUTTON2_TOKEN);
83         DynamicDrawableSpan imageSpan2 =
84                 new CenterAlignedDynamicDrawableSpan(
85                         getActivity(), R.drawable.pairing_button2_icon);
86         if (indexOfHomeButtonImage != -1) {
87             builder.setSpan(
88                     imageSpan2,
89                     indexOfHomeButtonImage,
90                     indexOfHomeButtonImage + BUTTON2_TOKEN.length(),
91                     SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
92         }
93 
94         setExtraText(builder);
95         if (getExtraInstructionContentDescription(getActivity()) != null) {
96             setExtraContentDescription(getExtraInstructionContentDescription(getActivity()));
97         }
98     }
99 
getExtraInstructionContentDescription(Context context)100     static String getExtraInstructionContentDescription(Context context) {
101         if (context == null) {
102             return null;
103         }
104         String mainDescription =
105                 context.getResources().getString(R.string.accessories_add_bluetooth_inst_extra);
106         String button1 = context.getResources().getString(R.string.accessories_pairing_button1);
107         String button2 = context.getResources().getString(R.string.accessories_pairing_button2);
108         if (TextUtils.isEmpty(mainDescription) || TextUtils.isEmpty(button1)
109                 || TextUtils.isEmpty(button2)) {
110             return null;
111         }
112         return context.getResources()
113                 .getString(R.string.accessories_add_bluetooth_inst_extra, button1, button2);
114     }
115 }
116