1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _UI_INPUT_APPLICATION_H
18 #define _UI_INPUT_APPLICATION_H
19 
20 #include <string>
21 
22 #include <android/gui/InputApplicationInfo.h>
23 
24 #include <binder/IBinder.h>
25 #include <binder/Parcel.h>
26 #include <binder/Parcelable.h>
27 
28 #include <utils/RefBase.h>
29 #include <utils/Timers.h>
30 
31 namespace android {
32 
33 /*
34  * Handle for an application that can receive input.
35  *
36  * Used by the native input dispatcher as a handle for the window manager objects
37  * that describe an application.
38  */
39 class InputApplicationHandle {
40 public:
getInfo()41     inline const gui::InputApplicationInfo* getInfo() const { return &mInfo; }
42 
getName()43     inline std::string getName() const { return !mInfo.name.empty() ? mInfo.name : "<invalid>"; }
44 
getDispatchingTimeout(std::chrono::nanoseconds defaultValue)45     inline std::chrono::nanoseconds getDispatchingTimeout(
46             std::chrono::nanoseconds defaultValue) const {
47         return mInfo.token ? std::chrono::milliseconds(mInfo.dispatchingTimeoutMillis)
48                            : defaultValue;
49     }
50 
getApplicationToken()51     inline sp<IBinder> getApplicationToken() const { return mInfo.token; }
52 
53     bool operator==(const InputApplicationHandle& other) const {
54         return getName() == other.getName() && getApplicationToken() == other.getApplicationToken();
55     }
56 
57     bool operator!=(const InputApplicationHandle& other) const { return !(*this == other); }
58 
59     /**
60      * Requests that the state of this object be updated to reflect
61      * the most current available information about the application.
62      *
63      * This method should only be called from within the input dispatcher's
64      * critical section.
65      *
66      * Returns true on success, or false if the handle is no longer valid.
67      */
68     virtual bool updateInfo() = 0;
69 
70 protected:
71     InputApplicationHandle() = default;
72     virtual ~InputApplicationHandle() = default;
73 
74     gui::InputApplicationInfo mInfo;
75 };
76 
77 } // namespace android
78 
79 #endif // _UI_INPUT_APPLICATION_H
80