Since C++ cannot import java header files, it is a bit troublesome for C++ services to access java services.
The native service acts as the client, and its code is located in the following directory:
android/frameworks/native/services/customizemanagerservice
WMS acts as the server side, and its code path is as follows:
android/frameworks/base/services/core/java/com/android/server/wm
1. Add a custom interface in frameworks/base/core/java/android/view/IWindowManager.aidl
+ /** + * set customize. + */ + int setcustomize();
2. Add the corresponding implementation in frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
+
+ @Override
+ public int setcustomize() {
+ Slog.d(TAG, "setcustomize");
+ return 1;
+ }
3. Create the following files under frameworks/native/services/customizemanagerservice
IWindowManager.cpp*
WindowManager.cpp*
include_windowmanager/IWindowManager.h
include_windowmanager/WindowManager.h
include_windowmanager/IWindowManager.h
#ifndef ANDROID_IWINDOW_MANAGER_H
#define ANDROID_IWINDOW_MANAGER_H
#ifndef __ANDROID_VNDK__
#include <binder/IInterface.h>
namespace android {
// ------------------------------------------------------------------------------------
class IWindowManager : public IInterface
{
public:
DECLARE_META_INTERFACE(WindowManager)
virtual int setcustomize() = 0;
enum {
CHECK_IF_WEARING = IBinder::FIRST_CALL_TRANSACTION,
};
};
// ------------------------------------------------------------------------------------
} // namespace android
#else // __ANDROID_VNDK__
#error "This header is not visible to vendors"
#endif // __ANDROID_VNDK__
#endif // ANDROID_IACTIVITY_MANAGER_H
IWindowManager.cpp*
#include <WindowManager.h>
#include <IWindowManager.h>
#include <binder/Parcel.h>
#include <utils/Errors.h>
namespace android {
// ------------------------------------------------------------------------------------
class BpWindowManager : public BpInterface<IWindowManager>
{
public:
explicit BpWindowManager(const sp<IBinder>& impl)
: BpInterface<IWindowManager>(impl)
{
}
virtual int setcustomize()
{
Parcel data, reply;
data.writeInterfaceToken(IWindowManager::getInterfaceDescriptor());
status_t ret = remote()->transact(SET_CUSTOMIZE, data, & reply);
if (ret == NO_ERROR) {
return reply.readInt32();
}
return -1;
}
};
// ------------------------------------------------------------------------------------
IMPLEMENT_META_INTERFACE(WindowManager, "android.view.IWindowManager")
} // namespace android
include_windowmanager/WindowManager.h
#pragma once
#ifndef __ANDROID_VNDK__
#include <IWindowManager.h>
#include <utils/threads.h>
// ---------------------------------------------------------------------------
namespace android {
class WindowManager
{
public:
WindowManager();
int setcustomize();
status_t linkToDeath(const sp<IBinder::DeathRecipient>& recipient);
status_t unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient);
private:
Mutex mLock;
sp<IWindowManager> mService;
sp<IWindowManager> getService();
};
} // namespace android
// ---------------------------------------------------------------------------
#else // __ANDROID_VNDK__
#error "This header is not visible to vendors"
#endif // __ANDROID_VNDK__
WindowManager.cpp*
#include <mutex>
#include <unistd.h>
#include <WindowManager.h>
#include <binder/Binder.h>
#include <binder/IServiceManager.h>
#include <utils/SystemClock.h>
namespace android {
WindowManager::WindowManager()
{
}
sp<IWindowManager> WindowManager::getService()
{
std::lock_guard<Mutex> scoped_lock(mLock);
int64_t startTime = 0;
sp<IWindowManager> service = mService;
while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
sp<IBinder> binder = defaultServiceManager()->checkService(String16("window"));
if (binder == nullptr) {
// Wait for the window service to come back...
if (startTime == 0) {
startTime = uptimeMillis();
ALOGI("Waiting for window service");
} else if ((uptimeMillis() - startTime) > 1000000) {
ALOGW("Waiting too long for window service, giving up");
service = nullptr;
break;
}
usleep(25000);
} else {
service = interface_cast<IWindowManager>(binder);
mService = service;
}
}
return service;
}
int WindowManager::setcustomize()
{
sp<IWindowManager> service = getService();
return service != nullptr ? service->setcustomize() : -1;
}
status_t WindowManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
sp<IWindowManager> service = getService();
if (service != nullptr) {
return IInterface::asBinder(service)->linkToDeath(recipient);
}
return INVALID_OPERATION;
}
status_t WindowManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
sp<IWindowManager> service = getService();
if (service != nullptr) {
return IInterface::asBinder(service)->unlinkToDeath(recipient);
}
return INVALID_OPERATION;
}
} // namespace android
4. Call java service in native service
CustomizeManagerService.cpp
#include "WindowManager.h"
int CustomizeManagerService::setcustomize(){
WindowManager wm;
return wm.setcustomize();
}