Advertisement
Guest User

Untitled

a guest
Dec 30th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 31.64 KB | None | 0 0
  1. Enter_Win.class
  2. ======================================
  3. package com.example.mazal.akatonshildprojct;
  4.  
  5. import android.annotation.SuppressLint;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.nfc.Tag;
  9. import android.provider.Settings;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.Button;
  15.  
  16. public class Enter_Win extends AppCompatActivity {
  17.  
  18.     Context context;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_enter__win);
  24.  
  25.         setPointer();
  26.         chackLogin();
  27.     }
  28.  
  29.     private void setPointer() {
  30.  
  31.         this.context = this;
  32.  
  33.  
  34.     }
  35.  
  36.     private void chackLogin() {
  37.     @SuppressLint("HardwareIds")
  38.     String s=Settings.Secure.getString(context.getContentResolver(),
  39.             Settings.Secure.ANDROID_ID);
  40.         if(User_SharedPref.checkLogin(s,true,context )){
  41.             if(User_SharedPref.getSePeferencesUserStatus(context)==Status.user)
  42.  
  43.             startActivity(new Intent(context, userScreen.class));
  44.  
  45.         }else if(User_SharedPref.getSePeferencesUserStatus(context)==Status.helper){
  46.  
  47.             startActivity(new Intent(context, hlperScreen.class));
  48.         }
  49.     }
  50.  
  51.  
  52.  
  53.     public void onClick(View view) {
  54.  
  55.  
  56.         switch (view.getId()) {
  57.             case R.id.btnEnter:
  58.  
  59.               /* User_SharedPref.setLogin(Settings.Secure.getString(context.getContentResolver(),
  60.                         Settings.Secure.ANDROID_ID),true,context );
  61.                 Log.e("user login:","set to true");*/
  62.  
  63.  
  64.                 startActivity(new Intent(context, userScreen.class));
  65.                 break;
  66.             case R.id.btnReg:
  67.  
  68.               /*  User_SharedPref.setLogin(Settings.Secure.getString(context.getContentResolver(),
  69.                         Settings.Secure.ANDROID_ID),false,context );
  70.                 Log.e("user login:","set to false");*/
  71.                 startActivity(new Intent(context, Register.class));
  72.                 break;
  73.  
  74.         }
  75.     }
  76. }
  77.  
  78.  
  79. User_SharedPref.class
  80. =====================================================================
  81. package com.example.mazal.akatonshildprojct;
  82.  
  83. import android.content.Context;
  84. import android.content.SharedPreferences;
  85. import android.location.Location;
  86. import android.util.Log;
  87.  
  88. import java.sql.Time;
  89. import java.text.ParseException;
  90. import java.text.SimpleDateFormat;
  91. import java.util.Date;
  92.  
  93. /**
  94.  * Created by mazal on 20/12/2017.
  95.  */
  96.  
  97. public class User_SharedPref {
  98.     public static Status getSePeferencesUserStatus(Context context){
  99.  
  100.         SharedPreferences prefs=context.getSharedPreferences("userLoginPrefs",Context.MODE_PRIVATE);
  101.         if(prefs!=null && !prefs.getString("userStatus","def").equals("block")){
  102.             if(prefs.getString("userStatus","def").equals("user")){
  103.                 return Status.user;
  104.             }else if(prefs.getString("userStatus","def").equals("helper")){
  105.                 return Status.helper;
  106.             }
  107.         }
  108.         return null;
  109.     }
  110.  
  111.     public static boolean checkLogin( String userDeviceID ,boolean userIsLogin,  Context context)
  112.     {
  113.         //declaration of shared preferences
  114.         SharedPreferences prefs=context.getSharedPreferences("userLoginPrefs",Context.MODE_PRIVATE);
  115.         //get user pass
  116.         if(prefs==null){
  117.             Log.e("check Login","retern false");
  118.         return  false;
  119.             }
  120.       /*  String spUser=prefs.getString(userName,null);
  121.  
  122.         String spPass=prefs.getString(userPass,null);*/
  123.  
  124.         String spUserDeviceID=prefs.getString("userDeviceID","def");
  125.  
  126.         boolean spUserLogin=prefs.getBoolean("userIsLogin",false);
  127.         //check if password matches
  128.         if (userDeviceID.equals(spUserDeviceID)&& userIsLogin==spUserLogin )
  129.         {
  130.             Log.e("check Login","retern true");
  131.             return true;
  132.  
  133.         }
  134.         Log.e("check Login","retern false");
  135.         return false;
  136.     }
  137.     public static void setLogin(String userDeviceID, Status status ,boolean userIsLogin,Context context){
  138.  
  139.         SharedPreferences prefs=context.getSharedPreferences("userLoginPrefs",Context.MODE_PRIVATE);
  140.  
  141.         SharedPreferences.Editor editor = prefs.edit();
  142.  
  143.         editor.putString("userDeviceID",userDeviceID);
  144.         editor.putString("userStatus", status.name());
  145.         editor.putBoolean("userIsLogin",userIsLogin);
  146.  
  147.  
  148.         editor.commit();
  149.                 Log.e("set Login:","end");
  150.     }
  151.  
  152.     public  static void SetUserPrefInfo(User user, Context context){
  153.  
  154.         SharedPreferences prefs=context.getSharedPreferences("userPrefs",Context.MODE_PRIVATE);
  155.  
  156.         SharedPreferences.Editor editor = prefs.edit();
  157.  
  158.         editor.putString("userName",user.get_userName());
  159.         editor.putString("userPass",user.getUserPass());
  160.         editor.putString("userFirstName",user.getUserFirstName());
  161.         editor.putString("userLastName",user.getUserLastName());
  162.         editor.putString("userPhonNum",user.getUserPhonNum());
  163.         editor.putString("userGander",user.getUserGander());
  164.         editor.putString("userDeviseId",user.getUserDeviseId());
  165.         editor.putString("userPic",user.getUserPic());
  166.         editor.putString("userStatus",user.getUserStatus().name());
  167.         editor.putInt("userWarning",user.getUserWarning());
  168.         editor.putInt("userPoint",user.getUserPoint());
  169.         SimpleDateFormat dateFormat=new SimpleDateFormat("dd/mm/yyyy");
  170.         editor.putString("userAge",dateFormat.format(user.getUserAge()));
  171.         if(user.getUserGpsLocation()!=null) {
  172.             editor.putString("userGpsLatitude", ((Double) user.getUserGpsLocation().getLatitude()).toString());
  173.             editor.putString("userGpsAltitude", ((Double) user.getUserGpsLocation().getAltitude()).toString());
  174.         }else
  175.             editor.putString("userGpsLatitude", null);
  176.         editor.putString("userGpsAltitude", null);
  177.  
  178.         editor.commit();
  179.         Log.e("set UserPrefInfo: ","end");
  180.  
  181.     }
  182.  
  183.  public static User getUserPrefInfo(Context context){
  184.  
  185.      SharedPreferences prefs=context.getSharedPreferences("userPrefs",Context.MODE_PRIVATE);
  186.      Date userAge=new Date();
  187.      try {
  188.           userAge=new SimpleDateFormat("dd/mm/yyyy").parse(prefs.getString("userAge","00/00/0000"));
  189.      } catch (ParseException e) {
  190.          e.printStackTrace();
  191.      }
  192.         User user= new User(prefs.getString("userDeviseId",null),prefs.getString("userName",null),prefs.getString("userPass",null),
  193.                 prefs.getString("userPhonNum",null),Status.valueOf(prefs.getString("userStatus",null)),
  194.                userAge, prefs.getString("userGander",null), prefs.getString("userLastName",null),
  195.                 prefs.getString("userFirstName",null),prefs.getString("userPic","userPic"),prefs.getInt("userWarning",0)
  196.                 ,prefs.getInt("userPoint",0));
  197.  
  198.      if((prefs.getString("userFirstName",null))!=null
  199.              && (prefs.getString("userFirstName",null))!= null)
  200.      {
  201.          Location location = new Location("");
  202.          Double altitude = Double.valueOf(prefs.getString("userGpsAltitude", "0.0"));
  203.          Double latitude = Double.valueOf(prefs.getString("userGpsLatitude", "0.0"));
  204.          location.setAltitude(altitude);
  205.          location.setLatitude(latitude);
  206.          user.setUserGpsLocation(location);
  207.      }
  208.          return user;
  209.      }
  210.  }
  211.  
  212.  
  213.  
  214. Register.class
  215. ===============================================================
  216. package com.example.mazal.akatonshildprojct;
  217.  
  218. import android.content.Intent;
  219. import android.provider.Settings.Secure;
  220. import android.annotation.SuppressLint;
  221. import android.content.Context;
  222. import android.os.AsyncTask;
  223. import android.support.v7.app.AppCompatActivity;
  224. import android.os.Bundle;
  225. import android.util.Log;
  226. import android.view.View;
  227. import android.widget.Button;
  228. import android.widget.CompoundButton;
  229. import android.widget.EditText;
  230. import android.widget.ImageButton;
  231. import android.widget.RadioButton;
  232. import android.widget.TextView;
  233. import android.widget.Toast;
  234.  
  235. import com.cloudant.client.api.ClientBuilder;
  236. import com.cloudant.client.api.CloudantClient;
  237. import com.cloudant.client.api.Database;
  238.  
  239. import java.text.ParseException;
  240. import java.text.SimpleDateFormat;
  241. import java.util.Date;
  242.  
  243. public class Register extends AppCompatActivity {
  244.  
  245.     Context context;
  246.     final String API_KEY="theaverseedessillessided";
  247.     final String API_SECRET="e3a4e995ba3a0015d4ff01c8aa87202bf184c8b0";
  248.     final String API_GATEWAY="06daea71-b918-4743-86ed-0c1e101b0786-bluemix";
  249.     final String API_DB="hero_db";
  250.  
  251.     EditText edtFirstName,edtLastName,edtPass,edtOkPass,edtDate,edtUserName;
  252.     RadioButton rdbMale,rdbFemale,rdbUser,rdbHelper;
  253.     Button ibtnProfile;
  254.     User user;
  255.  
  256.  
  257.     @Override
  258.     protected void onCreate(Bundle savedInstanceState) {
  259.         super.onCreate(savedInstanceState);
  260.         setContentView(R.layout.activity_register);
  261.  
  262.         pointer();
  263.     }
  264.  
  265.     private void pointer() {
  266.  
  267.         this.context=this;
  268.  
  269.         edtFirstName=findViewById(R.id.register_name);
  270.          edtLastName=findViewById(R.id.register_last_name);
  271.         edtPass=findViewById(R.id.register_user_password);
  272.          edtOkPass=findViewById(R.id.register_OKUser_name);
  273.          edtDate=findViewById(R.id.register_birthday);
  274.         edtUserName=findViewById(R.id.register_user_name);
  275.  
  276.         rdbMale=findViewById(R.id.register_male);
  277.          rdbFemale=findViewById(R.id.register_female);
  278.         rdbHelper=findViewById(R.id.register_i_am_user);
  279.         rdbUser=findViewById(R.id.register_i_am_volunteer);
  280.  
  281.          ibtnProfile=findViewById(R.id.register_picture);
  282.  
  283.        setRadioButton();
  284.  
  285.  
  286.     }
  287.  
  288.     private void setRadioButton() {
  289.  
  290.         rdbMale.setChecked(true);
  291.         rdbUser.setChecked(true);
  292.         rdbFemale.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  293.             @Override
  294.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  295.                 compoundButton.setChecked(b);
  296.                 rdbMale.setChecked(!b);
  297.             }
  298.         });
  299.         rdbMale.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  300.             @Override
  301.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  302.                 compoundButton.setChecked(b);
  303.                 rdbFemale.setChecked(!b);
  304.             }
  305.         });
  306.  
  307.         rdbUser.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  308.             @Override
  309.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  310.                 compoundButton.setChecked(b);
  311.                 rdbHelper.setChecked(!b);
  312.             }
  313.         });
  314.         rdbHelper.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  315.             @Override
  316.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  317.                 compoundButton.setChecked(b);
  318.                 rdbUser.setChecked(!b);
  319.             }
  320.         });
  321.  
  322.     }
  323.  
  324.     public void onClick(View view) {
  325.  
  326.         switch (view.getId()) {
  327.  
  328.             case R.id.btn_Registration: {
  329.                 Log.e("Ragister", " switch 1");
  330.                 setUser();
  331.             }
  332.  
  333.                 break;
  334.  
  335.  
  336.  
  337.         }
  338.  
  339.     }
  340.  
  341.     @SuppressLint("StaticFieldLeak")
  342.     private void setUser() {
  343.         Log.e("Ragister"," btn fonction" );
  344.  
  345.  
  346.         new AsyncTask<Void, Void, Void>() {
  347.  
  348.             @Override
  349.             protected Void doInBackground(Void... voids) {
  350.                 Log.e("Ragister"," AsyncTask" );
  351.                 boolean isOK=true;
  352.                 //lets connect to our DB.
  353.                 CloudantClient client = ClientBuilder
  354.                         .account(API_GATEWAY)
  355.                         .username(API_KEY)
  356.                         .password(API_SECRET)
  357.                         .build();
  358.  
  359.                 Database db = client.database(API_DB, false);
  360.  
  361.                 Log.e("Ragister"," conect to db" );
  362.               /*  db.save(new User("0000","testUser","1234","0000"
  363.                         , com.example.mazal.akatonshildprojct.Status.user,new Date(),"Male","sdfsfdds"
  364.                         ,"zxczc","user_pic",0,0));
  365.                 Log.e("Ragister"," test user seved" );*/
  366.  
  367.                if(!db.contains(edtUserName.getText().toString()) & !edtUserName.getText().toString().equals("")) {
  368.                     Log.e("Ragister","can't find on db "+edtUserName.getText().toString()+",and no empty" );
  369.                     if (edtPass.getText().toString().equals(edtOkPass.getText().toString())) {
  370.                         Log.e("Ragister",edtPass.getText().toString()+" equals to "+(edtOkPass.getText().toString()) );
  371.                         String android_id = Secure.getString(context.getContentResolver(),
  372.                                 Secure.ANDROID_ID);
  373.  
  374.                         Date userAge=new Date();
  375.                         try {
  376.                             userAge=new SimpleDateFormat("dd/mm/yyyy").parse(edtDate.getText().toString());
  377.                         } catch (ParseException e) {
  378.                             e.printStackTrace();
  379.                         }
  380.  
  381.                         user = new User(android_id,edtUserName.getText().toString(),edtPass.getText().toString(),"",
  382.                                 getNewStatus(),userAge,getGender(), edtLastName.getText().toString(),edtFirstName.getText().toString(),
  383.                                 ibtnProfile.getText().toString(),0,0);
  384.                         Log.e("Ragister",user.toString());
  385.                         db.save(user);
  386.                         User_SharedPref.SetUserPrefInfo(user,context);
  387.  
  388.  
  389.                     }else{
  390.  
  391.                         Log.e("Ragister","Pass: "+edtPass.getText().toString()+" not equals to OkPass: "+edtOkPass.getText().toString());
  392.                     }
  393.                 }else{
  394.  
  395.                     Log.e("Ragister","user name exists or empty");
  396.                 }
  397.  
  398.                 return null;
  399.             }
  400.  
  401.             @Override
  402.             protected void onPostExecute(Void aVoid) {
  403.                 Toast.makeText(context, "Data was saved...", Toast.LENGTH_SHORT).show();
  404.                 if(getNewStatus().name().equals("user")){
  405.  
  406.                     startActivity(new Intent(context, userScreen.class));
  407.                 }else{
  408.  
  409.                     startActivity(new Intent(context, hlperScreen.class));
  410.                 }
  411.  
  412.             }
  413.         }.execute();
  414.  
  415.     }
  416.  
  417. private Status getNewStatus(){
  418.  
  419.         if(rdbUser.isChecked()){
  420.  
  421.             return  Status.user;
  422.         }else
  423.         {
  424.  
  425.             return Status.helper;
  426.         }
  427.     }
  428.     private String getGender(){
  429.  
  430.         if(rdbMale.isChecked()){
  431.  
  432.             return  "Male";
  433.         }else
  434.         {
  435.  
  436.             return "Female";
  437.         }
  438.     }
  439.  
  440.  
  441. }
  442.  
  443.  
  444. User.class
  445. ================================================================================================
  446. package com.example.mazal.akatonshildprojct;
  447.  
  448. import android.content.Context;
  449. import android.location.Location;
  450. import android.os.AsyncTask;
  451. import android.provider.Settings;
  452.  
  453. import java.util.Date;
  454.  
  455.     /**
  456.      * Created by mazal on 11/12/2017.
  457.      */
  458.     enum Status{user,helper,block;}
  459.     public class User {
  460.  
  461.  
  462.         private String _id;    //unick user name
  463.         private String userPass;
  464.         private String userPhonNum;
  465.         private String userDeviseId;
  466.         private Status userStatus;
  467.         private Date userAge;
  468.         private String userGander;
  469.         private String userLastName;
  470.         private String userName;
  471.         private Location userGpsLocation;
  472.         private String userPic;
  473.         private int userWarning;
  474.         private int userPoint;
  475.  
  476.  
  477.  
  478.  
  479.  
  480.         public User(String userDeviseId, String userName, String userPass, String userPhonNum, Status userStatus,
  481.                     Date userAge, String userGander, String userLastName, String name, String userPic, int userWarning,int userPoint) {
  482.             this._id = userName;
  483.             this.userPass = userPass;
  484.             this.userPhonNum = userPhonNum;
  485.             this.userDeviseId = userDeviseId;
  486.             this.userStatus = userStatus;
  487.             this.userAge = userAge;
  488.             this.userGander = userGander;
  489.             this.userLastName = userLastName;
  490.             this.userName = name;
  491.             if(userPic!=null)
  492.             {this.userPic = userPic;}
  493.             else
  494.             { this.userPic="user_pic";}
  495.             this.userWarning = userWarning;
  496.             this.userPoint = userPoint;
  497.  
  498.         }
  499.  
  500.  
  501.  
  502.         public String get_userName() {
  503.             return _id;
  504.         }
  505.  
  506.  
  507.         public String getUserPass() {
  508.             return userPass;
  509.         }
  510.  
  511.         public void setUserPass(String userPass) {
  512.             this.userPass = userPass;
  513.         }
  514.  
  515.         public String getUserPhonNum() {
  516.             return userPhonNum;
  517.         }
  518.  
  519.         public void setUserPhonNum(String userPhonNum) {
  520.             this.userPhonNum = userPhonNum;
  521.         }
  522.  
  523.         public String getUserDeviseId() {
  524.  
  525.             return userDeviseId;
  526.         }
  527.  
  528.         public void setUserDeviseId(Context context) {
  529.             this.userDeviseId = Settings.Secure.getString(context.getContentResolver(),
  530.                     Settings.Secure.ANDROID_ID);
  531.         }
  532.  
  533.         public Status getUserStatus() {
  534.             return userStatus;
  535.         }
  536.  
  537.         public void setUserStatus(Status userStatus) {
  538.             this.userStatus = userStatus;
  539.         }
  540.  
  541.         public Date getUserAge() {
  542.             return userAge;
  543.         }
  544.  
  545.         public void setUserAge(Date userAge) {
  546.             this.userAge = userAge;
  547.         }
  548.  
  549.         public String getUserGander() {
  550.             return userGander;
  551.         }
  552.  
  553.         public void setUserGander(String userGander) {
  554.             this.userGander = userGander;
  555.         }
  556.  
  557.         public String getUserLastName() {
  558.             return userLastName;
  559.         }
  560.  
  561.         public void setUserLastName(String userLastName) {
  562.             this.userLastName = userLastName;
  563.         }
  564.  
  565.         public String getUserFirstName() {
  566.             return userName;
  567.         }
  568.  
  569.         public void setUserFirstName(String userFirstName) {
  570.             this.userName = userFirstName;
  571.         }
  572.  
  573.         public Location getUserGpsLocation() {
  574.             return userGpsLocation;
  575.         }
  576.  
  577.         public void setUserGpsLocation(Location userGpsLocation) {
  578.             this.userGpsLocation = userGpsLocation;
  579.         }
  580.  
  581.         public String getUserPic() {
  582.             return userPic;
  583.         }
  584.  
  585.         public void setUserPic(String userPic) {
  586.             this.userPic = userPic;
  587.         }
  588.  
  589.  
  590.         public void setUserDeviseId(String userDeviseId) {
  591.             this.userDeviseId = userDeviseId;
  592.         }
  593.  
  594.  
  595.         public int getUserWarning() {
  596.             return userWarning;
  597.         }
  598.  
  599.         public void setUserWarning(int userWarning) {
  600.  
  601.             this.userWarning = userWarning;
  602.             if(this.userWarning>=3){
  603.                 setUserStatus(Status.block);
  604.             }
  605.         }
  606.  
  607.         public int getUserPoint() {
  608.             return userPoint;
  609.         }
  610.  
  611.         public void setUserPoint(int userPoint) {
  612.             this.userPoint = userPoint;
  613.         }
  614.  
  615.         @Override
  616.         public String toString() {
  617.             return "User{" +
  618.                     "_id='" + _id + '\'' +
  619.                     ", userPass='" + userPass + '\'' +
  620.                     ", userPhonNum='" + userPhonNum + '\'' +
  621.                     ", userDeviseId='" + userDeviseId + '\'' +
  622.                     ", userStatus=" + userStatus +
  623.                     ", userAge=" + userAge +
  624.                     ", userGander='" + userGander + '\'' +
  625.                     ", userLastName='" + userLastName + '\'' +
  626.                     ", userName='" + userName + '\'' +
  627.                     ", userGpsLocation=" + userGpsLocation +
  628.                     ", userPic='" + userPic + '\'' +
  629.                     ", userPoint='" + userPoint + '\'' +
  630.                     ", userWarning=" + userWarning +
  631.                     '}';
  632.         }
  633. }
  634.  
  635.  
  636.  
  637. hlperScreen.class
  638. =========================================================================
  639. package com.example.mazal.akatonshildprojct;
  640.  
  641.  
  642. import java.io.IOException;
  643. import java.util.List;
  644.  
  645. import android.app.AlertDialog;
  646. import android.app.Dialog;
  647. import android.app.ListActivity;
  648. import android.content.DialogInterface;
  649. import android.content.Intent;
  650. import android.content.SharedPreferences;
  651. import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  652. import android.os.Bundle;
  653.  
  654. import android.view.View;
  655.  
  656. import android.widget.Toast;
  657.  
  658.  
  659. import android.annotation.SuppressLint;
  660. import android.content.Context;
  661.  
  662. import android.os.AsyncTask;
  663.  
  664.  
  665. import android.support.v7.app.AppCompatActivity;
  666.  
  667. import android.widget.TextView;
  668. import android.widget.Toast;
  669.  
  670. import com.cloudant.client.api.ClientBuilder;
  671. import com.cloudant.client.api.CloudantClient;
  672. import com.cloudant.client.api.Database;
  673.  
  674.  
  675.  
  676. public class hlperScreen extends AppCompatActivity{
  677.     final Context context=this;
  678.     final String API_KEY="theaverseedessillessided";
  679.     final String API_SECRET="e3a4e995ba3a0015d4ff01c8aa87202bf184c8b0";
  680.     final String API_GATEWAY="06daea71-b918-4743-86ed-0c1e101b0786-bluemix";
  681.     final String API_DB="hero_db";
  682.     List<String> ID;
  683.     String S="";
  684.     TextView txtMsg;
  685.     List<User> listUser;
  686.     User u;
  687.  
  688.  
  689.     @SuppressLint("StaticFieldLeak")
  690.     @Override
  691.     protected void onCreate(Bundle savedInstanceState) {
  692.         super.onCreate(savedInstanceState);
  693.         setContentView(R.layout.activity_hlper_screen);
  694.  
  695.         txtMsg=findViewById(R.id.txtMsg);
  696.  
  697.         new AsyncTask<Void, Void, Void>() {
  698.  
  699.             @Override
  700.             protected Void doInBackground(Void... voids) {
  701.  
  702.  
  703.         CloudantClient client = ClientBuilder
  704.                 .account(API_GATEWAY)
  705.                 .username(API_KEY)
  706.                 .password(API_SECRET)
  707.                 .build();
  708.  
  709.         Database db = client.database(API_DB, false);
  710.  
  711.                 try {
  712.                     listUser =db.getAllDocsRequestBuilder().includeDocs(true).build().getResponse().getDocsAs(User.class);
  713.                 } catch (IOException e) {
  714.                     e.printStackTrace();
  715.                 }
  716.  
  717.                 return null;
  718.             }
  719.  
  720.             @Override
  721.             protected void onPostExecute(Void aVoid) {
  722.                 txtMsg.setText("null");
  723.                 //  txtMsg.setText(u.toString());
  724.                 if(listUser!=null){
  725.            /* for(String s:ID){
  726.                S+=s.toString()+"\n";
  727.             }*/
  728.  
  729.                   for (User user:listUser ) {
  730.             S+=user.toString()+"\n";
  731.         }  txtMsg.setText(S);
  732.                 }
  733.                 Toast.makeText(context, "Data was saved...", Toast.LENGTH_SHORT).show();
  734.  
  735.             }
  736.         }.execute();
  737.  
  738.  
  739.  
  740.     }
  741.  
  742. }
  743.  
  744.  
  745.  
  746.  
  747. userScreen.class
  748. ===================================================================
  749. package com.example.mazal.akatonshildprojct;
  750.  
  751. import android.provider.Settings.Secure;
  752. import android.Manifest;
  753. import android.annotation.SuppressLint;
  754. import android.content.Context;
  755. import android.content.DialogInterface;
  756. import android.content.pm.PackageManager;
  757. import android.location.Address;
  758. import android.location.Criteria;
  759. import android.location.Geocoder;
  760. import android.location.Location;
  761. import android.location.LocationListener;
  762. import android.location.LocationManager;
  763. import android.os.AsyncTask;
  764. import android.os.Build;
  765. import android.support.annotation.NonNull;
  766. import android.support.v4.app.ActivityCompat;
  767. import android.support.v4.content.ContextCompat;
  768. import android.support.v7.app.AlertDialog;
  769. import android.support.v7.app.AppCompatActivity;
  770. import android.os.Bundle;
  771. import android.util.Log;
  772. import android.view.View;
  773. import android.widget.TextView;
  774. import android.widget.Toast;
  775.  
  776. import com.cloudant.client.api.ClientBuilder;
  777. import com.cloudant.client.api.CloudantClient;
  778. import com.cloudant.client.api.Database;
  779.  
  780. import java.io.IOException;
  781. import java.security.Provider;
  782. import java.util.ArrayList;
  783. import java.util.List;
  784. import java.util.Locale;
  785.  
  786. public class userScreen extends AppCompatActivity implements LocationListener{
  787.  
  788.    final Context context=this;
  789.     final String API_KEY="theaverseedessillessided";
  790.     final String API_SECRET="e3a4e995ba3a0015d4ff01c8aa87202bf184c8b0";
  791.     final String API_GATEWAY="06daea71-b918-4743-86ed-0c1e101b0786-bluemix";
  792.     final String API_DB="hero_db";
  793.     Location myLocation=null;
  794.     LocationManager gpsManager;
  795.     boolean gpsEnabled=false;
  796.     boolean gpsRedy=false;
  797.     TextView txtMsg;
  798.     User thisUser;
  799.  
  800.  
  801.  
  802.     @Override
  803.     protected void onCreate(Bundle savedInstanceState) {
  804.         super.onCreate(savedInstanceState);
  805.         setContentView(R.layout.activity_user_screen);
  806.  
  807.         setPointr();
  808.         setGPS();
  809.  
  810.     }
  811.     @SuppressLint("StaticFieldLeak")
  812.     public void onClick(View view) {
  813.  
  814.         if (myLocation != null) {
  815.  
  816.  
  817.             new AsyncTask<Void, Void, Void>() {
  818.  
  819.                 @Override
  820.                 protected Void doInBackground(Void... voids) {
  821.                     //lets connect to our DB.
  822.                     CloudantClient client = ClientBuilder
  823.                             .account(API_GATEWAY)
  824.                             .username(API_KEY)
  825.                             .password(API_SECRET)
  826.                             .build();
  827.  
  828.                     Database db = client.database(API_DB, false);
  829.  
  830.                     // A java type that can be serialezed to JSON
  831.                     //Bizzo Tomer = new Bizzo("tomerHaTachun","Tomer",32,true,true,45000,true);
  832.                     //Bizzo Itzik = new Bizzo("Itzik", 31, true,true,8500,false);
  833.                     //Bizzo Itzik_After = new Bizzo("Itzik",32,true,false,52000,true);
  834.                     //Bizzo Shaul = new Bizzo("Shaul",33,false,false,12000,false);
  835.  
  836.  
  837.                     db.save(thisUser);
  838.                     //db.save(Itzik);
  839.                     //db.save(Itzik_After);
  840.                     //db.save(Shaul);
  841.  
  842.                     // Bizzo myBizzoTachun = db.find(Bizzo.class,"tomerHaTachun");
  843.                     // Log.e("test", "doInBackground: "+myBizzoTachun.name+" age:"+myBizzoTachun.age);
  844.                     return null;
  845.                 }
  846.  
  847.                 @Override
  848.                 protected void onPostExecute(Void aVoid) {
  849.                     Toast.makeText(context, "Data was saved...", Toast.LENGTH_SHORT).show();
  850.                     txtMsg.setText("The request sent ");
  851.                 }
  852.             }.execute();
  853.         }
  854.  
  855.     }
  856.  
  857.     private void setPointr() {
  858.  
  859.  
  860.         txtMsg=findViewById(R.id.txtMsg);
  861.        // thisUser=new User(myLocation,Status.user, Secure.getString(context.getContentResolver(),
  862.        //         Secure.ANDROID_ID),"user");
  863.  
  864.     }
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.     private void setGPS() {
  872.  
  873.     //deal with GPS
  874.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  875.  
  876.         return;
  877.     }
  878.     gpsManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  879.      //   gpsManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30, 50, this);
  880.     //for mobile device only, since we don't have a network GPRS on our F emulator
  881.     gpsManager.requestLocationUpdates(getBestProvider(),30,50,this);
  882. }
  883.  
  884.     private String getBestProvider()
  885.     {
  886.         gpsManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  887.         Criteria criteria= new Criteria();
  888.         criteria.setAccuracy(Criteria.ACCURACY_FINE);
  889.         criteria.setAltitudeRequired(true);
  890.         criteria.setBearingRequired(true);
  891.         Log.e("provider", "getBestProvider: "+gpsManager.getBestProvider(criteria,true));
  892.  
  893.         return gpsManager.getBestProvider(criteria,true);
  894.  
  895.     }
  896.  
  897.     private void requestGpsPerm() {
  898.         List<String> permList=new ArrayList<>();
  899.         int finePerm = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
  900.         int corasePerm = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
  901.         int audioPerm = ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO);
  902.  
  903.         gpsEnabled=true;
  904.         if (finePerm != PackageManager.PERMISSION_GRANTED) {
  905.             permList.add(Manifest.permission.ACCESS_FINE_LOCATION);
  906.             gpsEnabled=false;
  907.         }
  908.         if (corasePerm != PackageManager.PERMISSION_GRANTED)
  909.         {
  910.             permList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
  911.             gpsEnabled=false;
  912.         }
  913.         if (audioPerm != PackageManager.PERMISSION_GRANTED)
  914.         {
  915.             permList.add(Manifest.permission.RECORD_AUDIO);
  916.         }
  917.  
  918.         if (!permList.isEmpty())
  919.         {
  920.             ActivityCompat.requestPermissions(this, permList.toArray(new String[permList.size()]),100);
  921.         }
  922.         else
  923.         {
  924.  
  925.             gpsManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  926.  
  927.  
  928.             gpsManager.requestLocationUpdates(getBestProvider(), 30, 50, this);
  929.            
  930.  
  931.         }
  932.  
  933.     }
  934.  
  935.  
  936.  
  937.  
  938.     @Override
  939.     public void onLocationChanged(Location location) {
  940.         gpsRedy=true;
  941.         this.myLocation=location;
  942.         Log.e("GPS", "onLocationChanged: "+location.getLatitude()+","+location.getLongitude());
  943.  
  944.        // txtMsg.setText("onLocationChanged: "+location.getLatitude()+","+location.getLongitude());
  945.         //Geocoder....
  946.       /*  Geocoder gcd;
  947.         if (Build.VERSION.SDK_INT >=21) //android 5.00 and above
  948.         {
  949.             //gcd = new Geocoder(this, Locale.forLanguageTag("en"));
  950.             gcd = new Geocoder(this,Locale.forLanguageTag("he"));
  951.         }
  952.         else
  953.         {
  954.             gcd = new Geocoder(this);
  955.         }
  956.         List<Address> addresses = null;
  957.         try {
  958.             addresses = gcd.getFromLocation(myLocation.getLatitude(),myLocation.getLongitude(),1);
  959.         } catch (IOException e) {
  960.             e.printStackTrace();
  961.         }
  962.         if (addresses==null) return;
  963.         if (addresses.size()>0)
  964.         {
  965.             Log.e("GCD", "onLocationChanged: "+ addresses.get(0).toString() );
  966.         }   */
  967.     }
  968.  
  969.     @Override
  970.     public void onStatusChanged(String s, int i, Bundle bundle) {
  971.         Toast.makeText(context, "GPS Status changed", Toast.LENGTH_SHORT).show();
  972.     }
  973.  
  974.     @Override
  975.     public void onProviderEnabled(String s) {
  976.         Toast.makeText(context, "GPS is on", Toast.LENGTH_SHORT).show();
  977.     }
  978.  
  979.     @Override
  980.     public void onProviderDisabled(String s) {
  981.         Toast.makeText(context, "GPS is off", Toast.LENGTH_SHORT).show();
  982.     }
  983.  
  984.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  985.         if (requestCode==100)
  986.         {
  987.             Log.e("perm", "onRequestPermissionsResult: "+permissions.toString() );
  988.             Log.e("grant", "onRequestPermissionsResult: "+grantResults );
  989.  
  990.         }
  991.     }
  992.  
  993.  
  994. }
  995.  
  996.  
  997.  
  998. add to manifest
  999. =========================================================
  1000.  
  1001.  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  1002.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  1003.     <uses-permission android:name="android.permission.INTERNET" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement