Archive for April, 2017

android: how to read Settings provider

April 27, 2017

This post show how to read Setting provider.

device configuration
[ro.build.version.release]: [5.1]

what is Setting provider
Setting provides contains global system-level device preferences. Its backend is a sqlite database named settings. The database contains three data tables, Global, System, and Secure. Its frontend com.android.settings application.

how to query Setting provider from adb shell

  1. Query Global data table from Setting database
  2. $ adb shell content query --uri content://settings/secure
    Row: 0 _id=1, name=airplane_mode_on, value=0
    Row: 1 _id=2, name=theater_mode_on, value=0
    Row: 2 _id=3, name=airplane_mode_radios, value=cell,bluetooth,wifi,nfc,wimax
    Row: 3 _id=4, name=airplane_mode_toggleable_radios, value=bluetooth,wifi,nfc
    Row: 4 _id=5, name=assisted_gps_enabled, value=0
    Row: 5 _id=6, name=auto_time, value=1
    Row: 6 _id=7, name=auto_time_zone, value=1
    Row: 7 _id=8, name=stay_on_while_plugged_in, value=0
    Row: 8 _id=9, name=wifi_sleep_policy, value=2
    Row: 9 _id=11, name=package_verifier_enable, value=1
    Row: 10 _id=13, name=wifi_networks_available_notification_on, value=1
    Row: 11 _id=14, name=bluetooth_on, value=0
    Row: 12 _id=15, name=cdma_cell_broadcast_sms, value=1
    Row: 13 _id=16, name=data_roaming, value=0
    Row: 14 _id=18, name=mobile_data, value=1
    Row: 15 _id=19, name=netstats_enabled, value=1
    .......
    
  3. Query System data table from Setting database
  4. $ adb shell content query --uri content://settings/system
    Row: 0 _id=1, name=volume_music, value=8
    Row: 1 _id=2, name=volume_ring, value=8
    Row: 2 _id=3, name=volume_system, value=15
    Row: 3 _id=4, name=volume_voice, value=4
    Row: 4 _id=5, name=volume_alarm, value=8
    Row: 5 _id=6, name=volume_notification, value=8
    Row: 6 _id=7, name=volume_bluetooth_sco, value=7
    Row: 7 _id=9, name=mute_streams_affected, value=46
    Row: 8 _id=10, name=vibrate_when_ringing, value=0
    Row: 9 _id=11, name=dim_screen, value=1
    Row: 10 _id=12, name=screen_off_timeout, value=180000
    Row: 11 _id=13, name=dtmf_tone_type, value=0
    Row: 12 _id=14, name=hearing_aid, value=0
    Row: 13 _id=15, name=tty_mode, value=0
    Row: 14 _id=16, name=screen_brightness, value=204
    Row: 15 _id=17, name=screen_brightness_mode, value=0
    ......
    
  5. Query Secure data table from Setting database
  6. $ adb shell content query --uri content://settings/system
    Row: 0 _id=1, name=location_providers_allowed, value=
    Row: 1 _id=2, name=mock_location, value=0
    Row: 2 _id=3, name=backup_enabled, value=0
    Row: 3 _id=4, name=backup_transport, value=com.google.android.backup/.BackupTransportService
    Row: 4 _id=5, name=mount_play_not_snd, value=1
    Row: 5 _id=6, name=mount_ums_autostart, value=0
    Row: 6 _id=7, name=mount_ums_prompt, value=1
    Row: 7 _id=8, name=mount_ums_notify_enabled, value=1
    Row: 8 _id=9, name=accessibility_script_injection, value=0
    Row: 9 _id=10, name=accessibility_web_content_key_bindings, value=0x13=0x01000100; 0x14=0x01010100; 0x15=0x02000001; 0x16=0x02010001; 0x200000013=0x02000601; 0x200000014=0x02010601; 0x200000015=0x03020101; 0x200000016=0x03010201; 0x200000023=0x02000301; 0x200000024=0x02010301; 0x200000037=0x03070201; 0x200000038=0x03000701:0x03010701:0x03020701;
    Row: 10 _id=11, name=long_press_timeout, value=500
    Row: 11 _id=12, name=touch_exploration_enabled, value=0
    Row: 12 _id=14, name=accessibility_script_injection_url, value=https://ssl.gstatic.com/accessibility/javascript/android/AndroidVox_v1.js
    Row: 13 _id=15, name=lockscreen.disabled, value=1
    Row: 14 _id=16, name=screensaver_enabled, value=0
    Row: 15 _id=17, name=screensaver_activate_on_dock, value=1
    ......
    
  7. In Setting database, select * from global where name=wifi_auto_join
  8. $ adb shell content query --uri content://settings/global --where "name=\'wifi_auto_join\'"
    Row: 0 _id=52, name=wifi_auto_join, value=1
    

how to query Setting provider from java

  • Use content provider to read global table from Settings provider
  • Settings providers help read data from settings sqlite database
  • Cursor cursor = context.getContentResolver().query(Settings.Global.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) {
        Log.d(TAG, cursor.getString(0) + ", " + cursor.getString(1) + ", " + cursor.getString(2));
    }
    

    conclusion
    This post shows how to read settings database from adb shell and java code.

    Advertisements

    android: ndk: use cmake to build a native module

    April 26, 2017

    This post demonstrates building a native module for android devices with cmake

    host configuration
    OS X El Captain Version 10.11.4

    target configuration
    [ro.build.version.release]: [5.1]
    [ro.product.cpu.abi]: [armeabi-v7a].

    how to build native code with ndk-build

    • Download NDK(Native Development Kit)
    • NDK of different releases could be founded from https://developer.android.com/ndk/downloads/index.html

      $ wget https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
      $ unzip https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
      
    • Create project directory
    • $ mkdir project
      $ cd project/
      
    • Edit hello.c
    • #include <stdio.h>
      
      int main() {
          printf("hello world\n");
          return 0;
      }
      
    • Edit CMakeLists.txt
    • PROJECT (hello)
      ADD_EXECUTABLE (hello hello.c)
      
    • Build hello module
    • $ mkdir build
      $ cd build
      $ cmake -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=../../android-ndk-r13b -DCMAKE_TOOLCHAIN_FILE=../../android-ndk-r13b/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 ..
      cmake -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=../../android-ndk-r13b -DCMAKE_TOOLCHAIN_FILE=../../android-ndk-r13b/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 ..
      -- Check for working C compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
      -- Check for working C compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -- works
      -- Detecting C compiler ABI info
      -- Detecting C compiler ABI info - done
      -- Detecting C compile features
      -- Detecting C compile features - done
      -- Check for working CXX compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++
      -- Check for working CXX compiler: /Users/chengyihe/workspace/cmake-ndk/android-ndk-r13b/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -- works
      -- Detecting CXX compiler ABI info
      -- Detecting CXX compiler ABI info - done
      -- Detecting CXX compile features
      -- Detecting CXX compile features - done
      -- Configuring done
      -- Generating done
      -- Build files have been written to: /Users/chengyihe/workspace/cmake-ndk/project/build
      $ make
      Scanning dependencies of target hello
      [ 50%] Building C object CMakeFiles/hello.dir/hello.c.o
      [100%] Linking C executable hello
      [100%] Built target hello
      
    • Run the hello executable in the target device
    • $ adb push hello /data/local/tmp/hello
      [100%] /data/local/tmp/hello
      $ adb shell /data/hello
      WARNING: linker: /data/hello: unused DT entry: type 0x6ffffffe arg 0x378
      WARNING: linker: /data/hello: unused DT entry: type 0x6fffffff arg 0x1
      hello world
      

    conclusion
    This post show how to build a native executable for android devices with cmake and ndk.

    android: ndk: use ndk-build to build a native module

    April 26, 2017

    This post demonstrates building a native module with ndk-build.

    host configuration
    OS X El Captain Version 10.11.4

    target configuration
    [ro.build.version.release]: [5.1]
    [ro.product.cpu.abi]: [armeabi-v7a]

    what is ndk
    Native Development Kit to build native code for android

    what is ndk-build
    A convenient script in NDK to build native code

    how to build native code with ndk-build

    1. Download NDK(Native Development Kit)
    2. NDK of different releases could be founded from https://developer.android.com/ndk/downloads/index.html

      $ wget https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
      $ unzip https://dl.google.com/android/repository/android-ndk-r13b-darwin-x86_64.zip
      
    3. Edit hello.c
    4. #include <stdio.h>
      
      int main() {
          printf("hello world\n");
          return 0;
      }
      
    5. Edit Android.mk
    6. LOCAL_PATH := $(call my-dir)
      
      include $(CLEAR_VARS)
      
      LOCAL_CFLAGS += -fPIE
      LOCAL_LDFLAGS += -fPIE -pie
      LOCAL_MODULE := hello
      LOCAL_SRC_FILES := hello.c 
      
      include $(BUILD_EXECUTABLE)
      
    7. Edit Application.mk
    8. APP_ABI := armeabi-v7a
      APP_PLATFORM := 21
      APP_BUILD_SCRIPT := Android.mk
      
    9. Build native module
    10. $ ../android-ndk-r13b/ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=./Application.mk 
      [armeabi-v7a] Compile thumb  : hello <= hello.c
      [armeabi-v7a] Executable     : hello
      [armeabi-v7a] Install        : hello => libs/armeabi-v7a/hello
      
    11. Run the module in target device
    12. $ adb push libs/armeabi-v7a/hello /data/
      [100%] /data/hello
      $ adb shell /data/hello
      WARNING: linker: /data/hello: unused DT entry: type 0x6ffffffe arg 0x38c
      WARNING: linker: /data/hello: unused DT entry: type 0x6fffffff arg 0x1
      hello world
      

    conclusion
    This post show how to build a native executable with ndk-build.


    %d bloggers like this: