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

Android Open Source » UnTagged » interactive museum guide 
interactive museum guide » museum » android » screens » StartScreen.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.decode.IntentIntegrator;
import museum.android.decode.IntentResult;
import museum.android.persistence.Settings;
import museum.android.utility.StatisticType;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Screen that gets displayed on application start.
 * 
 * @author Norbert Lanzanasto
 * 
 */
public class StartScreen extends Activity implements OnClickListener {

  private static final String TAG = "StartScreen";
  private Context mContext = this;
  private Button butScanServerQR;
  private Button butScanObjectQR;
  private Button butGetBestRatings;
  private Button butGetMostVisited;
  private Button butGetMyRoute;
  private ImageView imgLogo;

  /**
   * Called when the activity is first created. Initializes 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);

    Settings.initInstance(this);

    setContentView(R.layout.start);

    imgLogo = (ImageView) this.findViewById(R.id.stiLogo);
    imgLogo.setImageResource(R.drawable.sti_logo);
    butScanServerQR = (Button) this.findViewById(R.id.butScanServerQRCode);
    butScanServerQR.setOnClickListener(this);
    butScanObjectQR = (Button) this.findViewById(R.id.butScanObjectQRCode);
    butScanObjectQR.setOnClickListener(this);
    butGetBestRatings = (Button) this.findViewById(R.id.butBestRated);
    butGetBestRatings.setOnClickListener(this);
    butGetMostVisited = (Button) this.findViewById(R.id.butMostVisited);
    butGetMostVisited.setOnClickListener(this);
    butGetMyRoute = (Button) this.findViewById(R.id.butMyRoute);
    butGetMyRoute.setOnClickListener(this);
  }

  /**
   * Called when an activity you launched exits, giving you the requestCode
   * you started it with, the resultCode it returned, and any additional data
   * from it. Gets called after scanning the qr-code. Displays an error
   * message if an error occured or calls a method to parse the result of the
   * scanning.
   * 
   * @param requestCode
   *            The integer request code originally supplied to
   *            startActivityForResult(), allowing you to identify who this
   *            result came from.
   * @param resultCode
   *            The integer result code returned by the child activity through
   *            its setResult().
   * @param data
   *            An Intent, which can return result data to the caller (various
   *            data can be attached to Intent "extras").
   */
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case IntentIntegrator.REQUEST_CODE:
      if (resultCode != RESULT_CANCELED) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(
            requestCode, resultCode, data);
        if (scanResult != null) {
          String serverUrl = data.getStringExtra("SCAN_RESULT");
          Log.d(TAG, "Server IP address: " + serverUrl);
          Settings.INSTANCE.setServerUrl(serverUrl);
        } else {
          Log.d(TAG, "no server ip address");
          Toast
              .makeText(getApplicationContext(),
                  "QR code could not be decoded!",
                  Toast.LENGTH_SHORT).show();
          finish();
        }
      } else {
        Log.d(TAG, "scan canceled");
      }
      break;
    default:
      Log.d(TAG, "no scan result");
      Toast.makeText(getApplicationContext(),
          "QR code could not be decoded!", Toast.LENGTH_SHORT).show();
      finish();
    }
  }

  /**
   * 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.start_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;
    case (R.id.menu_reset_id):
      Settings.INSTANCE.setUserId("");
      Log.d(TAG, "user id deleted");
      break;
    }
    return true;
  }

  /**
   * Called when a view has been clicked.
   * 
   * @param v
   *            The view that was clicked.
   */
  public void onClick(View v) {
    if (v.equals(butScanServerQR)) {
      IntentIntegrator.initiateScan(StartScreen.this);
      Settings.INSTANCE.setUserId("");
    } else {
      if (Settings.INSTANCE.hasServerUrl()) {
        if (v.equals(butScanObjectQR)) {
          Intent intent = new Intent(mContext, ObjectScreen.class);
          intent.putExtra("scan", true);
          startActivity(intent);
        } else if (v.equals(butGetBestRatings)) {
          Intent intent = new Intent(mContext, StatisticScreen.class);
          intent.putExtra("type", StatisticType.RATE.ordinal());
          startActivity(intent);
        } else if (v.equals(butGetMostVisited)) {
          Intent intent = new Intent(mContext, StatisticScreen.class);
          intent.putExtra("type", StatisticType.VISIT.ordinal());
          startActivity(intent);
        } else if (v.equals(butGetMyRoute)) {
          Intent intent = new Intent(mContext, StatisticScreen.class);
          intent.putExtra("type", StatisticType.ROUTE.ordinal());
          startActivity(intent);
        }
      } else {
        Log.d(TAG, "no server address set");
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder
            .setMessage(
                "IP address of the server is not spezified. Please scan the QR code.")
            .setCancelable(false).setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog,
                      int id) {
                    IntentIntegrator
                        .initiateScan(StartScreen.this);
                    Settings.INSTANCE.setUserId("");
                  }
                });
        AlertDialog alert = builder.create();
        alert.show();
      }
    }
  }
}
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.