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.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ImageView;
26 import android.widget.TextView;
27 
28 import androidx.core.content.ContextCompat;
29 import androidx.fragment.app.Fragment;
30 import androidx.lifecycle.ViewModelProviders;
31 
32 import com.android.car.companiondevicesupport.R;
33 import com.android.car.companiondevicesupport.api.external.AssociatedDevice;
34 import com.android.car.companiondevicesupport.feature.trust.TrustedDeviceConstants;
35 
36 
37 /** Fragment that shows the details of an associated device. */
38 public class AssociatedDeviceDetailFragment extends Fragment {
39     private final static String TAG = "AssociatedDeviceDetailFragment";
40     private TextView mDeviceName;
41     private TextView mConnectionStatus;
42     private TextView mConnectionText;
43     private ImageView mConnectionIcon;
44     private AssociatedDeviceViewModel mModel;
45 
46     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)47     public View onCreateView(
48             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
49         return inflater.inflate(R.layout.associated_device_detail_fragment, container, false);
50     }
51 
52     @Override
onViewCreated(View view, Bundle savedInstanceState)53     public void onViewCreated(View view, Bundle savedInstanceState) {
54         mDeviceName = view.findViewById(R.id.device_name);
55         mConnectionIcon = view.findViewById(R.id.connection_icon);
56         mConnectionText = view.findViewById(R.id.connection_text);
57         mConnectionStatus = view.findViewById(R.id.connection_status);
58 
59         mModel = ViewModelProviders.of(getActivity()).get(AssociatedDeviceViewModel.class);
60         mModel.getDeviceDetails().observe(this, this::setDeviceDetails);
61 
62         view.findViewById(R.id.connection_button).setOnClickListener(l -> {
63             mModel.toggleConnectionStatusForCurrentDevice();
64         });
65         view.findViewById(R.id.remove_button).setOnClickListener(v ->
66                 mModel.selectCurrentDeviceToRemove());
67         view.findViewById(R.id.trusted_device_feature_button).setOnClickListener(v ->
68                 mModel.startFeatureActivityForCurrentDevice(
69                         TrustedDeviceConstants.INTENT_ACTION_TRUSTED_DEVICE_SETTING));
70     }
71 
setDeviceDetails(AssociatedDeviceDetails deviceDetails)72     private void setDeviceDetails(AssociatedDeviceDetails deviceDetails) {
73         if (deviceDetails == null) {
74             return;
75         }
76         mDeviceName.setText(deviceDetails.getDeviceName());
77         if (!deviceDetails.isConnectionEnabled()) {
78             setConnectionDisabledStyle();
79             return;
80         }
81         setConnectionEnabledStyle();
82         if (deviceDetails.isConnected()) {
83             mConnectionStatus.setText(getString(R.string.connected));
84         } else {
85             mConnectionStatus.setText(getString(R.string.notDetected));
86         }
87     }
88 
setConnectionEnabledStyle()89     private void setConnectionEnabledStyle() {
90         mConnectionText.setText(getString(R.string.disable_device_connection_text));
91         mConnectionIcon.setImageDrawable(ContextCompat.getDrawable(getContext(),
92                 R.drawable.ic_phonelink_erase));
93         mConnectionStatus.setTextAppearance(R.style.ConnectionEnabled);
94     }
95 
setConnectionDisabledStyle()96     private void setConnectionDisabledStyle() {
97         mConnectionStatus.setText(getString(R.string.disconnected));
98         mConnectionStatus.setTextAppearance(R.style.ConnectionDisabled);
99         mConnectionText.setText(getString(R.string.enable_device_connection_text));
100         mConnectionIcon.setImageDrawable(ContextCompat.getDrawable(getContext(),
101                 R.drawable.ic_phonelink_ring));
102     }
103 }
104