File: ContextHubIntentEvent.java

package info (click to toggle)
android-platform-frameworks-base 1%3A10.0.0%2Br36-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 321,788 kB
  • sloc: java: 962,234; cpp: 274,314; xml: 242,770; python: 5,060; sh: 1,432; ansic: 494; makefile: 47; sed: 19
file content (259 lines) | stat: -rw-r--r-- 10,019 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright 2018 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.hardware.location;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.PendingIntent;
import android.content.Intent;

import com.android.internal.util.Preconditions;

/**
 * A helper class to retrieve information about a Intent event received for a PendingIntent
 * registered with {@link ContextHubManager.createClient(ContextHubInfo, PendingIntent, long)}.
 * This object can only be created through the factory method
 * {@link ContextHubIntentEvent.fromIntent(Intent)}.
 *
 * @hide
 */
@SystemApi
public class ContextHubIntentEvent {
    @ContextHubManager.Event private final int mEventType;

    @NonNull private final ContextHubInfo mContextHubInfo;

    private final long mNanoAppId;

    private final NanoAppMessage mNanoAppMessage;

    private final int mNanoAppAbortCode;

    private ContextHubIntentEvent(
            @NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType,
            long nanoAppId, NanoAppMessage nanoAppMessage, int nanoAppAbortCode) {
        mContextHubInfo = contextHubInfo;
        mEventType = eventType;
        mNanoAppId = nanoAppId;
        mNanoAppMessage = nanoAppMessage;
        mNanoAppAbortCode = nanoAppAbortCode;
    }

    private ContextHubIntentEvent(
            @NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType) {
        this(contextHubInfo, eventType, -1 /* nanoAppId */, null /* nanoAppMessage */,
                -1 /* nanoAppAbortCode */);
    }

    private ContextHubIntentEvent(
            @NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType,
            long nanoAppId) {
        this(contextHubInfo, eventType, nanoAppId, null /* nanoAppMessage */,
                -1 /* nanoAppAbortCode */);
    }

    private ContextHubIntentEvent(
            @NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType,
            long nanoAppId, @NonNull NanoAppMessage nanoAppMessage) {
        this(contextHubInfo, eventType, nanoAppId, nanoAppMessage, -1 /* nanoAppAbortCode */);
    }

    private ContextHubIntentEvent(
            @NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType,
            long nanoAppId, int nanoAppAbortCode) {
        this(contextHubInfo, eventType, nanoAppId, null /* nanoAppMessage */, nanoAppAbortCode);
    }

    /**
     * Creates a ContextHubIntentEvent object from an Intent received through a PendingIntent
     * registered with {@link ContextHubManager.createClient(ContextHubInfo, PendingIntent, long)}.
     *
     * @param intent the Intent object from an Intent event
     * @return the ContextHubIntentEvent object describing the event
     *
     * @throws IllegalArgumentException if the Intent was not a valid intent
     */
    @NonNull
    public static ContextHubIntentEvent fromIntent(@NonNull Intent intent) {
        Preconditions.checkNotNull(intent, "Intent cannot be null");

        hasExtraOrThrow(intent, ContextHubManager.EXTRA_CONTEXT_HUB_INFO);
        ContextHubInfo info = intent.getParcelableExtra(ContextHubManager.EXTRA_CONTEXT_HUB_INFO);
        if (info == null) {
            throw new IllegalArgumentException("ContextHubInfo extra was null");
        }

        int eventType = getIntExtraOrThrow(intent, ContextHubManager.EXTRA_EVENT_TYPE);
        ContextHubIntentEvent event;
        switch (eventType) {
            case ContextHubManager.EVENT_NANOAPP_LOADED:
            case ContextHubManager.EVENT_NANOAPP_UNLOADED:
            case ContextHubManager.EVENT_NANOAPP_ENABLED:
            case ContextHubManager.EVENT_NANOAPP_DISABLED:
            case ContextHubManager.EVENT_NANOAPP_ABORTED:
            case ContextHubManager.EVENT_NANOAPP_MESSAGE: // fall through
                long nanoAppId = getLongExtraOrThrow(intent, ContextHubManager.EXTRA_NANOAPP_ID);
                if (eventType == ContextHubManager.EVENT_NANOAPP_MESSAGE) {
                    hasExtraOrThrow(intent, ContextHubManager.EXTRA_MESSAGE);
                    NanoAppMessage message =
                            intent.getParcelableExtra(ContextHubManager.EXTRA_MESSAGE);
                    if (message == null) {
                        throw new IllegalArgumentException("NanoAppMessage extra was null");
                    }

                    event = new ContextHubIntentEvent(info, eventType, nanoAppId, message);
                } else if (eventType == ContextHubManager.EVENT_NANOAPP_ABORTED) {
                    int nanoAppAbortCode = getIntExtraOrThrow(
                            intent, ContextHubManager.EXTRA_NANOAPP_ABORT_CODE);
                    event = new ContextHubIntentEvent(info, eventType, nanoAppId, nanoAppAbortCode);
                } else {
                    event = new ContextHubIntentEvent(info, eventType, nanoAppId);
                }
                break;

            case ContextHubManager.EVENT_HUB_RESET:
                event = new ContextHubIntentEvent(info, eventType);
                break;

            default:
                throw new IllegalArgumentException("Unknown intent event type " + eventType);
        }

        return event;
    }

    /**
     * @return the event type of this Intent event
     */
    @ContextHubManager.Event
    public int getEventType() {
        return mEventType;
    }

    /**
     * @return the ContextHubInfo object describing the Context Hub this event was for
     */
    @NonNull
    public ContextHubInfo getContextHubInfo() {
        return mContextHubInfo;
    }

    /**
     * @return the ID of the nanoapp this event was for
     *
     * @throws UnsupportedOperationException if the event did not have a nanoapp associated
     */
    public long getNanoAppId() {
        if (mEventType == ContextHubManager.EVENT_HUB_RESET) {
            throw new UnsupportedOperationException(
                    "Cannot invoke getNanoAppId() on Context Hub reset event");
        }
        return mNanoAppId;
    }

    /**
     * @return the nanoapp's abort code
     *
     * @throws UnsupportedOperationException if this was not a nanoapp abort event
     */
    public int getNanoAppAbortCode() {
        if (mEventType != ContextHubManager.EVENT_NANOAPP_ABORTED) {
            throw new UnsupportedOperationException(
                    "Cannot invoke getNanoAppAbortCode() on non-abort event: " + mEventType);
        }
        return mNanoAppAbortCode;
    }

    /**
     * @return the message from a nanoapp
     *
     * @throws UnsupportedOperationException if this was not a nanoapp message event
     */
    @NonNull
    public NanoAppMessage getNanoAppMessage() {
        if (mEventType != ContextHubManager.EVENT_NANOAPP_MESSAGE) {
            throw new UnsupportedOperationException(
                    "Cannot invoke getNanoAppMessage() on non-message event: " + mEventType);
        }
        return mNanoAppMessage;
    }

    @Override
    public String toString() {
        String out = "ContextHubIntentEvent[eventType = " + mEventType
                + ", contextHubId = " + mContextHubInfo.getId();

        if (mEventType != ContextHubManager.EVENT_HUB_RESET) {
            out += ", nanoAppId = 0x" + Long.toHexString(mNanoAppId);
        }
        if (mEventType == ContextHubManager.EVENT_NANOAPP_ABORTED) {
            out += ", nanoAppAbortCode = " + mNanoAppAbortCode;
        }
        if (mEventType == ContextHubManager.EVENT_NANOAPP_MESSAGE) {
            out += ", nanoAppMessage = " + mNanoAppMessage;
        }

        return out + "]";
    }

    @Override
    public boolean equals(@Nullable Object object) {
        if (object == this) {
            return true;
        }

        boolean isEqual = false;
        if (object instanceof ContextHubIntentEvent) {
            ContextHubIntentEvent other = (ContextHubIntentEvent) object;
            if (other.getEventType() == mEventType
                    && other.getContextHubInfo().equals(mContextHubInfo)) {
                isEqual = true;
                try {
                    if (mEventType != ContextHubManager.EVENT_HUB_RESET) {
                        isEqual &= (other.getNanoAppId() == mNanoAppId);
                    }
                    if (mEventType == ContextHubManager.EVENT_NANOAPP_ABORTED) {
                        isEqual &= (other.getNanoAppAbortCode() == mNanoAppAbortCode);
                    }
                    if (mEventType == ContextHubManager.EVENT_NANOAPP_MESSAGE) {
                        isEqual &= other.getNanoAppMessage().equals(mNanoAppMessage);
                    }
                } catch (UnsupportedOperationException e) {
                    isEqual = false;
                }
            }
        }

        return isEqual;
    }

    private static void hasExtraOrThrow(Intent intent, String extra) {
        if (!intent.hasExtra(extra)) {
            throw new IllegalArgumentException("Intent did not have extra: " + extra);
        }
    }

    private static int getIntExtraOrThrow(Intent intent, String extra) {
        hasExtraOrThrow(intent, extra);
        return intent.getIntExtra(extra, -1 /* defaultValue */);
    }

    private static long getLongExtraOrThrow(Intent intent, String extra) {
        hasExtraOrThrow(intent, extra);
        return intent.getLongExtra(extra, -1 /* defaultValue */);
    }
}