4040import cn .salesuite .saf .inject .annotation .OnItemClick ;
4141import cn .salesuite .saf .inject .annotation .OnLongClick ;
4242import cn .salesuite .saf .inject .annotation .OnTouch ;
43+ import cn .salesuite .saf .utils .StringUtils ;
4344
4445/**
4546 * 可以注入view、resource、systemservice等等<br>
@@ -166,7 +167,15 @@ public Injector(Object object, View v) {
166167 context = v .getContext ();
167168 clazz = object .getClass ();
168169 activity = null ;
169- extras = null ;
170+ if (context != null ) {
171+ if (context instanceof Activity ) {
172+ extras = ((Activity ) context ).getIntent ().getExtras ();
173+ } else {
174+ extras = null ;
175+ }
176+ } else {
177+ extras = null ;
178+ }
170179 }
171180
172181
@@ -313,43 +322,13 @@ private void injectFields(Finder finder) {
313322 Object service = context .getSystemService (serviceName );
314323 injectIntoField (field , service );
315324 } else if (annotation .annotationType () == InjectExtra .class ) {
316- if (extras != null ) {
317- Object value = extras .get (((InjectExtra ) annotation ).key ());
318- if (value ==null ) {
319- if (field .getType ().getName ().equals (
320- java .lang .Integer .class .getName ())
321- || field .getType ().getName ().equals ("int" )) {
322- value = ((InjectExtra ) annotation ).defaultInt ();
323- } else if (field .getType ().getName ().equals (
324- java .lang .Boolean .class .getName ())
325- || field .getType ().getName ().equals ("boolean" )) {
326- value = ((InjectExtra ) annotation ).defaultBoolean ();
327- } else if (field .getType ().getName ().equals (
328- java .lang .String .class .getName ())) {
329- value = ((InjectExtra ) annotation ).defaultString ();
330- }
331- }
332-
333- injectIntoField (field , value );
334- } else {
335- Object value = null ;
336- if (field .getType ().getName ().equals (
337- java .lang .Integer .class .getName ())
338- || field .getType ().getName ().equals ("int" )) {
339- value = ((InjectExtra ) annotation ).defaultInt ();
340- } else if (field .getType ().getName ().equals (
341- java .lang .Boolean .class .getName ())
342- || field .getType ().getName ().equals ("boolean" )) {
343- value = ((InjectExtra ) annotation ).defaultBoolean ();
344- } else if (field .getType ().getName ().equals (
345- java .lang .String .class .getName ())) {
346- value = ((InjectExtra ) annotation ).defaultString ();
347- }
348-
349- if (value !=null ) {
350- injectIntoField (field , value );
351- }
325+ String extraKey = ((InjectExtra ) annotation ).key ();
326+ // key 没有使用
327+ if (StringUtils .isBlank (extraKey )) {
328+ extraKey = field .getName ();
352329 }
330+ setInjectExtra (field ,extras ,extraKey ,annotation );
331+
353332 } else if (annotation .annotationType () == InjectSupportFragment .class ) {
354333 int id = ((InjectSupportFragment ) annotation ).id ();
355334 Fragment fragment = findSupportFragment (field , id );
@@ -358,6 +337,32 @@ private void injectFields(Finder finder) {
358337 }
359338 }
360339 }
340+
341+ private void setInjectExtra (Field field , Bundle extras , String extraKey ,Annotation annotation ) {
342+
343+ Object value = extras != null ? extras .get (extraKey ) : null ;
344+ if (value ==null ) {
345+ if (field .getType ().getName ().equals (java .lang .Integer .class .getName ())
346+ || field .getType ().getName ().equals ("int" )) {
347+ value = ((InjectExtra ) annotation ).defaultInt ();
348+ } else if (field .getType ().getName ().equals (java .lang .Boolean .class .getName ())
349+ || field .getType ().getName ().equals ("boolean" )) {
350+ value = ((InjectExtra ) annotation ).defaultBoolean ();
351+ } else if (field .getType ().getName ().equals (java .lang .String .class .getName ())) {
352+ value = ((InjectExtra ) annotation ).defaultString ();
353+ } else if (field .getType ().getName ().equals (java .lang .Long .class .getName ())
354+ || field .getType ().getName ().equals ("long" )) {
355+ value = ((InjectExtra ) annotation ).defaultLong ();
356+ } else if (field .getType ().getName ().equals (java .lang .Double .class .getName ())
357+ || field .getType ().getName ().equals ("double" )) {
358+ value = ((InjectExtra ) annotation ).defaultDouble ();
359+ }
360+ }
361+
362+ if (value != null ) {
363+ injectIntoField (field , value );
364+ }
365+ }
361366
362367 /**
363368 * 查找fragment
0 commit comments