Android Open Source - geocamTalkAndroid U I Utils






From Project

Back to project page geocamTalkAndroid.

License

The source code is released under:

NASA OPEN SOURCE AGREEMENT VERSION 1.3 THIS OPEN SOURCE AGREEMENT ("AGREEMENT") DEFINES THE RIGHTS OF USE, REPRODUCTION, DISTRIBUTION, MODIFICATION AND REDISTRIBUTION OF CERTAIN COMPUTER SOFTWARE ORI...

If you think the Android project geocamTalkAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// __BEGIN_LICENSE__
// Copyright (C) 2008-2010 United States Government as represented by
// the Administrator of the National Aeronautics and Space Administration.
// All Rights Reserved.
// __END_LICENSE__
//w  w w  .  ja  va  2  s  .  c  om
package gov.nasa.arc.geocam.talk;

import gov.nasa.arc.geocam.talk.activity.GeoCamTalkActivity;
import gov.nasa.arc.geocam.talk.activity.GeoCamTalkCreateActivity;
import gov.nasa.arc.geocam.talk.activity.GeoCamTalkLogon;
import gov.nasa.arc.geocam.talk.activity.GeoCamTalkMapActivity;
import gov.nasa.arc.geocam.talk.activity.SettingsActivity;
import gov.nasa.arc.geocam.talk.bean.GeoCamTalkMessage;
import gov.nasa.arc.geocam.talk.exception.AuthenticationFailedException;
import gov.nasa.arc.geocam.talk.service.IAudioPlayer;
import gov.nasa.arc.geocam.talk.service.ISiteAuth;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * The Class UIUtils.  Provides an interface to complete global UI functions and begin new activities.
 */
public class UIUtils {
  
    /**
     * Display an exception at the log and to the user via {@link Toast}.
     *
     * @param context the context
     * @param e the e
     * @param additionalMessage the additional message
     */
    public static void displayException(Context context, Exception e, String additionalMessage)
    {
      Log.e("Talk", additionalMessage, e);
      StringBuilder sb = new StringBuilder();
      if(additionalMessage != null)
      {
        sb.append(additionalMessage + ": ");
      }
      if(e.getLocalizedMessage() != null){
          sb.append(e.getLocalizedMessage());        
      }
      Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
    }
    
    /**
     * Show the {@link GeoCamTalkMapActivity} for a given message.
     *
     * @param context The current activity context
     * @param talkMessage The {@link GeoCamTalkMessage} that the map activity will display
     */
    public static void showMapView(Context context, GeoCamTalkMessage talkMessage) {
      final Intent intent = new Intent(context, GeoCamTalkMapActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.putExtra(context.getString(R.string.latitude), talkMessage.getLatitude());
      intent.putExtra(context.getString(R.string.longitude), talkMessage.getLongitude());
      intent.putExtra(context.getString(R.string.accuracy), talkMessage.getAccuracy());
      context.startActivity(intent);      
    }
    
    /**
     * Logout the currently logged in user.
     *
     * @param siteAuth A {@link ISiteAuth} instance which is managing authorization.
     * @throws ClientProtocolException the client protocol exception
     * @throws AuthenticationFailedException the authentication failed exception
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void logout(ISiteAuth siteAuth)throws ClientProtocolException, AuthenticationFailedException, IOException
    {
      siteAuth.logoutAndUnregister();
    }

  /**
   * Invoke "home" action, returning to {@link GeoCamTalkActivity}.
   *
   * @param context The activity context to send the intent from.
   */
  public static void goHome(Context context) {
    final Intent intent = new Intent(context, GeoCamTalkActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }

  /**
   * Go to the {@link GeoCamTalkLogon} activity.
   *
   * @param context The activity context to send the intent from.
   */
  public static void goToLogin(Context context) {
    final Intent intent = new Intent(context, GeoCamTalkLogon.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }

  /**
   * Go to the {@link GeoCamTalkCreateActivity} activity to create a new GeoCam Talk Message.
   *
   * @param context The activity context to send the intent from.
   */
  public static void createTalkMessage(Context context) {
    final Intent intent = new Intent(context, GeoCamTalkCreateActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }

  /**
   * Play the audio of a {@link GeoCamTalkMessage} if available.
   *
   * @param context The activity context to send the intent from.
   * @param msg The {@link GeoCamTalkMessage} to play audio of.
   * @param player An instance of an {@link IAudioPlayer}
   * @param siteAuth The {@link ISiteAuth} to use to get attain audio from if not local
   * @throws ClientProtocolException the client protocol exception
   * @throws AuthenticationFailedException the authentication failed exception
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static void playAudio(Context context, GeoCamTalkMessage msg, IAudioPlayer player,
      ISiteAuth siteAuth) throws ClientProtocolException, AuthenticationFailedException,
      IOException {

    String audioUrl = msg.getAudioUrl();

    // No audio recorded with message
    if (msg.getAudio() == null && audioUrl == null) {
      return;
    }
    // We have audio, but not locally
    else if (msg.getAudio() == null && audioUrl != null) {
      String localFileName = siteAuth.getAudioFile(audioUrl, null);
      player.startPlaying(localFileName);
      File audioFile = new File(localFileName);
      int length = (int) audioFile.length();
      byte[] audioBytes = new byte[length];

      FileInputStream fis = new FileInputStream(audioFile);
      fis.read(audioBytes, 0, length);
      msg.setAudio(audioBytes);
    }
    player.startPlaying(msg.getAudio());
  }

  /**
   * Go to the {@link SettingsActivity} activity.
   *
   * @param context the context
   */
  public static void goToSettings(Context context) {
    final Intent intent = new Intent(context, SettingsActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }
}




Java Source Code List

gov.nasa.arc.geocam.talk.GeoCamTalkModule.java
gov.nasa.arc.geocam.talk.GeoCamTalkRoboApplication.java
gov.nasa.arc.geocam.talk.UIUtils.java
gov.nasa.arc.geocam.talk.activity.AuthenticatedBaseActivity.java
gov.nasa.arc.geocam.talk.activity.GeoCamTalkActivity.java
gov.nasa.arc.geocam.talk.activity.GeoCamTalkCreateActivity.java
gov.nasa.arc.geocam.talk.activity.GeoCamTalkLogon.java
gov.nasa.arc.geocam.talk.activity.GeoCamTalkMapActivity.java
gov.nasa.arc.geocam.talk.activity.GeoCamTalkMessageAdapter.java
gov.nasa.arc.geocam.talk.activity.SettingsActivity.java
gov.nasa.arc.geocam.talk.bean.GeoCamTalkMessage.java
gov.nasa.arc.geocam.talk.bean.ServerResponse.java
gov.nasa.arc.geocam.talk.bean.TalkServerIntent.java
gov.nasa.arc.geocam.talk.exception.AuthenticationFailedException.java
gov.nasa.arc.geocam.talk.service.AudioPlayer.java
gov.nasa.arc.geocam.talk.service.AudioRecorder.java
gov.nasa.arc.geocam.talk.service.C2DMReciever.java
gov.nasa.arc.geocam.talk.service.DatabaseHelper.java
gov.nasa.arc.geocam.talk.service.GeoCamSynchronizationTimerTask.java
gov.nasa.arc.geocam.talk.service.GeoLocationListener.java
gov.nasa.arc.geocam.talk.service.IAudioPlayer.java
gov.nasa.arc.geocam.talk.service.IAudioRecorder.java
gov.nasa.arc.geocam.talk.service.IDatabaseHelper.java
gov.nasa.arc.geocam.talk.service.IGeoCamSynchronizationTimerTask.java
gov.nasa.arc.geocam.talk.service.IIntentHelper.java
gov.nasa.arc.geocam.talk.service.IMessageStore.java
gov.nasa.arc.geocam.talk.service.ISiteAuth.java
gov.nasa.arc.geocam.talk.service.ITalkJsonConverter.java
gov.nasa.arc.geocam.talk.service.ITalkServer.java
gov.nasa.arc.geocam.talk.service.IntentHelper.java
gov.nasa.arc.geocam.talk.service.MessageStore.java
gov.nasa.arc.geocam.talk.service.SiteAuthCookie.java
gov.nasa.arc.geocam.talk.service.TalkJsonConverter.java
gov.nasa.arc.geocam.talk.service.TalkMapOverlay.java
gov.nasa.arc.geocam.talk.service.TalkServer.java