Created
June 28, 2010 20:46
-
-
Save tomgibara/456334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tomgibara.daisy.demo.provider; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.text.format.DateUtils; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.ImageView; | |
import android.widget.Toast; | |
import android.widget.ImageView.ScaleType; | |
/** | |
* | |
* This self-contained Android Activity class demonstrates how to display a | |
* flower that a user has picked from their Daisy Garden application. | |
* | |
* @author Tom Gibara | |
*/ | |
public class DaisyProviderDemo extends Activity implements OnClickListener { | |
//plant intent extras | |
public static final String EXTRA_DATA = "data"; // long: data that identifies the plant image | |
public static final String EXTRA_NAME = "name"; // String: the name of the plant | |
public static final String EXTRA_FAVORITE = "favorite"; // boolean: whether the plant has been favourited | |
public static final String EXTRA_PLANTED = "planted"; // long: timestamp of when the flower was planted | |
private static final int REQUEST_PLANT = 0; | |
private ImageView imageView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
//obligatory call to super | |
super.onCreate(savedInstanceState); | |
//set-up a view to display the flower | |
imageView = new ImageView(this); | |
imageView.setBackgroundColor(0xff008000); | |
imageView.setScaleType(ScaleType.FIT_CENTER); | |
setContentView(imageView); | |
//register us as an click listener on the imageView | |
imageView.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
//only interested in clicks on imageView | |
if (v != imageView) return; | |
//this is the intent that will kick-off the flower selection | |
final Intent intent = new Intent() | |
// this is just the generic 'pick' action | |
.setAction( Intent.ACTION_PICK ) | |
// this is the type of thing we're picking | |
.setType("vnd.tomgibara.daisy.plant/*") | |
//the following is a strictly optional customization | |
.putExtra("com.tomgibara.daisy.garden.planter.PICKING_MESSAGE", "Choose a flower to demo"); | |
startActivityForResult(intent, REQUEST_PLANT); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
//disregard any activity result that wasn't for us | |
if (requestCode != REQUEST_PLANT) super.onActivityResult(requestCode, resultCode, data); | |
if (resultCode != RESULT_OK) return; // user didn't select anything so nothing to do | |
//the data part contains the image URI for the flower | |
Uri imageUri = data.getData(); | |
//we can customize this eg. | |
imageUri = imageUri.buildUpon() | |
.appendQueryParameter("size", "256") //the desired size of the image in pixels | |
.appendQueryParameter("angle", "30") //angle through which the flower is rotated | |
.fragment("flower") // which element we want to draw, one of: plant, flower, petal | |
.build(); | |
//the extras contain information about the plant itself | |
final String name = data.getStringExtra(EXTRA_NAME); | |
final boolean favorite = data.getBooleanExtra(EXTRA_FAVORITE, false); | |
final long planted = data.getLongExtra(EXTRA_PLANTED, System.currentTimeMillis()); | |
final String plantedStr = DateUtils.formatDateTime(this, planted, DateUtils.FORMAT_ABBREV_ALL); | |
//compile the information into a single string for display | |
final String toast = String.format("Name: %s\nFavorite: %b\nPlanted: %s", name, favorite, plantedStr); | |
//finally display the result | |
imageView.setImageURI(imageUri); | |
Toast.makeText(this, toast, Toast.LENGTH_LONG).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment