MainActivity.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.ProgressDialog;
  10. import android.graphics.Color;
  11. import android.os.Bundle;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.TextView;
  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.IOException;
  23. import cz.msebera.android.httpclient.Header;
  24. public class MainActivity extends AppCompatActivity {
  25. function fn = new function(this);
  26. public boolean active = true;
  27. Button accept;
  28. Button deny;
  29. TextView status;
  30. TextView desc;
  31. boolean opt;
  32. @Override
  33. public void onBackPressed() {
  34. return;
  35. }
  36. @Override
  37. public void onPause() {
  38. super.onPause();
  39. active = false;
  40. }
  41. @Override
  42. public void onResume() {
  43. super.onResume();
  44. active = true;
  45. accept = (Button) findViewById(R.id.Accept);
  46. deny = (Button) findViewById(R.id.Deny);
  47. status = (TextView) findViewById(R.id.status);
  48. desc = (TextView) findViewById(R.id.descriptor);
  49. try {
  50. if (fn.readSetting("requestId").equals("0")) {
  51. desc.setText(R.string.No_req);
  52. accept.setEnabled(false);
  53. deny.setEnabled(false);
  54. } else {
  55. desc.setText(R.string.req);
  56. accept.setEnabled(true);
  57. deny.setEnabled(true);
  58. }
  59. } catch (JSONException | IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.activity_main);
  67. accept = (Button) findViewById(R.id.Accept);
  68. deny = (Button) findViewById(R.id.Deny);
  69. status = (TextView) findViewById(R.id.status);
  70. try {
  71. if (fn.readSetting("requestId").equals("0")) {
  72. status.setText("@string/No_req");
  73. accept.setEnabled(false);
  74. deny.setEnabled(false);
  75. } else {
  76. status.setText("@string/req");
  77. accept.setEnabled(true);
  78. deny.setEnabled(true);
  79. }
  80. } catch (JSONException | IOException e) {
  81. e.printStackTrace();
  82. }
  83. accept.setOnClickListener(new View.OnClickListener() {
  84. @Override
  85. public void onClick(View v) {
  86. try {
  87. opt = true;
  88. buildReply(true, accept, deny);
  89. } catch (JSONException | IOException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. });
  94. deny.setOnClickListener(new View.OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97. try {
  98. opt = false;
  99. buildReply(false, accept, deny);
  100. } catch (JSONException | IOException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. });
  105. }
  106. @Override
  107. public boolean onCreateOptionsMenu(Menu menu) {
  108. menu.add("Unregister");
  109. return super.onCreateOptionsMenu(menu);
  110. }
  111. @Override
  112. public boolean onOptionsItemSelected(MenuItem item) {
  113. final ProgressDialog progress = new ProgressDialog(this);
  114. System.out.println(item.getItemId());
  115. switch (item.getItemId()) {
  116. case 0:
  117. try {
  118. JSONObject json = new JSONObject();
  119. json.put("function", "unregister");
  120. json.put("username", fn.readSetting("username"));
  121. json.put("password", fn.readSetting("password"));
  122. json.put("registerCode", fn.readSetting("registerCode"));
  123. json.put("requestId", "0");
  124. fn.makeRequest(fn.readSetting("serverUrl"), json.toJSONString(), new AsyncHttpResponseHandler() {
  125. @Override
  126. public void onFailure(int code, Header[] headers, byte[] responseBody, Throwable error) {
  127. status.setTextColor(Color.parseColor("#ff1700"));
  128. status.setText("HTTP ERROR: " + code);
  129. status.setVisibility(View.VISIBLE);
  130. progress.dismiss();
  131. }
  132. @Override
  133. public void onSuccess(int code, Header[] headers, byte[] responseBody) {
  134. String res = new String(responseBody);
  135. Object obj = JSONValue.parse(res);
  136. JSONObject response = (JSONObject) obj;
  137. if (response.get("result") != null && response.get("result").equals("0") || Integer.parseInt(response.get("result").toString()) == 0) {
  138. status.setTextColor(Color.parseColor("#04ff00"));
  139. status.setText("SUCCESS");
  140. try {
  141. fn.logout();
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. }
  145. } else {
  146. status.setText(response.get("result").toString() + "\n" + response.get("resultText").toString());
  147. status.setTextColor(Color.parseColor("#ff1700"));
  148. }
  149. status.setVisibility(View.VISIBLE);
  150. progress.dismiss();
  151. fn.cancelNotify();
  152. }
  153. });
  154. break;
  155. } catch (IOException | JSONException e) {
  156. e.printStackTrace();
  157. }
  158. return true;
  159. }
  160. return true;
  161. }
  162. private void buildReply(boolean grand, Button accept, Button deny) throws JSONException, IOException {
  163. accept.setEnabled(false);
  164. deny.setEnabled(false);
  165. final ProgressDialog progress = new ProgressDialog(this);
  166. progress.setTitle((grand ? "Approving" : "Cancelling") + " request");
  167. progress.setMessage("Processing...");
  168. progress.setCancelable(false);
  169. progress.show();
  170. JSONObject json = new JSONObject();
  171. json.put("function", "confirm");
  172. json.put("requestId", fn.readSetting("requestId"));
  173. json.put("deviceId", fn.readSetting("deviceId"));
  174. json.put("apiKey", fn.readSetting("apiKey"));
  175. json.put("notificationId", fn.readSetting("notificationId"));
  176. json.put("confirmation", grand ? "approved" : "cancelled");
  177. fn.makeRequest(fn.readSetting("serverUrl"), json.toJSONString(), new AsyncHttpResponseHandler() {
  178. @Override
  179. public void onFailure(int code, Header[] headers, byte[] responseBody, Throwable error) {
  180. status.setTextColor(Color.parseColor("#ff1700"));
  181. status.setText("HTTP ERROR: " + code);
  182. status.setVisibility(View.VISIBLE);
  183. progress.dismiss();
  184. }
  185. @Override
  186. public void onSuccess(int code, Header[] headers, byte[] responseBody) {
  187. String res = new String(responseBody);
  188. Object obj = JSONValue.parse(res);
  189. JSONObject response = (JSONObject) obj;
  190. if (response.get("result") != null && response.get("result").equals("0") || Integer.parseInt(response.get("result").toString()) == 0) {
  191. status.setTextColor(Color.parseColor("#04ff00"));
  192. status.setText("SUCCESS");
  193. try {
  194. fn.writeSetting("requestId", "0");
  195. } catch (IOException | JSONException e) {
  196. e.printStackTrace();
  197. }
  198. finish();
  199. startActivity(getIntent());
  200. } else {
  201. status.setText(response.get("result").toString() + "\n" + response.get("resultText").toString());
  202. status.setTextColor(Color.parseColor("#ff1700"));
  203. }
  204. status.setVisibility(View.VISIBLE);
  205. progress.dismiss();
  206. fn.cancelNotify();
  207. }
  208. });
  209. }
  210. }