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

Commit cde601cc authored by Jyoti Bhayana's avatar Jyoti Bhayana
Browse files

Makes Android camera service available earlier (before Android boot-complete)

Bug:231557017
Test: Check the boot time of cameraserver and verify it
improved to around 4 sec

Change-Id: I2f85edcdbc2d0ddbe605457c1ef2124924a34f5e
parent f04f60a5
Loading
Loading
Loading
Loading
+75 −3
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@

namespace {
    const char* kPermissionServiceName = "permission";
    const char* kActivityServiceName = "activity";
    const char* kSensorPrivacyServiceName = "sensor_privacy";
    const char* kAppopsServiceName = "appops";
}; // namespace anonymous

namespace android {
@@ -167,6 +170,15 @@ static bool doesClientHaveSystemUid() {
    return (CameraThreadState::getCallingUid() < AID_APP_START);
}

void CameraService::onServiceRegistration(const String16& name, const sp<IBinder>&) {
    if (name != String16(kAppopsServiceName)) {
        return;
    }

    ALOGV("appops service registered. setting camera audio restriction");
    mAppOps.setCameraAudioRestriction(mAudioRestriction);
}

void CameraService::onFirstRef()
{

@@ -191,7 +203,19 @@ void CameraService::onFirstRef()
    mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
    mSensorPrivacyPolicy->registerSelf();
    mInjectionStatusListener = new InjectionStatusListener(this);

    // appops function setCamerAudioRestriction uses getService which
    // is blocking till the appops service is ready. To enable early
    // boot availability for cameraservice, use checkService which is
    // non blocking and register for notifications
    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> binder = sm->checkService(String16(kAppopsServiceName));
    if (!binder) {
        sm->registerForNotifications(String16(kAppopsServiceName), this);
    } else {
        mAppOps.setCameraAudioRestriction(mAudioRestriction);
    }

    sp<HidlCameraService> hcs = HidlCameraService::getInstance(this);
    if (hcs->registerAsService() != android::OK) {
        // Deprecated, so it will fail to register on newer devices
@@ -3792,7 +3816,7 @@ void CameraService::Client::OpsCallback::opChanged(int32_t op,
//                  UidPolicy
// ----------------------------------------------------------------------------

void CameraService::UidPolicy::registerSelf() {
void CameraService::UidPolicy::registerWithActivityManager() {
    Mutex::Autolock _l(mUidLock);

    if (mRegistered) return;
@@ -3809,6 +3833,27 @@ void CameraService::UidPolicy::registerSelf() {
    }
}

void CameraService::UidPolicy::onServiceRegistration(const String16& name, const sp<IBinder>&) {
    if (name != String16(kActivityServiceName)) {
        return;
    }

    registerWithActivityManager();
}

void CameraService::UidPolicy::registerSelf() {
    // Use check service to see if the activity service is available
    // If not available then register for notifications, instead of blocking
    // till the service is ready
    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> binder = sm->checkService(String16(kActivityServiceName));
    if (!binder) {
        sm->registerForNotifications(String16(kActivityServiceName), this);
    } else {
        registerWithActivityManager();
    }
}

void CameraService::UidPolicy::unregisterSelf() {
    Mutex::Autolock _l(mUidLock);

@@ -4022,7 +4067,9 @@ void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPack
// ----------------------------------------------------------------------------
//                  SensorPrivacyPolicy
// ----------------------------------------------------------------------------
void CameraService::SensorPrivacyPolicy::registerSelf() {

void CameraService::SensorPrivacyPolicy::registerWithSensorPrivacyManager()
{
    Mutex::Autolock _l(mSensorPrivacyLock);
    if (mRegistered) {
        return;
@@ -4037,6 +4084,27 @@ void CameraService::SensorPrivacyPolicy::registerSelf() {
    }
}

void CameraService::SensorPrivacyPolicy::onServiceRegistration(const String16& name,
                                                               const sp<IBinder>&) {
    if (name != String16(kSensorPrivacyServiceName)) {
        return;
    }

    registerWithSensorPrivacyManager();
}

void CameraService::SensorPrivacyPolicy::registerSelf() {
    // Use checkservice to see if the sensor_privacy service is available
    // If service is not available then register for notification
    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> binder = sm->checkService(String16(kSensorPrivacyServiceName));
    if (!binder) {
        sm->registerForNotifications(String16(kSensorPrivacyServiceName),this);
    } else {
        registerWithSensorPrivacyManager();
    }
}

void CameraService::SensorPrivacyPolicy::unregisterSelf() {
    Mutex::Autolock _l(mSensorPrivacyLock);
    mSpm.removeSensorPrivacyListener(this);
@@ -4046,6 +4114,10 @@ void CameraService::SensorPrivacyPolicy::unregisterSelf() {
}

bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
    if (!mRegistered) {
      registerWithSensorPrivacyManager();
    }

    Mutex::Autolock _l(mSensorPrivacyLock);
    return mSensorPrivacyEnabled;
}
+19 −3
Original line number Diff line number Diff line
@@ -71,7 +71,8 @@ class CameraService :
    public BinderService<CameraService>,
    public virtual ::android::hardware::BnCameraService,
    public virtual IBinder::DeathRecipient,
    public virtual CameraProviderManager::StatusListener
    public virtual CameraProviderManager::StatusListener,
    public virtual IServiceManager::LocalRegistrationCallback
{
    friend class BinderService<CameraService>;
    friend class CameraOfflineSessionClient;
@@ -101,6 +102,9 @@ public:
    // Implementation of BinderService<T>
    static char const* getServiceName() { return "media.camera"; }

    // Implementation of IServiceManager::LocalRegistrationCallback
    virtual void onServiceRegistration(const String16& name, const sp<IBinder>& binder) override;

                        // Non-null arguments for cameraServiceProxyWrapper should be provided for
                        // testing purposes only.
                        CameraService(std::shared_ptr<CameraServiceProxyWrapper>
@@ -714,7 +718,10 @@ private:

    // Observer for UID lifecycle enforcing that UIDs in idle
    // state cannot use the camera to protect user privacy.
    class UidPolicy : public BnUidObserver, public virtual IBinder::DeathRecipient {
    class UidPolicy :
        public BnUidObserver,
        public virtual IBinder::DeathRecipient,
        public virtual IServiceManager::LocalRegistrationCallback {
    public:
        explicit UidPolicy(sp<CameraService> service)
                : mRegistered(false), mService(service) {}
@@ -739,12 +746,16 @@ private:
        void registerMonitorUid(uid_t uid);
        void unregisterMonitorUid(uid_t uid);

        // Implementation of IServiceManager::LocalRegistrationCallback
        virtual void onServiceRegistration(const String16& name,
                        const sp<IBinder>& binder) override;
        // IBinder::DeathRecipient implementation
        virtual void binderDied(const wp<IBinder> &who);
    private:
        bool isUidActiveLocked(uid_t uid, String16 callingPackage);
        int32_t getProcStateLocked(uid_t uid);
        void updateOverrideUid(uid_t uid, String16 callingPackage, bool active, bool insert);
        void registerWithActivityManager();

        struct MonitoredUid {
            int32_t procState;
@@ -764,7 +775,8 @@ private:
    // If sensor privacy is enabled then all apps, including those that are active, should be
    // prevented from accessing the camera.
    class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener,
            public virtual IBinder::DeathRecipient {
            public virtual IBinder::DeathRecipient,
            public virtual IServiceManager::LocalRegistrationCallback {
        public:
            explicit SensorPrivacyPolicy(wp<CameraService> service)
                    : mService(service), mSensorPrivacyEnabled(false), mRegistered(false) {}
@@ -778,6 +790,9 @@ private:
            binder::Status onSensorPrivacyChanged(int toggleType, int sensor,
                                                  bool enabled);

            // Implementation of IServiceManager::LocalRegistrationCallback
            virtual void onServiceRegistration(const String16& name,
                                               const sp<IBinder>& binder) override;
            // IBinder::DeathRecipient implementation
            virtual void binderDied(const wp<IBinder> &who);

@@ -789,6 +804,7 @@ private:
            bool mRegistered;

            bool hasCameraPrivacyFeature();
            void registerWithSensorPrivacyManager();
    };

    sp<UidPolicy> mUidPolicy;