StatisticScreen.java :  » UnTagged » interactive-museum-guide » museum » android » screens » Android Open Source

Android Open Source » UnTagged » interactive museum guide 
interactive museum guide » museum » android » screens » StatisticScreen.java
/*
Copyright 2011 Dietmar Wieser, Norbert Lanzanasto, Margit Mutschlechner.
Distributed under the terms of the GNU Lesser General Public License (LGPL).

This file is part of Interactive Museum Guide.

Interactive Museum Guide is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Interactive Museum Guide 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Interactive Museum Guide.  If not, see <http://www.gnu.org/licenses/>.
*/

package museum.android.screens;

import museum.android.connect.ServerConnection;
import museum.android.persistence.Settings;
import museum.android.utility.StatisticType;
import museum.server.model.Statistic;
import museum.server.model.StatisticResponse;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Screen to display statistic information.
 * 
 * @author Norbert Lanzanasto
 * 
 */
public class StatisticScreen extends Activity {

  private static final String TAG = "StatisticScreen";
  private Context mContext = this;
  private StatisticResponse statResp;
  /** true if the activity was already started **/
  private boolean executeOnResume = false;

  /**
   * Called when the activity is first created. Calls a method to connect to
   * the server and initialize the screen.
   * 
   * @param savedInstanceState
   *            If the activity is being re-initialized after previously being
   *            shut down then this Bundle contains the data it most recently
   *            supplied in onSaveInstanceState(Bundle). Otherwise it is null.
   */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    connectToServer();
  }

  /**
   * Connects to the server to get the statistic information and displays it
   * when no error occured.
   */
  private void connectToServer() {
    int type = getIntent().getIntExtra("type", -1);
    statResp = null;

    if (type == StatisticType.RATE.ordinal()) {
      statResp = ServerConnection.getHighestRatedObjects();
    } else if (type == StatisticType.VISIT.ordinal()) {
      statResp = ServerConnection.getMostVisitedObjects();
    } else if (type == StatisticType.ROUTE.ordinal()) {
      String userId = Settings.INSTANCE.getUserId();
      if (userId == null || userId.equals("")) {
        Log.i("-----------",
            "no user id found for retrieving route info");
      } else {
        Log.d(TAG, "User id found: " + userId);
        statResp = ServerConnection.getMyRoute(userId);
      }
    }

    if (statResp != null && statResp.getItems() != null) {
      if (statResp.getErrorCode() == 0) {
        Log.i("-----------", "no error occured");
        setContentView(R.layout.statistic_list);
        final ListView aListView = (ListView) findViewById(R.id.ListView01);
        aListView.setAdapter(new StatisticListAdapter(this, statResp
            .getItems()));
        aListView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> arg0, View arg1,
              int arg2, long arg3) {
            StatisticListAdapter aListAdapter = (StatisticListAdapter) aListView
                .getAdapter();
            Statistic aStatistic = aListAdapter
                .getStatisticItemAt(arg2);

            System.out.println("item on click: "
                + aStatistic.getObjectName() + " db-id: "
                + aStatistic.getObjectId());

            Intent intent = new Intent(mContext, ObjectScreen.class);
            intent.putExtra("scan", false);
            intent.putExtra("object_id", aStatistic.getObjectId()
                + "");
            startActivity(intent);
          }
        });
      } else {
        Log.i("-----------", "error occured");
        Log.d(TAG, statResp.getErrorMsg());
        Toast.makeText(getApplicationContext(), statResp.getErrorMsg(),
            Toast.LENGTH_SHORT).show();
        finish();
      }
    } else {
      Log.d(TAG, "no object found");
      Toast.makeText(getApplicationContext(),
          "Sorry, server temporary not available!",
          Toast.LENGTH_SHORT).show();
      finish();
    }
  }

  /**
   * Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(),
   * for your activity to start interacting with the user. Connects to the
   * server to refresh the information that has to be displayd.
   */
  protected void onResume() {
    super.onResume();
    if (executeOnResume) {
      connectToServer();
    }
    executeOnResume = true;
  }

  /**
   * Initialize the contents of the Activity's standard options menu.
   * 
   * @param menu
   *            The options menu in which you place your items.
   * @return boolean You must return true for the menu to be displayed; if you
   *         return false it will not be shown.
   */
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
  }

  /**
   * This hook is called whenever an item in the options menu is selected.
   * 
   * @param item
   *            The menu item that was selected.
   * @return boolean Return false to allow normal menu processing to proceed,
   *         true to consume it here.
   */
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case (R.id.menu_quit):
      moveTaskToBack(true);
      break;
    }
    return true;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.