1 /*
2  * Copyright (C) 2014 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.loganalysis.item;
17 
18 import org.json.JSONArray;
19 import org.json.JSONException;
20 import org.json.JSONObject;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Set;
25 
26 /**
27  * An {@link IItem} used to qtaguid info.
28  */
29 public class QtaguidItem implements IItem {
30     /** Constant for JSON output */
31     public static final String USERS_KEY = "users";
32     /** Constant for JSON output */
33     public static final String UID_KEY = "uid";
34     /** Constant for JSON output */
35     public static final String RX_BYTES_KEY = "rx_bytes";
36     /** Constant for JSON output */
37     public static final String TX_BYTES_KEY = "tx_bytes";
38 
39     private Map<Integer, Row> mRows = new HashMap<Integer, Row>();
40 
41     private static class Row {
42         public int rxBytes = 0;
43         public int txBytes = 0;
44     }
45 
46     /**
47      * {@inheritDoc}
48      */
49     @Override
merge(IItem other)50     public IItem merge(IItem other) throws ConflictingItemException {
51         throw new ConflictingItemException("Qtaguid items cannot be merged");
52     }
53 
54     /**
55      * {@inheritDoc}
56      */
57     @Override
isConsistent(IItem other)58     public boolean isConsistent(IItem other) {
59        return false;
60     }
61 
62     /**
63      * {@inheritDoc}
64      */
65     @Override
toJson()66     public JSONObject toJson() {
67         JSONObject object = new JSONObject();
68         JSONArray users = new JSONArray();
69         for (int uid : getUids()) {
70             try {
71                 JSONObject user = new JSONObject();
72                 user.put(UID_KEY, uid);
73                 user.put(RX_BYTES_KEY, getRxBytes(uid));
74                 user.put(TX_BYTES_KEY, getTxBytes(uid));
75                 users.put(user);
76             } catch (JSONException e) {
77                 // ignore
78             }
79         }
80 
81         try {
82             object.put(USERS_KEY, users);
83         } catch (JSONException e) {
84             // ignore
85         }
86         return object;
87     }
88 
89     /**
90      * Get a set of UIDs seen in the qtaguid output.
91      */
getUids()92     public Set<Integer> getUids() {
93         return mRows.keySet();
94     }
95 
96     /**
97      * Add a row from the qtaguid output to the {@link QtaguidItem}.
98      *
99      * @param uid The UID from the output
100      * @param rxBytes the number of received bytes
101      * @param txBytes the number of sent bytes
102      */
addRow(int uid, int rxBytes, int txBytes)103     public void addRow(int uid, int rxBytes, int txBytes) {
104         Row row = new Row();
105         row.rxBytes = rxBytes;
106         row.txBytes = txBytes;
107         mRows.put(uid, row);
108     }
109 
110     /**
111      * Update a row from the qtaguid output to the {@link QtaguidParser}.
112      * It adds rxBytes and txBytes to the previously added row.
113      * contains(uid) should be true before calling this method.
114      *
115      * @param uid The UID from the output
116      * @param rxBytes the number of received bytes
117      * @param txBytes the number of sent bytes
118      * @throws IllegalArgumentException if the given UID is not added
119      */
updateRow(int uid, int rxBytes, int txBytes)120     public void updateRow(int uid, int rxBytes, int txBytes) {
121         if (!mRows.containsKey(uid)) {
122             throw new IllegalArgumentException("Given UID " + uid + " is not added to QtaguidItem");
123         }
124 
125         Row row = mRows.get(uid);
126         row.rxBytes += rxBytes;
127         row.txBytes += txBytes;
128     }
129 
130     /**
131      * Returns if the {@link QtaguidItem} contains a given UID.
132      */
contains(int uid)133     public boolean contains(int uid) {
134         return mRows.containsKey(uid);
135     }
136 
137     /**
138      * Get the number of received bytes for a given UID.
139      */
getRxBytes(int uid)140     public int getRxBytes(int uid) {
141         return mRows.get(uid).rxBytes;
142     }
143 
144     /**
145      * Get the number of sent bytes for a given UID.
146      */
getTxBytes(int uid)147     public int getTxBytes(int uid) {
148         return mRows.get(uid).txBytes;
149     }
150 }
151