This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsKodiTopmostApp.cpp
144 lines (120 loc) · 4.87 KB
/
IsKodiTopmostApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <utils/String8.h>
#include <utils/String16.h>
#include <binder/Parcel.h>
#include <binder/IBinder.h>
#include <binder/IInterface.h>
#include <binder/IServiceManager.h>
#include "IsKodiTopmostApp.h"
class IActivityTaskManager: public android::IInterface
{
public:
DECLARE_META_INTERFACE(ActivityTaskManager);
enum {
GET_TASKS = android::IBinder::FIRST_CALL_TRANSACTION + 21,
};
virtual bool getTasks(int maxNum, bool filterOnlyVisibleRecents, bool keepIntentExtra, android::Parcel *reply) = 0;
};
class BpActivityTaskManager: public android::BpInterface<IActivityTaskManager>
{
public:
explicit BpActivityTaskManager(const android::sp<android::IBinder> &impl)
: android::BpInterface<IActivityTaskManager>(impl)
{
}
virtual bool getTasks(int maxNum, bool filterOnlyVisibleRecents, bool keepIntentExtra, android::Parcel *reply) override
{
android::Parcel data;
data.writeInterfaceToken(IActivityTaskManager::getInterfaceDescriptor());
data.writeInt32(maxNum);
data.writeBool(filterOnlyVisibleRecents);
data.writeBool(keepIntentExtra);
return remote()->transact(GET_TASKS, data, reply) == android::OK;
}
};
IMPLEMENT_META_INTERFACE(ActivityTaskManager, "android.app.IActivityTaskManager");
static android::sp<IActivityTaskManager> activity_task = nullptr;
class ActivityTaskDeathRecipient : public android::IBinder::DeathRecipient
{
public:
virtual void binderDied(const android::wp<android::IBinder> &who) override
{
activity_task = nullptr;
}
};
const static android::sp<ActivityTaskDeathRecipient> gDeathNotifier(new ActivityTaskDeathRecipient);
static bool connectActivityTaskService()
{
const android::sp<android::IServiceManager> sm = android::defaultServiceManager();
if (!sm)
return false;
const android::sp<android::IBinder> binder = sm->getService(android::String16("activity_task"));
if (!binder)
return false;
activity_task = android::interface_cast<IActivityTaskManager>(binder);
if (!activity_task)
return false;
binder->linkToDeath(gDeathNotifier);
return true;
}
bool IsKodiTopmostApp()
{
static const android::StaticString16 kodiBaseActivity = android::StaticString16(u"org.xbmc.kodi");
static const size_t kodiBaseActivitySize = kodiBaseActivity.size();
android::Parcel reply;
if (!activity_task && !connectActivityTaskService())
return false;
if (!activity_task->getTasks(1, false, false, &reply))
return false;
// List shit
int32_t status = reply.readInt32();
if (status != android::OK)
return false;
int32_t length = reply.readInt32();
if (length != 1)
return false;
int32_t nullcheck = reply.readInt32();
if (!nullcheck)
return false;
/*reply.readInt32(); // RunningTaskInfo.id
reply.readInt32(); // TaskInfo.userId
reply.readInt32(); // TaskInfo.taskId
reply.readInt32(); // TaskInfo.displayId */
reply.setDataPosition(reply.dataPosition() + (sizeof(int32_t) * 4));
if (!reply.readBool()) // TaskInfo.isRunning
return false;
if (reply.readBool()) { // TypedObject "not null" boolean
// Intent
/*const char *baseIntent =*/ reply.readString8(); // mAction
// Trying to parse an Intent Parcel seems
// incredibly time-consuming on account of
// how extremely varying it is...
// So I won't. But I will write very unsafe
// code that makes a lot of assumptions.
// Right at the very end of the Intent
// are mContentUserHint and mExtras
// mContentUserHint is always likely
// to be -2 (UserHandle.USER_CURRENT)
// mExtras should always be an empty
// Bundle because keepIntentExtra is
// false
// Check to see if there's room enough
// for at least four integers:
// mContentUserHint, the empty mExtras
// length of a potential String16 and
// a String16 with at least two characters
do {
if (reply.readInt32() == -2 && reply.readInt32() == 0xffffffff)
goto readbaseActivityProbably;
} while (reply.dataAvail() >= sizeof(int32_t) * 4);
return false;
}
readbaseActivityProbably:
int32_t stringLengthMaybe = 0;
if (reply.readInt32(&stringLengthMaybe) != android::OK || stringLengthMaybe != kodiBaseActivitySize)
return false;
reply.setDataPosition(reply.dataPosition() - sizeof(int32_t));
android::String16 baseActivity;
if (reply.readString16(&baseActivity) == android::OK)
return baseActivity == kodiBaseActivity;
return false;
}