Tag Archives: trick

Simulating Pebble GPath in Rocky.js

RockyJS is a black magic voodoo from Pebble Dev team. It allows you to run your JavaScript code on the actual smartwatch (unlike PebbleJS that runs on the phone). When RockyJS debuted it ran as a simulation in a browser, but since then it matured and now runs in Pebble emulators and on actual hardware.

RockyJS changed drastically since that web release. It resembles C code less and takes more standardized JavaScript approach. During that transition some features were lost. One of them is Pebble GPath concept – a graphical object that consist of set of coordinates that you can freely move and rotate. In particular missing commands gpath_move_to, gpath_rotate_to and gpath_draw_outline that move, rotate and draw the GPath. When I was porting my first Pebble watchface to Rocky I used those extensively. You can read about that implementation complete with the source code here. But now the commands are gone and I needed a substitution. Continue reading →

Fixing problem with Yahoo geolocation service

Recently two Pebble watchfaces I’ve developed based on Paul Joel designes: Clean&Smart and Cobblestyle began to experience weather update issues – as in “weather was not updated at all”. Looking into the issue I found a weird thing: If you set your location manually – weather worked. But automatic weather based on phone location did not.

The way automatic location works is pretty straighforward. First this function is called to determine location:

function getLocation() {
  navigator.geolocation.getCurrentPosition(
    locationSuccess,
    locationError,
    {timeout: 15000, maximumAge: 60000}
  );
}

It uses phone location services (GPS, if its available, otherwise network-based location). In case of success it calls locationSuccess function. And in my tests it always called it, so location was determined successfully. Continue reading →

Detect IFRAME click from parent page

If you need to detect a click on a Web Page, that’s trivial: just catch Document or Body onclick event and any element on the page you click will bubble the event to your handler.

But if one of those elements is an IFRAME – that won’t work. IFRAMEs contain other pages and their events a contained within their content and don’t bubble up. Luckily there’s a way. Take a look this snippet of jQuery code:

$('IFRAME').on('load', function () {
   $(this).contents().on('click', function () {
      alert('Click Detected!');
   })
})

It attaches handler to IFRAME’s content’s onclick event, but only after IFRAME has already been loaded. Place this code in parent page that contains IFRAMEs and it will work universally across all browsers to detect when IFRAME was clicked.

NOTE: As usual in these scenarios, this works only if parent page and children pages comply with Same Origin Policy.

Cancel long running SQL Command in ASP.NET WebForm application

It’s an all too common scenario when your ASP.NET page takes too long to load and the culprit is slow, long running SQL query. It shouldn’t come to this, you should optimize your DB stuff to minimize delays, but if you’re trying to decode feline genome or find alien live in the neighboring galaxies – that’s unavoidable. So the page is running and at some point you decide enough is enough and decide you need to cancel it. But you want to do it gracefully, for example slow page is in an IFRAME and you want to remain in the parent page and you don’t want to close/reload the whole thing.

There’s a way. The idea is, every time you create an SqlCommand – you add it to static (shared in VB.NET) list. If command runs successfully – you remove it from the list. But if it takes too long – you can issue an AJAX call from client page to cancel the command stored in that list.

Thanks Arsalan Tamiz for posting this solution to my question on StackOverflow. His demo project was in C# (you can download it from the above link). but since most of my projects are in VB.NET – I did a conversion with some adjustments.
Continue reading →

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

Solution: Lenovo Thinkpad w540 black LCD screen (only external monitor works)

I spend ungodly amount of time trying to solve this issue, there were many similar issues reported on Lenovo forums, but no solutions and contacting Lenovo support is akin jumping thru hoops of fire.

The problem began after I plugged my brand new Thinkpad w540 into external monitor and set it to mode “Extended display (monitor + built-in screen). For a while everything worked fine, but then laptop own screen went black – and nothing could revive it. The external monitor worked fine and the maddening part was – when laptop booted – Lenovo logo appeared bright and shiny on laptop own screen, so I knew graphics card was OK. I ran full Lenovo System update, I updated NVidia drivers from Nvida site – nothing helped.

Then I tried to update Intel drivers for laptop’s HD Graphics 4600 card. Intel installer refused to install them because “The driver being installed is not validated for this computer“. To bypass that I

1. Unzipped the installer package into it’s own folder (you can use WinRar to unpack actual EXE or you can download Zip version of the package).
2. Went to Device Manager, right clicked on “HD Graphics 4600 card” driver and selected Update.
3. When prompted I selected “Browse my computer for driver software”
4. Selected “Let me pick from list of Device Drivers”
5. Clicked “Have disk” and browsed for an INF file inside of GRAPHICS folder of the unpacked driver files from Step 1

Installation commenced and lo and behold – laptop’s display sprang to life.

These instructions are specific to this laptop model and videocard, but I imagine they might be helpful in other similar situations. And hopefully Lenovo will take notice and finally update their drivers.

Access nested controls in ASP.NET Page PreInit event (when no Master Page is involved)

There’re situations when you need access ASP.NET web controls very early in page lifecycle, more specifically – in Page PreInit event – and you can, but only top-level controls. But what if you need to access child/nested controls? The example below uses Infragistics WebHierarchicalDataGrid as a child of Infragistics WebSplitter, but this pretty much applies to any such scenario.

Let’s say you have following layout

<ig:WebSplitter ID="WSP" runat="server">
   <Panes>
      <ig:SplitterPane runat="server">
         <Template>

            <ig:WebHierarchicalDataGrid ID="WHG" runat="server">
            </ig:WebHierarchicalDataGrid>

         </Template>
      </ig:SplitterPane>
      <ig:SplitterPane runat="server"></ig:SplitterPane>
   </Panes>
</ig:WebSplitter>

As you can see grid “WHG” is nested withing first pane of splittet “WSP. Let’s see what happens if you try access the controls in PagePreInit event: Continue reading →

Infragistics WebDataMenu: Manual postback from client-side Click event

There’s a a few possible scenarios when you need to manually to initiate server-side Click event of Infragistics WebDataMenu control. For example in client side click event you do some verification/user confirmation and upon positive confirmation (e.g. user clicks YES) – server-side Click event should kick in.

In a normal flow of event you can use set_cancel(bool) method to allow/disallow natural menu postback e.g.

function Menu_ItemClick(sender, eventArgs) {

   if (confirm('Are you sure?'))
       eventArgs.set_cancel(false)
   else
       eventArgs.set_cancel(true)

}

This works because this dialog stops code execution waiting for the user input. But what if you use something like jQueryUI dialog that relies on callback functions to get user feedback? In this case execution of the code continues immediately so you have to cancel postback right away and instead in the callback of the dialog initiate manual postback e.g

function Menu_ItemClick(sender, eventArgs) {

   $("#dialog").dialog({
      resizable: false,
      height:140,
      modal: true,
      buttons: {
        "Yes": function() {
          $(this).dialog("close");
          __doPostBack(sender.get_id(), 'ItemClick' + eventArgs._getPostArgs());
        },
        "No": function() {
          $(this).dialog("close");
        }
      }
   });  

   eventArgs.set_cancel(true)

}

The example above initiates modal confirmation jQuery UI dialog and immediately cancels menu postback (Line 18). Later if user clicks “Yes” button – dialog is closed and manual menu postback is initiated (Line 10) – it passes menu ID and correct postback argruments which result in server-side Click event of menu control. If user clicks “No” – dialog is closed and nothing else happens.

Compare 2 source files and add difference to 3rd

Beyond Compare 3-way merge

Sometimes when you patch or update your current version of software there is a need to apply the same changes to a previous version. Ordinary it’s a pain – you need to painstakingly determine which changes from the current source should go to previous version (because current source has changed a lot, new features added etc).

Enter Beyond Compare – coolest tool to do all kind of compassion. One of the best features – “3 way merge”. It allows you to compare 2 files and apply the difference to 3rd.

In our case here is what it can do:

  • Select new patched version of source file and compare it to old unpatched version
  • In menu select “Session –> Merge Files” – a middle pane appears that allows you to select 3rd file
  • In the middle tab select new unpatched version

That’s it! Beyond Compare will compare new patched and new unpatched version. If there’s a difference – those lines will be applied to old unpatched version. Otherwise if both new versions are the same – old version keeps it lines, even though they’re different from new version.

It’s all performed automatically all you have to do in the bottom pane (where merge result appear) select the filename to save to and hit “Save”