Java tutorial
/* * Copyright (C) 2013 Jozsef Szilvasi <jozsef.szilvasi@gmail.com> * * This file is part of NaturShutd. * * NaturShutd is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * NaturShutd is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NaturShutd. If not, see <http://www.gnu.org/licenses/>. */ package hu.naturlecso.naturshutd; import android.content.Intent; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import hu.naturlecso.naturshutd.dialog.AboutDialogFragment; import hu.naturlecso.naturshutd.dialog.AlertDialogFragment; public class MainActivity extends ActionBarActivity implements CommandsFragment.Callbacks, HostsDrawerFragment.Callbacks { private static final long INVALID_ID = -1L; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.main_fragmentcontainer, new CommandsFragment(), CommandsFragment.TAG).commit(); } /* Fragment managing the behaviors, interactions and presentation of the navigation drawer. */ HostsDrawerFragment mHostsDrawerFragment = (HostsDrawerFragment) getSupportFragmentManager() .findFragmentById(R.id.navigation_drawer); mHostsDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_about: showAboutDialog(); return true; default: return super.onOptionsItemSelected(item); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == EditActivity.EDIT_REQUEST_CODE) { if (resultCode == RESULT_OK) { Long id = data.getLongExtra(EditActivity.EDIT_RESULT_ID, INVALID_ID); EditActivity.ModelType modelType = (EditActivity.ModelType) data .getSerializableExtra(EditActivity.EXTRA_EDIT_MODEL_TYPE); switch (modelType) { case HOST: updateHost(id); break; case COMMAND: EditActivity.OperationType operationType = (EditActivity.OperationType) data .getSerializableExtra(EditActivity.EXTRA_EDIT_OPERATION_TYPE); doCommandOperation(operationType, id); break; default: throw new UnsupportedOperationException("Unknown ModelType."); } } } } private void doCommandOperation(EditActivity.OperationType operationType, long commandId) { CommandsFragment fragment = ((CommandsFragment) getSupportFragmentManager() .findFragmentByTag(CommandsFragment.TAG)); switch (operationType) { case ADD: fragment.addCommand(commandId); break; case DELETE: fragment.deleteCommand(commandId); break; case UPDATE: fragment.updateCommand(commandId); break; default: throw new UnsupportedOperationException("Unknown OperationType."); } } // @Override // public void onBackPressed() { // ManageFragment manageFragment = (ManageFragment) getSupportFragmentManager().findFragmentByTag(ManageFragment.TAG); // if (manageFragment != null && manageFragment.isVisible()) // manageFragment.backPressed(); // else // super.onBackPressed(); // } @Override public void onEditHost(Long id) { startEditHostActivity(id); } @Override public void onEditCommand(long hostId, Long commandId) { startEditCommandActivity(hostId, commandId); } @Override public void onHostSelected(long id) { updateHost(id); } @Override public void onAddNewHost() { startEditHostActivity(null); } private void showAboutDialog() { AboutDialogFragment dialogFragment = new AboutDialogFragment(); dialogFragment.show(getSupportFragmentManager(), AlertDialogFragment.TAG); } private void startEditHostActivity(Long hostId) { Intent intent = new Intent(this, EditActivity.class); intent.putExtra(EditActivity.EXTRA_HOST_ID, hostId); intent.putExtra(EditActivity.EXTRA_EDIT_MODEL_TYPE, EditActivity.ModelType.HOST); startActivityForResult(intent, EditActivity.EDIT_REQUEST_CODE); } private void startEditCommandActivity(long hostId, Long commandId) { Intent intent = new Intent(this, EditActivity.class); intent.putExtra(EditActivity.EXTRA_HOST_ID, hostId); intent.putExtra(EditActivity.EXTRA_COMMAND_ID, commandId); intent.putExtra(EditActivity.EXTRA_EDIT_MODEL_TYPE, EditActivity.ModelType.COMMAND); startActivity(intent); } // TODO change it with Observable private void updateHost(long id) { ((CommandsFragment) getSupportFragmentManager().findFragmentByTag(CommandsFragment.TAG)).updateHost(id); } }