Java tutorial
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.saloon.android.bluecactus.app.UI; import android.Manifest; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentSender; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.design.widget.TabLayout; import android.support.graphics.drawable.VectorDrawableCompat; import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.content.ContextCompat; import android.support.v4.content.res.ResourcesCompat; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.GooglePlayServicesUtil.*; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.common.GooglePlayServicesRepairableException; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.PendingResult; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationSettingsRequest; import com.google.android.gms.location.LocationSettingsResult; import com.google.android.gms.location.LocationSettingsStates; import com.google.android.gms.location.LocationSettingsStatusCodes; import com.google.android.gms.location.places.ui.PlacePicker; import com.saloon.android.bluecactus.R; import com.saloon.android.bluecactus.app.Models.Division; import com.saloon.android.bluecactus.app.Network.NetworkRequests; import com.saloon.android.bluecactus.app.Utils.GlobalVariables; import com.saloon.android.bluecactus.app.Utils.Locator; import java.util.ArrayList; import java.util.List; /** * Provides UI for the main screen. */ public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener { private DrawerLayout mDrawerLayout; public static ArrayList<Division> divisionList; private static final int LOCATION_PERMISSION_REQUEST_CODE = 4152; private LinearLayout containorLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); enableMyLocation(); // Location check Locator locator = new Locator(this); locator.getLocation(Locator.Method.NETWORK, listener); // Network request test with volley NetworkRequests networkRequest = new NetworkRequests(this); divisionList = networkRequest.getDivisions(); Log.i("Array List size: ", divisionList.size() + ""); GlobalVariables.getInstance().setArrayList(divisionList); // Adding Toolbar to Main screen Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Setting ViewPager for each Tabs ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); // Set Tabs inside Toolbar TabLayout tabs = (TabLayout) findViewById(R.id.tabs); tabs.setupWithViewPager(viewPager); // Create Navigation drawer and inlfate layout NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); // Adding menu icon to Toolbar ActionBar supportActionBar = getSupportActionBar(); if (supportActionBar != null) { VectorDrawableCompat indicator = VectorDrawableCompat.create(getResources(), R.drawable.ic_menu, getTheme()); indicator.setTint(ResourcesCompat.getColor(getResources(), R.color.white, getTheme())); supportActionBar.setHomeAsUpIndicator(indicator); supportActionBar.setDisplayHomeAsUpEnabled(true); } // Set behavior of Navigation drawer navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { // This method will trigger on item Click of navigation menu @Override public boolean onNavigationItemSelected(MenuItem menuItem) { // Set item in checked state menuItem.setChecked(true); Log.d("Menu Item id: ", menuItem.getTitle() + ""); handleMenuClick(menuItem.getTitle().toString()); // TODO: handle navigation // Toast.makeText(MainActivity.this, "Clicked: " + menuItem.getTitle(), Toast.LENGTH_SHORT).show(); // // Intent intent = new Intent(MainActivity.this, Appointment.class); // // MainActivity.this.startActivity(intent); // // Closing drawer on item click mDrawerLayout.closeDrawers(); return true; } }); // Adding Floating Action Button to bottom right of main view FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v, "Hello Snackbar!", Snackbar.LENGTH_LONG).show(); } }); } private void handleMenuClick(String title) { Log.d(title, title); if (title.equals("Appointments")) { // Toast.makeText(MainActivity.this, "Clicked: " + menuItem.getTitle(), Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this, Appointment.class); MainActivity.this.startActivity(intent); } if (title.equals("Logout")) { Log.d("Logout Called", ""); logoutUser(); } } public void logoutUser() { // Log.d("Logout User Called: ", ""); SharedPreferences settings = this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE); settings.edit().clear().commit(); Intent intent = new Intent(MainActivity.this, LoginActivity.class); MainActivity.this.startActivity(intent); } // Add Fragments to Tabs private void setupViewPager(ViewPager viewPager) { Adapter adapter = new Adapter(getSupportFragmentManager()); adapter.addFragment(new ListContentFragment(), "List"); adapter.addFragment(new TileContentFragment(), "Tile"); adapter.addFragment(new CardContentFragment(), "Card"); viewPager.setAdapter(adapter); } /** * Enables the My Location layer if the fine location permission has been granted. */ private void enableMyLocation() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermission(); } else { // Access to the location has been granted to the app. getLocation(); } } private void requestPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { Toast.makeText(this, "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show(); ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, getResources().getInteger(R.integer.LOCATION_PERMISSION_REQUEST_CODE)); } else { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, getResources().getInteger(R.integer.LOCATION_PERMISSION_REQUEST_CODE)); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case LOCATION_PERMISSION_REQUEST_CODE: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { getLocation(); } else { Toast.makeText(this, "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG) .show(); } break; } } @Override public void onClick(DialogInterface dialog, int which) { int titleId = getResources().getIdentifier("alertTitle", "id", "android"); if (titleId > 0) { TextView dialogTitle = (TextView) ((android.support.v7.app.AlertDialog) dialog).findViewById(titleId); if (dialogTitle != null) { // View v=containorLayout.findViewWithTag(dialogTitle); } } if (dialog != null) { dialog.dismiss(); } } private void getLocation() { Locator locator = new Locator(this); locator.getLocation(Locator.Method.NETWORK_THEN_GPS, listener); } private Locator.Listener listener = new Locator.Listener() { @Override public void onLocationFound(Location location) { String latitude = String.valueOf(location.getLatitude()); String longitude = String.valueOf(location.getLongitude()); Log.d("latitude", String.valueOf(location.getLatitude())); Log.d("longitude", String.valueOf(location.getLongitude())); SharedPreferences shared = getSharedPreferences("MyPrefs", MODE_PRIVATE); String userId = (shared.getString("id", "")); Log.d("Location userid: ", userId); if (userId != null && !userId.isEmpty() && latitude != null && !latitude.isEmpty()) { NetworkRequests networkRequests = new NetworkRequests(getApplicationContext()); networkRequests.sendUserLocationToServer(latitude, longitude, userId); } } @Override public void onLocationNotFound() { // savingFormToDB(); } }; static class Adapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public Adapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, 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; } else if (id == android.R.id.home) { mDrawerLayout.openDrawer(GravityCompat.START); } return super.onOptionsItemSelected(item); } }