Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- Button btnSingIn,btnRegister;
- RelativeLayout rootLayout;
- FirebaseAuth auth;
- FirebaseDatabase db;
- DatabaseReference users;
- private final static int PERMISSION = 1000;
- @Override
- protected void attachBaseContext(Context newBase) {
- super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //be4 set content view
- CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
- .setDefaultFontPath("fonts/MontserratAlternates-Regular.otf")
- .setFontAttrId(R.attr.fontPath)
- .build()
- );
- setContentView(R.layout.activity_main);
- //init firebase
- auth = FirebaseAuth.getInstance();
- db = FirebaseDatabase.getInstance();
- users = db.getReference("Rider");
- rootLayout = (RelativeLayout)findViewById(R.id.rootLayout);
- //init view
- btnRegister = (Button) findViewById(R.id.btnRegister);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showRegisterDialog();
- }
- });
- btnSingIn = (Button)findViewById(R.id.btnSignin);
- btnSingIn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- btnSingIn.setEnabled(false);
- showLoginDialog();
- }
- });
- }
- private void showLoginDialog() {
- AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setTitle("SIGN IN ");
- dialog.setMessage("Please use email to sign in");
- LayoutInflater inflater = LayoutInflater.from(this);
- View login_Layout = inflater.inflate(R.layout.layout_signin,null);
- final MaterialEditText editEmail = login_Layout.findViewById(R.id.editEmail);
- final MaterialEditText editPassword = login_Layout.findViewById(R.id.editPassword);
- dialog.setView(login_Layout);
- //set button
- dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- //set disable button sign in if is progressing
- btnSingIn.setEnabled(false);
- //check validation
- if (TextUtils.isEmpty(editEmail.getText().toString()))
- {
- Snackbar.make(rootLayout,"Please enter your email adress",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- if (TextUtils.isEmpty(editPassword.getText().toString()))
- {
- Snackbar.make(rootLayout,"Please enter your password",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- if (editPassword.getText().toString().length() < 6)
- {
- Snackbar.make(rootLayout,"Password too short !!!",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- final SpotsDialog waitingDialog = new SpotsDialog(MainActivity.this);
- waitingDialog.show();
- //login
- auth.signInWithEmailAndPassword(editEmail.getText().toString(),editPassword.getText().toString())
- .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
- @Override
- public void onSuccess(AuthResult authResult) {
- waitingDialog.show();
- startActivity(new Intent(MainActivity.this,Home.class));
- finish();
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- waitingDialog.show();
- Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
- .show();
- //active button
- btnSingIn.setEnabled(true);
- }
- });
- }
- });
- dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- dialog.show();
- }
- private void showRegisterDialog() {
- AlertDialog.Builder dialog = new AlertDialog.Builder(this);
- dialog.setTitle("REGISTER ");
- dialog.setMessage("Please use email to register");
- LayoutInflater inflater = LayoutInflater.from(this);
- View register_Layout = inflater.inflate(R.layout.layout_register,null);
- final MaterialEditText editEmail = register_Layout.findViewById(R.id.editEmail);
- final MaterialEditText editPassword = register_Layout.findViewById(R.id.editPassword);
- final MaterialEditText editName = register_Layout.findViewById(R.id.editName);
- final MaterialEditText editPhone = register_Layout.findViewById(R.id.editPhone);
- dialog.setView(register_Layout);
- //set button
- dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- //check validation
- if (TextUtils.isEmpty(editEmail.getText().toString()))
- {
- Snackbar.make(rootLayout,"Please enter your email adresse",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- if (TextUtils.isEmpty(editPhone.getText().toString()))
- {
- Snackbar.make(rootLayout,"Please enter your phone number",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- if (TextUtils.isEmpty(editPassword.getText().toString()))
- {
- Snackbar.make(rootLayout,"Please enter your password",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- if (editPassword.getText().toString().length() < 6)
- {
- Snackbar.make(rootLayout,"Password too short !!!",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- //register new user
- auth.createUserWithEmailAndPassword(editEmail.getText().toString(),editPassword.getText().toString())
- .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
- @Override
- public void onSuccess(AuthResult authResult) {
- //save user to db
- Rider user = new Rider();
- user.setEmail(editEmail.getText().toString());
- user.setName(editName.getText().toString());
- user.setPhone(editPhone.getText().toString());
- user.setPassword(editPassword.getText().toString());
- //user email to key
- users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
- .setValue(user)
- .addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- Snackbar.make(rootLayout,"Register success fully !!!",Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Snackbar.make(rootLayout,"Faild !!!"+e.getMessage(),Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- });
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception e) {
- Snackbar.make(rootLayout,"Faild !!!"+e.getMessage(),Snackbar.LENGTH_SHORT)
- .show();
- return;
- }
- });
- }
- });
- dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- dialog.show();
- }
- }
- public class Home extends AppCompatActivity
- implements NavigationView.OnNavigationItemSelectedListener,
- OnMapReadyCallback,
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- LocationListener{
- SupportMapFragment mapFragment;
- //location
- private GoogleMap mMap;
- private static final int MY_PERMISSION_REQUEST_CODE = 7000;
- private static final int PLAY_services_RES_REQUEST = 7001;
- private LocationRequest mLocationRequest;
- private GoogleApiClient mGoogleApiClient;
- private Location mLastLocation;
- private static int UPDATE_INTERVAL = 5000;
- private static int FASTEST_INTERVAL = 3000;
- private static int DISPLACEMENT = 10;
- DatabaseReference drivers;
- GeoFire geoFire;
- Marker mUserMarker;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- drawer.addDrawerListener(toggle);
- toggle.syncState();
- NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- navigationView.setNavigationItemSelectedListener(this);
- //maps
- mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- //geo fire
- drivers = FirebaseDatabase.getInstance().getReference("Drivers");
- geoFire = new GeoFire(drivers);
- setUpLocation();
- }
- //ctr o
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- switch (requestCode)
- {
- case MY_PERMISSION_REQUEST_CODE:
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
- {
- if (checkPlayServices())
- {
- buildGoogleApiClient();
- createLocationRequest();
- displayLocation();
- }
- }
- break;
- }
- }
- private void setUpLocation() {
- if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
- ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED )
- {
- //request run time permission
- ActivityCompat.requestPermissions(this , new String[]{
- android.Manifest.permission.ACCESS_COARSE_LOCATION,
- android.Manifest.permission.ACCESS_FINE_LOCATION,
- },MY_PERMISSION_REQUEST_CODE);
- }
- else
- {
- if (checkPlayServices())
- {
- buildGoogleApiClient();
- createLocationRequest();
- displayLocation();
- }
- }
- }
- private void displayLocation() {
- if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED &&
- ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED )
- {
- return;
- }
- mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- if (mLastLocation != null)
- {
- final double latitude = mLastLocation.getLatitude();
- final double longitude = mLastLocation.getLongitude();
- //update to firebase
- geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
- @Override
- public void onComplete(String key, DatabaseError error) {
- //add maker
- if (mUserMarker != null)
- mUserMarker.remove(); // remove alredy maker
- mUserMarker = mMap.addMarker(new MarkerOptions()
- .position(new LatLng(latitude,longitude))
- .title("Here You are"));
- //move camera to this position
- mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),15.0f));
- }
- });
- }
- else
- {
- Log.d("ERROR","Cannot get your location");
- }
- }
- private void createLocationRequest() {
- mLocationRequest = new LocationRequest();
- mLocationRequest.setInterval(UPDATE_INTERVAL);
- mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
- mLocationRequest.setPriority(mLocationRequest.PRIORITY_HIGH_ACCURACY);
- mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
- }
- private void buildGoogleApiClient() {
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- mGoogleApiClient.connect();
- }
- private boolean checkPlayServices() {
- int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
- if (resultCode != ConnectionResult.SUCCESS)
- {
- if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
- GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_services_RES_REQUEST).show();
- else {
- Toast.makeText(this, "This device is not supported", Toast.LENGTH_SHORT).show();
- finish();
- }
- return false;
- }
- return true;
- }
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- super.onBackPressed();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.home, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @SuppressWarnings("StatementWithEmptyBody")
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- // Handle navigation view item clicks here.
- int id = item.getItemId();
- if (id == R.id.nav_camera) {
- // Handle the camera action
- } else if (id == R.id.nav_gallery) {
- } else if (id == R.id.nav_slideshow) {
- } else if (id == R.id.nav_manage) {
- } else if (id == R.id.nav_share) {
- } else if (id == R.id.nav_send) {
- }
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- drawer.closeDrawer(GravityCompat.START);
- return true;
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- displayLocation();
- startLocationUpdates();
- }
- private void startLocationUpdates() {
- if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED &&
- ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED )
- {
- return;
- }
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, (com.google.android.gms.location.LocationListener) this);
- }
- @Override
- public void onConnectionSuspended(int i) {
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- }
- @Override
- public void onLocationChanged(Location location) {
- mLastLocation = location;
- displayLocation();
- }
- }
- E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.logo.tm.androidriderapp, PID: 16166
- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.logo.tm.androidriderapp/com.logo.tm.androidriderapp.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
- at android.app.ActivityThread.access$800(ActivityThread.java:178)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
- at android.os.Handler.dispatchMessage(Handler.java:111)
- at android.os.Looper.loop(Looper.java:194)
- at android.app.ActivityThread.main(ActivityThread.java:5631)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
- at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
- at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
- at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
- at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
- at com.logo.tm.androidriderapp.Home.onCreate(Home.java:76)
- at android.app.Activity.performCreate(Activity.java:6092)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
- at android.app.ActivityThread.access$800(ActivityThread.java:178)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
- at android.os.Handler.dispatchMessage(Handler.java:111)
- at android.os.Looper.loop(Looper.java:194)
- at android.app.ActivityThread.main(ActivityThread.java:5631)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Add Comment
Please, Sign In to add comment