Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f58800b6 authored by Jeffrey Huang's avatar Jeffrey Huang
Browse files

Migrate away from using ServiceManager

Create a wrapper class to access binder objects
published by statsd mainline module

Bug: 147923515
Test: atest com.google.android.statsd.gts.StatsdHostTestCases
Change-Id: I9ea3677d88c790c856e4e89318ae2ce67ac7df1e
parent 38330a08
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ import android.os.IPullAtomResultReceiver;
import android.os.IStatsManagerService;
import android.os.IStatsd;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.StatsFrameworkInitializer;
import android.util.AndroidException;
import android.util.Slog;
import android.util.StatsEvent;
@@ -702,7 +702,10 @@ public final class StatsManager {
            return mStatsManagerService;
        }
        mStatsManagerService = IStatsManagerService.Stub.asInterface(
                ServiceManager.getService(Context.STATS_MANAGER_SERVICE));
                StatsFrameworkInitializer
                .getStatsServiceManager()
                .getStatsManagerServiceRegisterer()
                .get());
        return mStatsManagerService;
    }

+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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;

import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.app.StatsManager;
import android.app.SystemServiceRegistry;
import android.content.Context;

/**
 * Class for performing registration for all stats services
 *
 * TODO(b/148225705) Change to @SystemApi(client=MODULE_LIBRARIES) when the build system is ready.
 * @hide
 */
@SystemApi
public class StatsFrameworkInitializer {
    private StatsFrameworkInitializer() {
    }

    private static volatile StatsServiceManager sStatsServiceManager;

    /**
     * Sets an instance of {@link StatsServiceManager} that allows
     * the statsd mainline module to register/obtain stats binder services. This is called
     * by the platform during the system initialization.
     *
     * @param statsServiceManager instance of {@link StatsServiceManager} that allows
     * the statsd mainline module to register/obtain statsd binder services.
     */
    public static void setStatsServiceManager(
            @NonNull StatsServiceManager statsServiceManager) {
        if (sStatsServiceManager != null) {
            throw new IllegalStateException("setStatsServiceManager called twice!");
        }

        if (statsServiceManager == null) {
            throw new NullPointerException("statsServiceManager is null");
        }

        sStatsServiceManager = statsServiceManager;
    }

    /** @hide */
    public static StatsServiceManager getStatsServiceManager() {
        return sStatsServiceManager;
    }

    /**
     * Called by {@link SystemServiceRegistry}'s static initializer and registers all statsd
     * services to {@link Context}, so that {@link Context#getSystemService} can return them.
     *
     * @throws IllegalStateException if this is called from anywhere besides
     * {@link SystemServiceRegistry}
     */
    public static void registerServiceWrappers() {
        SystemServiceRegistry.registerContextAwareService(
                Context.STATS_MANAGER,
                StatsManager.class,
                context -> new StatsManager(context)
        );
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@ import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.StatsFrameworkInitializer;
import android.os.StatFs;
import android.os.StatsLogEventWrapper;
import android.os.SynchronousResultReceiver;
@@ -750,7 +751,10 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
     * sStatsd with a null check.
     */
    private static IStatsd fetchStatsdService() {
        return IStatsd.Stub.asInterface(ServiceManager.getService("stats"));
        return IStatsd.Stub.asInterface(StatsFrameworkInitializer
            .getStatsServiceManager()
            .getStatsdServiceRegisterer()
            .get());
    }

    /**
+20 −0
Original line number Diff line number Diff line
@@ -8566,6 +8566,26 @@ package android.os {
    field public static final int TUPLE_VALUE_TYPE = 7; // 0x7
  }
  public class StatsFrameworkInitializer {
    method public static void registerServiceWrappers();
    method public static void setStatsServiceManager(@NonNull android.os.StatsServiceManager);
  }
  public class StatsServiceManager {
    method @NonNull public android.os.StatsServiceManager.ServiceRegisterer getStatsCompanionServiceRegisterer();
    method @NonNull public android.os.StatsServiceManager.ServiceRegisterer getStatsManagerServiceRegisterer();
    method @NonNull public android.os.StatsServiceManager.ServiceRegisterer getStatsdServiceRegisterer();
  }
  public static class StatsServiceManager.ServiceNotFoundException extends java.lang.Exception {
    ctor public StatsServiceManager.ServiceNotFoundException(@NonNull String);
  }
  public static final class StatsServiceManager.ServiceRegisterer {
    method @Nullable public android.os.IBinder get();
    method @Nullable public android.os.IBinder getOrThrow() throws android.os.StatsServiceManager.ServiceNotFoundException;
  }
  public class SystemConfigManager {
    method @NonNull @RequiresPermission(android.Manifest.permission.READ_CARRIER_APP_INFO) public java.util.Set<java.lang.String> getDisabledUntilUsedPreinstalledCarrierApps();
    method @NonNull @RequiresPermission(android.Manifest.permission.READ_CARRIER_APP_INFO) public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getDisabledUntilUsedPreinstalledCarrierAssociatedApps();
+3 −0
Original line number Diff line number Diff line
@@ -111,6 +111,8 @@ import android.os.Process;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.StatsFrameworkInitializer;
import android.os.StatsServiceManager;
import android.os.StrictMode;
import android.os.SystemClock;
import android.os.SystemProperties;
@@ -7514,6 +7516,7 @@ public final class ActivityThread extends ClientTransactionHandler {
     */
    public static void initializeMainlineModules() {
        TelephonyFrameworkInitializer.setTelephonyServiceManager(new TelephonyServiceManager());
        StatsFrameworkInitializer.setStatsServiceManager(new StatsServiceManager());
    }

    private void purgePendingResources() {
Loading