function.java 11 KB

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