Sunday, November 8, 2015

Rooting android and executing Super commands

Android 4.4.4 has introduced additional restrictions to turn on GPS/internet programatically. My requirement was to auto flip GPS/internet connection.
With Android 4.4.4 security restriction the android.permission.WRITE_SECURE_SETTINGS would stop the code to flip gps/internet connection. I managed to get around by rooting and following few simple steps:

Steps required to take:

1. Root you Android phone (Note: the warranty of the phone will be void once you root it also do backup data just in case mobile gets bricked)
  Rooting basically means to gain complete access to modify mobile settings at system level. 
  Download and install KingoRoot from here

 Note:  There is an android APK also for rooting, but it didn't work for me, so better to use windows desktop version.

2. Open KingoRoot desktop application and connect your Android device( ensure USB debbuging is turned on  follow link if not)

3. Follow instructions on KingoRoot desktop application and the mobile will have KingoROOT and Superuser APK installed after the mobile gets rebooted. At this stage, the root access is available to KingoRoot and can be given to other APKs who request it.

4. The user apps can now run commands to remove security restrictions: Some command i ran programmatically to remove GPS/internet flip restrictions are below:

a. "pm grant " + context.getPackageName() + " android.permission.WRITE_SECURE_SETTINGS";
b. "settings put global mobile_data 1";  // to enable=1 disable =0 mobile data
c. "svc data enable/disable";   //to enable/disable mobile data incase a. doesnt work.

The code used to run above commands from Android is below.

public static void runAsRoot(String[] cmds) {
try {
Process p = Runtime.getRuntime().exec("su"); // this give super user access
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
Log.i(TAG, "Calling root command : " + tmpCmd);
os.writeBytes(tmpCmd + "\n");
}
os.writeBytes("exit\n");
os.flush();
} catch (Exception e) {
e.printStackTrace();
}
}


Thursday, July 9, 2015

Cordova hurdles


Cordova setup steps (windows 7 + Android):

Pre-requisites
1. Download npm here  and setup. Its a package management tool to install cordova.
2. JDK 7
3. Android emulator or device. Ant might be required for older versions. newer version use gradle build system.

Setup cordova CLI (command line client)
1. From DOS prompt run >npm install -g cordova@x.x.x
Note: cordova specific version can be installed using x.x.x eg: 5.0.0 or remove it for default latest version.

Create Cordova project
  1. cordova create directoryName com.package AppName
  2. cd directoryName
  3. cordova platform add android@x.x.x
  4. cordova plugin add
  5. cordova build android/ios --release
  6. cordova run android/ios --device

Setup on eclipse
1. Follow blog
This is still work in progress... Will share once it is done.

Debug
Add Debug Console plugin to your project by running this command:
$ cordova plugin add org.apache.cordova.console
To see debug output adb logcat CordovaLog, but generates too much logs.

Or use remote debugging http://jsconsole.com/remote-debugging.html (way better logs)

Gotchas
  1. cordova build command first time downloads gradle. On cordova 5.1 had issue downloading it, the fix was as below :
    1. Stackoverflow link
    2. update ..\platforms\android\cordova\lib\build.js (search for .zip and replace to ../[].zip)
  2. Cordova plugin are downloaded from npm package (like maven). 
  3. aidl files failed to compile :  fix
aidl -I/Volumes/work/projects/java/android/cordova/hello/platforms/android/src -p/Volumes/work/projects/java/frameworks/android-sdk-mac_x86/platforms/android-22/framework.aidl IInAppBillingService.aidl

4. Android API 16+ requires JDK 7+ only. And mac Snow Leopard 32 bit doesnt support JDK 7 anymore (ridiculous). Mac is such a waste of time when it comes to backward compatibility.



5. I am yet to figure out how to run cordova(newer projects) from eclipse. Running from CLI is fine, but how can we debug like logcat in eclipse Juno?


6. The cordova.js ,cordova.jar and cordova_plugin.js are generated under \platforms\android\build\intermediates\assets\debug\www\cordova.js
Note the way plugins are included in later version of cordova is through JS.
7. To rename app : http://www.stefangordon.com/renaming-your-cordova-application/


Ionic

https://ionicframework.com/docs/intro/installation/ 1. npm install -g ionic cordova 2. ionic start letsgo-mobile blank 3. cd letsgo-mobile 4. ionic serve