Java tutorial
/* * </summary> * Source File : MainActivity.java * Project : MyTrack * Module : app * Owner : nirmal * </summary> * * <license> * Copyright 2016 Kasun Gunathilaka * * 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. * </license> */ package com.openarc.nirmal.mytrack; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.openarc.nirmal.mytrack.tasks.HomeTask; import com.openarc.nirmal.mytrack.tasks.MAWHTask; import com.openarc.nirmal.mytrack.util.MyTrackConfig; import com.openarc.nirmal.mytrack.view.HomeView; import com.openarc.nirmal.mytrack.view.MAWHDialogView; import de.hdodenhof.circleimageview.CircleImageView; public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener { HomeView homeView; MAWHDialogView mawhDialogView; AlertDialog mAlertDialog; View.OnTouchListener cvMAWHOnTouch = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { showMAWHDialog(); } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setTitle("My Track"); initialize(); new HomeTask(MainActivity.this, homeView).execute(); 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.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); if (!MyTrackConfig.getInstance().submitAuthorized) hideLeaveSubmit(navigationView); homeView.ivUser = (CircleImageView) navigationView.getHeaderView(0).findViewById(R.id.ivUser); homeView.tvUserName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.tvLoggedUser); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { if (mAlertDialog != null && mAlertDialog.isShowing()) { mAlertDialog.cancel(); } else { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); alertDialogBuilder.setMessage("Do you want to exit the application ?"); alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); System.exit(0); } }); alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mAlertDialog.cancel(); } }); mAlertDialog = alertDialogBuilder.create(); mAlertDialog.show(); mAlertDialog.getButton(DialogInterface.BUTTON_NEGATIVE) .setTextColor(getResources().getColor(R.color.colorAccent)); mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE) .setTextColor(getResources().getColor(R.color.colorPrimary)); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_about: AboutDialog(); break; case R.id.menu_logout: Intent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); break; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. switch (item.getItemId()) { case R.id.nav_contacts: startActivity(new Intent(this, ContactActivity.class)); break; case R.id.nav_view_attendance: startActivity(new Intent(this, AttendanceActivity.class)); break; case R.id.nav_submit_missing_attendance: startActivity(new Intent(this, AttendancesSubmitActivity.class)); break; case R.id.nav_leave_requests: startActivity(new Intent(this, LeaveRequestActivity.class)); break; case R.id.nav_leave_utilization: startActivity(new Intent(this, LeaveUtilizationActivity.class)); break; case R.id.nav_gate_pass_request: startActivity(new Intent(this, GatePassRequestActivity.class)); break; case R.id.nav_osm_contact: startActivity(new Intent(this, ContactActivity.class)); break; case R.id.nav_cir_contact: startActivity(new Intent(this, CIRContactActivity.class)); break; case R.id.nav_openarc_customers: startActivity(new Intent(this, CustomerActivity.class)); break; case R.id.nav_meeting_rooms: startActivity(new Intent(this, MeetingRoomActivity.class)); break; } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } private void initialize() { homeView = new HomeView(); homeView.rlLoading = (RelativeLayout) findViewById(R.id.rlLoading); homeView.rvBirthday = (RecyclerView) findViewById(R.id.rvBirthday); homeView.rvNewsfeed = (RecyclerView) findViewById(R.id.rvNewsfeed); homeView.tvCAWH = (TextView) findViewById(R.id.tvCAWH); homeView.tvMAWH = (TextView) findViewById(R.id.tvMAWH); homeView.tvSUI = (TextView) findViewById(R.id.tvSUI); homeView.cvMAWH = (CardView) findViewById(R.id.cvMAWH); homeView.cvMAWH.setOnTouchListener(cvMAWHOnTouch); } private void showMAWHDialog() { mawhDialogView = new MAWHDialogView(); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); View dialogView = getLayoutInflater().inflate(R.layout.dialog_view_mawh, null); alertDialogBuilder.setView(dialogView); ImageView ivClose = (ImageView) dialogView.findViewById(R.id.ivClose); TextView tvDialogTitle = (TextView) dialogView.findViewById(R.id.tvDialogTitle); tvDialogTitle.setText("Monthly AWH"); mawhDialogView.rvMawhDialog = (RecyclerView) dialogView.findViewById(R.id.rvMawhDialog); mawhDialogView.rlLoadingLayout = (RelativeLayout) dialogView.findViewById(R.id.rlLoadingLayout); final AlertDialog alertDialog = alertDialogBuilder.create(); ivClose.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { alertDialog.dismiss(); } return false; } }); new MAWHTask(mawhDialogView, MainActivity.this, alertDialog).execute(); } }