Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.service.restaurant;
- import android.accounts.Account;
- import android.accounts.AccountManager;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.support.annotation.Nullable;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.text.TextUtils;
- import android.util.Log;
- import android.util.SparseArray;
- import android.view.View;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.google.firebase.iid.FirebaseInstanceId;
- import com.service.restaurant.clients.Client;
- import com.service.restaurant.clients.ClientBase;
- import com.service.restaurant.view.Hall;
- import com.service.restaurant.view.TablesList;
- import com.service.restaurant.view.elements.Table;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.SocketTimeoutException;
- import java.net.URL;
- import java.net.URLDecoder;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.UUID;
- public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
- private AutoCompleteTextView login;
- private EditText password;
- private ProgressDialog progressDialog;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- Data.hallList = new SparseArray<>();
- Data.clientList = new ArrayList<>();
- ClientBase.list = new ArrayList<>();
- Calendar c = Calendar.getInstance();
- c.set(Calendar.HOUR_OF_DAY, 0);
- c.set(Calendar.MINUTE, 0);
- c.set(Calendar.SECOND, 0);
- c.set(Calendar.MILLISECOND, 0);
- Utils.SELECTED_DATE = c.getTime();
- // if (sharedPreferences.getString("unique_id", "").equals("")) {
- // createUniqueId();
- // }
- // ClientBase.fillClientBase();
- // String token = FirebaseInstanceId.getInstance().getToken();
- // Log.e("myLogs", " token " + token);
- Log.e("myLogs", " login " + AppPref.getInstance().getLogin());
- long date = AppPref.getInstance().getSavedDate();
- if (date == -1) {
- AppPref.getInstance().saveDate();
- }
- if (AppPref.getInstance().isLogin()) {
- getDataFromServer();
- }
- login = (AutoCompleteTextView) findViewById(R.id.login);
- password = (EditText) findViewById(R.id.password);
- Button signIn = (Button) findViewById(R.id.sign_in);
- signIn.setOnClickListener(this);
- Button register = (Button) findViewById(R.id.register);
- register.setOnClickListener(this);
- }
- // private void createUniqueId() {
- // SharedPreferences.Editor editor = sharedPreferences.edit();
- // editor.putString("unique_id", UUID.randomUUID().toString());
- // editor.apply();
- // }
- @Override
- protected void onDestroy() {
- if (progressDialog != null) {
- progressDialog.dismiss();
- }
- super.onDestroy();
- }
- private void getDataFromServer() {
- progressDialog = new ProgressDialog(this);
- progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- progressDialog.setCancelable(false);
- progressDialog.setTitle(getString(R.string.loading_data_from_server));
- progressDialog.show();
- new Thread(new Runnable() {
- public void run() {
- try {
- String url_login = Utils.SERVER_URL + "register?params=login";
- // url_login = "10.0.0.2:8080/ServerRest/RegisterServlet?params=login";
- JSONObject json = new JSONObject();
- JSONObject user = new JSONObject();
- if (AppPref.getInstance().isLogin()) {
- String login = AppPref.getInstance().getLogin();
- Log.e("myLogs", " login " + login);
- DatabaseHelper.DATABASE_NAME = login;
- user.put("name", login);
- url_login = Utils.SERVER_URL + "register?params=islogin";
- } else {
- DatabaseHelper.DATABASE_NAME = login.getText().toString();
- user.put("name", login.getText().toString());
- user.put("pass", password.getText().toString());
- }
- user.put("dbtime", Utils.newDay() ? Utils.SELECTED_DATE.getTime() : -1);
- json.put("user", user);
- if (Utils.newDay()) {
- AppPref.getInstance().saveDate();
- }
- Log.e("myLogs", " json " + json.toString());
- HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url_login).openConnection();
- httpURLConnection.setConnectTimeout(10000);
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- httpURLConnection.setRequestProperty("Content-Type", "application/json");
- DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
- wr.writeBytes(json.toString());
- wr.flush();
- wr.close();
- Log.e("myLogs", " >>> " + httpURLConnection.getResponseCode());
- if (httpURLConnection.getResponseCode() == 200) {
- BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
- String returnString = "";
- String resultJson = "";
- while ((returnString = in.readLine()) != null) {
- resultJson += returnString;
- }
- in.close();
- Log.e("myLogs", " res " + resultJson);
- DatabaseHelper helper = DatabaseHelper.getInstance(LoginActivity.this);
- DatabaseHelper.LOADING = true;
- Data.hallList = new SparseArray<Hall>();
- JSONObject parent = new JSONObject(resultJson);
- JSONArray restaurant = parent.getJSONArray("restaurant");
- for (int i = 0; i < restaurant.length(); i++) {
- JSONObject obj = restaurant.getJSONObject(i);
- int hallId = obj.getInt("hallId");
- String hallName = obj.getString("hallName");
- if (Data.hallList.get(hallId) == null) {
- Data.hallList.put(hallId, new Hall(hallId, hallName, 0));
- }
- }
- Utils.OFFSET = Data.hallList.size() > 1 ? 48 : 0;
- for (int i = 0; i < restaurant.length(); i++) {
- JSONObject obj = restaurant.getJSONObject(i);
- int hallId = obj.getInt("hallId");
- int tableId = obj.getInt("tableId");
- float tableX = (float) obj.getDouble("tableX");
- float tableY = (float) obj.getDouble("tableY");
- float tableSize = (float) obj.getDouble("tableSize");
- int tableForm = obj.getInt("tableForm");
- Data.hallList.get(hallId).getTablesList().add(new Table(tableId,
- (int) (tableX * MyApp.getInstance().getWidthPixels()),
- (int) (tableY * MyApp.getInstance().getWidthPixels() + Utils.OFFSET * MyApp.DENSITY),
- (int) (tableSize * MyApp.getInstance().getWidthPixels()),
- (int) (tableSize * MyApp.getInstance().getWidthPixels()), tableForm != 0));
- Data.hallList.get(hallId).incrTableSize();
- }
- JSONArray reserves = parent.getJSONArray("reserves");
- for (int i = 0; i < reserves.length(); i++) {
- JSONObject obj = reserves.getJSONObject(i);
- int hallId = obj.getInt("hallId");
- int tableId = obj.getInt("tableId");
- String clientname = obj.getString("clientname");
- int visitStatus = obj.getInt("visitstatus");
- long time = obj.getLong("time");
- int count = obj.getInt("count");
- for (int j = 0; j < Data.hallList.size(); j++) {
- TablesList list = Data.hallList.get(j).getTablesList();
- for (Table table : list) {
- if (table.getId() == tableId && hallId == j) {
- Calendar c = Calendar.getInstance();
- c.setTime(new Date(time));
- Calendar c1 = Calendar.getInstance();
- c1.setTime(Utils.SELECTED_DATE);
- boolean sameDay = c.get(Calendar.YEAR) == c1.get(Calendar.YEAR) &&
- c.get(Calendar.DAY_OF_YEAR) == c1.get(Calendar.DAY_OF_YEAR);
- if (sameDay) {
- String hour = "" + (c.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + c.get(Calendar.HOUR_OF_DAY)
- : c.get(Calendar.HOUR_OF_DAY));
- String minute = "" + (c.get(Calendar.MINUTE) < 10 ? "0" + c.get(Calendar.MINUTE)
- : c.get(Calendar.MINUTE));
- table.addReservePlate(hallId,
- clientname,
- hour + ":" + minute,
- time,
- count,
- visitStatus
- );
- }
- }
- }
- }
- // helper.addRowToReserveTable(time, hallId, tableId, status, phone, clientname, sugg, pref, visitStatus, count);
- }
- JSONArray clients = parent.getJSONArray("clients");
- for (int i = 0; i < clients.length(); i++) {
- JSONObject obj = clients.getJSONObject(i);
- String name = obj.getString("clientname");
- String phone = obj.getString("phone");
- String sugg = obj.getString("sugg");
- String preff = obj.getString("pref");
- String status = obj.getString("status");
- int countVisits = obj.getInt("visits");
- int countFails = obj.getInt("fails");
- ClientBase.list.add(new Client(status, name, phone, preff, sugg, countVisits, countFails));
- }
- if (progressDialog != null) {
- progressDialog.dismiss();
- }
- if (AppPref.getInstance().isLogin()) {
- callHallTabActivity();
- } else {
- callCalendarActivity();
- }
- helper.resetDB();
- for (int i = 0; i < restaurant.length(); i++) {
- JSONObject obj = restaurant.getJSONObject(i);
- int hallId = obj.getInt("hallId");
- String hallName = obj.getString("hallName");
- int tableId = obj.getInt("tableId");
- float tableX = (float) obj.getDouble("tableX");
- float tableY = (float) obj.getDouble("tableY");
- float tableSize = (float) obj.getDouble("tableSize");
- int tableForm = obj.getInt("tableForm");
- helper.addRowToCurrentTable(hallId, hallName, tableId,
- tableX,
- tableY,
- tableSize,
- tableForm != 0);
- }
- JSONArray def_config = parent.getJSONArray("default_config");
- for (int i = 0; i < def_config.length(); i++) {
- JSONObject obj = def_config.getJSONObject(i);
- int hallId = obj.getInt("hallId");
- String hallName = obj.getString("hallName");
- int tableId = obj.getInt("tableId");
- float tableX = (float) obj.getDouble("tableX");
- float tableY = (float) obj.getDouble("tableY");
- float tableSize = (float) obj.getDouble("tableSize");
- int tableForm = obj.getInt("tableForm");
- helper.addRowToDefaultTable(hallId, hallName, tableId,
- tableX,
- tableY,
- tableSize,
- tableForm != 0);
- }
- for (int i = 0; i < reserves.length(); i++) {
- JSONObject obj = reserves.getJSONObject(i);
- int hallId = obj.getInt("hallId");
- int tableId = obj.getInt("tableId");
- String status = obj.getString("status");
- String phone = obj.getString("phone");
- String clientname = obj.getString("clientname");
- String sugg = obj.getString("sugg");
- String pref = obj.getString("pref");
- int visitStatus = obj.getInt("visitstatus");
- long time = obj.getLong("time");
- int count = obj.getInt("count");
- helper.addRowToReserveTable(time, hallId, tableId, status, phone, clientname, sugg, pref, visitStatus, count);
- }
- for (int i = 0; i < clients.length(); i++) {
- JSONObject obj = clients.getJSONObject(i);
- String name = obj.getString("clientname");
- String phone = obj.getString("phone");
- String sugg = obj.getString("sugg");
- String preff = obj.getString("pref");
- String status = obj.getString("status");
- int countVisits = obj.getInt("visits");
- int countFails = obj.getInt("fails");
- helper.addClientToDB(status, name, phone, preff, sugg, countVisits, countFails);
- }
- if (!AppPref.getInstance().isLogin()) {
- AppPref.getInstance().saveLogin(login.getText().toString());
- }
- DatabaseHelper.LOADING = false;
- //
- // helper.fillHallList();
- //
- } else {
- httpURLConnection.disconnect();
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (progressDialog != null) {
- progressDialog.dismiss();
- AlertDialog.Builder adb = new AlertDialog.Builder(LoginActivity.this)
- .setTitle(R.string.error_load_data_from_server)
- .setPositiveButton(R.string.try_again, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- getDataFromServer();
- dialogInterface.dismiss();
- }
- })
- .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- adb.show();
- }
- }
- });
- }
- } catch (SocketTimeoutException e) {
- if (progressDialog != null) {
- progressDialog.dismiss();
- }
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- AlertDialog.Builder adb = new AlertDialog.Builder(LoginActivity.this)
- .setTitle(R.string.error_connect_to_server)
- .setPositiveButton(R.string.try_again, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- getDataFromServer();
- dialogInterface.dismiss();
- }
- })
- .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- adb.show();
- }
- });
- } catch (Exception e) {
- Log.d("Exception", e.toString());
- if (progressDialog != null) {
- progressDialog.dismiss();
- }
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- AlertDialog.Builder adb = new AlertDialog.Builder(LoginActivity.this)
- .setTitle(R.string.error_connect_to_server)
- .setPositiveButton(R.string.try_again, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- getDataFromServer();
- dialogInterface.dismiss();
- }
- })
- .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- adb.show();
- }
- });
- }
- }
- }).start();
- }
- private void callHallTabActivity() {
- Intent intent = new Intent(this, HallTabActivity.class);
- startActivity(intent);
- }
- @Override
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.sign_in:
- signIn();
- break;
- case R.id.register:
- callRegisterActivity();
- break;
- default:
- break;
- }
- }
- private void signIn() {
- getDataFromServer();
- }
- private void callCalendarActivity() {
- Intent intent = new Intent(this, CalendarActivity.class);
- startActivity(intent);
- }
- private void callRegisterActivity() {
- Intent intent = new Intent(this, RegisterActivity.class);
- startActivity(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement