Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.11 KB | None | 0 0
  1. package com.service.restaurant;
  2.  
  3. import android.accounts.Account;
  4. import android.accounts.AccountManager;
  5. import android.app.ProgressDialog;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9.  
  10. import android.content.pm.PackageInfo;
  11. import android.content.pm.PackageManager;
  12. import android.os.Bundle;
  13. import android.provider.Settings;
  14. import android.support.annotation.Nullable;
  15. import android.support.v4.app.ActivityCompat;
  16. import android.support.v7.app.AlertDialog;
  17. import android.support.v7.app.AppCompatActivity;
  18. import android.text.TextUtils;
  19. import android.util.Log;
  20. import android.util.SparseArray;
  21. import android.view.View;
  22. import android.widget.AutoCompleteTextView;
  23. import android.widget.Button;
  24. import android.widget.EditText;
  25. import android.widget.Toast;
  26.  
  27.  
  28. import com.google.firebase.iid.FirebaseInstanceId;
  29. import com.service.restaurant.clients.Client;
  30. import com.service.restaurant.clients.ClientBase;
  31. import com.service.restaurant.view.Hall;
  32. import com.service.restaurant.view.TablesList;
  33. import com.service.restaurant.view.elements.Table;
  34.  
  35. import org.json.JSONArray;
  36. import org.json.JSONObject;
  37.  
  38. import java.io.BufferedReader;
  39. import java.io.DataOutputStream;
  40. import java.io.IOException;
  41. import java.io.InputStreamReader;
  42. import java.net.HttpURLConnection;
  43. import java.net.SocketTimeoutException;
  44. import java.net.URL;
  45. import java.net.URLDecoder;
  46. import java.util.ArrayList;
  47. import java.util.Calendar;
  48. import java.util.Date;
  49. import java.util.UUID;
  50.  
  51.  
  52. public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
  53.  
  54. private AutoCompleteTextView login;
  55. private EditText password;
  56. private ProgressDialog progressDialog;
  57.  
  58.  
  59. @Override
  60. protected void onCreate(@Nullable Bundle savedInstanceState) {
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.activity_login);
  63.  
  64. Data.hallList = new SparseArray<>();
  65. Data.clientList = new ArrayList<>();
  66. ClientBase.list = new ArrayList<>();
  67.  
  68. Calendar c = Calendar.getInstance();
  69. c.set(Calendar.HOUR_OF_DAY, 0);
  70. c.set(Calendar.MINUTE, 0);
  71. c.set(Calendar.SECOND, 0);
  72. c.set(Calendar.MILLISECOND, 0);
  73. Utils.SELECTED_DATE = c.getTime();
  74.  
  75. // if (sharedPreferences.getString("unique_id", "").equals("")) {
  76. // createUniqueId();
  77. // }
  78.  
  79. // ClientBase.fillClientBase();
  80.  
  81. // String token = FirebaseInstanceId.getInstance().getToken();
  82. // Log.e("myLogs", " token " + token);
  83. Log.e("myLogs", " login " + AppPref.getInstance().getLogin());
  84.  
  85. long date = AppPref.getInstance().getSavedDate();
  86. if (date == -1) {
  87. AppPref.getInstance().saveDate();
  88. }
  89.  
  90. if (AppPref.getInstance().isLogin()) {
  91. getDataFromServer();
  92. }
  93.  
  94. login = (AutoCompleteTextView) findViewById(R.id.login);
  95. password = (EditText) findViewById(R.id.password);
  96.  
  97. Button signIn = (Button) findViewById(R.id.sign_in);
  98. signIn.setOnClickListener(this);
  99.  
  100. Button register = (Button) findViewById(R.id.register);
  101. register.setOnClickListener(this);
  102. }
  103.  
  104.  
  105. // private void createUniqueId() {
  106. // SharedPreferences.Editor editor = sharedPreferences.edit();
  107. // editor.putString("unique_id", UUID.randomUUID().toString());
  108. // editor.apply();
  109. // }
  110.  
  111. @Override
  112. protected void onDestroy() {
  113. if (progressDialog != null) {
  114. progressDialog.dismiss();
  115. }
  116. super.onDestroy();
  117. }
  118.  
  119. private void getDataFromServer() {
  120. progressDialog = new ProgressDialog(this);
  121. progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  122. progressDialog.setCancelable(false);
  123. progressDialog.setTitle(getString(R.string.loading_data_from_server));
  124. progressDialog.show();
  125.  
  126. new Thread(new Runnable() {
  127. public void run() {
  128.  
  129. try {
  130. String url_login = Utils.SERVER_URL + "register?params=login";
  131. // url_login = "10.0.0.2:8080/ServerRest/RegisterServlet?params=login";
  132. JSONObject json = new JSONObject();
  133. JSONObject user = new JSONObject();
  134.  
  135. if (AppPref.getInstance().isLogin()) {
  136. String login = AppPref.getInstance().getLogin();
  137. Log.e("myLogs", " login " + login);
  138. DatabaseHelper.DATABASE_NAME = login;
  139. user.put("name", login);
  140. url_login = Utils.SERVER_URL + "register?params=islogin";
  141. } else {
  142. DatabaseHelper.DATABASE_NAME = login.getText().toString();
  143. user.put("name", login.getText().toString());
  144. user.put("pass", password.getText().toString());
  145. }
  146. user.put("dbtime", Utils.newDay() ? Utils.SELECTED_DATE.getTime() : -1);
  147. json.put("user", user);
  148.  
  149. if (Utils.newDay()) {
  150. AppPref.getInstance().saveDate();
  151. }
  152.  
  153. Log.e("myLogs", " json " + json.toString());
  154.  
  155. HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url_login).openConnection();
  156. httpURLConnection.setConnectTimeout(10000);
  157. httpURLConnection.setRequestMethod("POST");
  158. httpURLConnection.setDoOutput(true);
  159. httpURLConnection.setDoInput(true);
  160. httpURLConnection.setRequestProperty("Content-Type", "application/json");
  161.  
  162. DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
  163. wr.writeBytes(json.toString());
  164. wr.flush();
  165. wr.close();
  166.  
  167. Log.e("myLogs", " >>> " + httpURLConnection.getResponseCode());
  168. if (httpURLConnection.getResponseCode() == 200) {
  169.  
  170. BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
  171.  
  172. String returnString = "";
  173. String resultJson = "";
  174.  
  175. while ((returnString = in.readLine()) != null) {
  176. resultJson += returnString;
  177. }
  178. in.close();
  179. Log.e("myLogs", " res " + resultJson);
  180.  
  181. DatabaseHelper helper = DatabaseHelper.getInstance(LoginActivity.this);
  182. DatabaseHelper.LOADING = true;
  183. Data.hallList = new SparseArray<Hall>();
  184. JSONObject parent = new JSONObject(resultJson);
  185. JSONArray restaurant = parent.getJSONArray("restaurant");
  186. for (int i = 0; i < restaurant.length(); i++) {
  187. JSONObject obj = restaurant.getJSONObject(i);
  188. int hallId = obj.getInt("hallId");
  189. String hallName = obj.getString("hallName");
  190. if (Data.hallList.get(hallId) == null) {
  191. Data.hallList.put(hallId, new Hall(hallId, hallName, 0));
  192. }
  193. }
  194.  
  195. Utils.OFFSET = Data.hallList.size() > 1 ? 48 : 0;
  196. for (int i = 0; i < restaurant.length(); i++) {
  197. JSONObject obj = restaurant.getJSONObject(i);
  198. int hallId = obj.getInt("hallId");
  199. int tableId = obj.getInt("tableId");
  200. float tableX = (float) obj.getDouble("tableX");
  201. float tableY = (float) obj.getDouble("tableY");
  202. float tableSize = (float) obj.getDouble("tableSize");
  203. int tableForm = obj.getInt("tableForm");
  204.  
  205. Data.hallList.get(hallId).getTablesList().add(new Table(tableId,
  206. (int) (tableX * MyApp.getInstance().getWidthPixels()),
  207. (int) (tableY * MyApp.getInstance().getWidthPixels() + Utils.OFFSET * MyApp.DENSITY),
  208. (int) (tableSize * MyApp.getInstance().getWidthPixels()),
  209. (int) (tableSize * MyApp.getInstance().getWidthPixels()), tableForm != 0));
  210. Data.hallList.get(hallId).incrTableSize();
  211. }
  212.  
  213. JSONArray reserves = parent.getJSONArray("reserves");
  214. for (int i = 0; i < reserves.length(); i++) {
  215. JSONObject obj = reserves.getJSONObject(i);
  216. int hallId = obj.getInt("hallId");
  217. int tableId = obj.getInt("tableId");
  218. String clientname = obj.getString("clientname");
  219. int visitStatus = obj.getInt("visitstatus");
  220. long time = obj.getLong("time");
  221. int count = obj.getInt("count");
  222. for (int j = 0; j < Data.hallList.size(); j++) {
  223. TablesList list = Data.hallList.get(j).getTablesList();
  224. for (Table table : list) {
  225. if (table.getId() == tableId && hallId == j) {
  226. Calendar c = Calendar.getInstance();
  227. c.setTime(new Date(time));
  228. Calendar c1 = Calendar.getInstance();
  229. c1.setTime(Utils.SELECTED_DATE);
  230. boolean sameDay = c.get(Calendar.YEAR) == c1.get(Calendar.YEAR) &&
  231. c.get(Calendar.DAY_OF_YEAR) == c1.get(Calendar.DAY_OF_YEAR);
  232. if (sameDay) {
  233. String hour = "" + (c.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + c.get(Calendar.HOUR_OF_DAY)
  234. : c.get(Calendar.HOUR_OF_DAY));
  235. String minute = "" + (c.get(Calendar.MINUTE) < 10 ? "0" + c.get(Calendar.MINUTE)
  236. : c.get(Calendar.MINUTE));
  237. table.addReservePlate(hallId,
  238. clientname,
  239. hour + ":" + minute,
  240. time,
  241. count,
  242. visitStatus
  243. );
  244. }
  245. }
  246. }
  247. }
  248. // helper.addRowToReserveTable(time, hallId, tableId, status, phone, clientname, sugg, pref, visitStatus, count);
  249. }
  250.  
  251. JSONArray clients = parent.getJSONArray("clients");
  252. for (int i = 0; i < clients.length(); i++) {
  253. JSONObject obj = clients.getJSONObject(i);
  254. String name = obj.getString("clientname");
  255. String phone = obj.getString("phone");
  256. String sugg = obj.getString("sugg");
  257. String preff = obj.getString("pref");
  258. String status = obj.getString("status");
  259. int countVisits = obj.getInt("visits");
  260. int countFails = obj.getInt("fails");
  261. ClientBase.list.add(new Client(status, name, phone, preff, sugg, countVisits, countFails));
  262. }
  263.  
  264. if (progressDialog != null) {
  265. progressDialog.dismiss();
  266. }
  267. if (AppPref.getInstance().isLogin()) {
  268. callHallTabActivity();
  269. } else {
  270. callCalendarActivity();
  271. }
  272.  
  273. helper.resetDB();
  274.  
  275. for (int i = 0; i < restaurant.length(); i++) {
  276. JSONObject obj = restaurant.getJSONObject(i);
  277. int hallId = obj.getInt("hallId");
  278. String hallName = obj.getString("hallName");
  279. int tableId = obj.getInt("tableId");
  280. float tableX = (float) obj.getDouble("tableX");
  281. float tableY = (float) obj.getDouble("tableY");
  282. float tableSize = (float) obj.getDouble("tableSize");
  283. int tableForm = obj.getInt("tableForm");
  284. helper.addRowToCurrentTable(hallId, hallName, tableId,
  285. tableX,
  286. tableY,
  287. tableSize,
  288. tableForm != 0);
  289. }
  290.  
  291. JSONArray def_config = parent.getJSONArray("default_config");
  292. for (int i = 0; i < def_config.length(); i++) {
  293. JSONObject obj = def_config.getJSONObject(i);
  294. int hallId = obj.getInt("hallId");
  295. String hallName = obj.getString("hallName");
  296. int tableId = obj.getInt("tableId");
  297. float tableX = (float) obj.getDouble("tableX");
  298. float tableY = (float) obj.getDouble("tableY");
  299. float tableSize = (float) obj.getDouble("tableSize");
  300. int tableForm = obj.getInt("tableForm");
  301. helper.addRowToDefaultTable(hallId, hallName, tableId,
  302. tableX,
  303. tableY,
  304. tableSize,
  305. tableForm != 0);
  306. }
  307.  
  308. for (int i = 0; i < reserves.length(); i++) {
  309. JSONObject obj = reserves.getJSONObject(i);
  310. int hallId = obj.getInt("hallId");
  311. int tableId = obj.getInt("tableId");
  312. String status = obj.getString("status");
  313. String phone = obj.getString("phone");
  314. String clientname = obj.getString("clientname");
  315. String sugg = obj.getString("sugg");
  316. String pref = obj.getString("pref");
  317. int visitStatus = obj.getInt("visitstatus");
  318. long time = obj.getLong("time");
  319. int count = obj.getInt("count");
  320. helper.addRowToReserveTable(time, hallId, tableId, status, phone, clientname, sugg, pref, visitStatus, count);
  321. }
  322.  
  323. for (int i = 0; i < clients.length(); i++) {
  324. JSONObject obj = clients.getJSONObject(i);
  325. String name = obj.getString("clientname");
  326. String phone = obj.getString("phone");
  327. String sugg = obj.getString("sugg");
  328. String preff = obj.getString("pref");
  329. String status = obj.getString("status");
  330. int countVisits = obj.getInt("visits");
  331. int countFails = obj.getInt("fails");
  332. helper.addClientToDB(status, name, phone, preff, sugg, countVisits, countFails);
  333. }
  334.  
  335. if (!AppPref.getInstance().isLogin()) {
  336. AppPref.getInstance().saveLogin(login.getText().toString());
  337. }
  338. DatabaseHelper.LOADING = false;
  339. //
  340. // helper.fillHallList();
  341. //
  342. } else {
  343. httpURLConnection.disconnect();
  344. runOnUiThread(new Runnable() {
  345. @Override
  346. public void run() {
  347. if (progressDialog != null) {
  348. progressDialog.dismiss();
  349. AlertDialog.Builder adb = new AlertDialog.Builder(LoginActivity.this)
  350. .setTitle(R.string.error_load_data_from_server)
  351. .setPositiveButton(R.string.try_again, new DialogInterface.OnClickListener() {
  352. @Override
  353. public void onClick(DialogInterface dialogInterface, int i) {
  354. getDataFromServer();
  355. dialogInterface.dismiss();
  356. }
  357. })
  358. .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  359. @Override
  360. public void onClick(DialogInterface dialogInterface, int i) {
  361. dialogInterface.dismiss();
  362. }
  363. });
  364. adb.show();
  365. }
  366. }
  367. });
  368. }
  369. } catch (SocketTimeoutException e) {
  370.  
  371. if (progressDialog != null) {
  372. progressDialog.dismiss();
  373. }
  374. runOnUiThread(new Runnable() {
  375. @Override
  376. public void run() {
  377. AlertDialog.Builder adb = new AlertDialog.Builder(LoginActivity.this)
  378. .setTitle(R.string.error_connect_to_server)
  379. .setPositiveButton(R.string.try_again, new DialogInterface.OnClickListener() {
  380. @Override
  381. public void onClick(DialogInterface dialogInterface, int i) {
  382. getDataFromServer();
  383. dialogInterface.dismiss();
  384. }
  385. })
  386. .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  387. @Override
  388. public void onClick(DialogInterface dialogInterface, int i) {
  389. dialogInterface.dismiss();
  390. }
  391. });
  392. adb.show();
  393. }
  394. });
  395.  
  396. } catch (Exception e) {
  397. Log.d("Exception", e.toString());
  398. if (progressDialog != null) {
  399. progressDialog.dismiss();
  400. }
  401. runOnUiThread(new Runnable() {
  402. @Override
  403. public void run() {
  404. AlertDialog.Builder adb = new AlertDialog.Builder(LoginActivity.this)
  405. .setTitle(R.string.error_connect_to_server)
  406. .setPositiveButton(R.string.try_again, new DialogInterface.OnClickListener() {
  407. @Override
  408. public void onClick(DialogInterface dialogInterface, int i) {
  409. getDataFromServer();
  410. dialogInterface.dismiss();
  411. }
  412. })
  413. .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  414. @Override
  415. public void onClick(DialogInterface dialogInterface, int i) {
  416. dialogInterface.dismiss();
  417. }
  418. });
  419. adb.show();
  420. }
  421. });
  422.  
  423. }
  424. }
  425. }).start();
  426.  
  427. }
  428.  
  429. private void callHallTabActivity() {
  430. Intent intent = new Intent(this, HallTabActivity.class);
  431. startActivity(intent);
  432. }
  433.  
  434. @Override
  435. public void onClick(View view) {
  436. switch (view.getId()) {
  437. case R.id.sign_in:
  438. signIn();
  439. break;
  440. case R.id.register:
  441. callRegisterActivity();
  442. break;
  443. default:
  444. break;
  445. }
  446. }
  447.  
  448. private void signIn() {
  449. getDataFromServer();
  450. }
  451.  
  452. private void callCalendarActivity() {
  453. Intent intent = new Intent(this, CalendarActivity.class);
  454. startActivity(intent);
  455. }
  456.  
  457. private void callRegisterActivity() {
  458. Intent intent = new Intent(this, RegisterActivity.class);
  459. startActivity(intent);
  460. }
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement