Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android4.3從app到kernel代碼追蹤gsensor所遇到的問題

android4.3從app到kernel代碼追蹤gsensor所遇到的問題

編輯:關於Android編程

我目前所遇到的問題是在android4.3代碼上追蹤gsensor的時候出現代碼的斷層,並且找不到對應的解決方法

希望各位又看到的指導一下:

packages/apps/Settings/src/com/android/settings/gsensor/GestureScreenEnabler.java

public GestureScreenEnabler(Context context, Switch switch_) {
52 mContext = context;
53 mSwitch = switch_;
54 sm = (GSensorManager)context.getSystemService(Context.GSENSOR_SERVICE);
55 if (sm == null) {
56 mSwitch.setEnabled(false);
57 }
58 mIntentFilter = new IntentFilter(GSensorManager.ACTION_STATE_CHANGED);
59 }

然後找到對應的GSensorManager服務

frameworks/base/core/java/android/hardware/GSensorManager.java

7 public class GSensorManager {
8
9 private static final boolean DEBUG = false;
10 private static final String TAG = "GSensorManager";
11
12 public static final String ACTION_STATE_CHANGED = "android.hardware.gsensormanager.STATE_CHANGED";
13 public static final String EXTRA_STATE = "android.hardware.gsensormanager.extra.STATE";
14
15 IGSensorManager mService;
16
17 public GSensorManager(IGSensorManager service) {
18 mService = service;
19 }
20
21 public void setEnabled(boolean isEnabled) {
22 try {
23 if (mService != null)
24 mService.setEnabled(isEnabled);
25 } catch (RemoteException e) {
26
27 }
28 }
29
30 public boolean isEnabled() {
31 try {
32 if (mService != null)
33 return mService.isEnabled();
34 } catch (RemoteException e) {
35 }
36 return false;
37 }
38
39 public int setReferenceStatus() {
40 try {
41 if (mService != null)
42 return mService.setReferenceStatus();
43 } catch (RemoteException e) {
44 }
45 return 0;
46 }
47
48
49 public void setThreshold(int level){
50 try {
51 if (mService != null)
52 mService.setThreshold(level);
53 } catch (RemoteException e) {
54 }
55 }

下面是定義的getSystemService服務的接口

frameworks/base/core/java/android/content/Context.java

public static final String GSENSOR_SERVICE = "gsensor";

public abstract Object getSystemService(String name);


在frameworks/base/core/java/android/app/ContextImpl.java中實現的接口函數

public Object getSystemService(String name) {
ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
return fetcher == null ? null : fetcher.getService(this);
}

注冊gsensor服務

registerService(GSENSOR_SERVICE, new ServiceFetcher() {
544 public Object createService(ContextImpl ctx) {
545 IBinder b = ServiceManager.getService(GSENSOR_SERVICE);
546 IGSensorManager service = IGSensorManager.Stub.asInterface(b);
547 return new GSensorManager(service);
548 }
549 });

frameworks/base/services/java/com/android/server/SystemServer.java

構建各功能和對應的服務的一一對應關系

if (SystemProperties.get("ro.gesturescreenon.support").equals("true")) {
792 try {
793 Slog.i(TAG,"gsensor service");
794 gSensor = new GSensorService(context);
795 ServiceManager.addService("gsensor", gSensor);
796 } catch (Throwable e) {
797 Slog.e(TAG,"Failure starting gsensor Service", e);
798 }
799 }

緊接著,下面的代碼就沒有辦法構建成功了。並且也不知道該如何操作下面的代碼了。


以下是我從hardware開始向APP曾追蹤代碼的說明:

hardware/libhardware/include/hardware/sensors.h

這裡是定義的一些sensor的結構,也就不一一列出它們了。

struct sensors_module_t {
826 struct hw_module_t common;
827
828 /**
829 * Enumerate all available sensors. The list is returned in "list".
830 * @return number of sensors in the list
831 */
832 int (*get_sensors_list)(struct sensors_module_t* module,
833 struct sensor_t const** list);
834 };


frameworks/base/services/jni/com_android_server_GSensorService.cpp

#define GSENSOR_DIR_NAME "/sys/bus/iio/devices/iio:device0"
#define GSENSOR_DEFAULT_THRESHOLD (8 * 32)
#define GSENSOR_REFERENCE_X "motion_lpa_threshold"
#define GSENSOR_REFERENCE_Y "motion_lpa_threshold"
#define GSENSOR_REFERENCE_Z "motion_lpa_threshold"

static int setEnabled(JNIEnv *env, jobject clazz, jboolean on) {
int enable = on;


int ret = write_sysfs_int("buffer/enable", 0);
if (ret < 0)
return -1;
ret = write_sysfs_int("power_state", 1);
if (ret < 0)
return -1;
ret = write_sysfs_int("motion_lpa_on", enable);
if (ret < 0)
return -1;
if (enable) {
ret = write_sysfs_int("motion_lpa_threshold", 31 * 32);
if (ret < 0)
return -1;
ret = write_sysfs_int("motion_lpa_duration", 200);
if (ret < 0)
return -1;
ret = write_sysfs_int("motion_lpa_freq", 2);
if (ret < 0)
return -1;
}
ret = write_sysfs_int("buffer/length", 6);
if (ret < 0)
return -1;
ret = write_sysfs_int("accl_enable", 1);
if (ret < 0)
return -1;
ret = write_sysfs_int("buffer/enable", 1);
if (ret < 0)
return -1;
ret = write_sysfs_int("irq_wake_enable", enable);
if (ret < 0)
return -1;
staticEnabled = on;
return 0;
}


static JNINativeMethod gMethods[] = {
{"_setEnabled", "(Z)I", (void*)setEnabled},
{"_isEnabled", "()Z", (void*)isEnabled},
{"_setReferenceStatus", "()I", (void*)setReferenceStatus},
{"_setThreshold", "(I)V", (void*)setThreshold},
{"_getThreshold", "()I",(void*)getThreshold},
{"_setReference","(III)V", (void*)setReference},
{"_getReference", "()[I", (void*)getReference},
};


int register_android_server_GSensorService(JNIEnv *env) {
return jniRegisterNativeMethods(env, "com/android/server/GSensorService",
gMethods, NELEM(gMethods));
}

frameworks/base/services/java/com/android/server/GSensorService.java

public void setEnabled(boolean isEnabled) throws RemoteException {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null);
Settings.System.putInt(mContext.getContentResolver(),Settings.System.GSENSOR_ENABLE,
isEnabled ? 1:0 );
int res = _setEnabled(isEnabled);
Intent intent = new Intent(GSensorManager.ACTION_STATE_CHANGED);
if (res == 0) {
//Send broadcast message to everyone else
intent.putExtra(GSensorManager.EXTRA_STATE, true);
} else {
intent.putExtra(GSensorManager.EXTRA_STATE, false);
}
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
Log.d(TAG,"gsensor gesture State Change Intent true");
mContext.sendBroadcast(intent);
}


private native int _setEnabled(boolean isEnabled);
private native boolean _isEnabled();
private native int _setReferenceStatus();
private native void _setThreshold(int level);
private native int _getThreshold();
private native void _setReference(int x, int y, int z);
private native int[] _getReference();


這樣的關系我實在是不明白,希望各位看到的給與指點。


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved