Tuesday, August 23, 2016

jni register native methods

env->RegisterNatives(clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])
static JNINativeMethod gMethods[] = {
    { "_nativeGetBoardHWVersion", "()Ljava/lang/String;", (void *) nativeGetBoardHWVersion },
    { "_nativeGetBoardSWVersion", "()Ljava/lang/String;", (void *) nativeGetBoardSWVersion },
    { "_native_enterUpdateMode",         "()I",                      (void *)native_enterUpdateMode },
    { "_native_init",         "()V",                              (void *)native_init },
    { "_native_setup",        "(Ljava/lang/Object;)V",            (void *)native_setup },
    { "_native_start_unix_socket_thread",        "()V",            (void *)native_start_unix_socket_thread },
    { "_native_release",            "()V",                        (void *)native_release },
    { "_native_finalize",            "()V",                       (void *)native_finalize },
};

Thursday, August 18, 2016

android permission - automatically granted

Automatically granted permissions
Some Android permission granted automatically. We call it Normal Permission (PROTECTION_NORMAL).

android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT

Permission Group Required runtime permission request

Tuesday, August 16, 2016

GearVRf Could not find :framework-debug

Use 1.0.3 ovr mobile sdk
https://developer.oculus.com/downloads/mobile/1.0.3/Oculus_Mobile_SDK/


copy
C:\Android\ovr_sdk_mobile_1.0.3\VrApi\Libs\Android\armeabi-v7a\libvrapi.so
to
C:\cygwin64\home\Administrator\works\GearVRf-Demos\gvr-360video\app\src\main\jniLibs\armeabi-v7a\libvrapi.so


Two options:
  • you can build the framework yourself; clone the GearVRf and the GearVRf-Demos repos in the same directory and then the GearVRf build automatically will copy the aar to GearVRf-Demos/gearvrf-libs/
cd C:\cygwin64\home\Administrator\works\GearVRf\GVRf\Framework
./gradlew assembleDebug
./gradlew assemble
  • download the gvrf-3.0.1.aar from https://github.com/Samsung/GearVRf/releases/tag/v3.0.1; copy it to GearVRf-Demos/gearvrf-libs and rename it to framework-debug.aar; please note the release instructions about the Oculus binaries which have to be manually copied, for now.
  • GearVRf v3.0.1

  • From the Oculus Mobile SDK:
  • Under your application's directory create jniLibs and copy libvrapi.so there
  • C:\cygwin64\home\Administrator\works\GearVRf-Demos\gvr-360video\app\src\main\jniLibs\armeabi-v7a\libvrapi.so


/data/app/org.gearvrf.sample-1/lib/arm/libvrapi.so (vrapi_Initialize+1644)


Monday, August 15, 2016

git (repo) init

Initialize the local directory as a Git repository.

git init

Add the files in your new local repository. This stages them for the first commit.

git add .

Commit the files that you've staged in your local repository.

git commit -s

In Terminal, add the URL for the remote repository where your local repository will be pushed.
git remote add origin ebmajor@github.com:peter.oh/hmd_service.git

git remote -v

Push the changes in your local repository to GitHub.
git push origin master

run repo sync with git --recursive option

1. in manifest, specify the attribute sync-s="true"
for example <default sync-s="true"/> to apply to all projects
or for a specific project, <project sync-s="true"/>
2. repo sync --fetch-submodules

Friday, August 12, 2016

PackageManager: Not granting permission because it was previously installed without

/data/system/packages.xml

What finally fixed it for me was to re-install the applications using the package manager, by running:

cd /data/app

for app in *.apk; do pm install -r $app; done

** another solution: run the application manually at least once.

Thursday, August 11, 2016

Why 'system' user cannot access /sdcard, follow-up question



Found the code and the answer to my question in case anyone else is
interested:
system/core/vold/volmgr.c
Processes are sent SIGTERM and then eventually SIGKILL if they don't
obey.

frameworks\base\core\java\android\os\Environment.java
private static void throwIfSystem() {
    if (Process.myUid() == Process.SYSTEM_UID) {
        Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
    }
}

Why 'system' user cannot access /sdcard, follow-up question



Found the code and the answer to my question in case anyone else is
interested:
system/core/vold/volmgr.c
Processes are sent SIGTERM and then eventually SIGKILL if they don't
obey.

frameworks\base\core\java\android\os\Environment.java
private static void throwIfSystem() {
    if (Process.myUid() == Process.SYSTEM_UID) {
        Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
    }
}

Android "App Not Found" error

Check AndroidManifest.xml -> <permission android:name="xxxx"> if xxxx is existing.

signing apk with platform key


The Android tree includes test-keys under build/target/product/security



Use <root-of-android-source-tree>/out/host/linux-x86/framework/signapk.jar to sign your app using
platform.x509.pem and platform.pk8 in <root-of-android-source-tree>/build/target/product/security
java -jar signapk.jar platform.x509.pem platform.pk8 YourApp-unsigned.apk YourApp-signed.apk


java -jar out/host/linux-x86/framework/signapk.jar ./build/target/product/security/platform.x509.pem ./build/target/product/security/platform.pk8 hmd_service.apk hmd_service-signed.apk

signing apk with platform key


The Android tree includes test-keys under build/target/product/security



Use <root-of-android-source-tree>/out/host/linux-x86/framework/signapk.jar to sign your app using
platform.x509.pem and platform.pk8 in <root-of-android-source-tree>/build/target/product/security
java -jar signapk.jar platform.x509.pem platform.pk8 YourApp-unsigned.apk YourApp-signed.apk


java -jar out/host/linux-x86/framework/signapk.jar ./build/target/product/security/platform.x509.pem ./build/target/product/security/platform.pk8 hmd_service.apk hmd_service-signed.apk

to check android permissions app granted

Dumpsys System Diagnostics

https://source.android.com/devices/tech/debug/dumpsys.html
to see only permissions that were granted (but omitting ones that were requested but not granted) use
adb shell dumpsys package packagename
and check grantedPermissions section at the bottom of the output.
To list all permissions (requested but not granted + requested and granted):
  1. Notice the APK of a package. You can run the same command
    adb shell dumpsys package packagename
    and get the APK path from codePath element of its output.

Monday, August 8, 2016

pre-built apk and shared library to sign platform key

1. (working folder example 'PreApp')
create a working folder under ./packages/apps/PreApp

2.
copy PreApp.apk and libpreapp.so to ./packages/apps/PreApp

3.
Create Android.mk under PreApp folder and compose like below
----------------------- Android.mk ------------------------------------
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := PreApp
LOCAL_CERTIFICATE := platform
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_REQUIRED_MODULES := libpreapp

include $(BUILD_PREBUILT)

include $(CLEAR_VARS)

LOCAL_MODULE := libpreapp
LOCAL_IS_HOST_MODULE :=
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_TAGS := optional
OVERRIDE_BUILT_MODULE_PATH := $(PRODUCT_OUT)/obj/lib
LOCAL_UNINSTALLABLE_MODULE :=
LOCAL_SRC_FILES := libpreapp.so
LOCAL_BUILT_MODULE_STEM := libpreapp.so
LOCAL_STRIP_MODULE :=
LOCAL_MODULE_STEM := libpreapp.so
LOCAL_CERTIFICATE :=
LOCAL_MODULE_PATH := $(PRODUCT_OUT)/system/lib
LOCAL_REQUIRED_MODULES :=
LOCAL_SHARED_LIBRARIES :=

include $(BUILD_PREBUILT)
------------------------------------------------------------------------------
4.
run "mmm ./packages/apps/PreApp" on android root or "mm" under "./packages/apps/PreApp".

5. /* To Do */
Check shared library's module path.
Installing PreApp.apk built from above script extracts libpreapp.so under /data/app/com.package.name/lib/arm.

Thursday, August 4, 2016

android jni thread


https://developer.android.com/training/articles/perf-jni.html


Threads


All threads are Linux threads, scheduled by the kernel. They're usually started from managed code (using Thread.start), but they can also be created elsewhere and then attached to the JavaVM. For example, a thread started with pthread_create can be attached with the JNI AttachCurrentThreador AttachCurrentThreadAsDaemon functions. Until a thread is attached, it has no JNIEnv, and cannot make JNI calls.
Attaching a natively-created thread causes a java.lang.Thread object to be constructed and added to the "main" ThreadGroup, making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread is a no-op.
Android does not suspend threads executing native code. If garbage collection is in progress, or the debugger has issued a suspend request, Android will pause the thread the next time it makes a JNI call.
Threads attached through JNI must call DetachCurrentThread before they exit. If coding this directly is awkward, in Android 2.0 (Eclair) and higher you can use pthread_key_create to define a destructor function that will be called before the thread exits, and call DetachCurrentThread from there. (Use that key with pthread_setspecific to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)

#define JNI_VERSION_1_6 0x00010006


android-ndk/platforms/android-17/arch-arm/usr/include/jni.h

#define JNI_VERSION_1_1 0x00010001
#define JNI_VERSION_1_2 0x00010002
#define JNI_VERSION_1_4 0x00010004
#define JNI_VERSION_1_6 0x00010006

Wednesday, August 3, 2016

Generating JNI header under Android Studio

For Android Studio projects the command reads like this:

cd C:\<path to your app>\src\main\java

javah -o ../jni/NameOfHeaderFile.h package.name.of.java.class.YourJavaClass

peteroh@PC0D83UU:~/workspace/hmd_service/app/src/main$ cd java/

peteroh@PC0D83UU:~/workspace/hmd_service/app/src/main/java$ javah -o ../jni/hmd_service_jni.h com.levr.hmd_service.HmdNative

==============================================
hmd_service_jni.h
==============================================

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_levr_hmd_service_HmdNative */

#ifndef _Included_com_levr_hmd_service_HmdNative
#define _Included_com_levr_hmd_service_HmdNative
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_levr_hmd_service_HmdNative
 * Method:    Init
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_levr_hmd_1service_HmdNative_Init
  (JNIEnv *, jclass, jstring);

#ifdef __cplusplus
}
#endif
#endif