2
0

MainActivity.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.dev.deben.implementation;
  2. import android.app.ProgressDialog;
  3. import android.graphics.Color;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import org.json.JSONException;
  12. import org.json.simple.JSONObject;
  13. import org.json.simple.JSONValue;
  14. import java.io.IOException;
  15. import java.io.StringWriter;
  16. public class MainActivity extends AppCompatActivity {
  17. function fn = new function(this);
  18. public boolean active = true;
  19. Button accept;
  20. Button deny;
  21. TextView status;
  22. TextView desc;
  23. @Override
  24. public void onBackPressed() {
  25. }
  26. @Override
  27. public void onPause() {
  28. super.onPause();
  29. active = false;
  30. }
  31. @Override
  32. public void onResume() {
  33. super.onResume();
  34. active = true;
  35. accept = (Button) findViewById(R.id.Accept);
  36. deny = (Button) findViewById(R.id.Deny);
  37. status = (TextView) findViewById(R.id.status);
  38. desc = (TextView) findViewById(R.id.descriptor);
  39. try {
  40. if(fn.readSetting("requestId").equals("0")) {
  41. desc.setText(R.string.No_req);
  42. accept.setEnabled(false);
  43. deny.setEnabled(false);
  44. }
  45. else {
  46. desc.setText(R.string.req);
  47. accept.setEnabled(true);
  48. deny.setEnabled(true);
  49. }
  50. } catch (JSONException | IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_main);
  58. accept = (Button) findViewById(R.id.Accept);
  59. deny = (Button) findViewById(R.id.Deny);
  60. status = (TextView) findViewById(R.id.status);
  61. try {
  62. if(fn.readSetting("requestId").equals("0")) {
  63. status.setText("@string/No_req");
  64. accept.setEnabled(false);
  65. deny.setEnabled(false);
  66. }
  67. else {
  68. status.setText("@string/req");
  69. accept.setEnabled(true);
  70. deny.setEnabled(true);
  71. }
  72. } catch (JSONException | IOException e) {
  73. e.printStackTrace();
  74. }
  75. accept.setOnClickListener(new View.OnClickListener() {
  76. @Override
  77. public void onClick(View v) {
  78. try {
  79. buildReply(true, accept, deny);
  80. } catch (JSONException | IOException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. });
  85. deny.setOnClickListener(new View.OnClickListener() {
  86. @Override
  87. public void onClick(View v) {
  88. try {
  89. buildReply(false, accept, deny);
  90. } catch (JSONException | IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. });
  95. }
  96. @Override
  97. public boolean onCreateOptionsMenu(Menu menu) {
  98. menu.add("Unregister");
  99. return super.onCreateOptionsMenu(menu);
  100. }
  101. @Override
  102. public boolean onOptionsItemSelected(MenuItem item) {
  103. System.out.println(item.getItemId());
  104. switch(item.getItemId()) {
  105. case 0:
  106. try {
  107. fn.logout();
  108. } catch (IOException | JSONException e) {
  109. e.printStackTrace();
  110. }
  111. break;
  112. }
  113. return true;
  114. }
  115. private void buildReply(boolean grand, Button accept, Button deny) throws JSONException, IOException {
  116. accept.setEnabled(false);
  117. deny.setEnabled(false);
  118. ProgressDialog progress = new ProgressDialog(this);
  119. progress.setTitle((grand ? "Approving" : "Cancelling") + " request");
  120. progress.setMessage("Processing...");
  121. progress.setCancelable(false);
  122. progress.show();
  123. JSONObject json = new JSONObject();
  124. json.put("function", "confirm");
  125. json.put("requestId", fn.readSetting("requestId"));
  126. json.put("deviceId", fn.readSetting("deviceId"));
  127. json.put("apiKey", fn.readSetting("apiKey"));
  128. json.put("notificationId", fn.readSetting("notificationId"));
  129. json.put("confirmation", grand ? "approved" : "cancelled");
  130. StringWriter out = new StringWriter();
  131. json.writeJSONString(out);
  132. String response = fn.makeRequest("POST", fn.readSetting("serverUrl"), out.toString());
  133. Object obj = JSONValue.parse(response);
  134. JSONObject res = (JSONObject) obj;
  135. if(res.get("result").equals("0") || Integer.parseInt(res.get("result").toString()) == 0) {
  136. status.setText("VPN request successfully " + (grand ? "APPROVED" : "CANCELLED"));
  137. if(grand) {
  138. status.setTextColor(Color.parseColor("#04ff00"));
  139. }
  140. else {
  141. status.setTextColor(Color.parseColor("#DF7401"));
  142. }
  143. fn.writeSetting("requestId", "0");
  144. }
  145. else {
  146. status.setText("Something went wrong, " + res.get("result").toString() + "\n" + res.get("resultText").toString());
  147. status.setTextColor(Color.parseColor("#ff1700"));
  148. }
  149. status.setVisibility(View.VISIBLE);
  150. accept.setEnabled(true);
  151. deny.setEnabled(true);
  152. progress.dismiss();
  153. }
  154. }