Android11 Framework Vendor下自定义系统服务

  • framework/base/core/java/android/test/mdm/IMdmManager.aidl
package  android.test.mdm;

interface ITestMdmManager {

    boolean setTest(int mode);
    int getTest();

}
  • framework/base/core/java/android/test/mdm/MdmManager.java
package android.test.mdm;

import android.os.RemoteException;
import android.content.Context;
import android.annotation.SystemApi;
import android.annotation.SystemService;

@SystemService(Context.TESTMDM_SERVICE)
public class TestMdmManager {

    private final ITestMdmManager mService;
    
    public TestMdmManager(ITestMdmManager mService){
        this.mService = mService;
    }
	/**
	* @hide
	*/
	@SystemApi	
   	public boolean setTest(int mode) {
        try {
            return mService.setTest(mode);
        } catch (RemoteException e) {
        }
        return false;
    }
    
	/**
	* @hide
	*/
	@SystemApi	
    public int getTest() {
        try {
            return mService.getTest();
        } catch (RemoteException e) {
        }
        return -1;
    }
}
  • vendor/testos/mdm/java/android/test/server/MdmService.java
package android.test.server;

import android.content.Context;
import android.test.mdm.ITestMdmManager;
import android.provider.Settings;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.os.Build;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.admin.DevicePolicyManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.provider.Telephony;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.location.LocationManager;
import android.location.Location;
import android.nfc.NfcAdapter;
import android.os.UserHandle;
import android.os.UserManager;
import android.app.KeyguardManager;
import android.media.AudioManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.usb.UsbManager;
import android.os.storage.StorageManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class TestMdmService extends ITestMdmManager.Stub {

  
    private final Context mContext;
    private WifiManager mWifiManager;
    private BluetoothManager mBluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    private TelephonyManager mTelephonyManager;
    private LocationManager mLocationManager;
    private UserManager mUserManager;
    private KeyguardManager mKeyguardManager = null;
    private AudioManager mAudioManager;
    private UsbManager mUsbManager;
    private UserHandle mUser = android.os.Process.myUserHandle();
    private StorageManager mStorageManager;

    public TestMdmService(Context context) {
        super();
        mContext = context;
    }

    public void systemReady() {
        mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
        mStorageManager = mContext.getSystemService(StorageManager.class);   
    }

    public boolean setTest(int mode){
       	//todo
        return true;
    }

    public int getTest() {
    	//todo
    	return 0;
    }
    
    private String listToString(List<String> stringList) {
        if (stringList == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        boolean flag = false;
        for (String string : stringList) {
            if (flag) {
                result.append(",");
            } else {
                flag = true;
            }
            result.append(string);
        }
        return result.toString();
    }

    private List<String> stringToList(String string) {
        List<String> list = new ArrayList<String>();
        if (string == null) {
            return null;
        }
        String[] array = string.split(",");
        for (String str : array) {
            list.add(str);
        }
        return list;
    }
}
  • vendor/testos/mdm/Android.bp
// [testos_android]
java_library_static{
        name:"testmdm_service",
        srcs:[
        "java/**/*.java",
        "java/**/*.aidl",
        ],
        vendor:true,
        installable:true,
        sdk_version:"30",
}
  • framework/base/core/java/android/content/Context.java
public abstract class Context {
             //@hide: TIME_ZONE_DETECTOR_SERVICE,
            ...
            TESTMDM_SERVICE,
})
public static final String TESTMDM_SERVICE = "testmdm";
  • framework/base/core/java/android/app/SystemServiceRegistry.java
import android.test.mdm.MdmManager;
import android.test.mdm.IMdmManager;
//...
	// [testos_android] register, MdmService start
	registerService(Context.TESTMDM_SERVICE, MdmManager.class,
				new CachedServiceFetcher<MdmManager>(){
		@Override
		public MdmManager createService(ContextImpl ctx) throws ServiceNotFoundException {
		IBinder b = ServiceManager.getServiceOrThrow(Context.TESTMDM_SERVICE);
		return new MdmManager(IMdmManger.Stub.asInterface(b));
		}});
	// [testos_android] register, MdmService end
//...

  • framework/base/services/java/com/android/server/SystemServer.java
import android.test.server.TestMdmService;

            // [testos_android] Add TestMdmService, start
             t.traceBegin("TestMdmService");
            try {
                ServiceManager.addService(Context.TESTMDM_SERVICE, 
                    new TestMdmService(context));
            } catch (Throwable e) {
                Slog.e(TAG, "Failure TestMdmService", e);
            }
            t.traceEnd();
            // [testos_android] Add TestMdmService, end 

			...

            // testmdm service ready
            t.traceBegin("TestMdmService Ready");
            try {
                TestMdmService testService = (TestMdmService)ServiceManager.getService(Context.TESTMDM_SERVICE);
                 testService.systemReady();
            } catch (Throwable e) {
                reportWtf("starting testService",e);
            }
           t.traceEnd();

  • framework/base/make/core/tasks/check_boot_jars/package_allow_list.txt
# [testos_android]
android\.test\.mdm
android\.test\.server
  • framework/base/sprd/xxxx/xxxx/product/xxxxxx/main.mk
PRODUCT_PACKAGES += \    
					testmdm_sevice
PRODUCT_ROOT_JARS += \    
					testmdm_sevice					
  • framework/base/sprd/xxxx/xxxx/product/var.mk
# [testos_android] add vendor config
BOARD_VENDOR_SEPOLICY_DIRS += vendor/testos/sepolicy/
  • framework/base/Android.bp
static_libs: [
		...
		"testmdm_service",
 ],
...
...
java_libray {
		...
		"testmdm_service",
}
  • vendor/testos/sepolicy/app.te
allow untrusted_app testmdm_service:service_manager find;
  • vendor/testos/sepolicy/service_contexts
testmdm                              u:object_r:testmdm_service:s0
  • vendor/testos/sepolicy/service.te
type testmdm_service, system_api_service,system_server_service, service_manager_type;

你可能感兴趣的:(framework,android,framework,android)