HAL开发全流程(二)

本文的源代码地址是:http://download.csdn.net/detail/yongyu_it/9544336


3、系统级Android Service开发

3.1  系统级Android Service服务接口定义(AIDL定义)

IJoffeeService.aidl文件

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os; 

/**
 * Binder interface that clients running in application context
 * can use to interface with remote service
 */
interface IJoffeeService {
	void callJoffeeMethod();
}

 3.1.1  将这个文件放在 根/frameworks/base/core/java/android/os下面 

3.1.2  修改 根/frameworks/base下的Android.mk文件 LOCAL_SRC_FILES 下添加 core/java/android/os/IJoffeeService.aidl

3.2  实现系统级Android Service服务接口

JoffeeService.java文件

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server;

import android.util.Log;
import android.util.Slog;

import android.os.IBinder;
import android.content.Context;

import android.os.IJoffeeService;  
import com.android.server.SystemService;  

public class JoffeeService extends SystemService {

    private final Context mContext;
    private static final String TAG = "JoffeeService";
    private static final boolean DEBUG = true;

    public JoffeeService(Context context) {
        super(context);
        if (DEBUG){
            Slog.d(TAG, "Build service");
        }
        mContext = context;

        try{
	    publishBinderService(context.JOFFEE_SERVICE, mService);
        }catch(Exception e){
	    Slog.d(TAG, e.getMessage());
        }      
    }
    
    /**
     * Called when service is started by the main system service
     */
    @Override
    public void onStart() {
        if (DEBUG){
            Slog.d(TAG, "Start service");
        }
        mNativePointer = init_native();
    }
    
    /**
     * Implementation of AIDL service interface
     */
    private final IBinder mService = new IJoffeeService.Stub() {
        /**
         * Implementation of the methods described in AIDL interface
         */
        @Override
        public void callJoffeeMethod() {
            if (DEBUG){
                Slog.d(TAG, "Call native service");
            }
            /*
             * We do not really need the nativePointer here;
             * Just to show how arguments are passed to JNI from Java
             */
            joffeeFunction_native(mNativePointer);
        }
    };
    
    /* Native functions declarations */
    private long mNativePointer;
    private static native long init_native();
    private static native void joffeeFunction_native(long nativePointer);

}


3.2.1  将这个文件放在 根/frameworks/base/services/java/com/android/server 下

3.2.2  修改若干Android框架源码

3.2.2.1  修改 根/frameworks/base/core/java/android/content 下的Context.java文件,将我们的服务标识写进去,便于我们在上层调用

在public abstract class Context类里面增加定义

public static final String JOFFEE_SERVICE = "joffee_service";

3.2.2.2  修改 根/frameworks/base/services/java/com/android/server 下的SystemServer.java文件,将这个我们服务添加进系统服务。

在DeviceStorageMonitorService服务启动语句之后增加一行代码启动我们的服务

mSystemServiceManager.startService(JoffeeService.class);

3.2.3  编译服务接口定义,在根目录下 mmm ./frameworks/base

编译结果在 根/out/target/product/generic/system/framework/framework.jar

3.2.4  编译服务实现代码,在根目录下 mmm ./frameworks/base/services

编译结果在 根/out/target/product/generic/system/framework/services.jar


特别注意1:由于这里的 添加新aidl文件 和 在Context类里面添加public字段 会改变android sdk的api,所以编译的时候必须先执行 make update-api,然后执行make(不能用make snod来代替)才行。不然启动系统时会出现“SurfaceFlinger: ro.sf.lcd_density must be defined as a build property”的错误导致系统起不来。


特别注意2:由于android 5.x开始,引入了非常严格的selinux权限管理机制。所以我们在开发系统级的Service时候,必须对其权限进行描述。

否则在publishBinderService(context.JOFFEE_SERVICE, mService);调用的时候就会出现“SELinux : avc:  denied  { add } for service=joffee_service scontext=u:r:system_server:s0 tcontext=u:object_r:default_android_service:s0 tclass=service_manager”

在上层APP调用getSystemService(JOFFEE_SERVICE);的时候会出现“SELinux : avc:  denied  { find } for service=joffee_service scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:hardware_service:s0 tclass=service_manager”

具体解决方法如下

在external/sepolicy/service.te里面定义类型:type joffee_service, system_api_service, system_server_service, service_manager_type;

在external/sepolicy/service_contexts里授权:joffee_service                            u:object_r:joffee_service:s0

注意在service_contexts授权文件里,前面一个“joffee_service”是Context.JOFFEE_SERVICE的值,即Service的调用标识。后一个“joffee_service”是在service.te类型文件里面定义的类型名

重新编译,刷boot.img,新安全策略生效


待续...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值