Monthly Archives: February 2015

Pebble: How to autoscroll large text

Last time i described how to load random string from resource. If you recall the code ended up with the line

text_layer_set_text(s_textlayer_quote, (char *)quote);

to display loaded text on the screen. But what if text is too large to fit on the screen? If you’re building a watchface, there’s no scrolllayer with user interaction available. But what we can do is automatically scroll the text for user convenience to gradually reveal entire content.

The trick is to create text layer larger than it’s container. In my case I am displaying text full screen on Pebble window which is 144×168 pixels, but I will create text layer with the height of 2000:

#define WINDOW_HEIGHT 168
#define WINDOW_WIDTH 144  
#define TEXTBOX_HEIGHT 2000
...
s_textlayer_quote = text_layer_create(GRect(0, 0, WINDOW_WIDTH, TEXTBOX_HEIGHT));

If the loaded text is larger then container window we now can dynamically detect the difference:

text_layer_set_text(s_textlayer_quote, (char *)quote);
  
// if height of quote > height of window, initiate animation to scroll
GSize text_size = text_layer_get_content_size(s_textlayer_quote);
int number_of_pixels = WINDOW_HEIGHT - text_size.h;
if (number_of_pixels < 0) {
    animate_quote(number_of_pixels);
}

Here we get dynamic size of loaded text content and if the difference between window height and actual text height is negative – call animation function, passing number of pixel we need to shift by.

Now to animation function. Continue reading →

Pebble: How to load random string from resource

There’s no doubt that Firefly is a greatest TV series in the history of all creation. That is why when I was learning resource handling in Pebble SDK I have decided to create a watchface that would display a random quote from Firefly. And thanks to Bill Hatcher of http://cubemonkey.net/quotes/ I obtained a plain TXT file with almost 500 quotes.

The file is in the format "quote1%quote2%quote3..." e.g. there is a “%” separator between the quotes, so I quickly wrote a small script that gives me a position of each percentage sign within the file, so I can create an array of the positions in my C code for Pebble:

#define NO_OF_QUOTES 472
int aQuotePointers[NO_OF_QUOTES] = {0, 206, 354, 417, 480, 554, 662, 695,... 88825}

which basically gave me position of each quote in the file and which I prepended with 0 and appended with filesize. Then I added the resource to my project (in CloudPeble environment it’s as easy as loading a BLOB resource and giving it a name). A Pebble watchface or watchapp can handle resources of up to 96K, fortunately file with quotes was less, otherwise some kind of string compression would have to be implemented.

After that it’s a trivial matter to generate random position, retrieve quote from that position and display it on a text layer:

// determining number of quote (that will give us address of begining and end)
srand(time(NULL));
int number_of_quote = rand() % NO_OF_QUOTES;
  
//determining size of quote and allocating memory
int size_of_quote = aQuotePointers[number_of_quote + 1] - aQuotePointers[number_of_quote] - 1;
uint8_t *quote = malloc(size_of_quote);

//loading quote, displaying and freeing memory
ResHandle rh = resource_get_handle(RESOURCE_ID_FIREFLY_QUOTES);
resource_load_byte_range(rh, aQuotePointers[number_of_quote] + 1, quote, size_of_quote);
quote[size_of_quote] = 0; //null terminating string
text_layer_set_text(s_textlayer_quote, (char *)quote);

Lines 2-3 generate random index for the array of quote pointers
Lines 6-7 calculate size of the quote (based on position of current and next quote) and allocate memory for the quote
Lines 10-11 load range from the resource based on index and size
Lines 12-13 0-terminate the loaded range and display data on the text layer.

It’s all pretty straightforward and works like a magic and the result you can see in published watchface: Blue Sun Quotes.

Some useful links:

Next time I will describe how I handled situation when loaded quote is too long to display on a text layer

Root your Android device without flashing custom recovery

HTC Rooted
It is fairly straightforward to root an Android phone using SDK platform tools (adb, fastboot), for example this is a very nice guide how to root HTC One M8. Basically you download SuperSU superuser manager to your device, download custom recovery image onto your computer and flash it to your device via command

fastboot flash recovery your_custom_recovery.img

then reboot into newly flashed recovery and flash the SuperSU. Boom, you’re done.

The problem with this approach, once you phone receives OTA (over the air) update (e.g. new version of Android) it needs original stock recovery to install it. If you have custom recovery (e.g. TWRP) – OTA update will fail. The solution is, when you root your phone, not to flash custom recovery, but just to reboot into it without flashing. Instead of above command, use

fastboot boot your_custom_recovery.img

This command will reboot your phone into custom recovery without flashing it. Then you can flash SuperSU and after reboot your phone will be rooted and original stock recovery remains.

You can even save the original stock recovery (after you rooted the phone) in case you do need to flash custom one. This way you can have a backup of stock recovery in case you need to flash it back to install OTA update. Below steps are for HTC One M8, but other devices will have similar approach:

Assuming that your phone is connected to PC, you have correct drivers installed and USB debugging mode enable.

run  "adb shell" command on your PC
"su" (watch your phone and grant permission if needed)
"dd if=/dev/block/mmcblk0p43 of=/sdcard/stock_recovery.img"

This will copy stock recovery of your HTC One M8 into file “stock_recovery.img” on the SD Card.

Credits go to this XDA thread

ASP.NET WebForms: Safe refresh after postback

It’s all too commons scenario in a Web Application, you initiate a postback by clicking a button (basically submitting a form), some action is performed, perhaps database is written to, all fine and good. And then you refresh the page. Or even page is refreshed for some purpose by a client-side JavaScript code. And the dreaded “resubmit” message appears, it differs from browser to browser, e.g. Firefox would say

“To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier”

And if you agree – the form is resubmitted again along with all the actions performed – not good.

This issue happens because the page is submitted via POST request and in order to refresh the page – POST request has to be resubmitted along with form action. And the solution is Post/Redirect/Get pattern. The idea is to take the page submitted via POST request and convert it into GET. In ASP.NET this can be achieved via simple Response.Redirect. You can redirect to another page or you can reload your current one:

Response.Redirect(Request.RawUrl)

This code redirects to original page URL, essentially reloading the from page server side. But now it’s a GET page, safe to refresh

Sideload APKs directly from your phone to FireTV/FireStick

ADP If you’re joining a grown crowd of cordcutters (people who disconnect their Cable TV services) you’re not a stranger to streaming. Devices like Roku and Chromecast go a long way to provide all your TV shows and movies need.

Amazon Fire TV and Fire Stick are the latest additions to the streaming gadgets. One advantage they have over other devices they run Android (albeit heavily modified). This gives you ability to install (sideload) ordinary Android apps onto these gadgets. There’re multiple tutorials on how to do it from desktop computers, but you have to download apps APKs onto desktop. Wouldn’t it be easier if you could do this directly from your phone?
Continue reading →