12-17-2010, 08:09 PM | #1 |
Enthusiast
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. |
12-21-2010, 08:48 PM | #2 |
Enthusiast
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. |
Advert | |
|
12-22-2010, 11:51 AM | #3 | |
Enthusiast
Posts: 35
Karma: 1566
Join Date: Nov 2010
Device: kindle 3wifi
|
Quote:
Code:
grant signedBy "Kindlet" { permission java.security.AllPermission; }; Surely, once I want to distribute the program, I'll release the code. |
|
02-03-2011, 06:03 AM | #4 |
Junior Member
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(); } } 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. |
02-07-2011, 09:50 PM | #5 |
Junior Member
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(); } |
Advert | |
|
02-07-2011, 10:33 PM | #6 |
Enthusiast
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. |
02-07-2011, 10:35 PM | #7 |
Enthusiast
Posts: 35
Karma: 1566
Join Date: Nov 2010
Device: kindle 3wifi
|
Perhaps it's a good excuse for me to update to 3.1.
|
|
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 |