Java tutorial
/* ShowActivity.java * * Copyright (C) 2011-2012 Christoph Brill * * This program 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 2 * of the License, or (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package de.egore911.drilog; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarDrawerToggle; import android.util.SparseArray; import android.view.*; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.DatePicker; import android.widget.ListView; import de.egore911.drilog.db.DbOpenHelper; import de.egore911.drilog.db.WatchesDao; import de.egore911.drilog.fragments.LogviewFragment; import de.egore911.drilog.tasks.JsonLoadTask; import de.egore911.drilog.util.Constants; import de.egore911.drilog.util.DateUtil; import de.egore911.drilog.util.DateUtil.SimpleDate; import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; /** * This class is used to display the whole log of a channel for a day. * * @author Christoph Brill <egore911@egore911.de> * @since 0.4 */ public class ShowActivity extends ActionBarActivity { /** * The list of monitored channels */ private List<String> channels; private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; private final SparseArray<LogviewFragment> fragments = new SparseArray<>(); private int mPosition = 0; private SimpleDate mSelectedDate; private String mAnchor; private Menu mMenu; private List<String> mWatchList = new ArrayList<>(0); public WatchesDao mWatchesDao; /** * Load the data from the DRI-log server using the {@link de.egore911.drilog.tasks.JsonLoadTask}. */ private void loadData() { LogviewFragment fragment = getCurrentFragment(); StringBuilder url = new StringBuilder(); url.append(Constants.BASE_URL); url.append("json.php?channel="); url.append(fragment.getArguments().getString("channel")); url.append("&date="); url.append(getDateString()); url.append("&mode=show"); new JsonLoadTask(this, fragment, mWatchList, getSupportFragmentManager(), mMenu).execute(url.toString()); } LogviewFragment getCurrentFragment() { LogviewFragment fragment = fragments.get(mPosition); if (fragment == null) { Bundle args = new Bundle(); args.putString("channel", channels.get(mPosition)); fragment = (LogviewFragment) Fragment.instantiate(ShowActivity.this, LogviewFragment.class.getName(), args); fragments.put(mPosition, fragment); } return fragment; } public String getAnchor() { return mAnchor; } public void setAnchor(String anchor) { this.mAnchor = anchor; } /** * @return the selected date as ISO-String */ private String getDateString() { if (mSelectedDate == null) { return "today"; } return DateUtil.getDateString(mSelectedDate); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } switch (item.getItemId()) { case R.id.options: Intent intent = new Intent(this, OptionsActivity.class); startActivity(intent); return true; case R.id.refresh: loadData(); return true; case R.id.choose_date: if (mSelectedDate == null) { mSelectedDate = new SimpleDate(); } Dialog dialog = new DatePickerDialog(ShowActivity.this, new DatePickerDialog.OnDateSetListener() { // Workaround for https://code.google.com/p/android/issues/detail?id=34860 boolean executed = false; @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { if (!executed) { mSelectedDate.year = year; mSelectedDate.month = monthOfYear; mSelectedDate.day = dayOfMonth; loadData(); } executed = true; } }, mSelectedDate.year, mSelectedDate.month, mSelectedDate.day) { }; dialog.show(); return true; default: return super.onOptionsItemSelected(item); } } public void updateDateText(String text) { if (mMenu != null) { MenuItem item = mMenu.findItem(R.id.choose_date); item.setTitle(text); } } @Override public final boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); this.mMenu = menu; updateDateText(DateUtil.getDateString(new Date())); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.choose_date).setVisible(!drawerOpen); menu.findItem(R.id.refresh).setVisible(!drawerOpen); menu.findItem(R.id.options).setVisible(!drawerOpen); return super.onPrepareOptionsMenu(menu); } @Override protected void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); if (mSelectedDate != null) { savedInstanceState.putString(Constants.STATE_DATE, DateUtil.getDateString(mSelectedDate)); } } @Override protected void onRestoreInstanceState(@Nonnull Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (savedInstanceState.containsKey(Constants.STATE_DATE)) { mSelectedDate = new SimpleDate(DateUtil.getDate(savedInstanceState.getString(Constants.STATE_DATE))); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); String[] intentChannels = getIntent().getStringArrayExtra(Constants.INTENT_CHANNELS); if (intentChannels == null) { channels = Arrays.asList(Constants.DEFAULT_CHANNELS); } else { channels = Arrays.asList(intentChannels); } DbOpenHelper.instance = new DbOpenHelper(this); mWatchesDao = new WatchesDao(); mWatchesDao.open(); setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // set a custom shadow that overlays the main content when the drawer opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // set up the drawer's list view with items and click listener mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, channels)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // enable ActionBar app icon to behave as action to toggle nav drawer getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); Bundle extras = getIntent().getExtras(); if (extras != null) { setAnchor(extras.getString("anchor")); if (extras.containsKey(Constants.STATE_DATE)) { mSelectedDate = (SimpleDate) extras.getSerializable(Constants.STATE_DATE); } if (extras.containsKey("channel")) { setPosition(channels.indexOf(extras.getString("channel"))); } } // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) { public void onDrawerClosed(View view) { getSupportActionBar().setTitle(channels.get(mPosition)); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public void onDrawerOpened(View drawerView) { getSupportActionBar().setTitle(R.string.app_name); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); setPosition(mPosition); if (savedInstanceState == null) { loadData(); } } private void setPosition(int position) { if (mPosition != position) { mDrawerList.setItemChecked(mPosition, false); } mPosition = position; mDrawerList.setItemChecked(mPosition, true); } /* The click listner for ListView in the navigation drawer */ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { setPosition(position); mDrawerLayout.closeDrawer(mDrawerList); loadData(); } } @Override protected void onResume() { mWatchesDao.open(); super.onResume(); //Load the list of usernames from the database that the application user is watching. mWatchList = mWatchesDao.getWatches(); loadData(); } @Override protected void onPause() { mWatchesDao.close(); super.onPause(); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } }