Monday, January 19, 2015

how to make a application wide locale change in Andorid

Today, While testing on Arabic language I found that app cannot query data from the database due to Arabic numeral system being different from English. So, I decided to enforce English on whole language

In the Manifest,

 <application
        android:name="MyApplication" >

Code:

public class MyApplication extends Application {
  @Override
  public void onCreate()
  {
    switchLocale()
    super.onCreate();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      switchLocale();
  }
}
public static void switchLocale(Context context) {
    try {
          Locale currentLocale = Locale.getDefault();
       
          if(currentLocale != null) {
              if (currentLocale != Locale.US || currentLocale != Locale.UK) {
                  final String languageToLoad  = "en";
                  Locale locale = new Locale(languageToLoad);
                  Locale.setDefault(locale);
               
                  Configuration config = new Configuration();
                  config.locale = locale;
                  context.getResources().updateConfiguration(config, null);
              }
          }
      } catch (Throwable e) {
       
      }
}

Monday, January 12, 2015

Android Booting Sequence Stages Step By Step

Android Booting Sequence Step By Step

In Booting of Android OS from power on

Stage 1 : Power on and boot ROM code execution
Stage 2 : The boot loader loading
Stage 3 : Starting of Linux kernel
Stage 4 : The init process
Stage 5 : Zygote and Dalvik
Stage 6 : The system server initiation

Stage 1 Power on and boot ROM code execution 

At power on the CPU will be in a state where no initializations have been done. Internal clocks are not set up and the only memory available is the internal RAM.
When power supplies are stable the execution will start with the Boot ROM code. The Boot ROM code will detect the boot media using a system register that maps to some physical balls on the ASIC. This is to determine where to find the first stage of the boot loader.
Once the boot media sequence is established the boot ROM will try to load the first stage boot loader to internal RAM. Once the boot loader is in place the boot ROM code will perform a jump and execution continues in the boot loader.

Stage 2 The boot loader loading

The first boot loader stage will detect and set up external RAM.
Once external RAM is available and the system is ready the to run something more significant the first stage will load the main boot loader and place it in external RAM.
The second stage of the boot loader is the first major program that will run. This may contain code to set up file systems, additional memory, network support, loading code for the modem CPU and setting up low level memory protections and security options.
Once the boot loader is done with any special tasks it will look for a Linux kernel to boot. It will load this from the boot media (or some other source depending on system configuration) and place it in the RAM.
Once the boot loader is done it will perform a jump to the Linux kernel, usually some decompression routine, and the kernel assumes system responsibility

Stage 3 Starting of Linux kernel

It will set up everything that is needed for the system to run. Initialize interrupt controllers, set up memory protections, caches and scheduling.
Once the memory management units and caches have been initialized the system will be able to use virtual memory and launch user space processes.
The kernel will look in the root file system for the init process (found under system/core/init in the Android open source tree) and launch it as the initial user space process.

Stage 4 The init process

The init process in Android will look for a file called init.rc. This is a script that describes the system services, file system and other parameters that need to be set up. The init.rc script is placed in system/core/rootdir in the Android open source project.
The init process will parse the init script and launch the system service processes.

Stage 5 Zygote and Dalvik

init runs the C++ program /system/bin/app_process, and gives the resulting process the name "zygote"
app_process executes, and executes a runtime environment for a dalvik class
app_process does a 'runtime.start("com.android.internal.os.ZygoteInit", startSystemServer)
com.android.internal.os.ZygoteInit:main() starts executing
The profiler is started the Zygote socket is registered (for later communication to start apps) classes and resources are preloaded if startSystemServer is set, then the system server is started
Zygote runs in "select loop mode", where a single process spins waiting for communication to start subsequent apps.
Eventually, a call is made to Zygote.forkAndSpecialize(), which does the actual forking

Stage 6 The system server initiation

The system server is the first java component to run in the system. It will start all the Android services such as telephony manager and bluetooth. Start up of each service is currently written directly into the run method of the system server. source can be found in the file frameworks/base/services/java/com/android/server/SystemServer.java in the open source project. Once the System Server is up and running and the system boot has completed there is a standard broadcast action called ACTION_BOOT_COMPLETED. To start your own service, register an alarm or otherwise make your application perform some action after boot you should register to receive this broadcast intent .



Monday, January 5, 2015

How to start express and websocket server in same port in NodeJS?

How to use the same port to start the web server (express) and web socket server in the same port in NodeJS ?


//app.js
var WebSocketServer = require('ws').Server
    , http = require('http')
    , express = require('express')
    , app = express();
app.use(express.static(__dirname + '/'));
var server = http.createServer(app);
var wss = new WebSocketServer({server:server});
wss.on('connection', function (ws) {
    ws.on('close', function () {
        console.log('close:');
    });
    ws.on('message', function (message) {
        console.log('message:', message);
    });
});
server.listen(3000);