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 10-08-2014, 01:09 AM   #1
ChronosDragon
Junior Member
ChronosDragon began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Oct 2014
Location: Seattle area
Device: Kindle 4 Non-Touch
Question Timer issues

Good day all,

I was exploring the world of kindle development, and as a simple introductory project (and potentially useful application) I thought I would make a clock app that periodically shows the time on the e-ink screen. (This would be nice to replace a digital alarm clock as the e-ink screen does not produce any ambient light). However, I've run into some difficulties.

Namely, the kindlet library Timer class does not seem to be firing its scheduled tasks. I've tried several variations of the schedule methods and different assumptions about the value it is accepting for delay/period arguments, but nothing seems to be working. It will run its task once, but won't update on the period it is supposed to and only runs subsequent updates when the kindle is awoken from the lock screen. Specific snippets of my code will be at the bottom of this post.

More than a simple solution for this, though, I'd like to know if there are any additional resources available for learning the kindle libraries. They don't seem to have any javadocs (in the case of the timer, since the API is the same I would assume it would work the same as the java util class of the same name, though my problems would indicate otherwise) and examples are sparse. I do appreciate the number of kindlets posted here which are open sourced on github, but these can be difficult to read through and are typically based around a very event-oriented control flow as far as I can tell.

Thanks for your time,
Andrew

My main class:
Spoiler:
Code:
package klock;

import com.amazon.kindle.kindlet.AbstractKindlet;
import com.amazon.kindle.kindlet.KindletContext;
import com.amazon.kindle.kindlet.ui.KLabel;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.amazon.kindle.kindlet.util.Timer;
import com.amazon.kindle.kindlet.util.TimerTask;

/**
 *
 * @author chronal
 */
public class Main extends AbstractKindlet {

    public KindletContext ctx;
    public KLabel kt;
    public SimpleDateFormat timeFormat;
    public Timer t;
    public TimerTask task;
    
    public void create(KindletContext context) {
        this.ctx = context;
        
        timeFormat = new SimpleDateFormat("HH:mm");
        timeFormat.setTimeZone(TimeZone.getTimeZone("PST"));
        
        try {
            kt = new KLabel("HH:mm", KLabel.CENTER); //timeFormat.format(new Date()), KLabel.CENTER);
            // Alignment 0: Center
            // Alignment 1: ? (Right?)
            // Alignment 2: Nothing?
            kt.setFont(new java.awt.Font(null, 0, 220));
            ctx.getRootContainer().add(kt);
            // Orientation 1: Upside down
            // Orientation 2: Sideways
            ctx.getOrientationController().lockOrientation(2);
            
            t = new Timer();
            t.schedule(
                new UpdateTimeTask(this),
                new Date(),
                15000
            );
            
        }
        catch (Throwable t) {
            t.printStackTrace();
        }
    }
    
    
    
    public void updateTimeText() {

    }
    
    
} 

class UpdateTimeTask extends TimerTask
{
    private Main main;
    public UpdateTimeTask(Main main) {
        this.main = main;
    }
    
    public void run() {
        if (main.kt != null) {
            main.kt.setText(main.timeFormat.format(new Date()));
        }
        main.ctx.getRootContainer().repaint(2000);
    }
    
}
ChronosDragon is offline   Reply With Quote
Old 10-08-2014, 02:27 AM   #2
knc1
Going Viral
knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.
 
knc1's Avatar
 
Posts: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
Your only problem is that the cpu is shutting down.

Search this forum for things such as "kindle clock" -
There are ways out of your problem.

Forum usage foo:
https://www.mobileread.com/forums/sho...d.php?t=237083

And don't forget about the filtering of this index by topic prefix.

Last edited by knc1; 10-08-2014 at 02:31 AM.
knc1 is offline   Reply With Quote
Advert
Old 10-08-2014, 09:56 AM   #3
hawhill
Wizard
hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.
 
hawhill's Avatar
 
Posts: 1,379
Karma: 2155307
Join Date: Nov 2010
Location: Goettingen, Germany
Device: Kindle Paperwhite, Kobo Mini
Timer.schedule() schedules the task once. So everything is working as it should. OP is probably looking for Timer.scheduleAtFixedRate(). Documentation is sparse, but available: http://kdk-javadocs.s3.amazonaws.com...til/Timer.html Note the difference between "fixed delay" and "fixed rate".
hawhill is offline   Reply With Quote
Old 10-08-2014, 10:06 AM   #4
knc1
Going Viral
knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.
 
knc1's Avatar
 
Posts: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
Thanks!

All I could remember for certain is that people have solved the problem of scheduling while the cpu sleeps.

- - - -

66 00 - I gotta get a real life.
knc1 is offline   Reply With Quote
Old 10-08-2014, 04:41 PM   #5
ChronosDragon
Junior Member
ChronosDragon began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Oct 2014
Location: Seattle area
Device: Kindle 4 Non-Touch
Quote:
Originally Posted by hawhill View Post
Timer.schedule() schedules the task once. So everything is working as it should. OP is probably looking for Timer.scheduleAtFixedRate(). Documentation is sparse, but available: http://kdk-javadocs.s3.amazonaws.com...til/Timer.html Note the difference between "fixed delay" and "fixed rate".
Thanks for the advice. I'd actually found that documentation at one point but forgotten about it (because it wasn't immediately useful to my problem at the time). Will bookmark for future reference.

I don't see anything in there about why fixed-delay and fixed-rate would be any different, but if CPU sleeping was an issue I guess I could see why one would work over the other. However, I don't think either of them are working: Just updated my code to use the scheduleAtFixedRate method and it's still not running the update (again unless you lock/unlock):

Huge phone screenshot

Updated portion of code:
Code:
            t = new Timer();
            t.scheduleAtFixedRate(
                new UpdateTimeTask(this),
                new Date(),  // Execute first now
                15000L        // Spacing between executions (15 s)
            );
(I also made the period argument a long literal because...well, I didn't really think there would be an interpretation error there but I'm trying to think of every possibility at this point)

(Also, just to be sure it was indeed running an updated version of the app, I had a couple of intermediate builds where I was playing with adding a version label. Didn't figure out any solution for the moment but I know it's running the new code.)

Thanks again for the help
ChronosDragon is offline   Reply With Quote
Advert
Old 11-12-2014, 12:52 AM   #6
ChronosDragon
Junior Member
ChronosDragon began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Oct 2014
Location: Seattle area
Device: Kindle 4 Non-Touch
Just as a follow up, I came back to this after leaving it for a month or so and got it working a different way. I never got the timer working, but I was able to achieve the same result with a thread:

Spoiler:

Code:
class WorkerThread extends Thread {
    private Main main;
    public volatile boolean shouldRun = true;
    public volatile boolean shouldExit = false;
    public WorkerThread(Main main) {
        this.main = main;
    }

    public void run() {
        for (;;) {
            if (shouldExit) { return; }
            if (shouldRun) {
                try {
                    updateScreen();
                    Thread.sleep(1000);
                } catch (Throwable t) {
                    t.printStackTrace();
                    return;
                }
            }
            else {
                try {
                    Thread.sleep(100);
                }
                catch (Throwable t) {
                    t.printStackTrace();
                    return; 
                }
            }
        }
    }
    
    private void updateScreen() {
        // Snipped: update all the labels
        main.ctx.getRootContainer().repaint(180);
    }
    
}
Which is called from create():

Code:
    public void create(KindletContext context) {
        // Snipped: label creation and positioning

        thread = new WorkerThread(this);
        thread.start();
    }
(thread is a variable of the main Kindlet class)


This works when the kindle is freshly started. However, I noticed that it doesn't exit cleanly. I do have a destroy method that looks like this:

Spoiler:
Code:
    public void destroy() {
        
        thread.shouldExit = true;
        thread.interrupt();
    }


But it doesn't seem to be working properly. If I abort the restart when the kindle notices the failure, and relaunch the app, it no longer updates. That seems odd, since it should be creating a new instance. I don't really know how kindlet session/lifecycle management works, so I just shrugged and ignored it. As long as I only run the app once per session, it works fine.

Anyway, thanks again for the help with this. I don't know if I'll continue making anything for the kindle, as I prefer development environments that aren't actively working against me, but it was an interesting experience nonetheless.

ChronosDragon is offline   Reply With Quote
Old 11-12-2014, 02:02 AM   #7
hawhill
Wizard
hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.hawhill ought to be getting tired of karma fortunes by now.
 
hawhill's Avatar
 
Posts: 1,379
Karma: 2155307
Join Date: Nov 2010
Location: Goettingen, Germany
Device: Kindle Paperwhite, Kobo Mini
So now you know why all the fancy apps here - with the notable exception of KUAL - do not use the Kindlet framework :-)
hawhill is offline   Reply With Quote
Old 11-12-2014, 10:23 PM   #8
ChronosDragon
Junior Member
ChronosDragon began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Oct 2014
Location: Seattle area
Device: Kindle 4 Non-Touch
Quote:
Originally Posted by hawhill View Post
So now you know why all the fancy apps here - with the notable exception of KUAL - do not use the Kindlet framework :-)
Hah, yeah. It's kind of a bitch to work with. I don't really feel like deploying over ssh every time I want to test my app, but maybe that's just my workflow*. In any case, probably won't be doing much with e-ink until a better platform comes around.

For posterity, I've dumped the source code into a zip and attached it to this post. Still haven't nailed down the bad thread exit, but with debug tools being what they are (functionally nonexistent), it doesn't really feel worthwhile to try and track down something that will only affect me when the app closes - which, since I'm using it as an actual clock, will end up being not very often.

*Come to think of it, it wouldn't be that much worse than signing the jar every time, which I automated with a batch file. Still.
Attached Files
File Type: zip Klock.zip (11.60 MB, 172 views)
ChronosDragon is offline   Reply With Quote
Reply

Tags
java, kindle 4, timer, timertask


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
First timer GrannyPan Introduce Yourself 7 06-29-2011 12:34 AM
First Timer EMJESS Introduce Yourself 3 06-24-2011 08:45 PM
Unutterably Silly Using an electronic timer lene1949 Lounge 15 09-11-2010 10:36 PM
Hello from an old new-timer ebuyer099 Introduce Yourself 1 05-21-2010 01:34 PM
first timer ChrisM12743 Introduce Yourself 3 03-29-2008 11:35 AM


All times are GMT -4. The time now is 02:00 AM.


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