2
0

function.java 12 KB

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