| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.hardware.location; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.annotation.SystemApi; |
| 21 |
import android.app.PendingIntent; |
| 22 |
import android.content.Intent; |
| 23 |
|
| 24 |
import java.util.Objects; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@SystemApi |
| 35 |
public class ContextHubIntentEvent { |
| 36 |
@ContextHubManager.Event private final int mEventType; |
| 37 |
|
| 38 |
@NonNull private final ContextHubInfo mContextHubInfo; |
| 39 |
|
| 40 |
private final long mNanoAppId; |
| 41 |
|
| 42 |
private final NanoAppMessage mNanoAppMessage; |
| 43 |
|
| 44 |
private final int mNanoAppAbortCode; |
| 45 |
|
| 46 |
private final int mClientAuthorizationState; |
| 47 |
|
| 48 |
private ContextHubIntentEvent( |
| 49 |
@NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType, |
| 50 |
long nanoAppId, NanoAppMessage nanoAppMessage, int nanoAppAbortCode, |
| 51 |
@ContextHubManager.AuthorizationState int clientAuthorizationState) { |
| 52 |
mContextHubInfo = contextHubInfo; |
| 53 |
mEventType = eventType; |
| 54 |
mNanoAppId = nanoAppId; |
| 55 |
mNanoAppMessage = nanoAppMessage; |
| 56 |
mNanoAppAbortCode = nanoAppAbortCode; |
| 57 |
mClientAuthorizationState = clientAuthorizationState; |
| 58 |
} |
| 59 |
|
| 60 |
private ContextHubIntentEvent( |
| 61 |
@NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType) { |
| 62 |
this(contextHubInfo, eventType, -1 , null , |
| 63 |
-1 , 0 ); |
| 64 |
} |
| 65 |
|
| 66 |
private ContextHubIntentEvent( |
| 67 |
@NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType, |
| 68 |
long nanoAppId) { |
| 69 |
this(contextHubInfo, eventType, nanoAppId, null , |
| 70 |
-1 , 0 ); |
| 71 |
} |
| 72 |
|
| 73 |
private ContextHubIntentEvent( |
| 74 |
@NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType, |
| 75 |
long nanoAppId, @NonNull NanoAppMessage nanoAppMessage) { |
| 76 |
this(contextHubInfo, eventType, nanoAppId, nanoAppMessage, -1 , |
| 77 |
0 ); |
| 78 |
} |
| 79 |
|
| 80 |
private ContextHubIntentEvent( |
| 81 |
@NonNull ContextHubInfo contextHubInfo, @ContextHubManager.Event int eventType, |
| 82 |
long nanoAppId, int nanoAppAbortCode) { |
| 83 |
this(contextHubInfo, eventType, nanoAppId, null , nanoAppAbortCode, |
| 84 |
0 ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
@NonNull |
| 97 |
public static ContextHubIntentEvent fromIntent(@NonNull Intent intent) { |
| 98 |
Objects.requireNonNull(intent, "Intent cannot be null"); |
| 99 |
|
| 100 |
hasExtraOrThrow(intent, ContextHubManager.EXTRA_CONTEXT_HUB_INFO); |
| 101 |
ContextHubInfo info = intent.getParcelableExtra(ContextHubManager.EXTRA_CONTEXT_HUB_INFO); |
| 102 |
if (info == null) { |
| 103 |
throw new IllegalArgumentException("ContextHubInfo extra was null"); |
| 104 |
} |
| 105 |
|
| 106 |
int eventType = getIntExtraOrThrow(intent, ContextHubManager.EXTRA_EVENT_TYPE); |
| 107 |
ContextHubIntentEvent event; |
| 108 |
switch (eventType) { |
| 109 |
case ContextHubManager.EVENT_NANOAPP_LOADED: |
| 110 |
case ContextHubManager.EVENT_NANOAPP_UNLOADED: |
| 111 |
case ContextHubManager.EVENT_NANOAPP_ENABLED: |
| 112 |
case ContextHubManager.EVENT_NANOAPP_DISABLED: |
| 113 |
case ContextHubManager.EVENT_NANOAPP_ABORTED: |
| 114 |
case ContextHubManager.EVENT_NANOAPP_MESSAGE: |
| 115 |
case ContextHubManager.EVENT_CLIENT_AUTHORIZATION: |
| 116 |
long nanoAppId = getLongExtraOrThrow(intent, ContextHubManager.EXTRA_NANOAPP_ID); |
| 117 |
if (eventType == ContextHubManager.EVENT_NANOAPP_MESSAGE) { |
| 118 |
hasExtraOrThrow(intent, ContextHubManager.EXTRA_MESSAGE); |
| 119 |
NanoAppMessage message = |
| 120 |
intent.getParcelableExtra(ContextHubManager.EXTRA_MESSAGE); |
| 121 |
if (message == null) { |
| 122 |
throw new IllegalArgumentException("NanoAppMessage extra was null"); |
| 123 |
} |
| 124 |
|
| 125 |
event = new ContextHubIntentEvent(info, eventType, nanoAppId, message); |
| 126 |
} else if (eventType == ContextHubManager.EVENT_NANOAPP_ABORTED) { |
| 127 |
int nanoAppAbortCode = getIntExtraOrThrow( |
| 128 |
intent, ContextHubManager.EXTRA_NANOAPP_ABORT_CODE); |
| 129 |
event = new ContextHubIntentEvent(info, eventType, nanoAppId, nanoAppAbortCode); |
| 130 |
} else if (eventType == ContextHubManager.EVENT_CLIENT_AUTHORIZATION) { |
| 131 |
int authState = getIntExtraOrThrow( |
| 132 |
intent, ContextHubManager.EXTRA_CLIENT_AUTHORIZATION_STATE); |
| 133 |
event = new ContextHubIntentEvent(info, eventType, nanoAppId, |
| 134 |
null , -1 , authState); |
| 135 |
} else { |
| 136 |
event = new ContextHubIntentEvent(info, eventType, nanoAppId); |
| 137 |
} |
| 138 |
break; |
| 139 |
|
| 140 |
case ContextHubManager.EVENT_HUB_RESET: |
| 141 |
event = new ContextHubIntentEvent(info, eventType); |
| 142 |
break; |
| 143 |
|
| 144 |
default: |
| 145 |
throw new IllegalArgumentException("Unknown intent event type " + eventType); |
| 146 |
} |
| 147 |
|
| 148 |
return event; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
@ContextHubManager.Event |
| 155 |
public int getEventType() { |
| 156 |
return mEventType; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
@NonNull |
| 163 |
public ContextHubInfo getContextHubInfo() { |
| 164 |
return mContextHubInfo; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public long getNanoAppId() { |
| 173 |
if (mEventType == ContextHubManager.EVENT_HUB_RESET) { |
| 174 |
throw new UnsupportedOperationException( |
| 175 |
"Cannot invoke getNanoAppId() on Context Hub reset event"); |
| 176 |
} |
| 177 |
return mNanoAppId; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public int getNanoAppAbortCode() { |
| 186 |
if (mEventType != ContextHubManager.EVENT_NANOAPP_ABORTED) { |
| 187 |
throw new UnsupportedOperationException( |
| 188 |
"Cannot invoke getNanoAppAbortCode() on non-abort event: " + mEventType); |
| 189 |
} |
| 190 |
return mNanoAppAbortCode; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@NonNull |
| 199 |
public NanoAppMessage getNanoAppMessage() { |
| 200 |
if (mEventType != ContextHubManager.EVENT_NANOAPP_MESSAGE) { |
| 201 |
throw new UnsupportedOperationException( |
| 202 |
"Cannot invoke getNanoAppMessage() on non-message event: " + mEventType); |
| 203 |
} |
| 204 |
return mNanoAppMessage; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
@ContextHubManager.AuthorizationState |
| 213 |
public int getClientAuthorizationState() { |
| 214 |
if (mEventType != ContextHubManager.EVENT_CLIENT_AUTHORIZATION) { |
| 215 |
throw new UnsupportedOperationException( |
| 216 |
"Cannot invoke getClientAuthorizationState() on non-authorization event: " |
| 217 |
+ mEventType); |
| 218 |
} |
| 219 |
return mClientAuthorizationState; |
| 220 |
} |
| 221 |
|
| 222 |
@NonNull |
| 223 |
@Override |
| 224 |
public String toString() { |
| 225 |
String out = "ContextHubIntentEvent[eventType = " + mEventType |
| 226 |
+ ", contextHubId = " + mContextHubInfo.getId(); |
| 227 |
|
| 228 |
if (mEventType != ContextHubManager.EVENT_HUB_RESET) { |
| 229 |
out += ", nanoAppId = 0x" + Long.toHexString(mNanoAppId); |
| 230 |
} |
| 231 |
if (mEventType == ContextHubManager.EVENT_NANOAPP_ABORTED) { |
| 232 |
out += ", nanoAppAbortCode = " + mNanoAppAbortCode; |
| 233 |
} |
| 234 |
if (mEventType == ContextHubManager.EVENT_NANOAPP_MESSAGE) { |
| 235 |
out += ", nanoAppMessage = " + mNanoAppMessage; |
| 236 |
} |
| 237 |
if (mEventType == ContextHubManager.EVENT_CLIENT_AUTHORIZATION) { |
| 238 |
out += ", clientAuthState = " + mClientAuthorizationState; |
| 239 |
} |
| 240 |
|
| 241 |
return out + "]"; |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public boolean equals(@Nullable Object object) { |
| 246 |
if (object == this) { |
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
boolean isEqual = false; |
| 251 |
if (object instanceof ContextHubIntentEvent) { |
| 252 |
ContextHubIntentEvent other = (ContextHubIntentEvent) object; |
| 253 |
if (other.getEventType() == mEventType |
| 254 |
&& other.getContextHubInfo().equals(mContextHubInfo)) { |
| 255 |
isEqual = true; |
| 256 |
try { |
| 257 |
if (mEventType != ContextHubManager.EVENT_HUB_RESET) { |
| 258 |
isEqual &= (other.getNanoAppId() == mNanoAppId); |
| 259 |
} |
| 260 |
if (mEventType == ContextHubManager.EVENT_NANOAPP_ABORTED) { |
| 261 |
isEqual &= (other.getNanoAppAbortCode() == mNanoAppAbortCode); |
| 262 |
} |
| 263 |
if (mEventType == ContextHubManager.EVENT_NANOAPP_MESSAGE) { |
| 264 |
isEqual &= other.getNanoAppMessage().equals(mNanoAppMessage); |
| 265 |
} |
| 266 |
if (mEventType == ContextHubManager.EVENT_CLIENT_AUTHORIZATION) { |
| 267 |
isEqual &= other.getClientAuthorizationState() == mClientAuthorizationState; |
| 268 |
} |
| 269 |
} catch (UnsupportedOperationException e) { |
| 270 |
isEqual = false; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
return isEqual; |
| 276 |
} |
| 277 |
|
| 278 |
private static void hasExtraOrThrow(Intent intent, String extra) { |
| 279 |
if (!intent.hasExtra(extra)) { |
| 280 |
throw new IllegalArgumentException("Intent did not have extra: " + extra); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
private static int getIntExtraOrThrow(Intent intent, String extra) { |
| 285 |
hasExtraOrThrow(intent, extra); |
| 286 |
return intent.getIntExtra(extra, -1 ); |
| 287 |
} |
| 288 |
|
| 289 |
private static long getLongExtraOrThrow(Intent intent, String extra) { |
| 290 |
hasExtraOrThrow(intent, extra); |
| 291 |
return intent.getLongExtra(extra, -1 ); |
| 292 |
} |
| 293 |
} |