键字: android, settings, cmd

20180822 tjy

转载请注明出处

Android 中有一个可执行文件settings,可以使得调试变得方便。
查看settings的代码,会有其他额外发现。

文件位置

 

device-name:/system/bin # ls -l settingsls -l settings-rwxr-xr-x 1 root shell 35 2018-08-03 04:17 settingsdevice-name:/system/bin #

settings用法

 

device-name:/ # settingssettingsSettings provider (settings) commands:  help      Print this help text.  get [--user  | current] NAMESPACE KEY      Retrieve the current value of KEY.  put [--user  | current] NAMESPACE KEY VALUE [TAG] [default]      Change the contents of KEY to VALUE.      TAG to associate with the setting.      {default} to set as the default, case-insensitive only for global/secure namespace  delete NAMESPACE KEY      Delete the entry for KEY.  reset [--user  | current] NAMESPACE {PACKAGE_NAME | RESET_MODE}      Reset the global/secure table for a package with mode.      RESET_MODE is one of {untrusted_defaults, untrusted_clear, trusted_defaults}, case-insensitive  list NAMESPACE      Print all defined keys.      NAMESPACE is one of {system, secure, global}, case-insensitive

settings list命令

 

device-name:/ # settings list globalsettings list globaladb_enabled=1add_users_when_locked=0airplane_mode_on=0airplane_mode_radios=cell,bluetooth,wifi,nfc,wimaxairplane_mode_toggleable_radios=bluetooth,wifi,nfcassisted_gps_enabled=1att_wifi_call_enable=0audio_safe_volume_state=3auto_time=1auto_time_zone=1bluetooth_disabled_profiles=0bluetooth_on=0boot_count=5call_auto_retry=0captive_portal_mode=1car_dock_sound=/system/media/audio/ui/Dock.oggcar_undock_sound=/system/media/audio/ui/Undock.oggcdma_cell_broadcast_sms=1data_roaming0=1data_roaming=1default_install_location=0default_restrict_background_data=0desk_dock_sound=/system/media/audio/ui/Dock.oggdesk_undock_sound=/system/media/audio/ui/Undock.oggdevelopment_settings_enabled=1device_provisioned=1dock_audio_media_enabled=1dock_sounds_enabled=0dock_sounds_enabled_when_accessbility=0emergency_affordance_needed=0emergency_tone=0heads_up_notifications_enabled=1lock_sound=/system/media/audio/ui/Lock.ogglow_battery_sound=/system/media/audio/ui/LowBattery.ogglow_battery_sound_timeout=0mobile_data0=1mobile_data=1mobile_data_always_on=0mode_ringer=2netstats_enabled=1network_recommendations_enabled=0package_verifier_enable=1power_sounds_enabled=1preferred_network_mode=10set_install_location=0stay_on_while_plugged_in=0subscription_mode=0theater_mode_on=0trusted_sound=/system/media/audio/ui/Trusted.oggunlock_sound=/system/media/audio/ui/Unlock.oggusb_mass_storage_enabled=1verifier_verify_adb_installs=0volte_vt_enabled=1wfc_ims_enabled=0wfc_ims_mode=1wifi_display_on=0wifi_max_dhcp_retry_count=9wifi_networks_available_notification_on=1wifi_on=0wifi_scan_always_enabled=0wifi_sleep_policy=2wifi_wakeup_available=0wifi_wakeup_enabled=1wireless_charging_started_sound=/system/media/audio/ui/WirelessChargingStarted.ogg

settings put和get

 

device-name:/ # settings get global bluetooth_onsettings get global bluetooth_on0device-name:/ # settings put global bluetooth_on 1settings put global bluetooth_on 1device-name:/ #

代码位置在frameworks/base/cmds/settings/settings,打开这个文件,发现它是一个shell,调用了cmd命令,cmd命令又在哪里呢?

 

//from http://androidxref.com/8.1.0_r33/xref/frameworks/base/cmds/settings/settings#!/system/bin/shcmd settings "$@"

在frameworks/native/cmds/cmd/cmd.cpp中,代码如下。
cmd通过IServiceManager去实现操作。

通过看代码,cmd -l可以把service都列出来。
cmd settings是通过调用 service settings中的方法来完成的。

 

//from http://androidxref.com/8.1.0_r33/xref/frameworks/native/cmds/cmd/cmd.cppint main(int argc, char* const argv[]){    signal(SIGPIPE, SIG_IGN);    sp proc = ProcessState::self();    // setThreadPoolMaxThreadCount(0) actually tells the kernel it's    // not allowed to spawn any additional threads, but we still spawn    // a binder thread from userspace when we call startThreadPool().    // This is safe because we only have 2 callbacks, neither of which    // block.    // See b/36066697 for rationale    proc->setThreadPoolMaxThreadCount(0);    proc->startThreadPool();    sp sm = defaultServiceManager();    fflush(stdout);    if (sm == NULL) {        ALOGW("Unable to get default service manager!");        aerr << "cmd: Unable to get default service manager!" << endl;        return 20;    }    if (argc == 1) {        aerr << "cmd: No service specified; use -l to list all services" << endl;        return 20;    }    if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {        Vector services = sm->listServices();        services.sort(sort_func);        aout << "Currently running services:" << endl;        for (size_t i=0; i service = sm->checkService(services[i]);            if (service != NULL) {                aout << "  " << services[i] << endl;            }        }        return 0;    }    Vector args;    for (int i=2; i service = sm->checkService(cmd);    if (service == NULL) {        ALOGW("Can't find service %s", argv[1]);        aerr << "cmd: Can't find service: " << argv[1] << endl;        return 20;    }    sp cb = new MyShellCallback();    sp result = new MyResultReceiver();#if DEBUG    ALOGD("cmd: Invoking %s in=%d, out=%d, err=%d", argv[1], STDIN_FILENO, STDOUT_FILENO,            STDERR_FILENO);#endif    // TODO: block until a result is returned to MyResultReceiver.    status_t err = IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args,            cb, result);    if (err < 0) {        const char* errstr;        switch (err) {            case BAD_TYPE: errstr = "Bad type"; break;            case FAILED_TRANSACTION: errstr = "Failed transaction"; break;            case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break;            case UNEXPECTED_NULL: errstr = "Unexpected null"; break;            default: errstr = strerror(-err); break;        }        ALOGW("Failure calling service %s: %s (%d)", argv[1], errstr, -err);        aout << "cmd: Failure calling service " << argv[1] << ": " << errstr << " ("                << (-err) << ")" << endl;        return err;    }    cb->mActive = false;    status_t res = result->waitForResult();#if DEBUG    ALOGD("result=%d", (int)res);#endif    return res;}

继续追一下IBinder::shellCommand代码,定义在frameworks/native/libs/binder/Binder.cpp。
后续补充Binder的逻辑。

 

//from http://androidxref.com/8.1.0_r33/xref/frameworks/native/libs/binder/Binder.cpp#65status_t IBinder::shellCommand(const sp& target, int in, int out, int err,    Vector& args, const sp& callback,    const sp& resultReceiver){    Parcel send;    Parcel reply;    send.writeFileDescriptor(in);    send.writeFileDescriptor(out);    send.writeFileDescriptor(err);    const size_t numArgs = args.size();    send.writeInt32(numArgs);    for (size_t i = 0; i < numArgs; i++) {        send.writeString16(args[i]);    }    send.writeStrongBinder(callback != NULL ? IInterface::asBinder(callback) : NULL);    send.writeStrongBinder(resultReceiver != NULL ? IInterface::asBinder(resultReceiver) : NULL);    return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);}



作者:tjy_2011
链接:https://www.jianshu.com/p/409bd28ce8b4
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

更多相关文章

  1. SeekBar自定义
  2. android NDK环境搭建
  3. android获取手机信息
  4. Android隐藏标题栏,全屏显示
  5. android中判断网络是否连接
  6. android中webview开启了硬件加速后会出现闪烁问题
  7. Android(安卓)和 PHP 之间进行数据加密传输
  8. android上传图片到服务器,求服务器那边和android的Activity的完整
  9. android实用代码

随机推荐

  1. android v7包
  2. android 2.2 迁移 android 4.0 theme sty
  3. (android)如何破解多盟广告sdk (一)
  4. Android MVP应用
  5. 【进阶】从linux到android,进程的方方面面
  6. Android 多媒体综述
  7. Android 如何建立你的菜单
  8. 【Android 修炼手册】常用技术篇 -- 聊聊
  9. QQ邮箱Android客户端产品体验报告
  10. android学习---通过android帮助学习TextV