2
0

function.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Feel free to copy/use it for your own project.
  3. * Keep in mind that it took me several days/weeks, beers and asperines to make this.
  4. * So be nice, and give me some credit, I won't bite and it won't hurt you.
  5. *
  6. * Created by Deben Oldert
  7. */
  8. package com.dev.deben.implementation;
  9. import android.app.Notification;
  10. import android.app.NotificationManager;
  11. import android.app.PendingIntent;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.os.PowerManager;
  15. import android.provider.Settings.Secure;
  16. import android.support.v4.app.NotificationCompat;
  17. import com.loopj.android.http.AsyncHttpClient;
  18. import com.loopj.android.http.AsyncHttpResponseHandler;
  19. import org.json.JSONException;
  20. import org.json.simple.JSONObject;
  21. import org.json.simple.JSONValue;
  22. import java.io.BufferedReader;
  23. import java.io.DataOutputStream;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.io.OutputStreamWriter;
  30. import java.io.StringWriter;
  31. import java.net.HttpURLConnection;
  32. import java.net.URL;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. import cz.msebera.android.httpclient.entity.StringEntity;
  36. public class function {
  37. private static Context ctx;
  38. private static String settings;
  39. private static String file = "settings.json";
  40. function(Context init) {
  41. ctx = init;
  42. }
  43. public boolean writeSetting(HashMap<String, String> set) throws IOException, JSONException {
  44. String jsonText = settingFile();
  45. if(jsonText != null) {
  46. Object obj = JSONValue.parse(jsonText);
  47. JSONObject json = (JSONObject) obj;
  48. for(Map.Entry<String, String> entry : set.entrySet()) {
  49. json.put(entry.getKey(), entry.getValue());
  50. }
  51. FileOutputStream fout = ctx.openFileOutput(file, Context.MODE_PRIVATE);
  52. OutputStreamWriter outwr = new OutputStreamWriter(fout);
  53. StringWriter out = new StringWriter();
  54. json.writeJSONString(out);
  55. String txt = out.toString();
  56. settings = txt;
  57. outwr.write(txt);
  58. outwr.close();
  59. return true;
  60. }
  61. else{
  62. return false;
  63. }
  64. }
  65. public boolean writeSetting(String key, String value) throws IOException, JSONException {
  66. String jsonText = settingFile();
  67. if(jsonText != null) {
  68. Object obj = JSONValue.parse(jsonText);
  69. JSONObject json = (JSONObject) obj;
  70. json.put(key, value);
  71. FileOutputStream fout = ctx.openFileOutput(file, Context.MODE_PRIVATE);
  72. OutputStreamWriter outwr = new OutputStreamWriter(fout);
  73. StringWriter out = new StringWriter();
  74. json.writeJSONString(out);
  75. String txt = out.toString();
  76. settings = txt;
  77. outwr.write(txt);
  78. outwr.close();
  79. return true;
  80. }
  81. else{
  82. return false;
  83. }
  84. }
  85. public void init() throws IOException, JSONException {
  86. settings = settingFile();
  87. }
  88. public String readSetting(String key) throws JSONException, IOException {
  89. Object obj = JSONValue.parse((settings != null) ? settings:settingFile());
  90. JSONObject json = (JSONObject) obj;
  91. String value = null;
  92. if (json.get(key) != null) {
  93. value = (String) json.get(key);
  94. }
  95. return value;
  96. }
  97. public void logout() throws IOException {
  98. System.out.println("Logging out");
  99. File del = new File(ctx.getFilesDir().getAbsolutePath(), file);
  100. del.delete();
  101. redirect("Login");
  102. }
  103. private String settingFile() throws IOException, JSONException {
  104. String jsonStr;
  105. System.out.println("Trying to read settings");
  106. try {
  107. FileInputStream fin = ctx.openFileInput(file);
  108. BufferedReader rd = new BufferedReader(new InputStreamReader(fin));
  109. StringBuilder sb = new StringBuilder();
  110. String line;
  111. while ((line = rd.readLine()) != null) {
  112. sb.append(line);
  113. }
  114. jsonStr = sb.toString();
  115. rd.close();
  116. fin.close();
  117. System.out.println("Read settings");
  118. }
  119. catch(IOException e){
  120. FileOutputStream fout = ctx.openFileOutput(file, Context.MODE_PRIVATE);
  121. OutputStreamWriter outwr = new OutputStreamWriter(fout);
  122. JSONObject json = new JSONObject();
  123. json.put("serviceType", "GCM");
  124. json.put("serviceNumber", "454250381809");
  125. json.put("notificationId", "");
  126. json.put("deviceId", Secure.getString(ctx.getContentResolver(), Secure.ANDROID_ID));
  127. json.put("username", "");
  128. json.put("password", "");
  129. json.put("registerCode", "");
  130. json.put("apiKey", "AIzaSyB67KpF-KSuZoPdnuy03TEIKRjHkBLEPpM");
  131. json.put("serverUrl", "http://192.168.2.240:8080/Implementation/SAS");
  132. json.put("requestId", "0");
  133. StringWriter out = new StringWriter();
  134. json.writeJSONString(out);
  135. jsonStr = out.toString();
  136. outwr.write(jsonStr);
  137. outwr.close();
  138. }
  139. System.out.println("return settings: " + jsonStr);
  140. return jsonStr;
  141. }
  142. private int getCharCode(String letter) {
  143. letter = letter.toLowerCase();
  144. switch(letter) {
  145. case "a":
  146. return 1;
  147. case "b":
  148. return 2;
  149. case "c":
  150. return 3;
  151. case "d":
  152. return 4;
  153. case "e":
  154. return 5;
  155. case "f":
  156. return 6;
  157. case "g":
  158. return 7;
  159. case "h":
  160. return 8;
  161. case "i":
  162. return 9;
  163. case "j":
  164. return 10;
  165. case "k":
  166. return 11;
  167. case "l":
  168. return 12;
  169. case "m":
  170. return 13;
  171. case "n":
  172. return 14;
  173. case "o":
  174. return 15;
  175. case "p":
  176. return 16;
  177. case "q":
  178. return 17;
  179. case "r":
  180. return 18;
  181. case "s":
  182. return 19;
  183. case "t":
  184. return 20;
  185. case "u":
  186. return 21;
  187. case "v":
  188. return 22;
  189. case "w":
  190. return 23;
  191. case "x":
  192. return 24;
  193. case "y":
  194. return 25;
  195. case "z":
  196. return 26;
  197. default:
  198. return 0;
  199. }
  200. }
  201. public String genRegCode(String str) {
  202. String first = str.substring(0, 1);
  203. String last = str.substring(str.length()-1);
  204. int firstCode = getCharCode(first);
  205. int lastCode = getCharCode(last);
  206. System.out.println("first: "+first+", "+firstCode);
  207. System.out.println("first: "+last+", "+lastCode);
  208. if(firstCode < 27 && firstCode > 0) {
  209. first = ""+firstCode;
  210. }
  211. else {
  212. return null;
  213. }
  214. if(lastCode < 27 && lastCode > 0) {
  215. last = ""+lastCode;
  216. }
  217. else {
  218. return null;
  219. }
  220. if(first.length() < 2) {
  221. first = "0"+first;
  222. }
  223. if(last.length() < 2) {
  224. last = "0"+last;
  225. }
  226. return first+last;
  227. }
  228. public boolean checkRegCode(String code, String str) {
  229. String newCode = genRegCode(str);
  230. return code.equals(newCode);
  231. }
  232. public void makeRequest(String url, String body, AsyncHttpResponseHandler handle) throws IOException {
  233. AsyncHttpClient client = new AsyncHttpClient();
  234. client.post(ctx, url, new StringEntity(body), "application/json", handle);
  235. }
  236. public void redirect(String act) {
  237. Intent intent;
  238. switch(act) {
  239. case "Main":
  240. intent = new Intent(ctx, MainActivity.class);
  241. break;
  242. case "Login":
  243. intent = new Intent(ctx, LoginActivity.class);
  244. break;
  245. default:
  246. intent = new Intent(ctx, LoginActivity.class);
  247. }
  248. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  249. ctx.startActivity(intent);
  250. }
  251. public int notifier(String title, String msg) {
  252. System.out.println("BUILDING NOTIFICATION");
  253. NotificationManager nMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
  254. PendingIntent pi = PendingIntent.getActivity(ctx, 0, new Intent(ctx, MainActivity.class), 0);
  255. PowerManager pm = (PowerManager)ctx.getSystemService(Context.POWER_SERVICE);
  256. boolean active = pm.isInteractive();
  257. PowerManager.WakeLock wl;
  258. if(!active) {
  259. wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "NOTIFY");
  260. wl.acquire(30000);
  261. }
  262. else {
  263. redirect("Main");
  264. }
  265. int id = (int)System.currentTimeMillis();
  266. Notification mBuilder = new NotificationCompat.Builder(ctx)
  267. .setSmallIcon(R.drawable.action_logo)
  268. .setContentTitle(title)
  269. .setContentText(msg)
  270. .setAutoCancel(true)
  271. .setContentIntent(pi)
  272. .setCategory(Notification.CATEGORY_CALL)
  273. .build();
  274. mBuilder.defaults |= Notification.DEFAULT_VIBRATE;
  275. mBuilder.defaults |= Notification.DEFAULT_SOUND;
  276. nMgr.notify(id, mBuilder);
  277. return id;
  278. }
  279. public void cancelNotify(int id) {
  280. NotificationManager nMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
  281. nMgr.cancel(id);
  282. }
  283. public void cancelNotify() {
  284. NotificationManager nMgr = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
  285. nMgr.cancelAll();
  286. }
  287. }