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.juliovaz.condio.activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; 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.LocalBroadcastManager; 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.TextView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import com.juliovaz.condio.Manifest; import com.juliovaz.condio.R; import com.juliovaz.condio.fragment.BuildingMessageFragment; import com.juliovaz.condio.fragment.ProductMaintenanceFragment; import com.juliovaz.condio.fragment.ReservationFragment; import com.juliovaz.condio.network.gcm.QuickstartPreferences; import com.juliovaz.condio.network.gcm.RegistrationIntentService; import com.juliovaz.condio.util.ConstantsCondio; import java.util.ArrayList; import java.util.List; /** * Provides UI for the main screen. */ public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private DrawerLayout mDrawerLayout; private FloatingActionButton fab; private BroadcastReceiver mRegistrationBroadcastReceiver; private boolean isReceiverRegistered; private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private ViewPager viewPager; private static int WRITE_STORAGE_PERMISSION = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Adding Toolbar to Main screen Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); fab = (FloatingActionButton) findViewById(R.id.btn_new_reservation); // Setting ViewPager for each Tabs 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 final 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); Context context = navigationView.getContext(); Intent intent; switch (menuItem.getItemId()) { case R.id.menu_item_home: intent = new Intent(context, MainActivity.class); context.startActivity(intent); break; case R.id.menu_item_reservation_history: intent = new Intent(context, ReservationHistoryActivity.class); context.startActivity(intent); fab.hide(); break; case R.id.menu_item_bills: intent = new Intent(context, BillActivity.class); context.startActivity(intent); break; default: break; } // Closing drawer on item click mDrawerLayout.closeDrawers(); return true; } }); /* --- checar se role == sindico ---- */ // Adding Floating Action Button to bottom right of main view fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Context context = view.getContext(); Intent intent = new Intent(context, ReservationActivity.class); context.startActivity(intent); } }); initGcm(); viewPagerPosition(); if (!isStoragePermissionGranted()) { Toast.makeText(this, "O App no ir funcionar direito sem essa permisso!", Toast.LENGTH_SHORT) .show(); } } public boolean isStoragePermissionGranted() { if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission( android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { Log.v(TAG, "Permission is granted"); return true; } else { Log.v(TAG, "Permission is revoked"); ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1); return false; } } else { //permission is automatically granted on sdk<23 upon installation Log.v(TAG, "Permission is granted"); return true; } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.v(TAG, "Permission: " + permissions[0] + "was " + grantResults[0]); //resume tasks needing this permission } } private void viewPagerPosition() { int position = getIntent().getIntExtra("fragment_name", 0); viewPager.setCurrentItem(position); } @Override protected void onResume() { super.onResume(); registerReceiver(); } @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); isReceiverRegistered = false; super.onPause(); } private void initGcm() { mRegistrationBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { } }; // Registering BroadcastReceiver registerReceiver(); if (checkPlayServices()) { // Start IntentService to register this application with GCM. Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } } private void registerReceiver() { if (!isReceiverRegistered) { LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE)); isReceiverRegistered = true; } } /** * Check the device to make sure it has the Google Play Services APK. If * it doesn't, display a dialog that allows users to download the APK from * the Google Play Store or enable it in the device's system settings. */ private boolean checkPlayServices() { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } else { Log.i(TAG, "Dispositivo no suportado para a PlayServices"); finish(); } return false; } return true; } // Add Fragments to Tabs private void setupViewPager(ViewPager viewPager) { Adapter adapter = new Adapter(getSupportFragmentManager()); adapter.addFragment(new ReservationFragment(), "Reservas"); adapter.addFragment(new BuildingMessageFragment(), "Mural de Avisos"); viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case ConstantsCondio.VIEW_PAGER_RESERVATIONS_POSITION: fab.show(); break; case ConstantsCondio.VIEW_PAGER_BOARD_MESSAGE_POSITION: fab.hide(); break; case ConstantsCondio.VIEW_PAGER_SERVICES_POSITION: fab.hide(); break; default: fab.hide(); break; } } @Override public void onPageScrollStateChanged(int state) { } }); } 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. 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); } }