Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "MediaAnalyticsItem" |
| 19 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 24 | |
| 25 | #include <binder/Parcel.h> |
| 26 | #include <utils/Errors.h> |
| 27 | #include <utils/Log.h> |
| 28 | #include <utils/Mutex.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 29 | #include <utils/SortedVector.h> |
| 30 | #include <utils/threads.h> |
| 31 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 32 | #include <binder/IServiceManager.h> |
| 33 | #include <media/IMediaAnalyticsService.h> |
| 34 | #include <media/MediaAnalyticsItem.h> |
Ray Essick | 79a89ef | 2017-04-24 15:52:54 -0700 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | #define DEBUG_SERVICEACCESS 0 |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 40 | #define DEBUG_API 0 |
| 41 | #define DEBUG_ALLOCATIONS 0 |
| 42 | |
| 43 | // after this many failed attempts, we stop trying [from this process] and just say that |
| 44 | // the service is off. |
| 45 | #define SVC_TRIES 2 |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 46 | |
| 47 | // the few universal keys we have |
| 48 | const MediaAnalyticsItem::Key MediaAnalyticsItem::kKeyAny = "any"; |
| 49 | const MediaAnalyticsItem::Key MediaAnalyticsItem::kKeyNone = "none"; |
| 50 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 51 | const char * const MediaAnalyticsItem::EnabledProperty = "media.metrics.enabled"; |
| 52 | const char * const MediaAnalyticsItem::EnabledPropertyPersist = "persist.media.metrics.enabled"; |
Ray Essick | d6a6c67 | 2017-01-25 14:28:51 -0800 | [diff] [blame] | 53 | const int MediaAnalyticsItem::EnabledProperty_default = 1; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | // access functions for the class |
| 57 | MediaAnalyticsItem::MediaAnalyticsItem() |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 58 | : mPid(-1), |
| 59 | mUid(-1), |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame] | 60 | mPkgVersionCode(0), |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 61 | mSessionID(MediaAnalyticsItem::SessionIDNone), |
| 62 | mTimestamp(0), |
Ray Essick | 92d23b4 | 2018-01-29 12:10:30 -0800 | [diff] [blame] | 63 | mFinalized(1), |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 64 | mPropCount(0), mPropSize(0), mProps(NULL) |
| 65 | { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 66 | mKey = MediaAnalyticsItem::kKeyNone; |
| 67 | } |
| 68 | |
| 69 | MediaAnalyticsItem::MediaAnalyticsItem(MediaAnalyticsItem::Key key) |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 70 | : mPid(-1), |
| 71 | mUid(-1), |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame] | 72 | mPkgVersionCode(0), |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 73 | mSessionID(MediaAnalyticsItem::SessionIDNone), |
| 74 | mTimestamp(0), |
Ray Essick | 92d23b4 | 2018-01-29 12:10:30 -0800 | [diff] [blame] | 75 | mFinalized(1), |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 76 | mPropCount(0), mPropSize(0), mProps(NULL) |
| 77 | { |
| 78 | if (DEBUG_ALLOCATIONS) { |
| 79 | ALOGD("Allocate MediaAnalyticsItem @ %p", this); |
| 80 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 81 | mKey = key; |
| 82 | } |
| 83 | |
| 84 | MediaAnalyticsItem::~MediaAnalyticsItem() { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 85 | if (DEBUG_ALLOCATIONS) { |
| 86 | ALOGD("Destroy MediaAnalyticsItem @ %p", this); |
| 87 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 88 | clear(); |
| 89 | } |
| 90 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 91 | void MediaAnalyticsItem::clear() { |
| 92 | |
| 93 | // clean allocated storage from key |
| 94 | mKey.clear(); |
| 95 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 96 | // clean various major parameters |
| 97 | mSessionID = MediaAnalyticsItem::SessionIDNone; |
| 98 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 99 | // clean attributes |
| 100 | // contents of the attributes |
Ray Essick | 58f5873 | 2017-10-02 10:56:18 -0700 | [diff] [blame] | 101 | for (size_t i = 0 ; i < mPropCount; i++ ) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 102 | clearProp(&mProps[i]); |
| 103 | } |
| 104 | // the attribute records themselves |
| 105 | if (mProps != NULL) { |
| 106 | free(mProps); |
| 107 | mProps = NULL; |
| 108 | } |
| 109 | mPropSize = 0; |
| 110 | mPropCount = 0; |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // make a deep copy of myself |
| 116 | MediaAnalyticsItem *MediaAnalyticsItem::dup() { |
| 117 | MediaAnalyticsItem *dst = new MediaAnalyticsItem(this->mKey); |
| 118 | |
| 119 | if (dst != NULL) { |
| 120 | // key as part of constructor |
| 121 | dst->mPid = this->mPid; |
| 122 | dst->mUid = this->mUid; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 123 | dst->mPkgName = this->mPkgName; |
| 124 | dst->mPkgVersionCode = this->mPkgVersionCode; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 125 | dst->mSessionID = this->mSessionID; |
| 126 | dst->mTimestamp = this->mTimestamp; |
| 127 | dst->mFinalized = this->mFinalized; |
| 128 | |
| 129 | // properties aka attributes |
| 130 | dst->growProps(this->mPropCount); |
| 131 | for(size_t i=0;i<mPropCount;i++) { |
| 132 | copyProp(&dst->mProps[i], &this->mProps[i]); |
| 133 | } |
| 134 | dst->mPropCount = this->mPropCount; |
| 135 | } |
| 136 | |
| 137 | return dst; |
| 138 | } |
| 139 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 140 | MediaAnalyticsItem &MediaAnalyticsItem::setSessionID(MediaAnalyticsItem::SessionID_t id) { |
| 141 | mSessionID = id; |
| 142 | return *this; |
| 143 | } |
| 144 | |
| 145 | MediaAnalyticsItem::SessionID_t MediaAnalyticsItem::getSessionID() const { |
| 146 | return mSessionID; |
| 147 | } |
| 148 | |
| 149 | MediaAnalyticsItem::SessionID_t MediaAnalyticsItem::generateSessionID() { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 150 | |
| 151 | if (mSessionID == SessionIDNone) { |
| 152 | // get one from the server |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 153 | MediaAnalyticsItem::SessionID_t newid = SessionIDNone; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 154 | sp<IMediaAnalyticsService> svc = getInstance(); |
| 155 | if (svc != NULL) { |
| 156 | newid = svc->generateUniqueSessionID(); |
| 157 | } |
| 158 | mSessionID = newid; |
| 159 | } |
| 160 | |
| 161 | return mSessionID; |
| 162 | } |
| 163 | |
| 164 | MediaAnalyticsItem &MediaAnalyticsItem::clearSessionID() { |
| 165 | mSessionID = MediaAnalyticsItem::SessionIDNone; |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | MediaAnalyticsItem &MediaAnalyticsItem::setTimestamp(nsecs_t ts) { |
| 170 | mTimestamp = ts; |
| 171 | return *this; |
| 172 | } |
| 173 | |
| 174 | nsecs_t MediaAnalyticsItem::getTimestamp() const { |
| 175 | return mTimestamp; |
| 176 | } |
| 177 | |
| 178 | MediaAnalyticsItem &MediaAnalyticsItem::setPid(pid_t pid) { |
| 179 | mPid = pid; |
| 180 | return *this; |
| 181 | } |
| 182 | |
| 183 | pid_t MediaAnalyticsItem::getPid() const { |
| 184 | return mPid; |
| 185 | } |
| 186 | |
| 187 | MediaAnalyticsItem &MediaAnalyticsItem::setUid(uid_t uid) { |
| 188 | mUid = uid; |
| 189 | return *this; |
| 190 | } |
| 191 | |
| 192 | uid_t MediaAnalyticsItem::getUid() const { |
| 193 | return mUid; |
| 194 | } |
| 195 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 196 | MediaAnalyticsItem &MediaAnalyticsItem::setPkgName(const std::string &pkgName) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 197 | mPkgName = pkgName; |
| 198 | return *this; |
| 199 | } |
| 200 | |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 201 | MediaAnalyticsItem &MediaAnalyticsItem::setPkgVersionCode(int64_t pkgVersionCode) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 202 | mPkgVersionCode = pkgVersionCode; |
| 203 | return *this; |
| 204 | } |
| 205 | |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 206 | int64_t MediaAnalyticsItem::getPkgVersionCode() const { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 207 | return mPkgVersionCode; |
| 208 | } |
| 209 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 210 | // this key is for the overall record -- "codec", "player", "drm", etc |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 211 | MediaAnalyticsItem &MediaAnalyticsItem::setKey(MediaAnalyticsItem::Key key) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 212 | mKey = key; |
| 213 | return *this; |
| 214 | } |
| 215 | |
| 216 | MediaAnalyticsItem::Key MediaAnalyticsItem::getKey() { |
| 217 | return mKey; |
| 218 | } |
| 219 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 220 | // number of attributes we have in this record |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 221 | int32_t MediaAnalyticsItem::count() const { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 222 | return mPropCount; |
| 223 | } |
| 224 | |
| 225 | // find the proper entry in the list |
| 226 | size_t MediaAnalyticsItem::findPropIndex(const char *name, size_t len) |
| 227 | { |
| 228 | size_t i = 0; |
| 229 | for (; i < mPropCount; i++) { |
| 230 | Prop *prop = &mProps[i]; |
| 231 | if (prop->mNameLen != len) { |
| 232 | continue; |
| 233 | } |
| 234 | if (memcmp(name, prop->mName, len) == 0) { |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | return i; |
| 239 | } |
| 240 | |
| 241 | MediaAnalyticsItem::Prop *MediaAnalyticsItem::findProp(const char *name) { |
| 242 | size_t len = strlen(name); |
| 243 | size_t i = findPropIndex(name, len); |
| 244 | if (i < mPropCount) { |
| 245 | return &mProps[i]; |
| 246 | } |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | void MediaAnalyticsItem::Prop::setName(const char *name, size_t len) { |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 251 | free((void *)mName); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 252 | mName = (const char *) malloc(len+1); |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 253 | LOG_ALWAYS_FATAL_IF(mName == NULL, |
| 254 | "failed malloc() for property '%s' (len %zu)", |
| 255 | name, len); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 256 | memcpy ((void *)mName, name, len+1); |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 257 | mNameLen = len; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 260 | // consider this "find-or-allocate". |
| 261 | // caller validates type and uses clearPropValue() accordingly |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 262 | MediaAnalyticsItem::Prop *MediaAnalyticsItem::allocateProp(const char *name) { |
| 263 | size_t len = strlen(name); |
| 264 | size_t i = findPropIndex(name, len); |
| 265 | Prop *prop; |
| 266 | |
| 267 | if (i < mPropCount) { |
| 268 | prop = &mProps[i]; |
| 269 | } else { |
| 270 | if (i == mPropSize) { |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 271 | if (growProps() == false) { |
| 272 | ALOGE("failed allocation for new props"); |
| 273 | return NULL; |
| 274 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 275 | } |
| 276 | i = mPropCount++; |
| 277 | prop = &mProps[i]; |
| 278 | prop->setName(name, len); |
| 279 | } |
| 280 | |
| 281 | return prop; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 284 | // used within the summarizers; return whether property existed |
| 285 | bool MediaAnalyticsItem::removeProp(const char *name) { |
| 286 | size_t len = strlen(name); |
| 287 | size_t i = findPropIndex(name, len); |
| 288 | if (i < mPropCount) { |
| 289 | Prop *prop = &mProps[i]; |
| 290 | clearProp(prop); |
| 291 | if (i != mPropCount-1) { |
| 292 | // in the middle, bring last one down to fill gap |
Ray Essick | 58f5873 | 2017-10-02 10:56:18 -0700 | [diff] [blame] | 293 | copyProp(prop, &mProps[mPropCount-1]); |
| 294 | clearProp(&mProps[mPropCount-1]); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 295 | } |
| 296 | mPropCount--; |
| 297 | return true; |
| 298 | } |
| 299 | return false; |
| 300 | } |
| 301 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 302 | // set the values |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 303 | void MediaAnalyticsItem::setInt32(MediaAnalyticsItem::Attr name, int32_t value) { |
| 304 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 305 | if (prop != NULL) { |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 306 | clearPropValue(prop); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 307 | prop->mType = kTypeInt32; |
| 308 | prop->u.int32Value = value; |
| 309 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 312 | void MediaAnalyticsItem::setInt64(MediaAnalyticsItem::Attr name, int64_t value) { |
| 313 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 314 | if (prop != NULL) { |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 315 | clearPropValue(prop); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 316 | prop->mType = kTypeInt64; |
| 317 | prop->u.int64Value = value; |
| 318 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 321 | void MediaAnalyticsItem::setDouble(MediaAnalyticsItem::Attr name, double value) { |
| 322 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 323 | if (prop != NULL) { |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 324 | clearPropValue(prop); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 325 | prop->mType = kTypeDouble; |
| 326 | prop->u.doubleValue = value; |
| 327 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 330 | void MediaAnalyticsItem::setCString(MediaAnalyticsItem::Attr name, const char *value) { |
| 331 | |
| 332 | Prop *prop = allocateProp(name); |
| 333 | // any old value will be gone |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 334 | if (prop != NULL) { |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 335 | clearPropValue(prop); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 336 | prop->mType = kTypeCString; |
| 337 | prop->u.CStringValue = strdup(value); |
| 338 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 341 | void MediaAnalyticsItem::setRate(MediaAnalyticsItem::Attr name, int64_t count, int64_t duration) { |
| 342 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 343 | if (prop != NULL) { |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 344 | clearPropValue(prop); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 345 | prop->mType = kTypeRate; |
| 346 | prop->u.rate.count = count; |
| 347 | prop->u.rate.duration = duration; |
| 348 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 352 | // find/add/set fused into a single operation |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 353 | void MediaAnalyticsItem::addInt32(MediaAnalyticsItem::Attr name, int32_t value) { |
| 354 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 355 | if (prop == NULL) { |
| 356 | return; |
| 357 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 358 | switch (prop->mType) { |
| 359 | case kTypeInt32: |
| 360 | prop->u.int32Value += value; |
| 361 | break; |
| 362 | default: |
| 363 | clearPropValue(prop); |
| 364 | prop->mType = kTypeInt32; |
| 365 | prop->u.int32Value = value; |
| 366 | break; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 367 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 370 | void MediaAnalyticsItem::addInt64(MediaAnalyticsItem::Attr name, int64_t value) { |
| 371 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 372 | if (prop == NULL) { |
| 373 | return; |
| 374 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 375 | switch (prop->mType) { |
| 376 | case kTypeInt64: |
| 377 | prop->u.int64Value += value; |
| 378 | break; |
| 379 | default: |
| 380 | clearPropValue(prop); |
| 381 | prop->mType = kTypeInt64; |
| 382 | prop->u.int64Value = value; |
| 383 | break; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 384 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 387 | void MediaAnalyticsItem::addRate(MediaAnalyticsItem::Attr name, int64_t count, int64_t duration) { |
| 388 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 389 | if (prop == NULL) { |
| 390 | return; |
| 391 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 392 | switch (prop->mType) { |
| 393 | case kTypeRate: |
| 394 | prop->u.rate.count += count; |
| 395 | prop->u.rate.duration += duration; |
| 396 | break; |
| 397 | default: |
| 398 | clearPropValue(prop); |
| 399 | prop->mType = kTypeRate; |
| 400 | prop->u.rate.count = count; |
| 401 | prop->u.rate.duration = duration; |
| 402 | break; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 403 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void MediaAnalyticsItem::addDouble(MediaAnalyticsItem::Attr name, double value) { |
| 407 | Prop *prop = allocateProp(name); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 408 | if (prop == NULL) { |
| 409 | return; |
| 410 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 411 | switch (prop->mType) { |
| 412 | case kTypeDouble: |
| 413 | prop->u.doubleValue += value; |
| 414 | break; |
| 415 | default: |
| 416 | clearPropValue(prop); |
| 417 | prop->mType = kTypeDouble; |
| 418 | prop->u.doubleValue = value; |
| 419 | break; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 420 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // find & extract values |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 424 | bool MediaAnalyticsItem::getInt32(MediaAnalyticsItem::Attr name, int32_t *value) { |
| 425 | Prop *prop = findProp(name); |
| 426 | if (prop == NULL || prop->mType != kTypeInt32) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 427 | return false; |
| 428 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 429 | if (value != NULL) { |
| 430 | *value = prop->u.int32Value; |
| 431 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 432 | return true; |
| 433 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 434 | |
| 435 | bool MediaAnalyticsItem::getInt64(MediaAnalyticsItem::Attr name, int64_t *value) { |
| 436 | Prop *prop = findProp(name); |
| 437 | if (prop == NULL || prop->mType != kTypeInt64) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 438 | return false; |
| 439 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 440 | if (value != NULL) { |
| 441 | *value = prop->u.int64Value; |
| 442 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 443 | return true; |
| 444 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 445 | |
| 446 | bool MediaAnalyticsItem::getRate(MediaAnalyticsItem::Attr name, int64_t *count, int64_t *duration, double *rate) { |
| 447 | Prop *prop = findProp(name); |
| 448 | if (prop == NULL || prop->mType != kTypeRate) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 449 | return false; |
| 450 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 451 | if (count != NULL) { |
| 452 | *count = prop->u.rate.count; |
| 453 | } |
| 454 | if (duration != NULL) { |
| 455 | *duration = prop->u.rate.duration; |
| 456 | } |
| 457 | if (rate != NULL) { |
| 458 | double r = 0.0; |
| 459 | if (prop->u.rate.duration != 0) { |
| 460 | r = prop->u.rate.count / (double) prop->u.rate.duration; |
| 461 | } |
| 462 | *rate = r; |
| 463 | } |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | bool MediaAnalyticsItem::getDouble(MediaAnalyticsItem::Attr name, double *value) { |
| 468 | Prop *prop = findProp(name); |
| 469 | if (prop == NULL || prop->mType != kTypeDouble) { |
| 470 | return false; |
| 471 | } |
| 472 | if (value != NULL) { |
| 473 | *value = prop->u.doubleValue; |
| 474 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 475 | return true; |
| 476 | } |
| 477 | |
| 478 | // caller responsible for the returned string |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 479 | bool MediaAnalyticsItem::getCString(MediaAnalyticsItem::Attr name, char **value) { |
| 480 | Prop *prop = findProp(name); |
Ray Essick | 9db7b81 | 2018-11-15 12:42:19 -0800 | [diff] [blame] | 481 | if (prop == NULL || prop->mType != kTypeCString) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 482 | return false; |
| 483 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 484 | if (value != NULL) { |
| 485 | *value = strdup(prop->u.CStringValue); |
| 486 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 487 | return true; |
| 488 | } |
| 489 | |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 490 | bool MediaAnalyticsItem::getString(MediaAnalyticsItem::Attr name, std::string *value) { |
| 491 | Prop *prop = findProp(name); |
| 492 | if (prop == NULL || prop->mType != kTypeCString) { |
| 493 | return false; |
| 494 | } |
| 495 | if (value != NULL) { |
| 496 | // std::string makes a copy for us |
| 497 | *value = prop->u.CStringValue; |
| 498 | } |
| 499 | return true; |
| 500 | } |
| 501 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 502 | // remove indicated keys and their values |
| 503 | // return value is # keys removed |
| 504 | int32_t MediaAnalyticsItem::filter(int n, MediaAnalyticsItem::Attr attrs[]) { |
| 505 | int zapped = 0; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 506 | if (attrs == NULL || n <= 0) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 507 | return -1; |
| 508 | } |
| 509 | for (ssize_t i = 0 ; i < n ; i++) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 510 | const char *name = attrs[i]; |
| 511 | size_t len = strlen(name); |
| 512 | size_t j = findPropIndex(name, len); |
| 513 | if (j >= mPropCount) { |
| 514 | // not there |
| 515 | continue; |
| 516 | } else if (j+1 == mPropCount) { |
| 517 | // last one, shorten |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 518 | zapped++; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 519 | clearProp(&mProps[j]); |
| 520 | mPropCount--; |
| 521 | } else { |
| 522 | // in the middle, bring last one down and shorten |
| 523 | zapped++; |
| 524 | clearProp(&mProps[j]); |
| 525 | mProps[j] = mProps[mPropCount-1]; |
| 526 | mPropCount--; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | return zapped; |
| 530 | } |
| 531 | |
| 532 | // remove any keys NOT in the provided list |
| 533 | // return value is # keys removed |
| 534 | int32_t MediaAnalyticsItem::filterNot(int n, MediaAnalyticsItem::Attr attrs[]) { |
| 535 | int zapped = 0; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 536 | if (attrs == NULL || n <= 0) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 537 | return -1; |
| 538 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 539 | for (ssize_t i = mPropCount-1 ; i >=0 ; i--) { |
| 540 | Prop *prop = &mProps[i]; |
| 541 | for (ssize_t j = 0; j < n ; j++) { |
| 542 | if (strcmp(prop->mName, attrs[j]) == 0) { |
| 543 | clearProp(prop); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 544 | zapped++; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 545 | if (i != (ssize_t)(mPropCount-1)) { |
| 546 | *prop = mProps[mPropCount-1]; |
| 547 | } |
| 548 | initProp(&mProps[mPropCount-1]); |
| 549 | mPropCount--; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | return zapped; |
| 555 | } |
| 556 | |
| 557 | // remove a single key |
| 558 | // return value is 0 (not found) or 1 (found and removed) |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 559 | int32_t MediaAnalyticsItem::filter(MediaAnalyticsItem::Attr name) { |
| 560 | return filter(1, &name); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 563 | // handle individual items/properties stored within the class |
| 564 | // |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 565 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 566 | void MediaAnalyticsItem::initProp(Prop *prop) { |
| 567 | if (prop != NULL) { |
| 568 | prop->mName = NULL; |
| 569 | prop->mNameLen = 0; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 570 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 571 | prop->mType = kTypeNone; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 572 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void MediaAnalyticsItem::clearProp(Prop *prop) |
| 576 | { |
| 577 | if (prop != NULL) { |
| 578 | if (prop->mName != NULL) { |
| 579 | free((void *)prop->mName); |
| 580 | prop->mName = NULL; |
| 581 | prop->mNameLen = 0; |
| 582 | } |
| 583 | |
| 584 | clearPropValue(prop); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | void MediaAnalyticsItem::clearPropValue(Prop *prop) |
| 589 | { |
| 590 | if (prop != NULL) { |
| 591 | if (prop->mType == kTypeCString && prop->u.CStringValue != NULL) { |
| 592 | free(prop->u.CStringValue); |
| 593 | prop->u.CStringValue = NULL; |
| 594 | } |
| 595 | prop->mType = kTypeNone; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void MediaAnalyticsItem::copyProp(Prop *dst, const Prop *src) |
| 600 | { |
| 601 | // get rid of any pointers in the dst |
| 602 | clearProp(dst); |
| 603 | |
| 604 | *dst = *src; |
| 605 | |
| 606 | // fix any pointers that we blindly copied, so we have our own copies |
| 607 | if (dst->mName) { |
| 608 | void *p = malloc(dst->mNameLen + 1); |
Ray Essick | 9ce18f7 | 2018-05-07 15:54:30 -0700 | [diff] [blame] | 609 | LOG_ALWAYS_FATAL_IF(p == NULL, |
| 610 | "failed malloc() duping property '%s' (len %zu)", |
| 611 | dst->mName, dst->mNameLen); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 612 | memcpy (p, src->mName, dst->mNameLen + 1); |
| 613 | dst->mName = (const char *) p; |
| 614 | } |
| 615 | if (dst->mType == kTypeCString) { |
| 616 | dst->u.CStringValue = strdup(src->u.CStringValue); |
| 617 | } |
| 618 | } |
| 619 | |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 620 | bool MediaAnalyticsItem::growProps(int increment) |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 621 | { |
| 622 | if (increment <= 0) { |
| 623 | increment = kGrowProps; |
| 624 | } |
| 625 | int nsize = mPropSize + increment; |
| 626 | Prop *ni = (Prop *)realloc(mProps, sizeof(Prop) * nsize); |
| 627 | |
| 628 | if (ni != NULL) { |
| 629 | for (int i = mPropSize; i < nsize; i++) { |
| 630 | initProp(&ni[i]); |
| 631 | } |
| 632 | mProps = ni; |
| 633 | mPropSize = nsize; |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 634 | return true; |
| 635 | } else { |
| 636 | ALOGW("MediaAnalyticsItem::growProps fails"); |
| 637 | return false; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 638 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Parcel / serialize things for binder calls |
| 642 | // |
| 643 | |
| 644 | int32_t MediaAnalyticsItem::readFromParcel(const Parcel& data) { |
| 645 | // into 'this' object |
| 646 | // .. we make a copy of the string to put away. |
| 647 | mKey = data.readCString(); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 648 | mPid = data.readInt32(); |
| 649 | mUid = data.readInt32(); |
| 650 | mPkgName = data.readCString(); |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 651 | mPkgVersionCode = data.readInt64(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 652 | mSessionID = data.readInt64(); |
Ray Essick | 92d23b4 | 2018-01-29 12:10:30 -0800 | [diff] [blame] | 653 | // We no longer pay attention to user setting of finalized, BUT it's |
| 654 | // still part of the wire packet -- so read & discard. |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 655 | mFinalized = data.readInt32(); |
Ray Essick | 92d23b4 | 2018-01-29 12:10:30 -0800 | [diff] [blame] | 656 | mFinalized = 1; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 657 | mTimestamp = data.readInt64(); |
| 658 | |
| 659 | int count = data.readInt32(); |
| 660 | for (int i = 0; i < count ; i++) { |
| 661 | MediaAnalyticsItem::Attr attr = data.readCString(); |
| 662 | int32_t ztype = data.readInt32(); |
| 663 | switch (ztype) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 664 | case MediaAnalyticsItem::kTypeInt32: |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 665 | setInt32(attr, data.readInt32()); |
| 666 | break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 667 | case MediaAnalyticsItem::kTypeInt64: |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 668 | setInt64(attr, data.readInt64()); |
| 669 | break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 670 | case MediaAnalyticsItem::kTypeDouble: |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 671 | setDouble(attr, data.readDouble()); |
| 672 | break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 673 | case MediaAnalyticsItem::kTypeCString: |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 674 | setCString(attr, data.readCString()); |
| 675 | break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 676 | case MediaAnalyticsItem::kTypeRate: |
| 677 | { |
| 678 | int64_t count = data.readInt64(); |
| 679 | int64_t duration = data.readInt64(); |
| 680 | setRate(attr, count, duration); |
| 681 | } |
| 682 | break; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 683 | default: |
| 684 | ALOGE("reading bad item type: %d, idx %d", |
| 685 | ztype, i); |
| 686 | return -1; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | int32_t MediaAnalyticsItem::writeToParcel(Parcel *data) { |
| 694 | if (data == NULL) return -1; |
| 695 | |
| 696 | |
| 697 | data->writeCString(mKey.c_str()); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 698 | data->writeInt32(mPid); |
| 699 | data->writeInt32(mUid); |
| 700 | data->writeCString(mPkgName.c_str()); |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 701 | data->writeInt64(mPkgVersionCode); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 702 | data->writeInt64(mSessionID); |
| 703 | data->writeInt32(mFinalized); |
| 704 | data->writeInt64(mTimestamp); |
| 705 | |
| 706 | // set of items |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 707 | int count = mPropCount; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 708 | data->writeInt32(count); |
| 709 | for (int i = 0 ; i < count; i++ ) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 710 | Prop *prop = &mProps[i]; |
| 711 | data->writeCString(prop->mName); |
| 712 | data->writeInt32(prop->mType); |
| 713 | switch (prop->mType) { |
| 714 | case MediaAnalyticsItem::kTypeInt32: |
| 715 | data->writeInt32(prop->u.int32Value); |
| 716 | break; |
| 717 | case MediaAnalyticsItem::kTypeInt64: |
| 718 | data->writeInt64(prop->u.int64Value); |
| 719 | break; |
| 720 | case MediaAnalyticsItem::kTypeDouble: |
| 721 | data->writeDouble(prop->u.doubleValue); |
| 722 | break; |
| 723 | case MediaAnalyticsItem::kTypeRate: |
| 724 | data->writeInt64(prop->u.rate.count); |
| 725 | data->writeInt64(prop->u.rate.duration); |
| 726 | break; |
| 727 | case MediaAnalyticsItem::kTypeCString: |
| 728 | data->writeCString(prop->u.CStringValue); |
| 729 | break; |
| 730 | default: |
| 731 | ALOGE("found bad Prop type: %d, idx %d, name %s", |
| 732 | prop->mType, i, prop->mName); |
| 733 | break; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | |
Ray Essick | 2014732 | 2018-11-17 09:08:39 -0800 | [diff] [blame] | 741 | const char *MediaAnalyticsItem::toCString() { |
| 742 | return toCString(PROTO_LAST); |
| 743 | } |
| 744 | |
| 745 | const char * MediaAnalyticsItem::toCString(int version) { |
| 746 | std::string val = toString(version); |
| 747 | return strdup(val.c_str()); |
| 748 | } |
| 749 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 750 | std::string MediaAnalyticsItem::toString() { |
Ray Essick | 5b77bd2 | 2018-01-23 16:11:06 -0800 | [diff] [blame] | 751 | return toString(PROTO_LAST); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 752 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 753 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 754 | std::string MediaAnalyticsItem::toString(int version) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 755 | |
| 756 | // v0 : released with 'o' |
| 757 | // v1 : bug fix (missing pid/finalized separator), |
| 758 | // adds apk name, apk version code |
| 759 | |
| 760 | if (version <= PROTO_FIRST) { |
| 761 | // default to original v0 format, until proper parsers are in place |
| 762 | version = PROTO_V0; |
| 763 | } else if (version > PROTO_LAST) { |
| 764 | version = PROTO_LAST; |
| 765 | } |
| 766 | |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 767 | std::string result; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 768 | char buffer[512]; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 769 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 770 | if (version == PROTO_V0) { |
| 771 | result = "("; |
| 772 | } else { |
| 773 | snprintf(buffer, sizeof(buffer), "[%d:", version); |
| 774 | result.append(buffer); |
| 775 | } |
| 776 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 777 | // same order as we spill into the parcel, although not required |
| 778 | // key+session are our primary matching criteria |
| 779 | result.append(mKey.c_str()); |
| 780 | result.append(":"); |
| 781 | snprintf(buffer, sizeof(buffer), "%" PRId64 ":", mSessionID); |
| 782 | result.append(buffer); |
| 783 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 784 | snprintf(buffer, sizeof(buffer), "%d:", mUid); |
| 785 | result.append(buffer); |
| 786 | |
| 787 | if (version >= PROTO_V1) { |
| 788 | result.append(mPkgName); |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 789 | snprintf(buffer, sizeof(buffer), ":%" PRId64 ":", mPkgVersionCode); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 790 | result.append(buffer); |
| 791 | } |
| 792 | |
| 793 | // in 'o' (v1) , the separator between pid and finalized was omitted |
| 794 | if (version <= PROTO_V0) { |
| 795 | snprintf(buffer, sizeof(buffer), "%d", mPid); |
| 796 | } else { |
| 797 | snprintf(buffer, sizeof(buffer), "%d:", mPid); |
| 798 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 799 | result.append(buffer); |
| 800 | |
| 801 | snprintf(buffer, sizeof(buffer), "%d:", mFinalized); |
| 802 | result.append(buffer); |
| 803 | snprintf(buffer, sizeof(buffer), "%" PRId64 ":", mTimestamp); |
| 804 | result.append(buffer); |
| 805 | |
| 806 | // set of items |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 807 | int count = mPropCount; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 808 | snprintf(buffer, sizeof(buffer), "%d:", count); |
| 809 | result.append(buffer); |
| 810 | for (int i = 0 ; i < count; i++ ) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 811 | Prop *prop = &mProps[i]; |
| 812 | switch (prop->mType) { |
| 813 | case MediaAnalyticsItem::kTypeInt32: |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 814 | snprintf(buffer,sizeof(buffer), |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 815 | "%s=%d:", prop->mName, prop->u.int32Value); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 816 | break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 817 | case MediaAnalyticsItem::kTypeInt64: |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 818 | snprintf(buffer,sizeof(buffer), |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 819 | "%s=%" PRId64 ":", prop->mName, prop->u.int64Value); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 820 | break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 821 | case MediaAnalyticsItem::kTypeDouble: |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 822 | snprintf(buffer,sizeof(buffer), |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 823 | "%s=%e:", prop->mName, prop->u.doubleValue); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 824 | break; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 825 | case MediaAnalyticsItem::kTypeRate: |
| 826 | snprintf(buffer,sizeof(buffer), |
| 827 | "%s=%" PRId64 "/%" PRId64 ":", prop->mName, |
| 828 | prop->u.rate.count, prop->u.rate.duration); |
| 829 | break; |
| 830 | case MediaAnalyticsItem::kTypeCString: |
| 831 | snprintf(buffer,sizeof(buffer), "%s=", prop->mName); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 832 | result.append(buffer); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 833 | // XXX: sanitize string for ':' '=' |
| 834 | result.append(prop->u.CStringValue); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 835 | buffer[0] = ':'; |
| 836 | buffer[1] = '\0'; |
| 837 | break; |
| 838 | default: |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 839 | ALOGE("to_String bad item type: %d for %s", |
| 840 | prop->mType, prop->mName); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 841 | break; |
| 842 | } |
| 843 | result.append(buffer); |
| 844 | } |
| 845 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 846 | if (version == PROTO_V0) { |
| 847 | result.append(")"); |
| 848 | } else { |
| 849 | result.append("]"); |
| 850 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 851 | |
| 852 | return result; |
| 853 | } |
| 854 | |
| 855 | // for the lazy, we offer methods that finds the service and |
| 856 | // calls the appropriate daemon |
| 857 | bool MediaAnalyticsItem::selfrecord() { |
| 858 | return selfrecord(false); |
| 859 | } |
| 860 | |
| 861 | bool MediaAnalyticsItem::selfrecord(bool forcenew) { |
| 862 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 863 | if (DEBUG_API) { |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 864 | std::string p = this->toString(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 865 | ALOGD("selfrecord of: %s [forcenew=%d]", p.c_str(), forcenew); |
| 866 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 867 | |
| 868 | sp<IMediaAnalyticsService> svc = getInstance(); |
| 869 | |
| 870 | if (svc != NULL) { |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 871 | MediaAnalyticsItem::SessionID_t newid = svc->submit(this, forcenew); |
| 872 | if (newid == SessionIDInvalid) { |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 873 | std::string p = this->toString(); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 874 | ALOGW("Failed to record: %s [forcenew=%d]", p.c_str(), forcenew); |
| 875 | return false; |
| 876 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 877 | return true; |
| 878 | } else { |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 879 | std::string p = this->toString(); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 880 | ALOGW("Unable to record: %s [forcenew=%d]", p.c_str(), forcenew); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | // get a connection we can reuse for most of our lifetime |
| 886 | // static |
| 887 | sp<IMediaAnalyticsService> MediaAnalyticsItem::sAnalyticsService; |
| 888 | static Mutex sInitMutex; |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 889 | static int remainingBindAttempts = SVC_TRIES; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 890 | |
| 891 | //static |
| 892 | bool MediaAnalyticsItem::isEnabled() { |
| 893 | int enabled = property_get_int32(MediaAnalyticsItem::EnabledProperty, -1); |
| 894 | |
| 895 | if (enabled == -1) { |
| 896 | enabled = property_get_int32(MediaAnalyticsItem::EnabledPropertyPersist, -1); |
| 897 | } |
| 898 | if (enabled == -1) { |
| 899 | enabled = MediaAnalyticsItem::EnabledProperty_default; |
| 900 | } |
| 901 | if (enabled <= 0) { |
| 902 | return false; |
| 903 | } |
| 904 | return true; |
| 905 | } |
| 906 | |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 907 | |
| 908 | // monitor health of our connection to the metrics service |
| 909 | class MediaMetricsDeathNotifier : public IBinder::DeathRecipient { |
| 910 | virtual void binderDied(const wp<IBinder> &) { |
| 911 | ALOGW("Reacquire service connection on next request"); |
| 912 | MediaAnalyticsItem::dropInstance(); |
| 913 | } |
| 914 | }; |
| 915 | |
| 916 | static sp<MediaMetricsDeathNotifier> sNotifier = NULL; |
| 917 | |
| 918 | // static |
| 919 | void MediaAnalyticsItem::dropInstance() { |
| 920 | Mutex::Autolock _l(sInitMutex); |
| 921 | remainingBindAttempts = SVC_TRIES; |
| 922 | sAnalyticsService = NULL; |
| 923 | } |
| 924 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 925 | //static |
| 926 | sp<IMediaAnalyticsService> MediaAnalyticsItem::getInstance() { |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 927 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 928 | static const char *servicename = "media.metrics"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 929 | int enabled = isEnabled(); |
| 930 | |
| 931 | if (enabled == false) { |
| 932 | if (DEBUG_SERVICEACCESS) { |
| 933 | ALOGD("disabled"); |
| 934 | } |
| 935 | return NULL; |
| 936 | } |
| 937 | |
Ray Essick | 79a89ef | 2017-04-24 15:52:54 -0700 | [diff] [blame] | 938 | // completely skip logging from certain UIDs. We do this here |
| 939 | // to avoid the multi-second timeouts while we learn that |
| 940 | // sepolicy will not let us find the service. |
| 941 | // We do this only for a select set of UIDs |
| 942 | // The sepolicy protection is still in place, we just want a faster |
| 943 | // response from this specific, small set of uids. |
| 944 | { |
| 945 | uid_t uid = getuid(); |
| 946 | switch (uid) { |
| 947 | case AID_RADIO: // telephony subsystem, RIL |
| 948 | return NULL; |
| 949 | break; |
| 950 | default: |
| 951 | // let sepolicy deny access if appropriate |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 956 | { |
| 957 | Mutex::Autolock _l(sInitMutex); |
| 958 | const char *badness = ""; |
| 959 | |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 960 | // think of remainingBindAttempts as telling us whether service==NULL because |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 961 | // (1) we haven't tried to initialize it yet |
| 962 | // (2) we've tried to initialize it, but failed. |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 963 | if (sAnalyticsService == NULL && remainingBindAttempts > 0) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 964 | sp<IServiceManager> sm = defaultServiceManager(); |
| 965 | if (sm != NULL) { |
| 966 | sp<IBinder> binder = sm->getService(String16(servicename)); |
| 967 | if (binder != NULL) { |
| 968 | sAnalyticsService = interface_cast<IMediaAnalyticsService>(binder); |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 969 | if (sNotifier != NULL) { |
| 970 | sNotifier = NULL; |
| 971 | } |
| 972 | sNotifier = new MediaMetricsDeathNotifier(); |
| 973 | binder->linkToDeath(sNotifier); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 974 | } else { |
| 975 | badness = "did not find service"; |
| 976 | } |
| 977 | } else { |
| 978 | badness = "No Service Manager access"; |
| 979 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 980 | |
| 981 | if (sAnalyticsService == NULL) { |
Ray Essick | 2ab3c43 | 2017-10-02 09:29:49 -0700 | [diff] [blame] | 982 | if (remainingBindAttempts > 0) { |
| 983 | remainingBindAttempts--; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 984 | } |
| 985 | if (DEBUG_SERVICEACCESS) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 986 | ALOGD("Unable to bind to service %s: %s", servicename, badness); |
| 987 | } |
| 988 | } |
| 989 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 990 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 991 | return sAnalyticsService; |
| 992 | } |
| 993 | } |
| 994 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 995 | // merge the info from 'incoming' into this record. |
| 996 | // we finish with a union of this+incoming and special handling for collisions |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 997 | bool MediaAnalyticsItem::merge(MediaAnalyticsItem *incoming) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 998 | |
| 999 | // if I don't have key or session id, take them from incoming |
| 1000 | // 'this' should never be missing both of them... |
| 1001 | if (mKey.empty()) { |
| 1002 | mKey = incoming->mKey; |
| 1003 | } else if (mSessionID == 0) { |
| 1004 | mSessionID = incoming->mSessionID; |
| 1005 | } |
| 1006 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1007 | // for each attribute from 'incoming', resolve appropriately |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 1008 | int nattr = incoming->mPropCount; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1009 | for (int i = 0 ; i < nattr; i++ ) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 1010 | Prop *iprop = &incoming->mProps[i]; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 1011 | const char *p = iprop->mName; |
| 1012 | size_t len = strlen(p); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 1013 | |
| 1014 | // should ignore a zero length name... |
| 1015 | if (len == 0) { |
| 1016 | continue; |
| 1017 | } |
| 1018 | |
| 1019 | Prop *oprop = findProp(iprop->mName); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1020 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 1021 | if (oprop == NULL) { |
| 1022 | // no oprop, so we insert the new one |
| 1023 | oprop = allocateProp(p); |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 1024 | if (oprop != NULL) { |
| 1025 | copyProp(oprop, iprop); |
| 1026 | } else { |
| 1027 | ALOGW("dropped property '%s'", iprop->mName); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 1028 | } |
Ray Essick | 9bb7e3b | 2017-10-23 13:01:48 -0700 | [diff] [blame] | 1029 | } else { |
| 1030 | copyProp(oprop, iprop); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | // not sure when we'd return false... |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
| 1038 | } // namespace android |
| 1039 | |