2
0

MainActivity.java 6.6 KB

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