Java tutorial
/* * WiFi Analyzer * Copyright (C) 2016 VREM Software Development <VREMSoftwareDevelopment@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/> */ package com.gammalabs.wifianalyzer.wifi; import android.content.Context; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import com.gammalabs.wifianalyzer.R; import com.gammalabs.wifianalyzer.wifi.model.WiFiData; import com.gammalabs.wifianalyzer.wifi.model.WiFiDetail; import com.gammalabs.wifianalyzer.wifi.scanner.UpdateNotifier; class AccessPointsAdapter extends BaseExpandableListAdapter implements UpdateNotifier { private final Context context; private AccessPointsAdapterData accessPointsAdapterData; private AccessPointDetail accessPointDetail; private AccessPointPopup accessPointPopup; AccessPointsAdapter(@NonNull Context context) { super(); this.context = context; setAccessPointsAdapterData(new AccessPointsAdapterData()); setAccessPointDetail(new AccessPointDetail()); setAccessPointPopup(new AccessPointPopup()); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { WiFiDetail wiFiDetail = (WiFiDetail) getGroup(groupPosition); View view = accessPointDetail.makeView(convertView, parent, wiFiDetail, false); attachPopup(view, wiFiDetail); ImageView groupIndicator = (ImageView) view.findViewById(R.id.groupIndicator); int childrenCount = getChildrenCount(groupPosition); if (childrenCount > 0) { groupIndicator.setVisibility(View.VISIBLE); groupIndicator.setImageResource( isExpanded ? R.drawable.ic_expand_less_black_24dp : R.drawable.ic_expand_more_black_24dp); groupIndicator.setColorFilter(ContextCompat.getColor(context, R.color.icons_color)); } else { groupIndicator.setVisibility(View.GONE); } return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { WiFiDetail wiFiDetail = (WiFiDetail) getChild(groupPosition, childPosition); View view = accessPointDetail.makeView(convertView, parent, wiFiDetail, true); attachPopup(view, wiFiDetail); view.findViewById(R.id.groupIndicator).setVisibility(View.GONE); return view; } @Override public void update(@NonNull WiFiData wiFiData) { accessPointsAdapterData.update(wiFiData); notifyDataSetChanged(); } @Override public int getGroupCount() { return accessPointsAdapterData.parentsCount(); } @Override public int getChildrenCount(int groupPosition) { return accessPointsAdapterData.childrenCount(groupPosition); } @Override public Object getGroup(int groupPosition) { return accessPointsAdapterData.parent(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return accessPointsAdapterData.child(groupPosition, childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } void setAccessPointsAdapterData(@NonNull AccessPointsAdapterData accessPointsAdapterData) { this.accessPointsAdapterData = accessPointsAdapterData; } void setAccessPointDetail(@NonNull AccessPointDetail accessPointDetail) { this.accessPointDetail = accessPointDetail; } void setAccessPointPopup(@NonNull AccessPointPopup accessPointPopup) { this.accessPointPopup = accessPointPopup; } private void attachPopup(@NonNull View view, @NonNull WiFiDetail wiFiDetail) { View popupView = view.findViewById(R.id.attachPopup); if (popupView != null) { accessPointPopup.attach(popupView, wiFiDetail); accessPointPopup.attach(view.findViewById(R.id.ssid), wiFiDetail); } } }