1 /* 2 * Copyright (C) 2017 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 package com.android.car.settings.wifi; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.graphics.drawable.Drawable; 21 import android.graphics.drawable.StateListDrawable; 22 import android.net.wifi.WifiManager; 23 import android.os.Bundle; 24 import android.support.car.ui.PagedListView; 25 import android.support.v7.widget.RecyclerView; 26 import android.util.Log; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.annotation.NonNull; 30 import android.annotation.Nullable; 31 import android.view.View.OnClickListener; 32 import android.view.ViewGroup; 33 import android.widget.ImageView; 34 import android.widget.TextView; 35 import android.widget.Toast; 36 37 import com.android.car.settings.R; 38 import com.android.car.settings.wifi.AccessPointListAdapter.ViewHolder; 39 import com.android.settingslib.wifi.AccessPoint; 40 41 import java.util.List; 42 43 /** 44 * Renders {@link AccessPoint} to a view to be displayed as a row in a list. 45 */ 46 public class AccessPointListAdapter 47 extends RecyclerView.Adapter<AccessPointListAdapter.ViewHolder> 48 implements PagedListView.ItemCap { 49 private static final String TAG = "AccessPointListAdapter"; 50 private static final int[] STATE_SECURED = { 51 com.android.settingslib.R.attr.state_encrypted 52 }; 53 private static final int[] STATE_NONE = {}; 54 private static int[] wifi_signal_attributes = {com.android.settingslib.R.attr.wifi_signal}; 55 56 private final StateListDrawable mWifiSld; 57 private final Context mContext; 58 private final CarWifiManager mCarWifiManager; 59 private final WifiManager.ActionListener mConnectionListener; 60 61 private List<AccessPoint> mAccessPoints; 62 63 public AccessPointListAdapter(@NonNull Context context, CarWifiManager carWifiManager, 64 @NonNull List<AccessPoint> accesssPoints) { 65 mContext = context; 66 mCarWifiManager = carWifiManager; 67 mAccessPoints = accesssPoints; 68 mWifiSld = (StateListDrawable) context.getTheme() 69 .obtainStyledAttributes(wifi_signal_attributes).getDrawable(0); 70 71 mConnectionListener = new WifiManager.ActionListener() { 72 @Override 73 public void onSuccess() { 74 } 75 @Override 76 public void onFailure(int reason) { 77 Toast.makeText(mContext, 78 R.string.wifi_failed_connect_message, 79 Toast.LENGTH_SHORT).show(); 80 } 81 }; 82 } 83 84 public void updateAccessPoints(@NonNull List<AccessPoint> accesssPoints) { 85 mAccessPoints = accesssPoints; 86 notifyDataSetChanged(); 87 } 88 89 public boolean isEmpty() { 90 return mAccessPoints.isEmpty(); 91 } 92 93 public class ViewHolder extends RecyclerView.ViewHolder { 94 private final ImageView mIcon; 95 private final TextView mWifiName; 96 private final TextView mWifiDesc; 97 98 public ViewHolder(View view) { 99 super(view); 100 mWifiName = (TextView) view.findViewById(R.id.title); 101 mWifiDesc = (TextView) view.findViewById(R.id.desc); 102 mIcon = (ImageView) view.findViewById(R.id.icon); 103 } 104 } 105 106 private class AccessPointClickListener implements OnClickListener { 107 private final AccessPoint mAccessPoint; 108 109 public AccessPointClickListener(AccessPoint accessPoint) { 110 mAccessPoint = accessPoint; 111 } 112 113 @Override 114 public void onClick(View v) { 115 // for new open unsecuried wifi network, connect to it right away 116 if (mAccessPoint.getSecurity() == AccessPoint.SECURITY_NONE && 117 !mAccessPoint.isSaved() && !mAccessPoint.isActive()) { 118 mCarWifiManager.connectToPublicWifi(mAccessPoint, mConnectionListener); 119 } else { 120 Intent intent = mAccessPoint.isSaved() 121 ? new Intent(mContext , WifiDetailActivity.class) 122 : new Intent(mContext, AddWifiActivity.class); 123 Bundle accessPointState = new Bundle(); 124 mAccessPoint.saveWifiState(accessPointState); 125 intent.putExtras(accessPointState); 126 mContext.startActivity(intent); 127 } 128 } 129 }; 130 131 @Override 132 public AccessPointListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 133 int viewType) { 134 View v = LayoutInflater.from(parent.getContext()) 135 .inflate(R.layout.list_item, parent, false); 136 ViewHolder vh = new ViewHolder(v); 137 return vh; 138 } 139 140 @Override 141 public void onBindViewHolder(ViewHolder holder, int position) { 142 AccessPoint accessPoint = mAccessPoints.get(position); 143 holder.itemView.setOnClickListener(new AccessPointClickListener(accessPoint)); 144 holder.mWifiName.setText(accessPoint.getConfigName()); 145 holder.mIcon.setImageDrawable(getIcon(accessPoint)); 146 String summary = accessPoint.getSummary(); 147 if (summary != null && !summary.isEmpty()) { 148 holder.mWifiDesc.setText(summary); 149 holder.mWifiDesc.setVisibility(View.VISIBLE); 150 } else { 151 holder.mWifiDesc.setVisibility(View.GONE); 152 } 153 } 154 155 @Override 156 public int getItemCount() { 157 return mAccessPoints.size(); 158 } 159 160 @Override 161 public void setMaxItems(int maxItems) { 162 // no limit in this list. 163 } 164 165 private Drawable getIcon(AccessPoint accessPoint) { 166 mWifiSld.setState((accessPoint.getSecurity() != AccessPoint.SECURITY_NONE) 167 ? STATE_SECURED 168 : STATE_NONE); 169 Drawable drawable = mWifiSld.getCurrent(); 170 drawable.setLevel(accessPoint.getLevel()); 171 drawable.invalidateSelf(); 172 return drawable; 173 } 174 } 175