1 /*
2  * Copyright (C) 2023 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.example.android.vdmdemo.common;
18 
19 import android.app.Activity;
20 import android.graphics.Color;
21 import android.os.Bundle;
22 import android.util.TypedValue;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.fragment.app.Fragment;
28 
29 import dagger.hilt.android.AndroidEntryPoint;
30 
31 import java.util.function.Consumer;
32 
33 import javax.inject.Inject;
34 
35 /** Fragment that holds the connectivity status UI. */
36 @AndroidEntryPoint(Fragment.class)
37 public final class ConnectivityFragment extends Hilt_ConnectivityFragment {
38 
39     @Inject ConnectionManager mConnectionManager;
40 
41     private TextView mStatus = null;
42     private int mDefaultBackgroundColor;
43 
44     private final Consumer<ConnectionManager.ConnectionStatus> mConnectionCallback =
45             (status) -> {
46                 switch (status.state) {
47                     case DISCONNECTED -> updateStatus(mDefaultBackgroundColor,
48                             R.string.disconnected);
49                     case INITIALIZED -> updateStatus(mDefaultBackgroundColor,
50                             R.string.initialized);
51                     case CONNECTING -> updateStatus(mDefaultBackgroundColor,
52                             R.string.connecting, status.remoteDeviceName);
53                     case CONNECTED -> updateStatus(Color.GREEN, R.string.connected,
54                             status.remoteDeviceName);
55                     case ERROR -> updateStatus(Color.RED, R.string.error, status.errorMessage);
56                 }
57             };
58 
ConnectivityFragment()59     public ConnectivityFragment() {
60         super(R.layout.connectivity_fragment);
61     }
62 
63     @Override
onViewCreated(@onNull View view, Bundle bundle)64     public void onViewCreated(@NonNull View view, Bundle bundle) {
65         super.onViewCreated(view, bundle);
66 
67         mStatus = requireActivity().requireViewById(R.id.connection_status);
68 
69         TypedValue background = new TypedValue();
70         requireActivity()
71                 .getTheme()
72                 .resolveAttribute(android.R.attr.windowBackground, background, true);
73         mDefaultBackgroundColor = background.isColorType() ? background.data : Color.WHITE;
74 
75         CharSequence currentTitle = requireActivity().getTitle();
76         String localEndpointId = ConnectionManager.getLocalEndpointId();
77         String title =
78                 requireActivity().getString(R.string.this_device, currentTitle, localEndpointId);
79         requireActivity().setTitle(title);
80 
81         mConnectionCallback.accept(mConnectionManager.getConnectionStatus());
82         mConnectionManager.addConnectionCallback(mConnectionCallback);
83     }
84 
85     @Override
onDestroyView()86     public void onDestroyView() {
87         super.onDestroyView();
88         mConnectionManager.removeConnectionCallback(mConnectionCallback);
89     }
90 
updateStatus(int backgroundColor, int resId, Object... formatArgs)91     private void updateStatus(int backgroundColor, int resId, Object... formatArgs) {
92         Activity activity = requireActivity();
93         activity.runOnUiThread(() -> {
94             mStatus.setText(activity.getString(resId, formatArgs));
95             mStatus.setBackgroundColor(backgroundColor);
96         });
97     }
98 }
99