Requesting Permissions at Run Time in Android

Request the permissions you need

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

Handle the permissions request response

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

References
https://developer.android.com/training/permissions/requesting.html

Calling Java Methods with JavaScript from Crosswalk

mXWalkView = (XWalkView) findViewById(R.id.activity_main);
XWalkSettings webSettings = mXWalkView.getSettings();
webSettings.setJavaScriptEnabled(true);
public class JsInterface {
	public JsInterface() {
	}
	
	@JavascriptInterface
	public String sayHello() {
		return "Hello World!";
	}
}
mXWalkView.addJavascriptInterface(new JsInterface(), "NativeInterface");
<a href="#" onclick="clicked()">Say Hello</a>
function clicked() {
    alert(NativeInterface.sayHello());
}

References
http://zhangwenli.com/blog/2014/08/25/crosswalk-calling-java-methods-with-javascript/
https://developer.android.com/guide/webapps/webview.html#BindingJavaScript
http://stackoverflow.com/questions/25478434/crosswalk-call-java-method-from-javascript

Embedding Crosswalk on Android

download Crosswalk .aar file
https://download.01.org/crosswalk/releases/crosswalk/android/maven2/

Then copy to libs folder

allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}
compile(name:'xwalk_core_library', ext:'aar')
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="iterator.ir.bmi.MainActivity"
             tools:ignore="MergeRootFrame">

    <org.xwalk.core.XWalkView
            android:id="@+id/xwalkview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</FrameLayout>
public class MainActivity extends XWalkActivity {

    XWalkView mXWalkView;

    @Override
    protected void onXWalkReady() {
// Do anyting with the embedding API
        mXWalkView.load("file:///android_asset/www/index.html", null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mXWalkView = (XWalkView) findViewById(R.id.xwalkview);
    }
}

References
https://developer.chrome.com/multidevice/webview/gettingstarted
https://crosswalk-project.org/documentation/android/embedding_crosswalk_rvw.html
http://stackoverflow.com/questions/40256449/intellij-idea-how-to-import-aar
https://download.01.org/crosswalk/releases/crosswalk/android/maven2/

How to flash an Android Device manually

fastboot erase boot
fastboot erase cache
fastboot erase recovery
fastboot erase system
fastboot flash bootloader "name of bootloader"
fastboot reboot-bootloader
fastboot flash radio "name of radio"
fastboot reboot-bootloader
fastboot -w update "name of zip file"

or 

fastboot flash system system.img
fastboot flash boot boot.img
fastboot flash recovery recovery.img
fastboot flash cache cache.img
fastboot erase userdata
fastboot flash userdata userdata.img
fastboot reboot

References
http://androidforums.com/threads/guide-how-to-flash-a-nexus-factory-image-manually.706533/

List of fastboot commands

fastboot
usage: fastboot [ <option> ] <command>
 
commands:
  update <filename>                        reflash device from update.zip
  flashall                                 flash boot + recovery + system
  flash <partition> [ <filename> ]         write a file to a flash partition
  erase <partition>                        erase a flash partition
  format <partition>                       format a flash partition
  getvar <variable>                        display a bootloader variable
  boot <kernel> [ <ramdisk> ]              download and boot kernel
  flash:raw boot <kernel> [ <ramdisk> ]    create bootimage and flash it
  devices                                  list all connected devices
  continue                                 continue with autoboot
  reboot                                   reboot device normally
  reboot-bootloader                        reboot device into bootloader
  help                                     show this help message
 
options:
  -w                                       erase userdata and cache (and format
                                           if supported by partition type)
  -u                                       do not first erase partition before
                                           formatting
  -s <specific device>                     specify device serial number
                                           or path to device port
  -l                                       with "devices", lists device paths
  -p <product>                             specify product name
  -c <cmdline>                             override kernel commandline
  -i <vendor id>                           specify a custom USB vendor id
  -b <base_addr>                           specify a custom kernel base address
  -n <page size>                           specify the nand page size. default:
2048
  -S <size>[K|M|G]                         automatically sparse files greater th
an
                                           size.  0 to disable
 

References
http://androidforums.com/threads/list-of-fastboot-command.714676/

Usefull ADB Commands

adb devices

adb reboot recovery

adb reboot-bootloader

adb push [source] [destination]

adb pull

adb install [source.apk]

adb install -r [source.apk] // installing while keeping the app's data, as you would on the market or with an APK (It means update )

fastboot oem unlock

fastboot devices

fastboot reboot

fastboot flash recovery

adb sideload update.zip

References
http://lifehacker.com/the-most-useful-things-you-can-do-with-adb-and-fastboot-1590337225
https://tektab.com/2015/10/31/android-bootloaderfastboot-mode-and-recovery-mode-explained/
https://stackoverflow.com/questions/8962226/testing-an-adb-update-versus-install