1- package com .genonbeta .CoolSocket .test .adapter ;
2-
3- import android .content .Context ;
4- import android .support .v4 .util .ArrayMap ;
5- import android .view .LayoutInflater ;
6- import android .view .View ;
7- import android .view .ViewGroup ;
8- import android .view .animation .AnimationSet ;
9- import android .widget .BaseAdapter ;
10- import android .widget .TextView ;
11- import android .widget .Toast ;
12-
13- import com .genonbeta .CoolSocket .test .R ;
14- import com .genonbeta .CoolSocket .test .helper .GAnimater ;
15- import com .genonbeta .CoolSocket .test .helper .PairListHelper ;
16- import com .genonbeta .CoolSocket .test .helper .PairListHelper .DeviceInfo ;
17- import com .genonbeta .CoolSocket .test .helper .PairListHelper .ResultHandler ;
18- import com .genonbeta .core .content .Intent ;
19-
20- import java .net .InetAddress ;
21-
22- public class PairListAdapter extends BaseAdapter
23- {
24- private Context mContext ;
25- private ResultHandler mHandler = new ResultHandler ()
26- {
27- @ Override
28- public void onDeviceFound (InetAddress inetAddress )
29- {
30- super .onDeviceFound (inetAddress );
31- mContext .sendBroadcast (Intent .getNotifyIntent ());
32- }
33- };
34- private ArrayMap <String , DeviceInfo > mIndex = new ArrayMap <>();
35-
36- public PairListAdapter (Context context )
37- {
38-
39- this .mContext = context ;
40- }
41-
42- public void requestUpdate ()
43- {
44- Toast .makeText (this .mContext , PairListHelper .update (this .mHandler ) ? "Scan started" : "Still scanning. When it ends, list will be refreshed" , Toast .LENGTH_SHORT ).show ();
45- }
46-
47- @ Override
48- public void notifyDataSetChanged ()
49- {
50- this .mIndex .clear ();
51-
52- for (String str : PairListHelper .getList ().keySet ())
53- {
54- mIndex .put (str , PairListHelper .getList ().get (str ));
55- }
56-
57- super .notifyDataSetChanged ();
58- }
59-
60- @ Override
61- public int getCount ()
62- {
63- return this .mIndex .size ();
64- }
65-
66- @ Override
67- public Object getItem (int i )
68- {
69- return this .mIndex .keyAt (i );
70- }
71-
72- @ Override
73- public long getItemId (int i )
74- {
75- return (long ) 0 ;
76- }
77-
78- @ Override
79- public View getView (int i , View view , ViewGroup viewGroup )
80- {
81- if (view == null )
82- view = LayoutInflater .from (this .mContext ).inflate (R .layout .list_pair , viewGroup , false );
83-
84- return getViewAt (view , i );
85- }
86-
87- public View getViewAt (View view , int i )
88- {
89- String str = (String ) getItem (i );
90- DeviceInfo deviceInfo = this .mIndex .get (str );
91-
92- TextView textView = (TextView ) view .findViewById (R .id .list_text2 );
93- ((TextView ) view .findViewById (R .id .list_text )).setText (str );
94-
95- StringBuilder stringBuilder = new StringBuilder ();
96-
97- if (!deviceInfo .deviceName .equals (textView .getText ().toString ()))
98- {
99- AnimationSet set = GAnimater .getAnimation (GAnimater .APPEAR );
100- view .setAnimation (set );
101- }
102-
103- if (deviceInfo .trebleShot || deviceInfo .coolSocket || deviceInfo .deviceController )
104- {
105- textView .setVisibility (View .VISIBLE );
106-
107- if (deviceInfo .trebleShot )
108- stringBuilder = stringBuilder .append ("TShot " );
109-
110- if (deviceInfo .coolSocket )
111- stringBuilder = stringBuilder .append ("CSocket " );
112-
113- if (deviceInfo .deviceController )
114- stringBuilder = stringBuilder .append ("DContrllr " );
115- }
116-
117- textView .setText (stringBuilder );
118-
119- return view ;
120- }
121- }
1+ package com .genonbeta .CoolSocket .test .adapter ;
2+
3+ import android .content .Context ;
4+ import android .support .v4 .util .ArrayMap ;
5+ import android .util .Log ;
6+ import android .view .LayoutInflater ;
7+ import android .view .View ;
8+ import android .view .ViewGroup ;
9+ import android .view .animation .AnimationSet ;
10+ import android .widget .BaseAdapter ;
11+ import android .widget .TextView ;
12+ import android .widget .Toast ;
13+
14+ import com .genonbeta .CoolSocket .test .R ;
15+ import com .genonbeta .CoolSocket .test .helper .GAnimater ;
16+ import com .genonbeta .CoolSocket .test .helper .PairListHelper ;
17+ import com .genonbeta .CoolSocket .test .helper .PairListHelper .DeviceInfo ;
18+ import com .genonbeta .CoolSocket .test .helper .PairListHelper .ResultHandler ;
19+ import com .genonbeta .core .content .Intent ;
20+
21+ import java .net .InetAddress ;
22+
23+ public class PairListAdapter extends BaseAdapter
24+ {
25+ private Context mContext ;
26+ private ResultHandler mHandler = new ResultHandler ()
27+ {
28+ @ Override
29+ public void onDeviceFound (InetAddress inetAddress )
30+ {
31+ super .onDeviceFound (inetAddress );
32+ mContext .sendBroadcast (Intent .getNotifyIntent ());
33+ }
34+ };
35+ private ArrayMap <String , DeviceInfo > mIndex = new ArrayMap <>();
36+
37+ public PairListAdapter (Context context )
38+ {
39+ this .mContext = context ;
40+ }
41+
42+ public void requestUpdate ()
43+ {
44+ Toast .makeText (this .mContext , PairListHelper .update (this .mHandler ) ? "Scan started" : "Still scanning. When it ends, list will be refreshed" , Toast .LENGTH_SHORT ).show ();
45+ }
46+
47+ @ Override
48+ public void notifyDataSetChanged ()
49+ {
50+ this .mIndex .clear ();
51+
52+ for (String str : PairListHelper .getList ().keySet ())
53+ {
54+ mIndex .put (str , PairListHelper .getList ().get (str ));
55+ }
56+
57+ super .notifyDataSetChanged ();
58+ }
59+
60+ @ Override
61+ public int getCount ()
62+ {
63+ return this .mIndex .size ();
64+ }
65+
66+ @ Override
67+ public Object getItem (int i )
68+ {
69+ return this .mIndex .keyAt (i );
70+ }
71+
72+ @ Override
73+ public long getItemId (int i )
74+ {
75+ return (long ) 0 ;
76+ }
77+
78+ @ Override
79+ public View getView (int i , View view , ViewGroup viewGroup )
80+ {
81+ if (view == null )
82+ view = LayoutInflater .from (this .mContext ).inflate (R .layout .list_pair , viewGroup , false );
83+
84+ return getViewAt (view , i );
85+ }
86+
87+ public View getViewAt (View view , int i )
88+ {
89+ String str = (String ) getItem (i );
90+ DeviceInfo deviceInfo = this .mIndex .get (str );
91+
92+
93+ ((TextView ) view .findViewById (R .id .list_text )).setText (str );
94+ TextView textView2 = (TextView ) view .findViewById (R .id .list_text2 );
95+ TextView textView3 = (TextView ) view .findViewById (R .id .list_text3 );
96+
97+ StringBuilder stringBuilder = new StringBuilder ();
98+
99+ if (!str .equals (textView2 .getText ().toString ()))
100+ {
101+ AnimationSet set = GAnimater .getAnimation (GAnimater .APPEAR );
102+ view .setAnimation (set );
103+ }
104+
105+ if (deviceInfo .trebleShot || deviceInfo .coolSocket || deviceInfo .deviceController )
106+ {
107+ textView2 .setVisibility (View .VISIBLE );
108+
109+ if (deviceInfo .trebleShot )
110+ stringBuilder = stringBuilder .append ("TShot " );
111+
112+ if (deviceInfo .coolSocket )
113+ stringBuilder = stringBuilder .append ("CSocket " );
114+
115+ if (deviceInfo .deviceController )
116+ stringBuilder = stringBuilder .append ("DContrllr " );
117+ }
118+
119+ textView2 .setText (stringBuilder );
120+ textView3 .setVisibility ((deviceInfo .deviceName != null ) ? View .VISIBLE : View .GONE );
121+ textView3 .setText (deviceInfo .deviceName );
122+
123+ return view ;
124+ }
125+ }
0 commit comments