• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.google.android.car.kitchensink.connectivity;
18 
19 import android.content.Context;
20 import android.graphics.Color;
21 import android.util.Log;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.ArrayAdapter;
26 import android.widget.Button;
27 import android.widget.TextView;
28 
29 import com.google.android.car.kitchensink.R;
30 import com.google.android.car.kitchensink.connectivity.ConnectivityFragment.NetworkItem;
31 
32 public class NetworkListAdapter extends ArrayAdapter<NetworkItem> {
33     private static final String TAG = NetworkListAdapter.class.getSimpleName();
34 
35     private Context mContext;
36     private NetworkItem[] mNetworkList; // keep list of objects
37     private ConnectivityFragment mFragment; // for calling things on button press
38 
NetworkListAdapter(Context context, NetworkItem[] items, ConnectivityFragment fragment)39     public NetworkListAdapter(Context context,  NetworkItem[] items,
40                               ConnectivityFragment fragment) {
41         super(context, R.layout.network_item, items);
42         mContext = context;
43         mFragment = fragment;
44         mNetworkList = items;
45 
46         Log.i(TAG, "Created NetworkListAdaptor");
47     }
48 
49     // Returns a list item view for each position
50     @Override
getView(int position, View convertView, ViewGroup parent)51     public View getView(int position, View convertView, ViewGroup parent) {
52         ViewHolder vh;
53         if (convertView == null) {
54             vh = new ViewHolder();
55             LayoutInflater inflater = LayoutInflater.from(mContext);
56             convertView = inflater.inflate(R.layout.network_item, parent, false);
57             vh.netActive = convertView.findViewById(R.id.network_active);
58             vh.netId = convertView.findViewById(R.id.network_id);
59             vh.netType = convertView.findViewById(R.id.network_type);
60             vh.netState = convertView.findViewById(R.id.network_state);
61             vh.connected = convertView.findViewById(R.id.network_connected);
62             vh.available = convertView.findViewById(R.id.network_available);
63             vh.roaming = convertView.findViewById(R.id.network_roaming);
64             vh.netIface = convertView.findViewById(R.id.network_iface);
65             vh.hwAddress = convertView.findViewById(R.id.hw_address);
66             vh.ipAddresses = convertView.findViewById(R.id.network_ip_addresses);
67             vh.dns = convertView.findViewById(R.id.network_dns);
68             vh.domains = convertView.findViewById(R.id.network_domains);
69             vh.routes = convertView.findViewById(R.id.network_routes);
70             vh.transports = convertView.findViewById(R.id.network_transports);
71             vh.capabilities = convertView.findViewById(R.id.network_capabilities);
72             vh.bandwidth = convertView.findViewById(R.id.network_bandwidth);
73             vh.requestButton = convertView.findViewById(R.id.network_request);
74             vh.defaultButton = convertView.findViewById(R.id.network_default);
75             vh.reportButton = convertView.findViewById(R.id.network_report);
76 
77             convertView.setTag(vh);
78         } else {
79             vh = (ViewHolder) convertView.getTag();
80         }
81 
82         // If there's data to fill for the given position in the list
83         if (position < getCount()) {
84             vh.netId.setText("" + mNetworkList[position].mNetId);
85             vh.netType.setText(mNetworkList[position].mType);
86             vh.netState.setText(mNetworkList[position].mState);
87             vh.connected.setText(mNetworkList[position].mConnected);
88             vh.available.setText(mNetworkList[position].mAvailable);
89             vh.roaming.setText(mNetworkList[position].mRoaming);
90             vh.netIface.setText(mNetworkList[position].mInterfaceName);
91             vh.hwAddress.setText(mNetworkList[position].mHwAddress);
92             vh.ipAddresses.setText(mNetworkList[position].mIpAddresses);
93             vh.dns.setText(mNetworkList[position].mDnsAddresses);
94             vh.domains.setText(mNetworkList[position].mDomains);
95             vh.routes.setText(mNetworkList[position].mRoutes);
96             vh.transports.setText(mNetworkList[position].mTransports);
97             vh.capabilities.setText(mNetworkList[position].mCapabilities);
98             vh.bandwidth.setText(mNetworkList[position].mBandwidth);
99 
100             // Active request indicator
101             vh.netActive.setBackgroundColor(mNetworkList[position].mRequested
102                     ? Color.parseColor("#5fdd6e")
103                     : Color.parseColor("#ff3d3d"));
104 
105             // Request to track button
106             setToggleButton(position, vh.requestButton, mNetworkList[position].mRequested,
107                     "Release", "Request", this::onRequestClicked);
108 
109             // Process default button
110             setToggleButton(position, vh.defaultButton, mNetworkList[position].mDefault,
111                     "Remove Default", "Set Default", this::onDefaultClicked);
112 
113             // Report network button
114             setPositionTaggedCallback(position, vh.reportButton, this::onReportClicked);
115         }
116 
117         // Alternate table row background color to make it easier to view
118         convertView.setBackgroundColor(((position % 2) != 0)
119                 ? Color.parseColor("#2A2E2D")
120                 : Color.parseColor("#1E1E1E"));
121 
122         return convertView;
123     }
124 
125     // Tags a button with its element position and assigned it's callback. The callback can then
126     // get the tag and use it as a position to know which data is associated with it
setPositionTaggedCallback(int position, Button button, View.OnClickListener l)127     private void setPositionTaggedCallback(int position, Button button, View.OnClickListener l) {
128         button.setTag(position);
129         button.setOnClickListener(l);
130     }
131 
setToggleButton(int position, Button button, boolean on, String ifOn, String ifOff, View.OnClickListener l)132     private void setToggleButton(int position, Button button, boolean on, String ifOn, String ifOff,
133             View.OnClickListener l) {
134         // Manage button text based on status
135         if (on) {
136             button.setText(ifOn);
137         } else {
138             button.setText(ifOff);
139         }
140         setPositionTaggedCallback(position, button, l);
141     }
142 
onRequestClicked(View view)143     private void onRequestClicked(View view) {
144         int position = (int) view.getTag();
145         if (mNetworkList[position].mRequested) {
146             mFragment.releaseNetworkById(mNetworkList[position].mNetId);
147             mNetworkList[position].mRequested = false;
148         } else {
149             mFragment.requestNetworkById(mNetworkList[position].mNetId);
150             mNetworkList[position].mRequested = true;
151         }
152         notifyDataSetChanged();
153     }
154 
onDefaultClicked(View view)155     private void onDefaultClicked(View view) {
156         int position = (int) view.getTag();
157         if (mNetworkList[position].mDefault) {
158             mFragment.clearBoundNetwork();
159             mNetworkList[position].mDefault = false;
160         } else {
161             for (int i = 0; i < mNetworkList.length; i++) {
162                 if (i != position) {
163                     mNetworkList[i].mDefault = false;
164                 }
165             }
166             mFragment.bindToNetwork(mNetworkList[position].mNetId);
167             mNetworkList[position].mDefault = true;
168         }
169         notifyDataSetChanged();
170     }
171 
onReportClicked(View view)172     private void onReportClicked(View view) {
173         int position = (int) view.getTag();
174         mFragment.reportNetworkbyId(mNetworkList[position].mNetId);
175     }
176 
refreshNetworks(NetworkItem[] networksIn)177     public void refreshNetworks(NetworkItem[] networksIn) {
178         mNetworkList = networksIn;
179         notifyDataSetChanged();
180     }
181 
182     @Override
getCount()183     public int getCount() {
184         return mNetworkList.length;
185     }
186 
187     static class ViewHolder {
188         public View netActive;
189         public TextView netId;
190         public TextView netType;
191         public TextView netState;
192         public TextView connected;
193         public TextView available;
194         public TextView roaming;
195         public TextView netIface;
196         public TextView hwAddress;
197         public TextView ipAddresses;
198         public TextView dns;
199         public TextView domains;
200         public TextView routes;
201         public TextView transports;
202         public TextView capabilities;
203         public TextView bandwidth;
204         public Button requestButton;
205         public Button defaultButton;
206         public Button reportButton;
207     }
208 }
209