Advertisement
Saleh_Zoabi

MY Task

Jan 9th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.40 KB | None | 0 0
  1. activity_main.xml
  2. =======================
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:layout_margin="16dp"
  7.     android:orientation="vertical">
  8.  
  9.     <!-- logo -->
  10.     <LinearLayout
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent"
  13.         android:layout_weight="1"
  14.         android:gravity="center"
  15.         android:orientation="vertical">
  16.  
  17.         <TextView
  18.             android:layout_width="wrap_content"
  19.             android:layout_height="wrap_content"
  20.             android:text="LOGO"
  21.             android:textSize="64dp"
  22.             android:layout_gravity="center"/>
  23.  
  24.     </LinearLayout>
  25.  
  26.     <!-- user input -->
  27.     <LinearLayout
  28.         android:layout_width="match_parent"
  29.         android:layout_height="match_parent"
  30.         android:layout_weight="1"
  31.         android:orientation="vertical"
  32.         android:gravity="center">
  33.  
  34.         <EditText
  35.             android:layout_width="match_parent"
  36.             android:layout_height="wrap_content"
  37.             android:hint="@string/userName"
  38.             android:inputType="textVisiblePassword|textNoSuggestions"
  39.             android:id="@+id/txtUser"/>
  40.  
  41.         <EditText
  42.             android:layout_width="match_parent"
  43.             android:layout_height="wrap_content"
  44.             android:hint="@string/userPass"
  45.             android:inputType="textWebPassword"
  46.             android:layout_marginTop="20dp"
  47.             android:id="@+id/txtPass"/>
  48.  
  49.     </LinearLayout>
  50.  
  51.     <!-- buttons -->
  52.     <LinearLayout
  53.         android:layout_width="match_parent"
  54.         android:layout_height="match_parent"
  55.         android:layout_weight="1"
  56.         android:orientation="horizontal"
  57.         android:gravity="top">
  58.  
  59.  
  60.         <Button
  61.             android:layout_width="match_parent"
  62.             android:layout_height="wrap_content"
  63.             android:layout_weight="1"
  64.             android:layout_margin="20dp"
  65.             android:text="@string/register"
  66.             android:textColor="#ffffff"
  67.             android:textSize="22sp"
  68.             android:background="#009fff"
  69.             android:id="@+id/btnRegister"
  70.             android:onClick="onClick" />
  71.  
  72.         <Button
  73.             android:layout_width="match_parent"
  74.             android:layout_height="wrap_content"
  75.             android:layout_weight="1"
  76.             android:layout_margin="20dp"
  77.             android:text="@string/login"
  78.             android:textColor="#ffffff"
  79.             android:textSize="22sp"
  80.             android:background="#009fff"
  81.             android:id="@+id/btnLogin"
  82.             android:onClick="onClick" />
  83.  
  84.     </LinearLayout>
  85.  
  86.  
  87.  
  88.  
  89. </LinearLayout>
  90.  
  91.  
  92. activity_Register.xml
  93. ========================
  94.  
  95. package com.example.sale7.myapplication3;
  96. import android.content.Context;
  97. import android.content.SharedPreferences;
  98. import android.widget.Toast;
  99.  
  100. /**
  101.  * Created by teacher on 12/21/2017.
  102.  */
  103.  
  104. public class UtlUserSP implements UserAble {
  105.     Context context;
  106.  
  107.     public UtlUserSP() { }
  108.  
  109.     @Override
  110.     public void setContext(Context context) {
  111.         this.context=context;
  112.     }
  113.  
  114.     @Override
  115.     public boolean registerUser(String userName, String userPass) {
  116.         //User Exists
  117.         if (userExists(userName)) {
  118.             Toast.makeText(context, "user already exists...", Toast.LENGTH_SHORT).show();
  119.             return false;
  120.         }
  121.         //save user and password
  122.  
  123.         //declaration of shared preference
  124.         SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
  125.  
  126.         //declaration of shared preference editor
  127.         SharedPreferences.Editor editor = prefs.edit();
  128.  
  129.         //put data inside, we use hash map, so we need KEY,VALUE (k,v)
  130.         editor.putString(userName, userPass);
  131.  
  132.         //sending data to my shared preferences file.
  133.         editor.commit();
  134.         return true;
  135.     }
  136.  
  137.     @Override
  138.     public boolean userExists( String userName) {
  139.         //declaration of shared prefernces
  140.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  141.  
  142.         //get user pass
  143.         String spPass=prefs.getString(userName,"na");
  144.  
  145.         //return if users exists, if not (na) return false
  146.         return !spPass.equals("na");
  147.     }
  148.  
  149.     @Override
  150.     public boolean checkUser( String userName, String userPass) {
  151.         //declaration of shared prefernces
  152.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  153.  
  154.         //get user password
  155.         String spPass=prefs.getString(userName,"na");
  156.  
  157.         //return the result if the passwords are match
  158.         return spPass.equals(userPass);
  159.     }
  160. }
  161.  
  162. =====================
  163.  
  164. MainActivity.java
  165. =================
  166. package com.example.sale7.myapplication3;
  167.  
  168. import android.support.v7.app.AppCompatActivity; // why is it not working well ??
  169. import android.os.Bundle;
  170. import android.view.View;
  171. import android.widget.EditText;
  172. import android.widget.Toast;
  173.  
  174. public class MainActivity extends AppCompatActivity { // My TASK PART 1
  175.  
  176.     final int DefualtName_DB = 11111;
  177.     final String DefualtPassword_DB = "Same";
  178.  
  179.     EditText txtUser, txtPass;
  180.  
  181.     @Override
  182.     protected void onCreate(Bundle savedInstanceState) { // "on Create" is the start of the LIFE OF "ACTIVITY"
  183.         super.onCreate(savedInstanceState);
  184.         setContentView(R.layout.activity_main);
  185.         setPointer();
  186.     }
  187.  
  188.     private void setPointer() {
  189.  
  190.         txtUser = findViewById(R.id.txtUser); //
  191.         txtPass = findViewById(R.id.txtPass);
  192.  
  193.  
  194.     }
  195.  
  196.     public void OnClick(View view) {
  197.  
  198.         String userName = txtUser.getText().toString(); // WE RELEATED THE XML PART TO THE JAVA PART
  199.         String userPass = txtPass.getText().toString(); // SAME
  200.  
  201.  
  202.         switch (view.getId()) {
  203.  
  204.             case (R.id.btnLogin):
  205.  
  206.  
  207.                 if (userName.equals(DefualtName_DB)) {
  208.                     if (userPass.equals(DefualtPassword_DB))
  209.  
  210.                         Toast.makeText(MainActivity.this, "Login Successfully", Toast.LENGTH_SHORT).show();
  211.                 } else if ((!userName.equals(DefualtName_DB)) || (!userPass.equals(DefualtPassword_DB))){
  212.  
  213.                     Toast.makeText(MainActivity.this, "Inccorect UserName Or Password", Toast.LENGTH_SHORT).show();
  214.  
  215.                 }
  216.  
  217.                 break;
  218.  
  219.  
  220.             case (R.id.btnRegister):
  221.  
  222.                 if (userName.length() == 0) {
  223.  
  224.                     Toast.makeText(MainActivity.this, "InEfficient Input(Name)", Toast.LENGTH_SHORT).show();
  225.  
  226.                     break;
  227.                 }
  228.  
  229.                 if (userPass.length() == 0) {
  230.  
  231.                     Toast.makeText(MainActivity.this, "InEfficient Input(Pass)", Toast.LENGTH_SHORT).show();
  232.  
  233.                     break;
  234.                 }
  235.  
  236.  
  237.                 //TODO create a new username <--> md5 password LinkedHashMap
  238.  
  239.                 break;
  240.         }
  241.  
  242.  
  243.     }
  244.  
  245.  
  246. }
  247.  
  248. ==========
  249. shared prefrences
  250. ========
  251. package com.example.sale7.myapplication3;
  252.  
  253. import android.content.Context;
  254. import android.content.SharedPreferences;
  255.  
  256. /**
  257.  * Created by teacher on 9/25/2017.
  258.  */
  259.  
  260. public class SharedPref {
  261.  
  262.     //for login screen
  263.     public static boolean checkPass(String userName,String userPass, Context context) // CHECKIF PASSWORD EXISTS IS THE SYSTEM.
  264.     {
  265.         //declaration of shared preferences
  266.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE); // ??
  267.         //get user pass
  268.         String spPass=prefs.getString(userName,"na"); // NA = NOT AVALIABLE
  269.         //check if password matches
  270.         if (userPass.equals(spPass))
  271.         {
  272.             return true;
  273.         }
  274.         return false;
  275.     }
  276.  
  277.     public static boolean checkUserExists(String userName, Context context) // CHECK IF USER EXISTS IN THE SYSTEM
  278.     {
  279.         //declaration of shared preferences
  280.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  281.         //get user pass
  282.         String spPass=prefs.getString(userName,"na");
  283.         //check if password matches
  284.         return !spPass.equals("na"); // na = not available
  285.     }
  286.  
  287.     public static void registerUser(String userName, String userPass, Context context) // WE SET A NEW USER NAME TO THIS CLASS..ALSO WE NEED AN EDITOR TO WRITE IN IT..
  288.     {
  289.         //declaration of shared preferences
  290.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);// PUT DATA ISNIDE..SAVE THE CHANGES(comit) AT END.
  291.  
  292.         //declaration of shared preferences Editor
  293.         SharedPreferences.Editor editor = prefs.edit();
  294.  
  295.         //put data inside, we use hash map , so we need KEY,VALUE (K,V)
  296.         editor.putString(userName,userPass);
  297.  
  298.         //sending data to my shared prefernces file, we have to commit the changes
  299.         editor.commit();
  300.     }
  301. }
  302. ==========
  303. UtilUserSP
  304. =============
  305. package com.example.sale7.myapplication3;
  306. import android.content.Context;
  307. import android.content.SharedPreferences;
  308. import android.widget.Toast;
  309.  
  310. /**
  311.  * Created by teacher on 12/21/2017.
  312.  */
  313.  
  314. public class UtlUserSP implements UserAble {
  315.     Context context;
  316.  
  317.     public UtlUserSP() { }
  318.  
  319.     @Override
  320.     public void setContext(Context context) {
  321.         this.context=context;
  322.     }
  323.  
  324.     @Override
  325.     public boolean registerUser(String userName, String userPass) {
  326.         //User Exists
  327.         if (userExists(userName)) {
  328.             Toast.makeText(context, "user already exists...", Toast.LENGTH_SHORT).show();
  329.             return false;
  330.         }
  331.         //save user and password
  332.  
  333.         //declaration of shared preference
  334.         SharedPreferences prefs = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
  335.  
  336.         //declaration of shared preference editor
  337.         SharedPreferences.Editor editor = prefs.edit();
  338.  
  339.         //put data inside, we use hash map, so we need KEY,VALUE (k,v)
  340.         editor.putString(userName, userPass);
  341.  
  342.         //sending data to my shared preferences file.
  343.         editor.commit();
  344.         return true;
  345.     }
  346.  
  347.     @Override
  348.     public boolean userExists( String userName) {
  349.         //declaration of shared prefernces
  350.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  351.  
  352.         //get user pass
  353.         String spPass=prefs.getString(userName,"na");
  354.  
  355.         //return if users exists, if not (na) return false
  356.         return !spPass.equals("na");
  357.     }
  358.  
  359.     @Override
  360.     public boolean checkUser( String userName, String userPass) {
  361.         //declaration of shared prefernces
  362.         SharedPreferences prefs=context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
  363.  
  364.         //get user password
  365.         String spPass=prefs.getString(userName,"na");
  366.  
  367.         //return the result if the passwords are match
  368.         return spPass.equals(userPass);
  369.     }
  370. }
  371. ========
  372.  
  373. UserAble InterFace
  374. ====
  375. package com.example.sale7.myapplication3;
  376.  
  377. import android.content.Context;
  378.  
  379. /**
  380.  * Created by User on 07/01/2018.
  381.  */
  382.  
  383. public interface UserAble {
  384.  
  385.     boolean registerUser(String userName, String userPass);
  386.     boolean userExists(String userName);
  387.     boolean checkUser(String userName,String userPass);
  388.     void setContext(Context context);
  389.  
  390.  
  391.  
  392.  
  393. }
  394. ==========
  395. package com.example.sale7.myapplication3;
  396.  
  397. import android.content.Context;
  398. import android.os.Bundle;
  399. import android.support.annotation.Nullable;
  400. import android.support.v7.app.AppCompatActivity;
  401. import android.view.View;
  402. import android.widget.Button;
  403. import android.widget.EditText;
  404. import android.widget.Toast;
  405.  
  406. import com.example.sale7.myapplication3.UtlUserSP;
  407.  
  408. /**
  409.  * Created by teacher on 12/21/2017.
  410.  */
  411.  
  412. public class RegisterActivity extends AppCompatActivity {
  413.     Context context;
  414.     EditText regUser,regPass1,regPass2;
  415.     EditText  rName,rpass,rCheck;
  416.     Button btnRegister,btnCancel,rCancel,rRegister;
  417.     UtlUserSP userUtil=new UtlUserSP();
  418.  
  419.     @Override
  420.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  421.         super.onCreate(savedInstanceState);
  422.         setContentView(R.layout.activity_register2);
  423.         setPointer();
  424.     }
  425.  
  426.     private void setPointer() {
  427.         this.context=this;
  428.         userUtil.setContext(context);
  429.  
  430.         // rName = findViewById(R.id.rUser);
  431.  
  432.  
  433.         // rpass = findViewById(R.id.)
  434.         regUser=findViewById(R.id.regUserName);
  435.         regPass1=findViewById(R.id.regPass1);
  436.         regPass2=findViewById(R.id.regPass2);
  437.  
  438.         btnCancel=findViewById(R.id.btnRegCancel);
  439.  
  440.         btnCancel.setOnClickListener(new View.OnClickListener() {
  441.             @Override
  442.             public void onClick(View view) {
  443.                 finish();
  444.             }
  445.         });
  446.  
  447.  
  448.         btnRegister =findViewById(R.id.btnRegRegister);
  449.  
  450.         btnRegister.setOnClickListener(new View.OnClickListener() {
  451.             @Override
  452.             public void onClick(View view) {
  453.  
  454.                 //CHECK IF PASSWORD O.K
  455.                 if (!regPass1.getText().toString().equals(regPass2.getText().toString()))
  456.                 {
  457.                     Toast.makeText (context, "Password not match", Toast.LENGTH_SHORT).show();
  458.                     return;
  459.                 }
  460.                 if (regPass1.getText().toString().length()<3)
  461.                 {
  462.                     Toast.makeText(context, "Password must be 3 letter minimum", Toast.LENGTH_SHORT).show();
  463.                     return;
  464.                 }
  465.                 if(!userUtil.registerUser(regUser.getText().toString(),regPass1.getText().toString()))
  466.                 {
  467.                     Toast.makeText(context, "Error in user registration....", Toast.LENGTH_SHORT).show();
  468.                     return;
  469.                 }
  470.                 Toast.makeText(context, "new user is add...", Toast.LENGTH_SHORT).show();
  471.                 finish();
  472.             }
  473.         });
  474.  
  475.     }
  476. }
  477.  
  478. =========
  479.  
  480. Register.java
  481. ======
  482. package com.example.sale7.myapplication3;
  483.  
  484. import android.support.v7.app.AppCompatActivity;
  485. import android.os.Bundle;
  486. import android.view.View;
  487. import android.widget.EditText;
  488. import android.widget.Toast;
  489.  
  490. public class Register extends AppCompatActivity implements View.OnClickListener {
  491.  
  492.     EditText rUser, rPass, rCheck;
  493.  
  494.     @Override
  495.     protected void onCreate(Bundle savedInstanceState) {
  496.         super.onCreate(savedInstanceState);
  497.         setContentView(R.layout.activity_register2);
  498.         setPointer();
  499.     }
  500.  
  501.     private void setPointer() {
  502.         rUser = (EditText) findViewById(R.id.regUserName);
  503.         rPass = (EditText) findViewById(R.id.regPass1);
  504.         rCheck = (EditText) findViewById(R.id.regPass2);
  505.     }
  506.  
  507.     @Override
  508.     public void onClick(View view) {
  509.         /*switch (view.getId()) {
  510.             case R.id.rCancel:
  511.                 finish();
  512.                 break;
  513.  
  514.             case R.id.rRegister:
  515.                 registerUser();
  516.                 break;
  517.  
  518.             default:
  519.                 Toast.makeText(this, "Error in buttons", Toast.LENGTH_SHORT).show();
  520.         }*/
  521.     }
  522.  
  523.     private void registerUser() {
  524.         //check if all fields filled
  525.         if (rUser.getText().toString().isEmpty() ||
  526.  
  527.                 rPass.getText().toString().isEmpty() ||
  528.  
  529.                 rCheck.getText().toString().isEmpty())
  530.         {
  531.             Toast.makeText(this, "Please fill all fields", Toast.LENGTH_LONG).show();
  532.             return;
  533.         }
  534.  
  535.         //check if minimum password length > 2
  536.         if (rPass.getText().toString().length()<3)
  537.         {
  538.             Toast.makeText(this, "Password is too short (3 minimum)", Toast.LENGTH_SHORT).show();
  539.             return;
  540.         }
  541.  
  542.         //check if password match
  543.         if (!rPass.getText().toString().equals(rCheck.getText().toString()))
  544.         {
  545.             Toast.makeText(this, "Password not match", Toast.LENGTH_LONG).show();
  546.             return;
  547.         }
  548.  
  549.         //check if user exists
  550.         if (SharedPref.checkUserExists(rUser.getText().toString(),this))
  551.         {
  552.             Toast.makeText(this, "User already exists", Toast.LENGTH_LONG).show();
  553.             return;
  554.         }
  555.  
  556.         // AFTER REGISTERATION CHECKOUT THE DATA WILL BE RESTORED IN RTHE DATA BASE
  557.         SharedPref.registerUser(rUser.getText().toString(),rPass.getText().toString(),this);
  558.         finish();
  559.     }
  560. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement