PageRenderTime 2001ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/app/src/main/java/com/slyotis/peakq/data/PQServerOld.java

https://gitlab.com/SlyOtis/PeakQ
Java | 369 lines | 312 code | 52 blank | 5 comment | 25 complexity | c1e94faf79d826dd0050fdb9c185768f MD5 | raw file
  1. package com.slyotis.peakq.data;
  2. import android.app.Activity;
  3. import android.util.Log;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import okhttp3.Call;
  7. import okhttp3.FormBody;
  8. import okhttp3.HttpUrl;
  9. import okhttp3.OkHttpClient;
  10. import okhttp3.Request;
  11. import okhttp3.Response;
  12. /**
  13. * Created by slyotis on 23.09.16.
  14. */
  15. @Deprecated
  16. public class PQServerOld {
  17. private static final String TAG = PQServerOld.class.getName();
  18. private static final String SOURCE = "http://peakq.no/peakq/dev.php";
  19. public static final int ERROR_DUPLICATE_EMAIL = 0;
  20. public static final int ERROR_DUPLICATE_PHONE = 1;
  21. public static final int ERROR_BAD_CONNECTION = 3;
  22. public static final int ERROR_UNEXPECTED = 4;
  23. public static final int ERROR_NO_MATCH = 5;
  24. public static final int ERROR_DUPLICATE_NAME = 6;
  25. public static final int ERROR_NO_IMAGE = 7;
  26. public interface Callback{
  27. void onFailure(int errorCode);
  28. void onSuccess(int requestId, final String response);
  29. }
  30. public interface ImageCallback{
  31. void onFailure(int errorCode);
  32. void onSuccess(int requestId, final InputStream response);
  33. }
  34. private static void post(FormBody body, okhttp3.Callback callback){
  35. HttpUrl.Builder urlBuilder = HttpUrl.parse(SOURCE).newBuilder();
  36. final Request request = new Request.Builder().url(urlBuilder.build())
  37. .post(body).build();
  38. OkHttpClient client = new OkHttpClient();
  39. client.newCall(request).enqueue(callback);
  40. }
  41. private static void get(okhttp3.Callback callback, String[] ... parms){
  42. HttpUrl.Builder urlBuilder = HttpUrl.parse(SOURCE).newBuilder();
  43. for(String[] param: parms){
  44. urlBuilder.addQueryParameter(param[0], param[1]);
  45. }
  46. final Request request = new Request.Builder().url(urlBuilder.build())
  47. .get().build();
  48. OkHttpClient client = new OkHttpClient();
  49. client.newCall(request).enqueue(callback);
  50. }
  51. private static abstract class ServerCallback implements okhttp3.Callback{
  52. private Callback callback;
  53. private Activity activity;
  54. private int requestId;
  55. public ServerCallback(int requestId, Callback callback, Activity activity) {
  56. this.callback = callback;
  57. this.activity = activity;
  58. this.requestId = requestId;
  59. }
  60. @Override
  61. public void onFailure(Call call, IOException e) {
  62. Log.d(TAG, e.getMessage());
  63. activity.runOnUiThread(new Runnable() {
  64. @Override
  65. public void run() {
  66. callback.onFailure(ERROR_BAD_CONNECTION);
  67. }
  68. });
  69. }
  70. @Override
  71. public void onResponse(Call call, Response response) throws IOException {
  72. if(!response.isSuccessful()){
  73. Log.d(TAG, response.body().string());
  74. activity.runOnUiThread(new Runnable() {
  75. @Override
  76. public void run() {
  77. callback.onFailure(ERROR_BAD_CONNECTION);
  78. }
  79. });
  80. } else {
  81. final String r = response.body().string();
  82. Log.d(TAG, "Testing response: " + r);
  83. if(checkResponse(r)){
  84. activity.runOnUiThread(new Runnable() {
  85. @Override
  86. public void run() {
  87. callback.onSuccess(requestId, r);
  88. }
  89. });
  90. return;
  91. } else Log.d(TAG, r);
  92. }
  93. }
  94. // Must override
  95. public abstract boolean checkResponse(String response);
  96. }
  97. private static class ServerImageCallback implements okhttp3.Callback{
  98. private ImageCallback callback;
  99. private Activity activity;
  100. private int requestId;
  101. public ServerImageCallback(int requestId, ImageCallback callback, Activity activity) {
  102. this.callback = callback;
  103. this.activity = activity;
  104. this.requestId = requestId;
  105. }
  106. @Override
  107. public void onFailure(Call call, IOException e) {
  108. Log.d(TAG, e.getMessage());
  109. activity.runOnUiThread(new Runnable() {
  110. @Override
  111. public void run() {
  112. callback.onFailure(ERROR_BAD_CONNECTION);
  113. }
  114. });
  115. }
  116. @Override
  117. public void onResponse(Call call, Response response) throws IOException {
  118. if(!response.isSuccessful()){
  119. Log.d(TAG, response.body().string());
  120. activity.runOnUiThread(new Runnable() {
  121. @Override
  122. public void run() {
  123. callback.onFailure(ERROR_BAD_CONNECTION);
  124. }
  125. });
  126. } else {
  127. final InputStream r = response.body().byteStream();
  128. activity.runOnUiThread(new Runnable() {
  129. @Override
  130. public void run() {
  131. callback.onSuccess(requestId, r);
  132. }
  133. });
  134. }
  135. }
  136. }
  137. public static void newUser(PQUser user, final Callback callback, final Activity activity){
  138. FormBody.Builder builder = new FormBody.Builder();
  139. builder.add("cmd", "newuser");
  140. builder.add("firstName", user.firstName);
  141. builder.add("lastName", user.lastName);
  142. builder.add("phone", user.phone);
  143. builder.add("email", user.email);
  144. builder.add("password", user.password);
  145. post(builder.build(), new ServerCallback(user.id, callback, activity){
  146. @Override
  147. public boolean checkResponse(String response) {
  148. if(response.contains("!DuplEmail")){
  149. callback.onFailure(ERROR_DUPLICATE_EMAIL);
  150. return false;
  151. } else if(response.contains("!DuplPhone")){
  152. callback.onFailure(ERROR_DUPLICATE_PHONE);
  153. return false;
  154. }
  155. return true;
  156. }
  157. });
  158. }
  159. public static void login(String username, String password, final Callback callback,
  160. final Activity activity){
  161. FormBody.Builder builder = new FormBody.Builder();
  162. builder.add("cmd", "login");
  163. builder.add("username", username);
  164. builder.add("password", password);
  165. post(builder.build(), new ServerCallback(-1, callback, activity){
  166. @Override
  167. public boolean checkResponse(String response) {
  168. if(response.contains("!Wrong")){
  169. callback.onFailure(ERROR_NO_MATCH);
  170. return false;
  171. }
  172. else return true;
  173. }
  174. });
  175. }
  176. //TODO: Must figure out
  177. public static void newMessage(){
  178. }
  179. public static void newGroup(int userid, String password, String name, String about,
  180. final Callback callback, final Activity activity){
  181. FormBody.Builder builder = new FormBody.Builder();
  182. builder.add("cmd", "newgroup");
  183. builder.add("userid", String.valueOf(userid));
  184. builder.add("password", password);
  185. builder.add("name", name);
  186. builder.add("about", about);
  187. post(builder.build(), new ServerCallback(userid, callback, activity){
  188. @Override
  189. public boolean checkResponse(String response) {
  190. if(response.contains("!Wrong")){
  191. activity.runOnUiThread(new Runnable() {
  192. @Override
  193. public void run() {
  194. callback.onFailure(ERROR_UNEXPECTED);
  195. }
  196. });
  197. return false;
  198. }
  199. if(response.contains("!DuplName")){
  200. activity.runOnUiThread(new Runnable() {
  201. @Override
  202. public void run() {
  203. callback.onFailure(ERROR_DUPLICATE_NAME);
  204. }
  205. });
  206. return false;
  207. }
  208. else return true;
  209. }
  210. });
  211. }
  212. public static void updateMemberships(int userid, String password, final Callback callback,
  213. final Activity activity){
  214. FormBody.Builder builder = new FormBody.Builder();
  215. builder.add("cmd", "memberships");
  216. builder.add("userid", String.valueOf(userid));
  217. builder.add("password", password);
  218. post(builder.build(), new ServerCallback(userid, callback, activity){
  219. @Override
  220. public boolean checkResponse(String response) {
  221. if(response.contains("!Error")){
  222. callback.onFailure(ERROR_UNEXPECTED);
  223. return false;
  224. }
  225. else return true;
  226. }
  227. });
  228. }
  229. public static void updateMemberships(int userid, String password, int groupid
  230. , boolean member, boolean push, final Callback callback, final Activity activity){
  231. FormBody.Builder builder = new FormBody.Builder();
  232. builder.add("cmd", "memberships");
  233. builder.add("userid", String.valueOf(userid));
  234. builder.add("password", password);
  235. builder.add("groupid", String.valueOf(groupid));
  236. builder.add("change", member? "1" : "-1");
  237. builder.add("push", push? "true" : "");
  238. post(builder.build(), new ServerCallback(groupid, callback, activity){
  239. @Override
  240. public boolean checkResponse(String response) {
  241. if(response.contains("!Error")){
  242. callback.onFailure(ERROR_UNEXPECTED);
  243. return false;
  244. }
  245. else return true;
  246. }
  247. });
  248. }
  249. public static void listGroups(int userid, String password, final Callback callback,
  250. final Activity activity){
  251. FormBody.Builder builder = new FormBody.Builder();
  252. builder.add("cmd", "listgroups");
  253. builder.add("userid", String.valueOf(userid));
  254. builder.add("password", password);
  255. post(builder.build(), new ServerCallback(userid, callback, activity){
  256. @Override
  257. public boolean checkResponse(String response) {
  258. if(response.contains("!Error")){
  259. callback.onFailure(ERROR_UNEXPECTED);
  260. return false;
  261. }
  262. else return true;
  263. }
  264. });
  265. }
  266. public static void listMessages(int userid, String password, final Callback callback,
  267. final Activity activity){
  268. FormBody.Builder builder = new FormBody.Builder();
  269. builder.add("cmd", "listmessages");
  270. builder.add("userid", String.valueOf(userid));
  271. builder.add("password", password);
  272. post(builder.build(), new ServerCallback(userid, callback, activity){
  273. @Override
  274. public boolean checkResponse(String response) {
  275. if(response.contains("!Error")){
  276. callback.onFailure(ERROR_UNEXPECTED);
  277. return false;
  278. }
  279. else return true;
  280. }
  281. });
  282. }
  283. public static void deleteMessage(int userid, String password, int messageid,
  284. final Callback callback, final Activity activity){
  285. FormBody.Builder builder = new FormBody.Builder();
  286. builder.add("cmd", "deletemessage");
  287. builder.add("userid", String.valueOf(userid));
  288. builder.add("password", password);
  289. builder.add("messageid", String.valueOf(messageid));
  290. post(builder.build(), new ServerCallback(messageid, callback, activity){
  291. @Override
  292. public boolean checkResponse(String response) {
  293. if(response.contains("!Error")){
  294. callback.onFailure(ERROR_UNEXPECTED);
  295. return false;
  296. }
  297. else return true;
  298. }
  299. });
  300. }
  301. public static void getImage(int imageId, final ImageCallback callback, final Activity activity){
  302. get(new ServerImageCallback(imageId, callback, activity), new String[]{"img", String.valueOf(imageId)});
  303. }
  304. public static Response getImage(int imageId){
  305. HttpUrl.Builder urlBuilder = HttpUrl.parse(SOURCE).newBuilder();
  306. urlBuilder.addQueryParameter("img", imageId + "");
  307. final Request request = new Request.Builder().url(urlBuilder.build())
  308. .get().build();
  309. OkHttpClient client = new OkHttpClient();
  310. try{
  311. return client.newCall(request).execute();
  312. }catch (IOException ex){
  313. Log.d(TAG, ex.getMessage());
  314. }
  315. return null;
  316. }
  317. }