Saturday, May 14, 2016

How to execute a file in node js.

With a text editor, create a file called web-server.js, with the following contents:

var http = require('http');
var serv = http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end('<marquee>Debug Report</marquee>');

Run the file:

node web-server.js

Friday, May 13, 2016

Enable debugging in Firebase Android

You can enable debugging in Firebase using following code.

Output will be look like following snapshot.

05-14 02:32:11.089 6645-7531/com.aybits.discoversaints.discoversaints D/PersistentConnection: pc_0 - Listening on /saints/video:{}
05-14 02:32:11.089 6645-7531/com.aybits.discoversaints.discoversaints D/PersistentConnection: pc_0 - Adding listen query: /saints/video:{}
05-14 02:32:18.001 6645-7531/com.aybits.discoversaints.discoversaints D/PersistentConnection: pc_0 - unlistening on /saints/video:{}
05-14 02:32:18.001 6645-7531/com.aybits.discoversaints.discoversaints D/PersistentConnection: pc_0 - removing query /saints/video:{}
05-14 02:32:18.002 6645-7531/com.aybits.discoversaints.discoversaints D/PersistentConnection: pc_0 - Listening on /saints/video:{}
05-14 02:32:18.002 6645-7531/com.aybits.discoversaints.discoversaints D/PersistentConnection: pc_0 - Adding listen query: /saints/video:{}

Monday, May 2, 2016

Converting Density Independent Pixels (dp) to Pixels (px) in Android

Sometimes we need to convert density independent pixels(dip/dp) to pixels to set view properties like width or height. Here how it is done using Java.

    Resources r = getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300,       r.getDisplayMetrics());

Monday, March 21, 2016

How to check the size of primitive data types in C++

The sizes of variables might be different depending on the compiler and the computer you are using.
Following code will produce correct size of various data types on your system.
#include <iostream>
using namespace std;

int main()
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   return 0;

Following is the sample result. Results may be differ according to your system.

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4

Vector container in C++ STL

Following code demonstrates the vector container (a C++ Standard Template) in C++.

#include <iostream>
#include <vector>
using namespace std;

int main()
   // create a vector to store int
   vector<int> vec;
   int i;

   // display the original size of vec
   cout << "vector size = " << vec.size() << endl;

   // push 5 values into the vector
   for(i = 0; i < 5; i++){

   // display extended size of vec
   cout << "extended vector size = " << vec.size() << endl;

   // access 5 values from the vector
   for(i = 0; i < 5; i++){
      cout << "value of vec [" << i << "] = " << vec[i] << endl;

   // use iterator to access the values
   vector<int>::iterator v = vec.begin();
   while( v != vec.end()) {
      cout << "value of v = " << *v << endl;

   return 0;

Friday, March 18, 2016

Enable Strict Mode in Android.

StrictMode works on a set of policies. There are presently two categories of policies: VM policies and thread policies. The former represent bad coding practices that pertain to your entire application, notably leaking SQLite Cursor objects and kin. The latter represent things that are bad when performed on the main application thread, notably flash I/O and network I/O.

The simplest thing to do is call the static enableDefaults() method on StrictMode from onCreate() of your first activity.

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;

public class FilesDemo extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        if (BuildConfig.DEBUG
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {

   private StrictMode.ThreadPolicy buildPolicy() {
          return(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());

Here, we are asking to flag all faults (detectAll()), logging any violations to LogCat(penaltyLog()).

BuildConfig.DEBUG is a flag that indicates if we are on a debug build or not. The BuildConfig class is code-generated alongside the R class, in whatever Java package we declared in the <manifest> element of our manifest.

Note that StrictMode will also report leaked open files. For example, if you create a FileOutputStream on a File and fail to close() it later, when the FileOutputStream (and related objects) are garbage-collected, StrictMode will report to you the fact that you failed to close the stream.

Sunday, November 15, 2015

How to disable Activity and enable it programmatically?

In this example there are two activities declared in AndroidManifest.xml file. SetupActivity is enabled and MainActivity is disabled by default.

             <action android:name=”android.intent.action.MAIN”/>
             <category android:name=”android.intent.category.LAUNCHER”/>
             <action android:name=”android.intent.action.MAIN”/>
             <category android:name=”android.intent.category.LAUNCHER”/>

You can enable MainActivity by using following code.

private void toggleActivities() {
     PackageManager packageManager = getPackageManager();
     // Enable the main activity
     packageManager.setComponentEnabledSetting(new ComponentName(this, MainActivity.class),
     // Disable the setup activity
     packageManager.setComponentEnabledSetting(new ComponentName(this, SetupActivity.class),

Sometimes you have a component, usually a Service, that you don’t want to expose to the rest of the system for security purposes. To do this, you can set the android:exported attribute to false, which will effectively hide that component from the rest of the system.