import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
class WorkerThread extends Thread implements Executor {
private Handler handler;
private boolean isAlive = false;
public WorkerThread() {
super("WorkerThread-" + System.currentTimeMillis());
}
public void run() {
Looper.prepare();
this.isAlive = true;
this.handler = new Handler();
synchronized (this) {
super.notifyAll();
}
Looper.loop();
}
public Handler getHandler() {
return this.handler;
}
public void stopThread() {
this.isAlive = false;
this.handler.getLooper().quit();
this.handler.removeCallbacksAndMessages(null);
}
public static WorkerThread createWorkerThread() {
WorkerThread thread = new WorkerThread();
synchronized (thread) {
thread.start();
try {
thread.wait();
} catch (InterruptedException e) {
throw new RuntimeException();
}
return thread;
}
}
public void execute(Runnable command)
{
if (this.isAlive)
this.handler.post(command);
}
}
Thursday, March 5, 2015
Android WorkerThread class
How to check whether device is rooted in Android
Normally we detect whether device is rooted by checking for su binary. Problem is su binary can be in many location based on rooting techniques.
protected static boolean checkForRoot()
{
String[] arrayOfString = new String[8];
arrayOfString[0] = "/sbin/";
arrayOfString[1] = "/system/bin/";
arrayOfString[2] = "/system/xbin/";
arrayOfString[3] = "/data/local/xbin/";
arrayOfString[4] = "/data/local/bin/";
arrayOfString[5] = "/system/sd/xbin/";
arrayOfString[6] = "/system/bin/failsafe/";
arrayOfString[7] = "/data/local/";
int j = arrayOfString.length;
int i = 0;
while (i < j)
{
String str = arrayOfString[i];
if (new File(str + "su").exists()) {
return true;
}
i += 1;
}
return false;
}
Sunday, February 22, 2015
How to insert a line into a file in NodeJS
How to insert a text in to a specific line in a text file using NodeJS.
function insertLine(file, lineNumber, insertText) {
var array = fs.readFileSync(file).toString().split("\n");
array.splice((lineNumber - 1), 0, insertText); fs.writeFileSync(file, array.join('\n'), 'utf8');
}
Friday, February 13, 2015
Fonts are distorted/blur after Windows Update in Vista 32/64 bit
After yesterday Windows Update (KB3013455) Courier New front got distorted very bad in my PC. It got bad to the level that it is not longer readable. This font change affected everywhere. Chrome, Notepad ++ ect and i was super mad for half of the day looking for a solution.
to fix this problem you must uninstall KB3013455 from Windows Updates and reboot the device
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 .
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);
Subscribe to:
Posts (Atom)


