Skip navigation

I’ve been dabbling in a bit of web development lately; the prototype paradigm for object-oriented javascript still confuses me, but I really enjoy the framework provided by the DOM and HTML5.  If anybody can recommend in the comments a friendly library that makes working with SVGs easier, I’d greatly appreciate it.  In much belated celebration of Independence Day, I present a silly little page based on this great HTML5/JS fireworks demo: it was intended to trace the whole SVG path with fireworks, but that turns out to be pretty tricky.  What it does do is part fluke, part partial success… wait a few seconds after the firework launching appears to be finished for the sparklers to kick in.  To start, just upload an SVG, cast a shifty glance skyward, and cue the fireworks guy!

nixons_back

My demo requires an SVG file to upload before the fireworks start, so here’s the git octo-cat (src)

Just a quick tip: if you accidentally pass a data type to someTextView.setText() that cannot be implicitly coerced into a CharSequence representation (like, say, an int), logcat will display the error ‘fatal error: Resource$NotFoundException’ citing some weird hex String resource id that you probably don’t recognize. I have no idea why the Android framework does this in response to mismatched data types, but there you have it– if you see a contextually nonsensical error about some resource not being found around a TextView’s setText() method, check the data type of the argument you’re passing to setText().
homer-doh

hugs for all!

Now just friends

Her beauty amplified

Forbidden fruit

Image

As every Android apps developer should know, an ANR is thrown by the system when the UI thread of your application fails to update itself for 5 seconds; it follows then that long or recurrent tasks should be executed on separate threads, so as to leave the UI thread free to update itself.  This much is simple enough.  Where things can get a bit tricky is when you have a recurrent task that needs to send messages to the UI thread, perhaps like this:

public class MyActivity extends Activity{

        public final int MSG_UPDATE_INFO = 0;

        private CustomHandler mvMainThreadHandler;

        @Override

        protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        mvMainThreadHandler = new CustomHandler(this,Looper.getMainLooper());

        MyRunnable runner = new MyRunnable();

        Thread myThread = new Thread(runner);

        myThread.start();

        }

        private class MyRunnable implements Runnable{
        private boolean bIsUpdating = true;

        public boolean getIsUpdating(){

        return bIsUpdating;

        }

        public void setIsUpdating(boolean b){

        bIsUpdating = b;

        }

        @Override

        public void run(){

        while(bIsUpdating){

        //do something while updating is true

        //send message to main thread to let it know

//something has been done

        mvMainThreadHandler.sendMessage(

mvMainThreadHandler.obtainMessage(MSG_UPDATE_INFO));

        }

        }
        }

        private static class CustomHandler extends Handler{
                
                private WeakReference<MyActivity> whAct;
                
                public CustomHandler(MyActivity act,Looper mainLooper){
                    super(mainLooper);
                    
                    whAct = new WeakReference<MyActivity>(act);
                    
                }

                @Override
                public void handleMessage(Message msg){
                    MyActivity hAct = whAct.get();
                    if(hAct != null){
                        
                        if(msg.what == hAct.MSG_UPDATE_INFO){
                          //stuff
                            
                        }
            
                    }//End if hAct != null
                    else{
                        Log.e(TAG, "hAct, derived from whAct.get(), is null");
                    }
                }
                
            }

        }

This is almost fine… except that the run() method of MyRunnable will send the MSG_UPDATE_INFO message to the UI thread on every CPU clock cycle (or as near to that as it can), which will congest the UI thread and stop it from updating itself possibly for long enough to cause an ANR.  The solution is simple– re-implement MyRunnable::run() like so:

@Override

public void run(){

while(bIsUpdating){

try{

Thread.sleep(1000);

}catch (InterruptedException e) {
                    
       e.printStackTrace();

}

//do something while updating is true

//send message to main thread to let it know

//something has been done

mvMainThreadHandler.sendMessage(mvMainThreadHandler.obtainMessage(MSG_UPDATE_INFO));

}

 

with that small but imperative change the info update thread cannot send messages to the UI thread more frequently than once per 1 second, which should clear up the UI thread’s execution pipeline sufficiently to prevent ANRs.  Note though that the 1 second sleep time was chosen arbitrarily and depending on your recurrent task, your target platform, and the rest of your app, it may need to be a specific value; that said, it worked for me!Image

My first language was Java, and though I’ve been working with C++ for a bit now certain things still trip me up now and again.  While working on a class that tokenizes std::string objects based on std::string delimiters, I came across the use-case of wanting to modify char data returned from a string’s c_str() method.  Since this returns a const char* I tried

char* cstr = myStr.c_str();

but that throws a compiler error because it attempts the quite rightly illegal conversion between a const pointer and a mutable one.  In Java the idiom above re-implemented as below would be legal

final String testS = "final";
String testS2 = testS;
testS2 = "mutable";

This is legal in Java only because Java allocates new memory space for testS2 and copies the content of testS into it behind the scenes.  testS remains immutable while testS2 is mutable.  One simple way to obtain the same result in C/C++ is via strncpy() as follows:

std::string myString = "Hello"

const char* cstr_final = myString.c_str();

char cstr_mut[sizeof(cstr_final)];

std::strncpy(cstr_mut,cstr_final,sizeof(cstr_final);

With that cstr_mut is a fully modifiable clone of the content pointed to by cstr_final 🙂

side note: its obvious from my other posts that I enjoy including pictures– I have to advise against entering the search query “c string” in google image search while at work as it seems that there is an article of particularly risque lingerie by that name  *facepalm*

6793050_f260

If you’ve ever tried to keep an instance of std::stringstream or another stream wrapper class as a member value in one of your own classes, you may have run across this set of friendly compiler errors:

“ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private.

iosfwd:55: error: within this context

iosfwd: In Copy Constructor ‘std::basic_stringstream…

streambuf:794: error” ‘std::basic_streambuf… is private

iosfwd:55: error: within this context

iosfwd: In Copy Constructor ‘std::basic_stringstream…”

This occurs because i/o streams cannot be copied!  You’d think they would bold that on every *stream page in the cplusplus.com/reference but sadly they don’t.  Anyway, if you want to work with streams you need to either pass around references/pointers to them or, preferably, create them on the stack as needed and let them pop off automatically.  More discussion available on StackOverflow here

crossed

As of at least ADT plugin version 21 there is a TID column displayed in Eclipse’s logcat viewer when you have display settings on maximum verbosity– this thread id is extremely important to parallel programming (or octopus oriented programming as I prefer to call it). However, the thread id will not be preserved if you simply copy and paste the content of this view from within Eclipse. Instead you’ll see something like [debug level] [timestamp] [invocation] (PID)… but no thread id. I don’t know if there is an easier way to grab the TID data directly from Eclipse, but it can be done using a remote adb shell as follows:

Execute the following from your PC’s command line: adb -s [target_device_serial_number] shell “logcat -v threadtime” > [target_output_file]

your session will appear to hang, but in actuality the device’s logcat output, exactly as shown in eclipse’s logcat view including the TID, is being written in real time to your output file. When you want to view the content, exit the logcat dump process with ctrl+c and open the file on your PC. Other options for logcat filtering etc. can be found here

Logcat

Castles of Code

Logical systematic magnificence

Creation ad infinitum

A19D55 Ch--teau-de-Chambord-castles-697047_1152_864Castle-1-Render

Howling Void

Emptiness burns as she turns away

Rejected again

Supermassive_black_hole