Saturday, 24 June 2017

Show SSIDs in Windows 10 command prompt

Problem:

In Windows, what is one quick way that I can list nearby detected wifi networks, and related information about access points?

I am currently working on an app and need to see if several access points share the same SSID, and IT does not allow me to install network stumblers or similar programs.

Solution:

In the command prompt, you can use either "netsh wlan show networks" or "netsh wlan show all".

Example output of "netsh wlan show all":

More detailed instructions:

  1. Press the following key combination: [windows key] + R
  2. Type in "cmd" in the Run dialog that appears, then click OK
  3. Type in "netsh wlan show all" or "netsh wlan show networks"
  4. A list of nearby access points currently detected will appear if you have done this correctly

Friday, 3 February 2017

C# Ticks to human-readable date/time

Problem:

I'd like to get a human-readable date/time for a number of Ticks in C#.

Tool:

Try the tool below :)

Ticks:
DateTime(UTC):
DateTime(local):

(Please note that this might not work in all browsers due to limitations in number size that a browser can handle. Also, please let me know if I goofed up the calculation. Thanks!)

Notes on calculation:

  • ticks to microtime = ticks / 10000
  • microtime to Unix time = microtime - 62135596800000
  • then convert Unix time to human-readable using Javascript Date()

References:

Friday, 20 January 2017

Restore my Xamarin project's NuGet and Xamarin-Component dependencies using CLI

Problem:

How do I restore all of my Xamarin project dependencies when neither Visual Studio's "extensions and updates", nor NuGet package manager GUI have the versions my project requires?

Similarly, how do I restore these packages in the command line so Jenkins can automatically build my Xamarin project?

My questions apply to fresh clones of my team's Xamarin project from Git, where the NuGet nor Xamarin component 3rd party dependencies aren't included in the project's repository.

Before you start:

The solution below assumes that you have knowledge of Xamarin, Xamarin components, NuGet, (Jenkins), the command line, and have a valid account for Xamarin. (It was written as a quick reminder. Please refer to the referenced links if more detail is needed.)

One solution:

One solution is to use the nuget.exe and xamarin-component.exe command line tools to restore NuGet and Xamarin component dependencies.

You can get the Xamarin component CLI executable from here: https://components.xamarin.com/submit/ (you'll need to sign in with your Xamarin account, and follow the instructions Microsoft provided)

You can get the NuGet CLI executable from here: https://dist.nuget.org/index.html

When you have these CLI tools installed (or copied to your project's root - not ideal, but helps to troubleshoot), you can then use them or include them in your script as follows:

  • navigate to the directory where your solution file is (e.g. YourSolutionName.sln)
  • 
    nuget.exe restore
    xamarin-component.exe restore YourSolutionName.sln
    
    
  • (first time) enter your Xamarin account credentials (needed for xamarin-component)

At this point, the output should give an indication of all 3rd party dependencies that were successfully restored.

Notes:

This solution was tested to work on a project that has dependencies about a year old (and are no longer available via the Visual Studio GUI). The versions of the tools tested were Visual Studio 2015 (14.0.25431.01 Update 3), Xamarin 4.2.2.11 (with corresponding Xamarin.Android 7.0.2.42 and Xamarin.iOS 10.3.1.8). Additionally, all 3rd party libraries were originally installed using Visual Studio's "Extensions and Updates..." GUI and NuGet package manager's GUI. Your mileage may vary, depending on versions and how your team has setup your Xamarin project.

This solution also depends on Microsoft/Xamarin still providing the CLI tools, as well as the 3rd party dependency versions specified in your project still existing for download.

Nonetheless, hopefully this has at least come in handy or given some new ideas :)

References:

Thursday, 5 January 2017

Delay lock screen display sleep time in Windows 10

Problem:

In Windows 10, how do I prevent my displays from turning off shortly after locking my screen?

Power savings nor display life are an issue for my situation.

Reason: I have an external dock that causes all my windows to move to my primary display after the displays are turned off.

Cautions:

This how-to assumes you are comfortable with editing the registry. Serious damage can be done if you make a mistake!

Workaround:

  1. enable "Console lock display off timeout" in the Advanced Power Settings via the registry:
    • HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\7516b95f-f776-4464-8c53-06167f40cc99\8EC4B3A5-6868-48c2-BE75-4F3044BE88A7
    • set "Attributes (type DWORD)" to 2
    • (to hide, set Attributes back to 1)
  2. open advanced power options (or close then reopen, if it was already open before the registry change)
  3. change timeout to desired value (example screenshot below):

Notes:

  • This was tested to work in Windows 10 Pro.
  • Changing this setting is strongly discouraged in situations where power saving or monitor life are important.
  • This only works for lock screens while a user is signed in.

References:

Wednesday, 4 January 2017

Android screenshot to computer with adb

Problem:

How do I use adb to get a screenshot from an Android device or emulator?

I'd like to target a specific device when more than one are connected to adb.

I already know how to open adb shell and put a device in debugging mode.

Solution:

One method is the following:

  1. Get list of devices:
    adb devices -l
    

    You should get a list that looks like:

  2. Once you have a device identifier, such as "emulator-5554", you can use similar commands as below. (Replace "emulator-5554" with your device identifier).
    adb -s emulator-5554 shell screencap -p /sdcard/screencap.png
    adb -s emulator-5554 pull /sdcard/screencap.png
    
  3. (Optional) remove the last screencap from your device (in this example, named "emulator-5554"):
    adb -s emulator-5554 shell rm /sdcard/screencap.png
    

Other notes:

If you've only got one device accessible to adb, you can simply do the following:

adb shell screencap -p /sdcard/screencap.png
adb pull /sdcard/screencap.png
adb shell rm /sdcard/screencap.png

If you'd like to see a one-liner to do the above, check out the neat example here: http://blog.shvetsov.com/2013/02/grab-android-screenshot-to-computer-via.html

References:

Tuesday, 13 December 2016

Java Enum get value from String

Problem:

In Java, I have an Enum and I'd like to be able to look up one of its value using a String.

Solution:

In order to get a specific Enum value from a String, use the java.lang.Enum.valueOf() function.

For instance, if I have the following Enum:

enum Day {Mo, Tu, We, Th, Fr, Sa, Su}

Then the following code gets Day.Th from the String "Th":

Day.valueOf("Th")
Another example, assuming the above Enum Day is defined:
Day myDayExample;
myDayExample = Day.valueOf("Th");

System.out.println(myDayExample); // prints: Th

Notes:

If either the Enum type or string is null, then java.lang.Enum.valueOf() will throw a NullPointerException.

Additionally, if no Enum constant with the name in the String exists, an IllegalArgumentException will be thrown.

For more information, check out the references below.

References:

Sunday, 11 December 2016

Efficient way to find the sign of a number in Java

Problem:

What is an efficient way to find the sign of a number in Java?

Solution:

To find the sign of a number, you can use the signum() function. (e.g. Math.signum(), Integer.signum(), Long.signum(), etc.)

For example:


int sign1 = (int) Math.signum(-123); // -1
int sign2 = (int) Math.signum(222);  //  1
int sign3 = Integer.signum(-1234);   // -1

double sign4 = Math.signum(-123.0);  // -1.0
float sign5 = Math.signum(111.0f);   //  1.0f

double sign6 = Math.signum(0.0);     //  0.0

Notes:

For more information (minimum JDK version, etc.), check out the references below.

References:

Javascript Unix time to UTC string in Chrome console

Problem:

In the Chrome Dev Tools console, I'd like to view a Unix Time numerical value/timestamp as a human-readable UTC date/time string.

Solution:

In the Chrome Dev Tools console (and in general), one way to view a Unix (POSIX/Epoch) Time as a human-readable UTC string is to use Javascript Date.

For example, if the number of seconds since Epoch is 1480528272, we can convert this in the console by:

new Date(1480528272*1000).toUTCString()

Note that we need to convert the number of seconds to milliseconds since Epoch by multiplying by 1000.

Notes:

Note that due to the size of a Javascript integer, overflow may occur if the desired timestamp is larger than Number.MAX_SAFE_INTEGER.

A Javascript Date object also has a valid range that must be adhered to. More information can be found in here.

References:

Saturday, 10 December 2016

GWT disable CellTable row highlight on mouse hover

Problem:

In GWT 2.7, how do I disable CellTable row highlight during mouse hover?

Solution:

Imagine your CellTable is named theTable. To disable the mouse-hover row highlighting, try the following:

theTable.setSkipRowHoverStyleUpdate(true);

Notes:

This was verified to work in GWT 2.7. Your mileage may vary with other versions. This reminder was meant as a quick reference for myself, so it's a bit brief. Check out the references below for more detail.

Also note that the above code might also work for GXT DataGrid, depending on version.

References:

Convert C# bytes to human-readable strings

Problem:

How do I convert a C# byte into a string so that I can read all 8 digits?

Solution:

Imagine that you have a C# byte named myByte. To get a string containing all 8 binary digits, you can use the following code:

  Convert.ToString(myByte, 2).PadLeft(8, '0');

Notes:

Also remember that the Convert.ToString method is in the System namespace.

References:

https://msdn.microsoft.com/en-us/library/system.convert.tostring(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.string.padleft(v=vs.110).aspx http://stackoverflow.com/questions/4829366/byte-to-binary-string-c-sharp-display-all-8-digits

C# selectively disable warnings

Problem:

I would like to selectively disable C# warnings.

Solution:

The syntax to selectively disable warnings is:

#pragma warning disable <warning number or warning list>
   <code block where warning is to be ignored>
#pragma warning restore <warning number or warning list>

Example:

#pragma warning disable 0618
  MyNecessaryObsoleteFunctionCall();
#pragma warning restore 0618

BadObsoleteCallThatShouldProduceWarning();

Notes:

C# compiler warnings are generally there for good reason, so it's better practice to resolve them rather than hide them. However, in some situations willfully ignoring/acknowledging a warning might be necessary (e.g. if warnings block your team's build, but the code is temporarily required for some important reason).

References:

https://msdn.microsoft.com/en-ca/library/441722ys.aspx http://stackoverflow.com/questions/968293/c-sharp-selectively-suppress-custom-obsolete-warnings

Tuesday, 29 November 2016

View GWT emulated Long in browser developer tools

Problem:

I would like to see the long value of a GWT emulated Long in my browser's developer tools.

I already know how to pause and debug my web app, but when I inspect a Long value, it looks similar to {h:0, m:544, l:54210}.

Solution:

Imagine that we have an emulated Long value: myLongVal={h:1, m:234, l:567}

To translate this to a human-readable Long (in most, but not all cases), type the following in the developer tools console:

myLongVal.h*Math.pow(2,44) + myLongVal.m*Math.pow(2,22) + myLongVal.l

(See screenshot)

Notes:

This was verified to work in GWT 2.7. Your mileage may vary for other versions.

The above method should work for Long values that are low enough. However, overflow may occur for larger emulated Long values as the cast to a Javascript double does not cover as many bits as a long integer.

Note that if you have a browser that actually supports 64-bit integers or larger, you might be able to use the following:

(myLongVal.h << 44) + (myLongVal.m << 22) + myLongVal.l

References:

Sunday, 27 November 2016

Chrome CPU throttling

Problem:

I would like to slow down, or throttle Chrome to test my web app's responsiveness. My computer is too fast to represent devices in the real world.

Solution:

Enable CPU throttling in Chrome Developer Tools:

  • F12 to open developer tools in Chrome
  • click on the "Timeline" tab
  • click the CPU throttling drop-down (beside the trash can/garbage collect icon) (see image below)
  • select your desired throttle speed

Notes:

This was verified to work in Chrome version 54. Your mileage my vary with different versions and on different machines.

Also note that while this may help simulate and uncover some performance issues for scripts during development, it may not completely simulate issues experienced on slower devices. Using a virtual machine, or a real test machine would generally be preferred for more comprehensive testing.

Nonetheless, a big "thank you" to the Chrome developers for including such a useful feature :)

References:

Package-level javadoc comments

Problem:

How can I add package-level javadoc comments to my Java project?

Solution:

  • In your package's directory, add a file named "package-info.java"
  • In this file, include the package-level javadoc comment, as well as the package declaration

Example:

myprojectroot/mypackage/package-info.java:

/**
 * Provides an example package-level javadoc comment.
 * 
 * @since 1.0
 */
package myprojectroot.mypackage;

Additional hints:

  • in Eclipse, you can create this file by adding a "new file" to the package (instead of a new Java class, etc.)

References:

Copy local scope variables in Chrome developer tools

Problem:

I would like to use Chrome developer tools to access local scoped variables when debugging Javascript applications.

I am already familiar with debugging with using developer tools, including setting breakpoints and pausing code execution.

How to reminder:

  • set break point to pause execution within the desired scope
  • to access the local scope variable, type it in the console while script execution is still paused in the desired scope (see image below)

Notes:

This was verified to work in Chrome version 54. Your mileage may vary using other versions.

Sunday, 20 November 2016

Clear icon cache in windows 7/8/10

Problem:

I want to clear the Windows icon cache because a Windows shortcut is currently displaying an old, incorrect icon.

Solution:

  • press [Windows Key]+R (to get the run dialog)
  • in windows 7/8, run the command: ie4uinit.exe -ClearIconCache
  • in windows 10, run the command: ie4uinit.exe -show

Additional notes:

This was verified to work in Windows 7 and Windows 10 without requiring a restart. Your mileage may vary.

References: