Android ScreenCap with ADB

I found a great blog post on how to capture a screenshot from a connected device using ADB – I know I can use eclipse or android studio to do it, but I wanted a quick grab from the command line, and I didn’t want to be bothered with capping it to the local disk of the android device.

Anyway, here’s the one liner that works on MacOS:

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

Cron on MacOS

I wanted to add a Cron job to my Mac – I just needed to poll a PHP script on a server once a day, so pretty simple.

The trick is using crontab with nano as the editor! In order to pull that off (I hate VI), I used the following command:

env EDITOR=nano crontab -e

Then it was just some pretty simple use of CURL to fetch from the URL of the PHP script:

25 20 * * * curl -s http://xxx.xxxxxxx.com/refreshTimeStamps.php > /dev/null

So, at 8:25pm every night, curl will pull from that URL, dump the output to null and not send me a mail – easy!

Clear GooglePlay InApp Test Purchases

Google has no helpful documentation on how to clear test purchases you’ve made on your device with a Alpha/Beta build.  If I clear the App’s local cache, and re-run it, then it just automatically remakes the purchases (which is a good thing – no restore purchase option required).

I found this on StackOverflow, and posted it here as a reminder to myself:

adb shell pm clear com.android.vending

Does the trick – remember to clear the App’s cached data store too.

Unity3D IAPs

I’ve been upgrading my Unity3D system to use the new Unity3D IAPS.  I’d been using a mixture of my own stuff on iOS and Unibill for Android, and wanted to clean up my code and simplify the cross-platform process.

Upgrading my code to reference UnityEngine.Purchasing system wasn’t too hard, but there was one thing I came across that cost me a lot of wasted time, the selected Store on Android.

For an Android build, you have a selection of store fronts you can support – Google Play, Amazon & Samsung.  (They just added a ‘Select Store at Runtime’ option too).

You make this selection using the Editor, but there is nothing to indicate what this selection is.  I had selected Amazon while messing around in the project, and promptly forgot I’d done that.  So when I finally called the code to initialize the purchase tokens, they would never authorize and always return ‘product not found’ error.  Initially I blamed this on the fact it can take Google Play 24 hours to activate a new purchase token, so I took a break…

The next day, the same problem, but I thought maybe the 24 hours wasn’t technically up yet, or maybe Google was taking longer than normal.  I pushed on with some other tasks, and checked the products by running the App from time to time on an Android device and watching ADB for the results – still nothing.

That’s when I remembered the store selector! I selected Google Play, rebuilt and presto, it worked! However, it would have been nice if either (or both)

(a) The Editor menu had a check mark beside the selected store

(b) The system spat out an ADB message with the name of the store it was checking

It also turns out you can select the store via a script, which I decided was the better option for me.

UnityEngine.Purchasing.UnityPurchasingEditor.TargetAndroidStore(androidStore)

where:

public AndroidStore androidStore;

is a property on a MonoBehaviour which creates a nice drop down in the inspector.

Non-breaking space in Unity3D/NGUI

I was messing with some text line wrapping an an app today, and needed to stop NGUI inserting a line break between a number and a label in a long string.  Using the escape sequence ‘\u00A0’ did the trick.

return category + "\u00A0#" + indexInCategory;

Now the last word in the string category and the number preceded with a # stay on the same line!

Note: I read that pressing ALT-Space on the Mac will insert this character.  It may do that, but MonoDevelop/NGUI don’t seem to care.  The escape literal worked, the ALT-Space just acted like a regular space.

Note: To insert a non-breaking space in a XML document without using Document Entities, use the following: