Profile.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package de.tudarmstadt.informatik.hostage.model;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.drawable.BitmapDrawable;
  9. import android.graphics.drawable.Drawable;
  10. import android.os.Bundle;
  11. import android.os.Parcel;
  12. import android.os.Parcelable;
  13. import java.io.Serializable;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16. import java.util.LinkedList;
  17. import java.util.List;
  18. import java.util.Map;
  19. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  20. /**
  21. * @author Alexander Brakowski
  22. * @created 14.01.14 18:04
  23. */
  24. public class Profile implements JSONSerializable<Profile> {
  25. public String mText;
  26. public String mLabel;
  27. public int mId;
  28. public boolean mActivated;
  29. transient public Bitmap mIcon;
  30. transient public int mIconId;
  31. public String mIconName;
  32. public String mIconPath;
  33. public boolean mEditable = false;
  34. public boolean mIsRandom = false;
  35. public HashMap<String, Boolean> mActiveProtocols = new HashMap<String, Boolean>();
  36. public String mGhostPorts = "";
  37. public boolean mGhostActive = false;
  38. public boolean mShowTooltip = false;
  39. public Profile(){
  40. this.mEditable = true;
  41. this.mActivated = false;
  42. this.mId = -1;
  43. }
  44. public Profile(int id, String label, String text, Bitmap icon, boolean editable){
  45. this.mId = id;
  46. this.mLabel = text;
  47. this.mText = label;
  48. this.mActivated = false;
  49. this.mIcon = icon;
  50. this.mEditable = editable;
  51. }
  52. public Profile(int id, String label, String text, int icon, boolean editable){
  53. this(id, text, label, BitmapFactory.decodeResource(MainActivity.context.getResources(), icon), editable);
  54. mIconId = icon;
  55. mIconName = MainActivity.context.getResources().getResourceName(icon);
  56. }
  57. public Profile(int id, String label, String text, String iconPath, boolean editable){
  58. this.mId = id;
  59. this.mLabel = label;
  60. this.mText = text;
  61. this.mActivated = false;
  62. this.mIconPath = iconPath;
  63. this.mEditable = editable;
  64. }
  65. public Profile(Parcel in) {
  66. mText = in.readString();
  67. mLabel = in.readString();
  68. mId = in.readInt();
  69. mActivated = in.readInt() == 1;
  70. mIconId = in.readInt();
  71. mIconName = in.readString();
  72. mIconPath = in.readString();
  73. mEditable = in.readInt() == 1;
  74. mActiveProtocols = (HashMap<String,Boolean>) in.readSerializable();
  75. }
  76. public void setIcon(Bitmap bitmap){
  77. this.mIcon = bitmap;
  78. }
  79. public void setIcon(int icon){
  80. this.mIcon = BitmapFactory.decodeResource(MainActivity.context.getResources(), icon);
  81. }
  82. public Bitmap getIconBitmap(){
  83. if(this.mIcon != null) return mIcon;
  84. if(this.mIconId != 0){
  85. this.mIcon = BitmapFactory.decodeResource(MainActivity.context.getResources(), mIconId);
  86. return this.mIcon;
  87. }
  88. if(this.mIconName != null && !this.mIconName.isEmpty()){
  89. this.mIconId = MainActivity.context.getResources().getIdentifier(this.mIconName,
  90. "drawable", "de.tudarmstadt.informatik.hostage");
  91. this.mIcon = BitmapFactory.decodeResource(MainActivity.context.getResources(), this.mIconId);
  92. return this.mIcon;
  93. }
  94. if(this.mIconPath != null){
  95. BitmapFactory.Options options = new BitmapFactory.Options();
  96. options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  97. Bitmap bitmap = BitmapFactory.decodeFile(this.mIconPath, options);
  98. return bitmap;
  99. }
  100. return null;
  101. }
  102. public boolean isProtocolActive(String protocol){
  103. if(!mActiveProtocols.containsKey(protocol)) return false;
  104. return mActiveProtocols.get(protocol);
  105. }
  106. public List<String> getActiveProtocols(){
  107. List<String> list = new LinkedList<String>();
  108. for(Map.Entry<String, Boolean> entry: this.mActiveProtocols.entrySet()){
  109. if(entry.getValue()){
  110. list.add(entry.getKey());
  111. }
  112. }
  113. return list;
  114. }
  115. public Drawable getIconDrawable(){
  116. return new BitmapDrawable(MainActivity.context.getResources(), getIconBitmap());
  117. }
  118. public boolean isEditable(){
  119. return this.mEditable;
  120. }
  121. public Profile cloneProfile(){
  122. return new Profile(mId, mLabel, mText, mIcon, mEditable);
  123. }
  124. public Integer[] getGhostPorts(){
  125. String[] splits = this.mGhostPorts.split(",");
  126. Integer[] ports = new Integer[splits.length];
  127. for(int i=0; i<splits.length; i++){
  128. if(!splits[i].equals("")) {
  129. ports[i] = Integer.valueOf(splits[i]);
  130. }
  131. }
  132. return ports;
  133. }
  134. public JSONObject toJSON(){
  135. JSONObject jsonObj = new JSONObject();
  136. try {
  137. jsonObj.put("text", mText);
  138. jsonObj.put("label", mLabel);
  139. jsonObj.put("id", mId);
  140. jsonObj.put("activated", mActivated);
  141. jsonObj.put("icon_name", mIconName);
  142. jsonObj.put("icon_path", mIconPath);
  143. jsonObj.put("editable", mEditable);
  144. jsonObj.put("random", mIsRandom);
  145. jsonObj.put("ghost_active", mGhostActive);
  146. jsonObj.put("ghost_ports", mGhostPorts);
  147. jsonObj.put("active_protocols", new JSONObject(mActiveProtocols));
  148. } catch (JSONException e) {
  149. e.printStackTrace();
  150. }
  151. return jsonObj;
  152. }
  153. public Profile fromJSON(JSONObject json){
  154. mText = json.optString("text", "");
  155. mLabel = json.optString("label", "");
  156. mId = json.optInt("id", -1);
  157. mActivated = json.optBoolean("activated", false);
  158. mIconName = json.optString("icon_name", null);
  159. mIconPath = json.optString("icon_path", null);
  160. mEditable = json.optBoolean("editable", true);
  161. mIsRandom = json.optBoolean("random", false);
  162. mGhostActive = json.optBoolean("ghost_active", false);
  163. mGhostPorts = json.optString("ghost_ports", "");
  164. JSONObject activeProtocols = json.optJSONObject("active_protocols");
  165. if(activeProtocols != null){
  166. Iterator keys = activeProtocols.keys();
  167. while(keys.hasNext()){
  168. String protocol = (String) keys.next();
  169. try {
  170. mActiveProtocols.put(protocol, activeProtocols.getBoolean(protocol));
  171. } catch (JSONException e) {
  172. e.printStackTrace();
  173. }
  174. }
  175. }
  176. return this;
  177. }
  178. }