factory class for generating various intents : Intent « Core Class « Android






factory class for generating various intents

    

/*
 Copyright (c) 2010, Sungjin Han <meinside@gmail.com>
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  * Neither the name of meinside nor the names of its contributors may be
    used to endorse or promote products derived from this software without
    specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 */

//package org.andlib.helpers;

import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;

/**
 * factory class for generating various intents
 * <br>
 * <br>
 * last update: 2011.01.24.
 * 
 * @author meinside@gmail.com
 * @since 2010.11.03.
 *
 */
public class IntentFactory
{
  public enum MediaType {
    IMAGE,
    AUDIO,
    VIDEO,
  };

  public enum MediaLocation {
    INTERNAL,
    EXTERNAL,
  };

  /**
   * generates an intent for selecting media
   * <br>
   * <br>
   * (when type == <b>MediaType.VIDEO</b>, picker doesn't work.
   * <br>
   *  instead, it looks for a video player to play selected one.)
   *  
   * @param title
   * @param type
   * @param location
   * @return
   */
  public static Intent getMediaPickerIntent(String title, MediaType type, MediaLocation location)
  {
    Uri uri = null;
    if(type == MediaType.IMAGE)
    {
      if(location == MediaLocation.INTERNAL)
        uri = android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI;
      else
        uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }
    else if(type == MediaType.AUDIO)
    {
      if(location == MediaLocation.INTERNAL)
        uri = android.provider.MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
      else
        uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    }
    else if(type == MediaType.VIDEO)
    {
      if(location == MediaLocation.INTERNAL)
        uri = android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI;
      else
        uri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    }
    else
      return null;

    return Intent.createChooser(new Intent(Intent.ACTION_PICK, uri), title);
  }

  /**
   * generates an intent for selecting media
   * <br>
   * <br>
   * (when type == <b>MediaType.AUDIO</b>, picker doesn't work.
   *  instead, it looks for an audio player to play selected one.)
   * 
   * @param title
   * @param type
   * @return
   */
  public static Intent getMediaContentIntent(String title, MediaType type)
  {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    if(type == MediaType.IMAGE)
      intent.setType("image/*");
    else if(type == MediaType.AUDIO)
      intent.setType("audio/*");
    else if(type == MediaType.VIDEO)
      intent.setType("video/*");
    
    return Intent.createChooser(intent, title);
  }

  /**
   * generates an intent for taking photos/videos that saves result to given uri
   * 
   * @param type either one of <b>MediaType.IMAGE</b> or <b>MediaType.VIDEO</b>
   * @param outputUri
   * @return null if error
   */
  public static Intent getCameraIntent(MediaType type, Uri outputUri)
  {
    Intent intent = null;
    if(type == MediaType.IMAGE)
    {
      intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
    }
    else if(type == MediaType.VIDEO)
    {
      intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
    }
    return intent;
  }
}

   
    
    
    
  








Related examples in the same category

1.Using Intent to open other Activity
2.Create Intent to open a Uri
3.extends IntentService
4.Phone Intent
5.Map Intent
6.Market Intent
7.Load Activity with Intent
8.Using Intent to make another phone call
9.extends IntentService to create your own Intent
10.Adding data bundle to Intent
11.Using Intent to show other Activities
12.Start Intent with Utility class
13.Load library Activity with Intent
14.Capture Image with Intent
15.Intent.ACTION_MEDIA_MOUNTED
16.Using Intent to record audio
17.Video Player Intent
18.Video Capture Intent
19.Implementing an application service that will run in response to an alarm, allowing us to move long duration work out of an intent receiver.
20.Example of various Intent flags to modify the activity stack.
21.Sample code that invokes the speech recognition intent API.
22.extends IntentService to upload a file
23.Open Web Page Intent
24.Is Intent Available
25.start MMS Intent
26.Access the Internet
27.Pdf viewer
28.Rotation One Demo
29.Media activity