4
4
import android .graphics .Color ;
5
5
import android .support .v7 .widget .RecyclerView ;
6
6
import android .text .TextUtils ;
7
+ import android .util .Log ;
8
+ import android .util .Pair ;
7
9
import android .view .LayoutInflater ;
8
10
import android .view .View ;
9
11
import android .view .ViewGroup ;
14
16
import org .json .JSONObject ;
15
17
16
18
import java .util .ArrayList ;
19
+ import java .util .Collection ;
20
+ import java .util .Collections ;
21
+ import java .util .Comparator ;
22
+ import java .util .Iterator ;
17
23
import java .util .List ;
24
+ import java .util .Map ;
18
25
26
+ import fuzion24 .device .vulnerability .test .VulnerabilityDescriptor ;
19
27
import fuzion24 .device .vulnerability .test .VulnerabilityTestResult ;
20
28
import fuzion24 .device .vulnerability .test .adapter .viewholder .RecyclerItemViewHolder ;
21
29
import fuzion24 .device .vulnerability .vulnerabilities .helper .BinaryAssets ;
22
30
23
31
public class RecyclerAdapter extends RecyclerView .Adapter <RecyclerView .ViewHolder > {
24
32
25
- private List <VulnerabilityTestResult > mResults ;
33
+ private static final String TAG = "RecycleAdapter" ;
34
+ final private List <Pair <VulnerabilityTestResult , VulnerabilityDescriptor >> mResults ;
26
35
private Context mContext ;
27
- private JSONObject mVulnMap ;
28
36
29
37
public RecyclerAdapter (Context context , List <VulnerabilityTestResult > itemList ) {
30
38
this .mContext = context ;
31
- this . mResults = itemList ;
39
+ List < Pair < VulnerabilityTestResult , VulnerabilityDescriptor >> res = new ArrayList <>() ;
32
40
33
41
try {
34
- String jsonVulns = BinaryAssets .extractAsset (context , "vuln_map.json" );
35
- mVulnMap = new JSONObject (jsonVulns );
36
- } catch (Exception e ) {
42
+ Map <String , VulnerabilityDescriptor > vMap = VulnerabilityDescriptor .getParsedVulnMap (context );
43
+
44
+ for (VulnerabilityTestResult vtr : itemList ) {
45
+ String cveOrId = vtr .getCVEorID ();
46
+ VulnerabilityDescriptor vd = vMap .get (cveOrId );
47
+ if (vd == null ){
48
+ Log .d (TAG , cveOrId + " has a null vulnerability descriptor" );
49
+ }
50
+ res .add (Pair .create (vtr , vd ));
51
+ }
52
+
53
+ Collections .sort (res , new Comparator <Pair <VulnerabilityTestResult , VulnerabilityDescriptor >>() {
54
+ @ Override
55
+ public int compare (Pair <VulnerabilityTestResult , VulnerabilityDescriptor > lhs , Pair <VulnerabilityTestResult , VulnerabilityDescriptor > rhs ) {
56
+ VulnerabilityDescriptor lhDesc = lhs .second ;
57
+ VulnerabilityDescriptor rhDesc = rhs .second ;
58
+ return lhDesc .getCVEDate ().before (rhDesc .getCVEDate ()) ? 1 : -1 ;
59
+ }
60
+ });
61
+ }catch (Exception e ){
37
62
e .printStackTrace ();
38
63
}
64
+ mResults = res ;
39
65
}
40
66
41
67
@ Override
@@ -46,52 +72,23 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
46
72
return RecyclerItemViewHolder .newInstance (view );
47
73
}
48
74
49
- private List <String > extractStringArray (JSONObject obj , String arrayName ) throws Exception {
50
- JSONArray jsonStringArray = obj .getJSONArray (arrayName );
51
- List <String > items = new ArrayList <String >();
52
- for (int i = 0 ; i < jsonStringArray .length (); i ++) {
53
- items .add (jsonStringArray .getString (i ));
54
- }
55
- return items ;
56
- }
57
75
58
76
@ Override
59
77
public void onBindViewHolder (RecyclerView .ViewHolder viewHolder , int position ) {
60
78
RecyclerItemViewHolder holder = (RecyclerItemViewHolder ) viewHolder ;
61
- VulnerabilityTestResult item = mResults .get (position );
79
+ Pair < VulnerabilityTestResult , VulnerabilityDescriptor > item = mResults .get (position );
62
80
63
- holder .setItemTestName (item .getCVEorID ());
81
+ VulnerabilityTestResult vulnRes = item .first ;
82
+ VulnerabilityDescriptor vulnDesc = item .second ;
64
83
65
- JSONObject jobj = null ;
66
- String description = null ;
67
- String impact = null ;
68
- Double cvssV2Score = null ;
69
- String cveDate = null ;
70
- List <String > externalLinks = null ;
71
- List <String > altNames = null ;
72
- List <String > patches = null ;
84
+ holder .setItemTestName (vulnRes .getCVEorID ());
73
85
74
- try {
75
- jobj = mVulnMap .getJSONObject (item .getCVEorID ());
76
- String cve = jobj .getString ("cve" );
77
- altNames = extractStringArray (jobj , "altnames" );
78
- description = jobj .getString ("description" );
79
- impact = jobj .getString ("impact" );
80
- externalLinks = extractStringArray (jobj , "external_links" );
81
- cvssV2Score = jobj .getDouble ("cvssv2" );
82
- patches = extractStringArray (jobj , "patch" );
83
- cveDate = jobj .getString ("cvedate" );
84
-
85
- } catch (Exception e ) {
86
- //We dont have an entry or are missing necessary components of it
87
- e .printStackTrace ();
88
- }
89
86
90
- if (item .getException () != null ) {
91
- holder .setItemTestResult (mContext .getString (R .string .error_test , item .getException ().getMessage ()));
87
+ if (vulnRes .getException () != null ) {
88
+ holder .setItemTestResult (mContext .getString (R .string .error_test , vulnRes .getException ().getMessage ()));
92
89
holder .setItemTestResultColor (mContext .getResources ().getColor (R .color .orange ));
93
90
} else {
94
- if (item .getResult ()) {
91
+ if (vulnRes .getResult ()) {
95
92
holder .setItemTestResultColor (mContext .getResources ().getColor (R .color .red ));
96
93
holder .setItemTestResult (mContext .getString (R .string .test_result_failure ));
97
94
} else {
@@ -100,19 +97,16 @@ public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
100
97
}
101
98
}
102
99
103
- if (!TextUtils .isEmpty (description )) {
104
- holder .setItemTestResultDescription (description );
105
- holder .setButtonShowDetailsClickListner (mContext , item .getCVEorID (), altNames , description , impact , externalLinks , patches , cvssV2Score , cveDate );
106
- } else {
107
- holder .setItemTestResultDescription (mContext .getString (R .string .information_not_available ));
108
-
109
- if (jobj != null ) {
110
- holder .setButtonShowDetailsClickListner (mContext , item .getCVEorID (), altNames , description , impact , externalLinks , patches , cvssV2Score , cveDate );
111
- } else {
112
- holder .setButtonShowDetailsClickListner (mContext , item .getCVEorID ());
113
- }
114
- }
115
-
100
+ holder .setItemTestResultDescription (vulnDesc .getDescription ());
101
+ holder .setButtonShowDetailsClickListner (mContext ,
102
+ vulnRes .getCVEorID (),
103
+ vulnDesc .getAltNames (),
104
+ vulnDesc .getDescription (),
105
+ vulnDesc .getImpact (),
106
+ vulnDesc .getExternalLinks (),
107
+ vulnDesc .getPatches (),
108
+ vulnDesc .getCVSSV2Score (),
109
+ vulnDesc .getCVEDate ().toString ());
116
110
}
117
111
118
112
@ Override
0 commit comments