Android 11 adds new system services

1. Write the .aidl file

Storage location: frameworks/base/core/java/android/os

package android.os;

interface ISystemVoiceServer {

    void setHeightVoice(int flag);

    void setBassVoice(int flag);

    void setReverbVoice(int flag);

}

2. Add the .aidl file to frameworks/base/Android.bp

filegroup {
    name: "framework-core-sources",
    srcs: [
        "core/java/**/*.java",
        "core/java/**/*.aidl",
    ],
    path: "core/java",
}

Note: In the android.bp file, the aidl file in the core/java/ directory is added to the compiled file by default, so this step does not need to be operated.

Since Android 11 is stricter on syntax detection, we first add ignore to our newly added files:

// TODO(b/145644363): move this to under StubLibraries.bp or ApiDocs.bp
metalava_framework_docs_args = "--manifest $(location core/res/AndroidManifest.xml) " +
    "--ignore-classes-on-classpath " +
    "--hide-package com.android.server " +
    "--error UnhiddenSystemApi " +
    "--hide RequiresPermission " +
    "--hide CallbackInterface " +
    "--hide MissingPermission --hide BroadcastBehavior " +
    "--hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol " +
    "--hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo " +
    "--force-convert-to-warning-nullability-annotations +*:-android.*:+android.icu.*:-dalvik.* " +
    "--api-lint-ignore-prefix android.icu. " +
    "--api-lint-ignore-prefix java. " +
    "--api-lint-ignore-prefix android.os. " +   //新增这一行 
    "--api-lint-ignore-prefix android.app. " +  //新增这一行  
    "--api-lint-ignore-prefix junit. " +
    "--api-lint-ignore-prefix org. "

3. Context.java adds the service registration name, and adds the service name for quick registration and quick reference

Modification location: frameworks/base/core/java/android/content/

  //增加新增定义服务名称  
  /**
     * Use with {@link #getSystemService(String)} to retrieve a
     * {@link  android.app.SystemVoiceManager} for accessing
     * text services.
     *
     * @see #getSystemService(String)
     */
  
    public static final String SYSTEMVOCIE_SERVICER = "systemvoice";
    

    /** @hide */
    @StringDef(suffix = { "_SERVICE" }, value = {
            POWER_SERVICE,
            SYSTEMVOCIE_SERVICER,   //此处新增服务
            WINDOW_SERVICE,
            LAYOUT_INFLATER_SERVICE,
            ......}

4. Create new SystemVoiceService.java and SystemVoiceManager.java

Storage location: frameworks\base\services\core\java\com\android\server\SystemVoiceService.java

frameworks\base\core\java\android\app\SystemVoiceManager.java

package com.android.server;

import android.app.SystemVoiceManager;
import android.content.Context;
import android.os.ISystemVoiceServer;
import android.os.RemoteException;
import com.android.server.SystemService;
import com.android.internal.app.IAppOpsService;


public class SystemVoiceService extends SystemService {
    private final String TAG = "SystemVoiceService";

    private Context mContext;
    private IAppOpsService mAppOps;
    private SystemVoiceManager mManager;

    public SystemVoiceService(Context context) {
        super(context);
        this.mContext = context;
    }

    public void systemReady(IAppOpsService appOps) {
        mAppOps = appOps;
        if (mManager == null) {
            mManager = (SystemVoiceManager) mContext.getSystemService(Context.SYSTEMVOICE_SERVICE);
        }
    }

    @Override
    public void onStart() {
        publishBinderService(Context.SYSTEMVOICE_SERVICE, new BinderService());
    }


    private final class BinderService extends ISystemVoiceServer.Stub {

        @Override
        public void setHeightVoice(int flag) throws RemoteException {
            
        }

        @Override
        public void setBassVoice(int flag) throws RemoteException {

        }

        @Override
        public void setReverbVoice(int flag) throws RemoteException {

        }
    }
}
package android.app;

import android.content.Context;
import android.os.ISystemVoiceServer;
import android.util.Log;
import android.annotation.SystemService;
import android.os.RemoteException;


@SystemService(Context.SYSTEMVOICE_SERVICE)
public class SystemVoiceManager {

    private static final String TAG = "SystemVoiceManager";
    private ISystemVoiceServer mService;
    private Context context;

    public SystemVoiceManager(Context ctx, ISystemVoiceServer service) {
        mService = service;
        context = ctx;
    }

    public void setHeightVoice(int flag) {
        try {
            mService.setHeightVoice(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void setBassVoice(int flag) {
        try {
            mService.setBassVoice(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void setReverbVoice(int flag) {
        try {
            mService.setReverbVoice(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

5. Register the service in SystemServer.java

Modify location: frameworks\base\services\java\com\android\server

import com.android.server.SystemVoiceService ;//导包
 
private SystemVoiceService  mSystemVoiceService; //定义

  //系统服务加入的位置加入以下内容
  t.traceBegin("StartSystemVoiceManager");
  mSystemVoiceService = mSystemServiceManager.startService(SystemVoiceService.class);
  t.traceEnd();

  //系统服务加入的位置加入以下内容
   t.traceBegin("MakeSystemVoiceManagerServiceReady");
   try {
           // TODO: use boot phase
       mSystemVoiceService.systemReady(mActivityManagerService.getAppOpsService());
    } catch (Throwable e) {
           reportWtf("making SystemVoice Manager Service ready", e);
    }
     t.traceEnd();

6. Static {} of SystemServiceRegistry, and register the service in it

Modification location: frameworks\base\core\java\android\app

import android.os.ISystemVoiceServer;//导包

//SystemVoiceManager is in the same directory, so there is no need to import packages

   registerService(Context.SYSTEMVOICE_SERVICE, SystemVoiceManager.class,
                new CachedServiceFetcher<SystemVoiceManager>() {
                    @Override
                    public SystemVoiceManager createService(ContextImpl ctx)
                            throws ServiceNotFoundException {
                        IBinder b = ServiceManager.getServiceOrThrow(
                                Context.SYSTEMVOICE_SERVICE);
                        return new SystemVoiceManager(
                                ctx.getOuterContext(),ISystemVoiceService.Stub.asInterface(b));
                    }});

The above steps are 90% complete when our custom system service is completed, but we still have the last and most important step:

Then we just need to add SELinux rules related to this custom service SystemVoiceService. For later verification, open selinux

Remember this command adb shell setenforce 1 # 1 is open #0 is closed

The selinux rules of Android 11 are placed in the system/sepolicy directory:

Service.te and service_contexts must add the configuration of SystemVoiceService:

//在以下目录文件
./prebuilts/api/30.0/public/service.te   # 需要
./public/service.te                 # 需要
//添加以下内容
type systemvoice_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;

//在以下目录文件
./prebuilts/api/30.0/private/service_contexts   # 需要
./private/service_contexts         # 需要
//添加以下内容
systemvoice_service                      u:object_r:isystemvoice_service:s0

After adding the configuration, the Android11 ​​version also needs to modify the following ignore configuration in the following directory to compile normally

在以下目录
./prebuilts/api/30.0/private/compat/29.0/29.0.ignore.cil
./prebuilts/api/30.0/private/compat/28.0/28.0.ignore.cil
./prebuilts/api/30.0/private/compat/27.0/27.0.ignore.cil
./prebuilts/api/30.0/private/compat/26.0/26.0.ignore.cil
./private/compat/29.0/29.0.ignore.cil
./private/compat/28.0/28.0.ignore.cil
./private/compat/27.0/27.0.ignore.cil
./private/compat/26.0/26.0.ignore.cil

At this point, the android 11 system service has been added!.

8. Verification:

After the compilation is complete, enter adb shell

#service list

Check whether there is an added service in the service list: itest

If there is no step-by-step troubleshooting, refer to the previous step to see which step is wrong

Verify in code if it exists

Find and compile the latest generated class.jar file and import it into Androidstudio (how to import it from Baidu)

Compiled class.jar directory\out\target\common\obj\JAVA_LIBRARIES\framework_intermediates

If it is not generated, execute the make javac-check-framework command and it will be generated! ! !

I think my writing is good, brother, move your little hand to make a fortune, give it a thumbs up! ! !

Your recognition will be the motivation for me to continue writing! !

Guess you like

Origin blog.csdn.net/a546036242/article/details/129683832