About jontas

I like to make things that people find useful.

SQLServer Adding a column to a table

Adding a column to existing table is not that hard. One thing to keep in mind, if the column is not nullable then a default value is mandatory.

ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} 
DEFAULT {DEFAULT_VALUE}

Git SSL certificate problem – how to turn off SSL validation for a repo

This will start out with an error such as the following:

$ git pull origin master
error: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed while accessing https://github.com/mopidy/mopidy.git/info/refs

If it is acceptable to turn off the SSL validation instead of actually solving the issue this will turn off validation for the current repo

git config --local http.sslVerify false

If you would rather have this as a default behaviour for git then the following will do it for all repos

git global --local http.sslVerify false

and for those that would rather add to the .git/config file directly the entry looks like

[http]
    sslVerify = false

Samsung Omnia II Master reset

In order to do a factory reset simply open the phone and dial “*2767*3855#”

The phone will restart and then do a master reset (make sure the battery is not too low now, that will be bad).

Another way should be “Settings > Basic Settings > Memory Settings > Clear memory > Enter + 1234 and confirm”

Another way is to hold all of the following buttons at once “green dial + power + volume up+ keylock” that will give you a question if you are sure (green dial confirms).

Android garbage collection types

We are all used to seeing lines like “D/dalvikvm( 8285): GC_CONCURRENT freed 494K, 9% free 14648K/16071K, paused 2ms+2ms” in the logcat, but what does the types mean (why is the device Garbage collecting just then)?

From the sources “dalvik/vm/alloc/Heap.h” we find that

typedef enum {
    /* Not enough space for an "ordinary" Object to be allocated. */
    GC_FOR_MALLOC,
    /* Automatic GC triggered by exceeding a heap occupancy threshold. */
    GC_CONCURRENT,
    /* Explicit GC via Runtime.gc(), VMRuntime.gc(), or SIGUSR1. */
    GC_EXPLICIT,
    /* GC to try to reduce heap footprint to allow more non-GC'ed memory. */
    GC_EXTERNAL_ALLOC,
    /* GC to dump heap contents to a file, only used under WITH_HPROF */
    GC_HPROF_DUMP_HEAP
} GcReason;

And a slightly more expanded explanation is:
GC_FOR_MALLOC was triggered because there wasn’t enough memory left on the heap to perform an allocation.

GC_EXPLICIT means that the garbage collector has been explicitly asked to collect.

GC_CONCURRENT Is triggered when the heap has reached a certain amount of objects to collect.

GC_EXTERNAL_ALLOC is triggered when the VM is trying to reduce the amount of memory used for collectable objects, to make room for more non-collectable.

Java safe conversion of float to int

In most cases this is really not needed, if possible please keep the float as a float and work with it as such, but if needed the following works

public static Integer floatToInteger(float value){
    if (Math.round(value) < Integer.MIN_VALUE || Math.round(value) > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
            (value + " cannot be cast to int without changing its value.");
    }
    return (int) Math.round(value);
}

(It might be needed for instance for a switch/case as that don’t work with float – or to work with external/legacy API’s that we can not or wish not to modify)

Git: Fetch a (existing) remote repository to local repository

Lets say that there is an existing repository that we should pull the files from.

mkdir local-repo
cd local-repo
git init
git remote add origin user@server:remote-repo
git pull origin master

What is done here is

  • git init Initializes the directory as a git repository (creates .git directory and add needed files)
  • git remote add name url adds a remote repository with a name (origin) and url (user@server:remote-repo)
  • git pull name branch pull the existing content from the given brach (master)

The main difference bewteen this and creating a new remote repository is that no files are added and no initial commit is being made – since there is content in the remote repository we are joining.

Sony Smart Extras (Smartwatch) Keeping the screen on – controlling the screen state

In the utils package there is a function that can be used to control the screen state.

    /**
     * Set the accessory screens state.
     *
     * @see Control.Intents#SCREEN_STATE_AUTO
     * @see Control.Intents#SCREEN_STATE_DIM
     * @see Control.Intents#SCREEN_STATE_OFF
     * @see Control.Intents#SCREEN_STATE_ON
     *
     * @param state The screen state.
     */
    protected void setScreenState(final int state) {
        if (Dbg.DEBUG) {
            Dbg.d("setScreenState: " + state);
        }
        Intent intent = new Intent(Control.Intents.CONTROL_SET_SCREEN_STATE_INTENT);
        intent.putExtra(Control.Intents.EXTRA_SCREEN_STATE, state);
        sendToHostApp(intent);
    }

So all we need to do in order to control the screen state is to call this with the correct screen state intent. The following example will keep the screen on

 setScreenState(Intents.SCREEN_STATE_ON);

Android: Rotate a bitmap

Here is a small sample that will rotate a bitmap.

Bitmap animation = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.eye_rotate, mBitmapOptions);
 
Bitmap bitmap = Bitmap.createBitmap(animation.getWidth(), animation.getHeight(), BITMAP_CONFIG);
bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
 
Matrix matrix = new Matrix();
matrix.reset();
matrix.setTranslate(0, 0);
matrix.postRotate(degrees, (animation.getWidth()/2), (animation.getHeight()/2));        	
 
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
int xPos = 0;
int yPos = 0;
Rect src = new Rect(xPos, yPos, xPos + animation.getWidth(), yPos + animation.getHeight());
Rect dst = new Rect(0, 0, animation.getWidth(), animation.getHeight());
 
canvas.drawBitmap(mBackground, src, dst, paint);
canvas.drawBitmap(animation, matrix, null);