1 /*
2  * Copyright (C) 2020 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.companiondevicesupport.activity;
18 
19 import static com.android.car.connecteddevice.util.SafeLog.loge;
20 
21 import android.annotation.NonNull;
22 import android.graphics.Typeface;
23 import android.os.Bundle;
24 import android.text.Html;
25 import android.text.SpannableString;
26 import android.text.Spanned;
27 import android.text.style.StyleSpan;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.TextView;
32 
33 import androidx.fragment.app.Fragment;
34 
35 import com.android.car.companiondevicesupport.R;
36 
37 /** Fragment that notifies the user to select the car. */
38 public class AddAssociatedDeviceFragment extends Fragment {
39     private static final String DEVICE_NAME_KEY = "deviceName";
40     private static final String TAG = "AddAssociatedDeviceFragment";
41 
newInstance(@onNull String deviceName)42     static AddAssociatedDeviceFragment newInstance(@NonNull String deviceName) {
43         Bundle bundle = new Bundle();
44         bundle.putString(DEVICE_NAME_KEY, deviceName);
45         AddAssociatedDeviceFragment fragment = new AddAssociatedDeviceFragment();
46         fragment.setArguments(bundle);
47         return fragment;
48     }
49 
50     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)51     public View onCreateView(
52             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
53         return inflater.inflate(R.layout.add_associated_device_fragment, container, false);
54     }
55 
56     @Override
onViewCreated(View view, Bundle savedInstanceState)57     public void onViewCreated(View view, Bundle savedInstanceState) {
58         Bundle bundle = getArguments();
59         String deviceName = bundle.getString(DEVICE_NAME_KEY);
60         TextView selectTextView = view.findViewById(R.id.associated_device_select_device);
61         setDeviceNameForAssociation(selectTextView, deviceName);
62     }
63 
setDeviceNameForAssociation(TextView textView, String deviceName)64     private void setDeviceNameForAssociation(TextView textView, String deviceName) {
65         if (textView == null) {
66             loge(TAG, "No valid TextView to show device name.");
67             return;
68         }
69         String selectText = getString(R.string.associated_device_select_device, deviceName);
70         Spanned styledSelectText = Html.fromHtml(selectText, Html.FROM_HTML_MODE_LEGACY);
71         textView.setText(styledSelectText);
72     }
73 }
74