Android6.0增加一个系统服务

自己写的一个给android6.0系统添加自定义服务的patch,保存下以后备用。

From 7048940231dc53e08e5928c0712f3dcbfb29b2c0 Mon Sep 17 00:00:00 2001
****************
Date: Thu, 24 Jan 2019 15:29:47 +0800
Subject: [PATCH] add the WatchService to framework

---
 frameworks/base/Android.mk                         |  1 +
 .../java/android/app/SystemServiceRegistry.java    | 12 ++++++
 .../base/core/java/android/content/Context.java    |  4 ++
 .../core/java/android/watch/IWatchManager.aidl     | 24 ++++++++++++
 .../base/core/java/android/watch/WatchManager.java | 45 ++++++++++++++++++++++
 .../com/android/server/watch/WatchService.java     | 44 +++++++++++++++++++++
 .../java/com/android/server/SystemServer.java      | 22 +++++++++--
 7 files changed, 149 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 frameworks/base/Android.mk
 mode change 100644 => 100755 frameworks/base/core/java/android/app/SystemServiceRegistry.java
 mode change 100644 => 100755 frameworks/base/core/java/android/content/Context.java
 create mode 100755 frameworks/base/core/java/android/watch/IWatchManager.aidl
 create mode 100755 frameworks/base/core/java/android/watch/WatchManager.java
 create mode 100755 frameworks/base/services/core/java/com/android/server/watch/WatchService.java
 mode change 100644 => 100755 frameworks/base/services/java/com/android/server/SystemServer.java

diff --git a/frameworks/base/Android.mk b/frameworks/base/Android.mk
old mode 100644
new mode 100755
index 0fd7642..9b64d16
--- a/frameworks/base/Android.mk
+++ b/frameworks/base/Android.mk
@@ -417,6 +417,7 @@ LOCAL_SRC_FILES += \
     packages/services/PacProcessor/com/android/net/IProxyService.aidl \
     packages/services/Proxy/com/android/net/IProxyCallback.aidl \
     packages/services/Proxy/com/android/net/IProxyPortListener.aidl \
+    core/java/android/watch/IWatchManager.aidl \
 
 # FRAMEWORKS_BASE_JAVA_SRC_DIRS comes from build/core/pathmap.mk
 LOCAL_AIDL_INCLUDES += $(FRAMEWORKS_BASE_JAVA_SRC_DIRS)
diff --git a/frameworks/base/core/java/android/app/SystemServiceRegistry.java b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
old mode 100644
new mode 100755
index 3d264c6..afce332
--- a/frameworks/base/core/java/android/app/SystemServiceRegistry.java
+++ b/frameworks/base/core/java/android/app/SystemServiceRegistry.java
@@ -96,6 +96,8 @@ import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.Vibrator;
 import android.os.storage.StorageManager;
+import android.watch.WatchManager;
+import android.watch.IWatchManager;
 import android.print.IPrintManager;
 import android.print.PrintManager;
 import android.hardware.fingerprint.FingerprintManager;
@@ -117,6 +119,7 @@ import android.view.accessibility.CaptioningManager;
 import android.view.inputmethod.InputMethodManager;
 import android.view.textservice.TextServicesManager;
 
+
 import java.util.HashMap;
 
 /**
@@ -704,6 +707,15 @@ final class SystemServiceRegistry {
             public RadioManager createService(ContextImpl ctx) {
                 return new RadioManager(ctx);
             }});
+        
+        registerService(Context.WATCH_SERVICE, WatchManager.class,
+            new CachedServiceFetcher() {
+            @Override
+            public WatchManager createService(ContextImpl ctx) {
+                IBinder b = ServiceManager.getService(Context.WATCH_SERVICE);
+                IWatchManager service = IWatchManager.Stub.asInterface(b);
+                return new WatchManager(service,ctx);
+            }});
     }
 
     /**
diff --git a/frameworks/base/core/java/android/content/Context.java b/frameworks/base/core/java/android/content/Context.java
old mode 100644
new mode 100755
index 758b6ff..7b54b18
--- a/frameworks/base/core/java/android/content/Context.java
+++ b/frameworks/base/core/java/android/content/Context.java
@@ -2511,6 +2511,7 @@ public abstract class Context {
             MEDIA_PROJECTION_SERVICE,
             MIDI_SERVICE,
             RADIO_SERVICE,
+            WATCH_SERVICE,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ServiceName {}
@@ -2680,6 +2681,9 @@ public abstract class Context {
      */
     public abstract String getSystemServiceName(Class serviceClass);
 
+    
+    public static final String WATCH_SERVICE = "watch";
+    
     /**
      * Use with {@link #getSystemService} to retrieve a
      * {@link android.os.PowerManager} for controlling power management,
diff --git a/frameworks/base/core/java/android/watch/IWatchManager.aidl b/frameworks/base/core/java/android/watch/IWatchManager.aidl
new file mode 100755
index 0000000..a30a57b
--- /dev/null
+++ b/frameworks/base/core/java/android/watch/IWatchManager.aidl
@@ -0,0 +1,24 @@
+/**
+ * Copyright (c) 2007, 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.watch;
+
+/** {@hide} */
+interface IWatchManager
+{
+    int getTargetStep();
+}
+
diff --git a/frameworks/base/core/java/android/watch/WatchManager.java b/frameworks/base/core/java/android/watch/WatchManager.java
new file mode 100755
index 0000000..bc4f887
--- /dev/null
+++ b/frameworks/base/core/java/android/watch/WatchManager.java
@@ -0,0 +1,45 @@
+/*
+ * 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 android.watch;
+
+import android.content.Context;
+import android.os.RemoteException;
+import android.util.Slog;
+
+public class WatchManager {
+    private static final String TAG = "WatchManager";
+    private static final boolean DEBUG = true;
+
+    private final IWatchManager mService;
+    private Context mContext;
+    
+    public WatchManager(IWatchManager service, Context ctx) {
+        mService = service;
+        mContext = ctx;
+    }
+    
+    public int getTargetStep(){
+        Slog.i(TAG,"WatchManager getTargetStep called,return 1000");
+        int step = 0;
+        try {
+            step = mService.getTargetStep();
+        } catch (RemoteException ex) {
+        }
+        return step;
+    }
+    
+}
diff --git a/frameworks/base/services/core/java/com/android/server/watch/WatchService.java b/frameworks/base/services/core/java/com/android/server/watch/WatchService.java
new file mode 100755
index 0000000..552dd44
--- /dev/null
+++ b/frameworks/base/services/core/java/com/android/server/watch/WatchService.java
@@ -0,0 +1,44 @@
+/*
+ * 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.watch;
+
+import android.content.Context;
+import android.util.Slog;
+
+import android.watch.IWatchManager;
+
+
+public class WatchService extends IWatchManager.Stub{
+    private static final String TAG = "WatchService";
+    private static final boolean DEBUG = true;

+    
+    private final Context mContext;
+
+    public WatchService(Context context) {
+        super();
+        mContext = context;
+    }
+    
+   
+    public int getTargetStep(){
+        Slog.i(TAG,"WatchService getTargetStep called,return 1000");
+        return 1000;
+    }
+
+  
+}
diff --git a/frameworks/base/services/java/com/android/server/SystemServer.java b/frameworks/base/services/java/com/android/server/SystemServer.java
old mode 100644
new mode 100755
index c4b2df8..5bd7718
--- a/frameworks/base/services/java/com/android/server/SystemServer.java
+++ b/frameworks/base/services/java/com/android/server/SystemServer.java
@@ -93,6 +93,7 @@ import com.android.server.usb.UsbService;
 import com.android.server.wallpaper.WallpaperManagerService;
 import com.android.server.webkit.WebViewUpdateService;
 import com.android.server.wm.WindowManagerService;
+import com.android.server.watch.WatchService;
 
 import dalvik.system.VMRuntime;
 
@@ -437,6 +438,8 @@ public final class SystemServer {
         MmsServiceBroker mmsService = null;
         EntropyMixer entropyMixer = null;
         CameraService cameraService = null;
+        
+        WatchService watchService = null;
 
         boolean disableStorage = SystemProperties.getBoolean("config.disable_storage", false);
         boolean disableBluetooth = SystemProperties.getBoolean("config.disable_bluetooth", false);
@@ -446,8 +449,10 @@ public final class SystemServer {
         boolean disableNetwork = SystemProperties.getBoolean("config.disable_network", false);
         boolean disableNetworkTime = SystemProperties.getBoolean("config.disable_networktime", false);
         boolean isEmulator = SystemProperties.get("ro.kernel.qemu").equals("1");
-    boolean disableAtlas = SystemProperties.getBoolean("config.disable_atlas", true);
-
+        boolean disableAtlas = SystemProperties.getBoolean("config.disable_atlas", true);
+        
+        boolean disableWatch = SystemProperties.getBoolean("config.disable_watch", false);
+        
         try {
             Slog.i(TAG, "Reading configuration...");
             SystemConfig.getInstance();
@@ -831,7 +836,18 @@ public final class SystemServer {
             } catch (Throwable e) {
                 reportWtf("starting Audio Service", e);
             }
-
+            Slog.i(TAG, "Tim_SS disableWatch disableWatch=" + disableWatch);
+            if(!disableWatch){
+                try {
+                    Slog.i(TAG, "Watch Service");
+                    watchService = new WatchService(context);
+                    ServiceManager.addService(Context.WATCH_SERVICE, watchService);
+                } catch (Throwable e) {
+                    reportWtf("starting Watch Service", e);
+                }
+
+            }
+            
             if (!disableNonCoreServices) {
                 mSystemServiceManager.startService(DockObserver.class);
             }
-- 
1.9.1

在根目录编译:

make update-api -j8

mmm framework/base/service

把service.jar和service.odex push到相应目录。

app内使用方法:

        WatchManager watchManager =(WatchManager) getSystemService(Context.WATCH_SERVICE);
        if(watchManager != null){
            int step = watchManager.getTargetStep();
            Log.e("Tim_SS","watchserver app step=" + step);
        }

 

你可能感兴趣的:(Android源码分析)