Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// SaveInfoApi
- package com.example.cristinica.foodhelper.apiConnector;
- import android.util.Log;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * Created by cristi.nica on 3/24/2018.
- */
- public class SaveInfoApi {
- public static String apiURL = "http://10.81.130.112/register.php";
- public static String saveInfo(String email, String newMail, String name, String reprezentant, String phone, String address) {
- HttpURLConnection connection = null;
- try {
- URL url = new URL(apiURL);
- JSONObject postDataParams = new JSONObject();
- postDataParams.put("email", email);
- postDataParams.put("email_nou", newMail);
- postDataParams.put("nume_companie", name);
- postDataParams.put("telefon", phone);
- postDataParams.put("adresa", address);
- postDataParams.put("nume_reprezentant", reprezentant);
- connection = (HttpURLConnection) url.openConnection();
- connection.setReadTimeout(15000);
- connection.setConnectTimeout(15000);
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type",
- "application/json");
- connection.setDoInput(true);
- connection.setDoOutput(true);
- OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
- out.write(postDataParams.toString());
- out.close();
- StringBuilder sb = new StringBuilder();
- sb.append("");
- int HttpResult = connection.getResponseCode();
- if (HttpResult == HttpURLConnection.HTTP_OK) {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- connection.getInputStream(), "utf-8"));
- String line = null;
- while ((line = br.readLine()) != null) {
- sb.append(line + "\n");
- }
- br.close();
- } else {
- System.out.println(connection.getResponseMessage());
- }
- return sb.toString();
- } catch (Exception e) {
- return new String("Exception: " + e.getMessage());
- } finally {
- connection.disconnect();
- }
- }
- }
- //// Login
- package com.example.cristinica.foodhelper;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.example.cristinica.foodhelper.apiConnector.LoginApi;
- import com.example.cristinica.foodhelper.models.LoginModel;
- import com.example.cristinica.foodhelper.models.RegisterModel;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import java.util.ArrayList;
- public class Login extends AppCompatActivity {
- Button goToRegister;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- goToRegister = findViewById(R.id.goToRegister);
- goToRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(Login.this, Register.class);
- startActivity(intent);
- }
- });
- final EditText companyMail = findViewById(R.id.email_login);
- final EditText pass = findViewById(R.id.password_login);
- Button register = findViewById(R.id.register2);
- final SharedPreferences sharedPreferences = Login.this.getSharedPreferences("type", Context
- .MODE_PRIVATE);
- //final SharedPreferences.Editor editor = sharedPreferences.edit();
- final int type = sharedPreferences.getInt("type", -1);
- Button login = findViewById(R.id.login2);
- if (type == 0) {
- login.setBackgroundDrawable(getResources().getDrawable(R.drawable.round));
- goToRegister.setBackgroundDrawable(getResources().getDrawable(R.drawable.round));
- } else if (type == 1) {
- login.setBackgroundDrawable(getResources().getDrawable(R.drawable.round2));
- goToRegister.setBackgroundDrawable(getResources().getDrawable(R.drawable.round2));
- } else {
- Log.v("da", "daaaa2");
- }
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- @SuppressLint("StaticFieldLeak") AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
- LoginModel loginModel = new LoginModel();
- String s;
- @Override
- protected Void doInBackground(Void... params) {
- s = LoginApi.login(companyMail.getText().toString(), pass.getText().toString());
- Log.v("primit", s);
- Gson g = new Gson();
- loginModel = g.fromJson(s, LoginModel.class);
- return null;
- }
- @SuppressLint("ShowToast")
- protected void onPostExecute(Void param) {
- if (loginModel.status.equals("ok")) {
- final SharedPreferences sharedPreferences3 = Login.this.getSharedPreferences("user", Context.MODE_PRIVATE);
- SharedPreferences.Editor editorUser = sharedPreferences3.edit();
- editorUser.putString("user", s);
- editorUser.apply();
- if (type == 0) {
- SharedPreferences sharedPreferences2 = Login.this.getSharedPreferences("logged", Context
- .MODE_PRIVATE);
- SharedPreferences.Editor editor2 = sharedPreferences2.edit();
- editor2.putInt("logged", 1);
- editor2.apply();
- Intent intent = new Intent(Login.this, GiverActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- }
- if (type == 1) {
- SharedPreferences sharedPreferences2 = Login.this.getSharedPreferences("logged", Context
- .MODE_PRIVATE);
- SharedPreferences.Editor editor2 = sharedPreferences2.edit();
- editor2.putInt("logged", 1);
- editor2.apply();
- Intent intent = new Intent(Login.this, ReceiverActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- }
- } else {
- Toast.makeText(getApplicationContext(), "Login failed", Toast.LENGTH_SHORT);
- }
- }
- };
- task.execute();
- }
- });
- }
- }
- //// MainActivity
- package com.example.cristinica.foodhelper;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.RelativeLayout;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- RelativeLayout help = findViewById(R.id.share_food);
- RelativeLayout need = findViewById(R.id.need_food);
- SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("type", Context
- .MODE_PRIVATE);
- final SharedPreferences.Editor editor = sharedPreferences.edit();
- SharedPreferences sharedPreferences2 = MainActivity.this.getSharedPreferences("logged", Context
- .MODE_PRIVATE);
- int logged = sharedPreferences2.getInt("logged", -1);
- int type = sharedPreferences.getInt("type", -1);
- if (logged == 1 && type == 0) {
- Intent intent = new Intent(MainActivity.this, GiverActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- } else if (logged == 1 && type == 1) {
- Intent intent = new Intent(MainActivity.this, ReceiverActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- }
- //companie
- help.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- editor.putInt("type", 0);
- editor.apply();
- Intent intent = new Intent(MainActivity.this, Login.class);
- startActivity(intent);
- }
- });
- //azil
- need.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- editor.putInt("type", 1);
- editor.apply();
- Intent intent = new Intent(MainActivity.this, Login.class);
- startActivity(intent);
- }
- });
- }
- }
- ///// SettingsActivity
- package com.example.cristinica.foodhelper;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import com.example.cristinica.foodhelper.apiConnector.RegisterApi;
- import com.example.cristinica.foodhelper.apiConnector.SaveInfoApi;
- import com.example.cristinica.foodhelper.models.LoginModel;
- import com.example.cristinica.foodhelper.models.RegisterModel;
- import com.google.gson.Gson;
- public class SettingsActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_settings);
- Button logout = findViewById(R.id.logout);
- final SharedPreferences sharedPreferences = SettingsActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
- final TextView name = findViewById(R.id.editText2);
- final TextView email = findViewById(R.id.editText3);
- final TextView address = findViewById(R.id.editText5);
- final TextView phone = findViewById(R.id.editText4);
- final TextView reprezentant = findViewById(R.id.editText6);
- final LoginModel loginModel;
- Gson g = new Gson();
- loginModel = g.fromJson(sharedPreferences.getString("user", ""), LoginModel.class);
- if (loginModel.nume != null)
- name.setText(loginModel.nume);
- if (loginModel.email != null)
- email.setText(loginModel.email);
- if (loginModel.adresa != null)
- address.setText(loginModel.adresa);
- if (loginModel.telefon != null)
- phone.setText(loginModel.telefon);
- if (loginModel.nume_reprezentant != null)
- reprezentant.setText(loginModel.nume_reprezentant);
- SharedPreferences sharedPreferences2 = SettingsActivity.this.getSharedPreferences("type", Context
- .MODE_PRIVATE);
- int type = sharedPreferences2.getInt("type", 1);
- if (type == 0) {
- logout.setBackgroundDrawable(getResources().getDrawable(R.drawable.round));
- } else if (type == 1) {
- logout.setBackgroundDrawable(getResources().getDrawable(R.drawable.round2));
- }
- logout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- SharedPreferences sharedPreferences2 = SettingsActivity.this.getSharedPreferences("logged", Context
- .MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences2.edit();
- editor.putInt("logged", 0);
- editor.apply();
- Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- }
- });
- Button save = findViewById(R.id.save);
- if (type == 0) {
- save.setBackgroundDrawable(getResources().getDrawable(R.drawable.round));
- } else if (type == 1) {
- save.setBackgroundDrawable(getResources().getDrawable(R.drawable.round2));
- }
- save.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- @SuppressLint("StaticFieldLeak") AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
- RegisterModel registerModel = new RegisterModel();
- @Override
- protected Void doInBackground(Void... params) {
- String s = SaveInfoApi.saveInfo(loginModel.email, email.getText().toString(), name.getText().toString(),
- reprezentant.getText().toString(), phone.getText().toString(), address.getText().toString());
- Log.v("am primit", s);
- Gson g = new Gson();
- registerModel = g.fromJson(s, RegisterModel.class);
- return null;
- }
- protected void onPostExecute(Void param) {
- }
- };
- task.execute();
- finish();
- }
- });
- }
- }
- //// Acitivity_receiver.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v4.widget.DrawerLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- tools:openDrawer="start">
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:isScrollContainer="true"
- android:id="@+id/home">
- <com.example.cristinica.foodhelper.SlidingTabLayout
- android:id="@+id/tabsHome"
- android:layout_width="match_parent"
- android:layout_height="45dp"
- android:paddingTop="5dp"
- android:paddingBottom="5dp"
- android:background="@color/colorPrimary"
- app:tabMode="scrollable"
- />
- <android.support.v4.view.ViewPager
- android:id="@+id/view_pagerHome"
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:layout_weight="1">
- </android.support.v4.view.ViewPager>
- </LinearLayout>
- </android.support.v4.widget.DrawerLayout>
- ////// Activity Settings XML
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:textAlignment="center"
- tools:context="com.example.cristinica.foodhelper.SettingsActivity">
- <TextView
- android:id="@+id/editText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_centerInParent="true"
- android:layout_centerVertical="true"
- android:layout_marginTop="14dp"
- android:textSize="20sp"
- android:ems="10"
- android:text="Tell more about you"
- android:textAlignment="center" />
- <RelativeLayout
- android:id="@+id/companyName"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/editText"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="11dp">
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="18dp"
- android:layout_marginStart="18dp"
- app:srcCompat="@drawable/user_name" />
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_marginLeft="13dp"
- android:layout_marginStart="13dp"
- android:layout_toEndOf="@+id/imageView"
- android:layout_toRightOf="@+id/imageView"
- android:ems="10"
- android:inputType="textPersonName"
- android:hint="Nume" />
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/email"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/companyName"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="11dp">
- <ImageView
- android:id="@+id/imageView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="18dp"
- android:layout_marginStart="18dp"
- app:srcCompat="@drawable/envelope" />
- <EditText
- android:id="@+id/editText3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_marginLeft="13dp"
- android:layout_marginStart="13dp"
- android:layout_toEndOf="@+id/imageView2"
- android:layout_toRightOf="@+id/imageView2"
- android:ems="10"
- android:inputType="textPersonName"
- android:hint="Email" />
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/address"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/email"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="11dp">
- <ImageView
- android:id="@+id/imageView4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="18dp"
- android:layout_marginStart="18dp"
- app:srcCompat="@drawable/placeholder" />
- <EditText
- android:id="@+id/editText5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_marginLeft="13dp"
- android:layout_marginStart="13dp"
- android:layout_toEndOf="@+id/imageView4"
- android:layout_toRightOf="@+id/imageView4"
- android:ems="10"
- android:inputType="textPersonName"
- android:hint="Adresa" />
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/phone"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/address"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="11dp">
- <ImageView
- android:id="@+id/imageView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="18dp"
- android:layout_marginStart="18dp"
- app:srcCompat="@drawable/smartphone_call" />
- <EditText
- android:id="@+id/editText4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_marginLeft="13dp"
- android:layout_marginStart="13dp"
- android:layout_toEndOf="@+id/imageView3"
- android:layout_toRightOf="@+id/imageView3"
- android:ems="10"
- android:inputType="textPersonName"
- android:hint="Numar de telefon" />
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/reprezentant"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_below="@+id/phone"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="11dp">
- <ImageView
- android:id="@+id/imageView6"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="18dp"
- android:layout_marginStart="18dp"
- app:srcCompat="@drawable/user_name" />
- <EditText
- android:id="@+id/editText6"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_marginLeft="13dp"
- android:layout_marginStart="13dp"
- android:layout_toEndOf="@+id/imageView6"
- android:layout_toRightOf="@+id/imageView6"
- android:ems="10"
- android:inputType="textPersonName"
- android:hint="Nume Reprezentant" />
- </RelativeLayout>
- <Button
- android:id="@+id/logout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="36dp"
- android:text="Logout" />
- <Button
- android:id="@+id/save"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignEnd="@+id/logout"
- android:layout_alignRight="@+id/logout"
- android:layout_below="@+id/reprezentant"
- android:layout_marginTop="11dp"
- android:text="Save" />
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement