Advertisement
Guest User

SmartHome 2

a guest
Mar 28th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.49 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:orientation="vertical"
  7.     android:background="#ffffff">
  8.     <LinearLayout
  9.         android:layout_width="match_parent"
  10.         android:layout_height="match_parent"
  11.         android:layout_weight="1"
  12.         android:orientation="vertical">
  13.         <ImageView
  14.             android:layout_width="match_parent"
  15.             android:layout_height="match_parent"
  16.             android:src="@drawable/logo_mysh"/>
  17.     </LinearLayout>
  18.     <LinearLayout
  19.         android:layout_width="match_parent"
  20.         android:layout_height="match_parent"
  21.         android:layout_weight="1"
  22.         android:orientation="vertical"
  23.         android:gravity="center"
  24.         android:layout_marginLeft="@dimen/edge"
  25.         android:layout_marginRight="@dimen/edge">
  26.         <EditText
  27.             android:layout_width="match_parent"
  28.             android:layout_height="wrap_content"
  29.             android:hint="@string/user_name"
  30.             android:id="@+id/txtUser"/>
  31.         <EditText
  32.             android:layout_width="match_parent"
  33.             android:layout_height="wrap_content"
  34.             android:hint="@string/user_pass"
  35.             android:id="@+id/txtPass"
  36.             android:inputType="textPassword"/>
  37.     </LinearLayout>
  38.     <LinearLayout
  39.         android:layout_width="match_parent"
  40.         android:layout_height="match_parent"
  41.         android:layout_weight="1"
  42.         android:orientation="vertical">
  43.         <LinearLayout
  44.             android:layout_width="match_parent"
  45.             android:layout_height="match_parent"
  46.             android:orientation="horizontal"
  47.             android:layout_weight="1"
  48.             android:layout_marginRight="@dimen/edge"
  49.             android:layout_marginLeft="@dimen/edge"
  50.             android:gravity="center">
  51.             <Button
  52.                 android:layout_width="match_parent"
  53.                 android:layout_height="wrap_content"
  54.                 android:layout_weight="1"
  55.                 android:background="#009fff"
  56.                 android:textColor="#ffffff"
  57.                 android:text="@string/btnLogin"
  58.                 android:textSize="22sp"
  59.                 android:layout_margin="@dimen/edge"
  60.                 android:onClick="btnLogin"/>
  61.             <Button
  62.                 android:layout_width="match_parent"
  63.                 android:layout_height="wrap_content"
  64.                 android:layout_weight="1"
  65.                 android:background="#009fff"
  66.                 android:textColor="#ffffff"
  67.                 android:text="@string/btnRegister"
  68.                 android:textSize="22sp"
  69.                 android:layout_margin="@dimen/edge"
  70.                 android:id="@+id/btnRegister"/>
  71.         </LinearLayout>
  72.         <LinearLayout
  73.             android:layout_width="match_parent"
  74.             android:layout_height="2dp"
  75.             android:background="#000000"/>
  76.         <LinearLayout
  77.             android:layout_width="match_parent"
  78.             android:layout_height="match_parent"
  79.             android:orientation="horizontal"
  80.             android:layout_weight="1">
  81.             <Button
  82.                 android:layout_width="match_parent"
  83.                 android:layout_height="wrap_content"
  84.                 android:layout_weight="1"
  85.                 android:background="#009fff"
  86.                 android:textColor="#ffffff"
  87.                 android:text="@string/btnAr"
  88.                 android:textSize="16sp"
  89.                 android:layout_margin="@dimen/edge"
  90.                 android:id="@+id/btnAr"
  91.                 android:onClick="onClick"/>
  92.             <Button
  93.                 android:layout_width="match_parent"
  94.                 android:layout_height="wrap_content"
  95.                 android:layout_weight="1"
  96.                 android:background="#009fff"
  97.                 android:textColor="#ffffff"
  98.                 android:text="@string/btnHE"
  99.                 android:textSize="16sp"
  100.                 android:layout_margin="@dimen/edge"
  101.                 android:id="@+id/btnHe"
  102.                 android:onClick="onClick"/>
  103.             <Button
  104.                 android:layout_width="match_parent"
  105.                 android:layout_height="wrap_content"
  106.                 android:layout_weight="1"
  107.                 android:background="#009fff"
  108.                 android:textColor="#ffffff"
  109.                 android:text="@string/btnRu"
  110.                 android:textSize="16sp"
  111.                 android:layout_margin="@dimen/edge"
  112.                 android:id="@+id/btnRu"
  113.                 android:onClick="onClick"/>
  114.             <Button
  115.                 android:layout_width="match_parent"
  116.                 android:layout_height="wrap_content"
  117.                 android:layout_weight="1"
  118.                 android:background="#009fff"
  119.                 android:textColor="#ffffff"
  120.                 android:text="@string/btnEn"
  121.                 android:textSize="16sp"
  122.                 android:layout_margin="@dimen/edge"
  123.                 android:id="@+id/btnEn"
  124.                 android:onClick="onClick"/>
  125.         </LinearLayout>
  126.     </LinearLayout>
  127. </LinearLayout>
  128.  
  129.  
  130. actyivity_registration.xml
  131. ==============================
  132. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  133.     android:layout_width="match_parent"
  134.     android:layout_height="match_parent"
  135.     android:orientation="vertical"
  136.     android:layout_margin="@dimen/edge"
  137.     android:background="#ffffff">
  138.  
  139.     <LinearLayout
  140.         android:layout_width="match_parent"
  141.         android:layout_height="match_parent"
  142.         android:orientation="vertical"
  143.         android:layout_weight="1">
  144.         <ImageView
  145.             android:layout_width="match_parent"
  146.             android:layout_height="match_parent"
  147.             android:src="@drawable/logo_mysh"/>
  148.     </LinearLayout>
  149.     <LinearLayout
  150.         android:layout_width="match_parent"
  151.         android:layout_height="match_parent"
  152.         android:orientation="vertical"
  153.         android:layout_weight="1"
  154.         android:gravity="center">
  155.         <EditText
  156.             android:layout_width="match_parent"
  157.             android:layout_height="wrap_content"
  158.             android:hint="@string/user_name"
  159.             android:id="@+id/newUserName"/>
  160.         <EditText
  161.             android:layout_width="match_parent"
  162.             android:layout_height="wrap_content"
  163.             android:hint="@string/user_pass"
  164.             android:id="@+id/newUserPass"/>
  165.         <EditText
  166.             android:layout_width="match_parent"
  167.             android:layout_height="wrap_content"
  168.             android:hint="@string/user_pass"
  169.             android:id="@+id/newUserPassCheck"/>
  170.  
  171.     </LinearLayout>
  172.     <LinearLayout
  173.         android:layout_width="match_parent"
  174.         android:layout_height="match_parent"
  175.         android:orientation="horizontal"
  176.         android:layout_weight="1"
  177.         android:gravity="center">
  178.         <Button
  179.             android:layout_width="match_parent"
  180.             android:layout_height="wrap_content"
  181.             android:textColor="#ffffff"
  182.             android:background="#009fff"
  183.             android:text="@string/btnLogin"
  184.             android:textSize="22sp"
  185.             android:id="@+id/btnNewUserReg"
  186.             android:layout_weight="1"
  187.             android:layout_margin="@dimen/edge"/>
  188.         <Button
  189.             android:layout_width="match_parent"
  190.             android:layout_height="wrap_content"
  191.             android:textColor="#ffffff"
  192.             android:background="#009fff"
  193.             android:text="@string/btnCancel"
  194.             android:textSize="22sp"
  195.             android:id="@+id/btnCancel"
  196.             android:layout_weight="1"
  197.             android:layout_margin="@dimen/edge"/>
  198.     </LinearLayout>
  199. </LinearLayout>
  200.  
  201.  
  202. UserAble.java
  203. ========================
  204. package com.example.teacher.mysmarthome;
  205.  
  206. public interface UserAble {
  207.     //check user if not exists - boolean
  208.     //add user - void
  209.     //delete user - void
  210.     //update user - void
  211.     //check user password -boolean
  212.  
  213.     boolean userExists(String userName);
  214.     void addUser(String userName, String userPass);
  215.     void deleteUser(String userName);
  216.     void updateUser(String userName,String newUserPass);
  217.     boolean checkUser(String userName,String userPass);
  218. }
  219.  
  220.  
  221. MainActivity.java
  222. =====================
  223. package com.example.teacher.mysmarthome;
  224.  
  225. import android.content.Context;
  226. import android.content.Intent;
  227. import android.support.v7.app.AppCompatActivity;
  228. import android.os.Bundle;
  229. import android.view.View;
  230. import android.widget.Button;
  231. import android.widget.EditText;
  232. import android.widget.Toast;
  233.  
  234. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  235.     EditText txtUser,txtPass;
  236.     Button btnRegister;
  237.     Context context;
  238.     private final String TEST_USER="zeev";
  239.     private final String TEST_PASS="12345";
  240.     SPusers myUsers;
  241.  
  242.     @Override
  243.     protected void onCreate(Bundle savedInstanceState) {
  244.         super.onCreate(savedInstanceState);
  245.         setContentView(R.layout.activity_main);
  246.         setPointer();
  247.         myUsers = new SPusers(this);
  248.  
  249.     }
  250.  
  251.     private void setPointer() {
  252.         context=this;
  253.         txtUser=findViewById(R.id.txtUser);
  254.         txtPass=findViewById(R.id.txtPass);
  255.         btnRegister=findViewById(R.id.btnRegister);
  256.         btnRegister.setOnClickListener(new View.OnClickListener() {
  257.             @Override
  258.             public void onClick(View view) {
  259.                 //create a new intent
  260.                 Intent intent=new Intent(context,Registration.class);
  261.                 startActivity(intent);
  262.  
  263.                 //bad practice
  264.                 //startActivity(new Intent(context,Registration.class));
  265.             }
  266.         });
  267.     }
  268.  
  269.     public void btnLogin(View view)
  270.     {
  271.         //Toast.makeText(this, "Hello my brother from another mother", Toast.LENGTH_LONG).show();
  272.         String myUser=txtUser.getText().toString();
  273.         String myPass=txtPass.getText().toString();
  274.  
  275.         //check if user exists...
  276.  
  277.     }
  278.  
  279.     @Override
  280.     public void onClick(View view) {
  281.         switch (view.getId())
  282.         {
  283.             case R.id.btnAr:
  284.                 Toast.makeText(context, "Arabic", Toast.LENGTH_SHORT).show();
  285.                 break;
  286.  
  287.             case R.id.btnEn:
  288.                 Toast.makeText(context, "English", Toast.LENGTH_SHORT).show();
  289.                 break;
  290.  
  291.             case R.id.btnHe:
  292.                 Toast.makeText(context, "Hebrew", Toast.LENGTH_SHORT).show();
  293.                 break;
  294.  
  295.             case R.id.btnRu:
  296.                 Toast.makeText(context, "Russion", Toast.LENGTH_SHORT).show();
  297.                 break;
  298.  
  299.             default:
  300.                 Toast.makeText(context, "WTF?!?!?!?", Toast.LENGTH_SHORT).show();
  301.                 break;
  302.         }
  303.     }
  304. }
  305.  
  306.  
  307. SPUsers.java
  308. ====================
  309. package com.example.teacher.mysmarthome;
  310.  
  311. import android.content.Context;
  312. import android.content.SharedPreferences;
  313. import android.preference.PreferenceManager;
  314. import android.widget.Toast;
  315.  
  316. public class SPusers implements UserAble {
  317.     //our shared preferances (saving data to our device)
  318.     SharedPreferences sp; //for reading
  319.     SharedPreferences.Editor spe; //for writing
  320.     Context context;
  321.  
  322.     public SPusers(Context context) {
  323.         this.context=context;
  324.         sp=context.getSharedPreferences("users",Context.MODE_PRIVATE);
  325.         spe=sp.edit();
  326.     }
  327.  
  328.     @Override
  329.     public boolean userExists(String userName) {
  330.         return sp.getString(userName,"-1").equals("-1");
  331.     }
  332.  
  333.     @Override
  334.     public void addUser(String userName, String userPass) {
  335.         spe.putString(userName,userPass);
  336.         spe.commit();
  337.         //Toast.makeText(context, "WE GOT MIRA IN THE SYSTEM!!!", Toast.LENGTH_SHORT).show();
  338.     }
  339.  
  340.     @Override
  341.     public void deleteUser(String userName) {
  342.  
  343.     }
  344.  
  345.     @Override
  346.     public void updateUser(String userName, String newUserPass) {
  347.  
  348.     }
  349.  
  350.     @Override
  351.     public boolean checkUser(String userName, String userPass) {
  352.         /*
  353.         if (userExists(userName))
  354.         {
  355.             if (sp.getString(userName,"-1").equals(userPass))
  356.             {
  357.                 return true;
  358.             }
  359.             else
  360.             {
  361.                 return false;
  362.             }
  363.         }
  364.         return false;
  365.         */
  366.         return sp.getString(userName,"-1").equals(userPass);
  367.     }
  368.  
  369.     public String hatib(String userName)
  370.     {
  371.         return sp.getString(userName,"-1");
  372.     }
  373. }
  374.  
  375.  
  376. Registration.java
  377. ========================
  378. package com.example.teacher.mysmarthome;
  379.  
  380. import android.support.v7.app.AppCompatActivity;
  381. import android.os.Bundle;
  382. import android.widget.Button;
  383. import android.widget.EditText;
  384.  
  385. public class Registration extends AppCompatActivity {
  386.     EditText txtUser,txtPass,txtPass2;
  387.     Button btnRegister,btnCancel;
  388.  
  389.     @Override
  390.     protected void onCreate(Bundle savedInstanceState) {
  391.         super.onCreate(savedInstanceState);
  392.         setContentView(R.layout.activity_registration);
  393.         setPointer();
  394.     }
  395.  
  396.     private void setPointer() {
  397.         //class work, add pointers
  398.     }
  399.  
  400.     private void registerUser()
  401.     {
  402.         //class work,
  403.         //1. check if password larger then 3 symblos
  404.         //2. check if passwords match
  405.         //3. if user exists
  406.         //4. add user
  407.         //5. give error or successful
  408.  
  409.         //to close activity, use the command finish();
  410.     }
  411.  
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement