Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Readers > Amazon Kindle > Kindle Developer's Corner

Notices

Reply
 
Thread Tools Search this Thread
Old 12-17-2010, 08:09 PM   #1
kindle3zeng
Enthusiast
kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.
 
Posts: 35
Karma: 1566
Join Date: Nov 2010
Device: kindle 3wifi
Is is possible to use the internal API when writing kindlets?

Maybe this is a silly question, but I'm new to java and I just want to use the audio apis, which is not provided yet.

So I tried to see how audio was manipulated in the amazon framework. I found that com.amazon.kindle.kindlet.internal.e implemented KindleContext and it had a method a() returning AudioManager; and I gussed that's what I needed.

However, when I tried to cast the the parameter to start(KindleContext ctx) to com.amazon.kindle.kindlet.internal.e, it threw a java.lang.NoClassDefFoundError exception saying com.amazon.kindle.kindlet.internal.e is missing.

Note that it had no problem to compile.
kindle3zeng is offline   Reply With Quote
Old 12-21-2010, 08:48 PM   #2
dvhh
Enthusiast
dvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with othersdvhh plays well with others
 
Posts: 27
Karma: 2600
Join Date: Nov 2010
Location: Osaka, Japan
Device: Kindle 3
I think it depend on the authorization you gave to your java program in your policy file.
So this is really dependent on your user to alter those files.
Either distribute your code (for verification), with the instruction to alter the security or rely on available api under developer key.
dvhh is offline   Reply With Quote
Advert
Old 12-22-2010, 11:51 AM   #3
kindle3zeng
Enthusiast
kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.
 
Posts: 35
Karma: 1566
Join Date: Nov 2010
Device: kindle 3wifi
Quote:
Originally Posted by dvhh View Post
I think it depend on the authorization you gave to your java program in your policy file.
So this is really dependent on your user to alter those files.
Either distribute your code (for verification), with the instruction to alter the security or rely on available api under developer key.
Thanks for the reply. But I tried to put this on the external.policy:

Code:
grant signedBy "Kindlet" {
     permission java.security.AllPermission;
};
and it did not help, that same exception was thrown.

Surely, once I want to distribute the program, I'll release the code.
kindle3zeng is offline   Reply With Quote
Old 02-03-2011, 06:03 AM   #4
jsstylos
Junior Member
jsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud of
 
Posts: 3
Karma: 27344
Join Date: Feb 2011
Device: Kindle 3
Hi there. I'm trying to get started with Kindle programming and your post intrigued me enough to look into the internal API issue.

I originally thought that the NoClassDefFoundError problem was a result of an obfuscation trick -- the KindleImplementation-1.1.jar contains a package called com.amazon.kindle.kindlet.internal.e, which seems to prevent the compiler from referencing the class of the same name. But I also ran into the same NoClassDefFoundError issue when trying to reference the interfaces in the com.amazon.kindle.kindlet.media package for the purpose of doing instanceof checks and casts. This confuses me much more than the problem referencing the internal e class. I'd love if someone could help shed light on this.

One can, however, access the internal.e class and the kindlet.media interfaces even without being able to directly reference them, though it requires elevated permissions to perform reflection, and is rather laborious to program.

Here is some code I wrote that plays a test.mp3 in the /mnt/us/developer/YourAppName/work/test.mp3 directory.

Code:
    private void playAudio () {
        Class contextClass = context.getClass();
        Method[] methods = contextClass.getDeclaredMethods();
        Method audioMethod = methods[1];
        
        Object audioManagerObject;
        Class audioManagerClass;
        
        try {
        	audioManagerObject = audioMethod.invoke(context, new Object[0]);
        	audioManagerClass = audioManagerObject.getClass();
        	
        	Method[] audioManagerMethods = audioManagerClass.getDeclaredMethods();
        	Method createAudioPlayerMethod = audioManagerMethods[10];
        	Class mediaSourceClass = createAudioPlayerMethod.getParameterTypes()[0];
        	Constructor[] mediaSourceConstructors = mediaSourceClass.getConstructors();
        	Constructor mediaSourceInputStreamConstructor = mediaSourceConstructors[3];
        	//Constructor mediaSourceUrlConstructor = mediaSourceConstructors[1]; // Use this to play from a url
        	
        	Object[] mediaSourceArgs = new Object[1];
        	mediaSourceArgs[0] = new FileInputStream(context.getHomeDirectory().getAbsolutePath() + "/" + "test.mp3");
        	Object mediaSourceObject = mediaSourceInputStreamConstructor.newInstance(mediaSourceArgs);
        	
        	Object[] createAudioPlayerArgs = new Object[1];
        	createAudioPlayerArgs[0] = mediaSourceObject;
        	Object audioObject = createAudioPlayerMethod.invoke(audioManagerObject, createAudioPlayerArgs);
        	Class audioClass = audioObject.getClass();
        	
        	Method[] audioMethods = audioClass.getDeclaredMethods();
        	        	
        	Method playMethod = audioMethods[12];
        	playMethod.invoke(audioObject, new Object[0]);
        	
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		}	
    }
The code uses reflection (which will throw an exception if you don't use the java.security.AllPermission trick, or perhaps some lesser permission that will just allow reflection) to list the classes and methods at runtime. The code currently uses hardcoded indices to pull out the appropriate methods and classes (these indices were determined by displaying the method names and parameter arguments for each of the methods in the list; instead of using hardcoded indices I should probably write a helper method to lookup the right method or class based on name and argument types, if there's not already a better helper in the reflection API).

This is clearly very messy code to write -- especially if you're mucking about with listeners and the like -- so I would really like to just be able to reference the classes and interfaces in the com.amazon.kindle.kindlet.media package at least directly. If someone can figure out how to do that, that would be very handy.
jsstylos is offline   Reply With Quote
Old 02-07-2011, 09:50 PM   #5
jsstylos
Junior Member
jsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud ofjsstylos has much to be proud of
 
Posts: 3
Karma: 27344
Join Date: Feb 2011
Device: Kindle 3
For those who are interested, this just got a whole lot easier with firmware 3.1 and Kindlet-1.2.jar, and no longer requires any extra permissions:

Code:
AudioManager audioManager = context.getAudioManager();
try {
	Audio audio = audioManager.createAudioPlayer(new MediaSource(new FileInputStream(context.getHomeDirectory().getAbsolutePath() + "/" + "test.mp3")));
	audio.play();
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (AudioException e) {
	e.printStackTrace();
}
jsstylos is offline   Reply With Quote
Advert
Old 02-07-2011, 10:33 PM   #6
kindle3zeng
Enthusiast
kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.
 
Posts: 35
Karma: 1566
Join Date: Nov 2010
Device: kindle 3wifi
Wow, that's great.

I just found the problem we encountered. There is a resource file specifying whitelisted classes: com.amazon.kindle.kindlet.internal.security.whiteL istedClassName. I just put com.amazon.kindle.kindlet.internal.e there, no more NoClassDefFoundError exception occurred.
kindle3zeng is offline   Reply With Quote
Old 02-07-2011, 10:35 PM   #7
kindle3zeng
Enthusiast
kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.kindle3zeng once ate a cherry pie in a record 7 seconds.
 
Posts: 35
Karma: 1566
Join Date: Nov 2010
Device: kindle 3wifi
Perhaps it's a good excuse for me to update to 3.1.
kindle3zeng is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Guide: How to write Kindlets KukMan Kindle Developer's Corner 67 07-08-2013 07:23 AM
Goodreads has published an API EricLandes Calibre 6 01-12-2011 04:39 PM
Amazon blocking Kindle Books in API? anurag Amazon Kindle 1 08-24-2009 08:46 PM
writing to a SD card instead of internal memory? hapax legomenon Bookeen 1 04-24-2008 03:09 AM
Ubook plugin api Dopedangel Reading and Management 0 08-25-2007 06:54 AM


All times are GMT -4. The time now is 05:50 PM.


MobileRead.com is a privately owned, operated and funded community.