| 1 | /* |
| 2 | * Copyright (C) 2020 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "NativePowerManagerTest" |
| 19 | |
| 20 | #include "jni.h" |
| 21 | |
| 22 | #include <android_util_Binder.h> |
| 23 | #include <binder/IServiceManager.h> |
| 24 | #include <nativehelper/JNIHelp.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include <android/os/IPowerManager.h> |
| 28 | #include <android/WorkSource.h> |
| 29 | #include <android/PowerSaveState.h> |
| 30 | #include <android/BatterySaverPolicyConfig.h> |
| 31 | #include <android-base/stringprintf.h> |
| 32 | #include <android-base/strings.h> |
| 33 | |
| 34 | using namespace android::os; |
| 35 | using android::base::StringPrintf; |
| 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | #define FIND_CLASS(var, className) \ |
| 40 | var = env->FindClass(className); \ |
| 41 | LOG_FATAL_IF(!(var), "Unable to find class %s", className); |
| 42 | |
| 43 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 44 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 45 | LOG_FATAL_IF(!(var), "Unable to find field %s", fieldName); |
| 46 | |
| 47 | #define GET_STATIC_METHOD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 48 | var = env->GetStaticMethodID(clazz, fieldName, fieldDescriptor); \ |
| 49 | LOG_FATAL_IF(!(var), "Unable to find method %s", fieldName); |
| 50 | |
| 51 | static jclass gParcelClazz; |
| 52 | static jfieldID gParcelDataFieldID; |
| 53 | static jmethodID gParcelObtainMethodID; |
| 54 | static struct BatterySaverPolicyConfigFieldId { |
| 55 | jfieldID adjustBrightnessFactor; |
| 56 | jfieldID advertiseIsEnabled; |
| 57 | jfieldID deferFullBackup; |
| 58 | jfieldID deferKeyValueBackup; |
| 59 | jfieldID deviceSpecificSettings; |
| 60 | jfieldID disableAnimation; |
| 61 | jfieldID disableAod; |
| 62 | jfieldID disableLaunchBoost; |
| 63 | jfieldID disableOptionalSensors; |
| 64 | jfieldID disableVibration; |
| 65 | jfieldID enableAdjustBrightness; |
| 66 | jfieldID enableDataSaver; |
| 67 | jfieldID enableFirewall; |
| 68 | jfieldID enableNightMode; |
| 69 | jfieldID enableQuickDoze; |
| 70 | jfieldID forceAllAppsStandby; |
| 71 | jfieldID forceBackgroundCheck; |
| 72 | jfieldID locationMode; |
| 73 | jfieldID soundTriggerMode; |
| 74 | } gBSPCFieldIds; |
| 75 | |
| 76 | static jobject nativeObtainParcel(JNIEnv* env) { |
| 77 | jobject parcel = env->CallStaticObjectMethod(gParcelClazz, gParcelObtainMethodID); |
| 78 | if (parcel == nullptr) { |
| 79 | jniThrowException(env, "java/lang/IllegalArgumentException", "Obtain parcel failed."); |
| 80 | } |
| 81 | return parcel; |
| 82 | } |
| 83 | |
| 84 | static Parcel* nativeGetParcelData(JNIEnv* env, jobject obj) { |
| 85 | Parcel* parcel = reinterpret_cast<Parcel*>(env->GetLongField(obj, gParcelDataFieldID)); |
| 86 | if (parcel && parcel->objectsCount() != 0) { |
| 87 | jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid parcel object."); |
| 88 | } |
| 89 | parcel->setDataPosition(0); |
| 90 | return parcel; |
| 91 | } |
| 92 | |
| 93 | static jobject nativeObtainWorkSourceParcel(JNIEnv* env, jobject /* obj */, jintArray uidArray, |
| 94 | jobjectArray nameArray) { |
| 95 | std::vector<int32_t> uids; |
| 96 | std::optional<std::vector<std::optional<String16>>> names = std::nullopt; |
| 97 | |
| 98 | if (uidArray != nullptr) { |
| 99 | jint *ptr = env->GetIntArrayElements(uidArray, 0); |
| 100 | for (jint i = 0; i < env->GetArrayLength(uidArray); i++) { |
| 101 | uids.push_back(static_cast<int32_t>(ptr[i])); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (nameArray != nullptr) { |
| 106 | std::vector<std::optional<String16>> namesVec; |
| 107 | for (jint i = 0; i < env->GetArrayLength(nameArray); i++) { |
| 108 | jstring string = (jstring) (env->GetObjectArrayElement(nameArray, i)); |
| 109 | const char *rawString = env->GetStringUTFChars(string, 0); |
| 110 | namesVec.push_back(std::make_optional<String16>(String16(rawString))); |
| 111 | } |
| 112 | names = std::make_optional(std::move(namesVec)); |
| 113 | } |
| 114 | |
| 115 | WorkSource ws = WorkSource(uids, names); |
| 116 | jobject wsParcel = nativeObtainParcel(env); |
| 117 | Parcel* parcel = nativeGetParcelData(env, wsParcel); |
| 118 | status_t err = ws.writeToParcel(parcel); |
| 119 | if (err != OK) { |
| 120 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 121 | StringPrintf("WorkSource writeToParcel failed %d", err).c_str()); |
| 122 | } |
| 123 | parcel->setDataPosition(0); |
| 124 | return wsParcel; |
| 125 | } |
| 126 | |
| 127 | static void nativeUnparcelAndVerifyWorkSource(JNIEnv* env, jobject /* obj */, jobject wsParcel, |
| 128 | jintArray uidArray, jobjectArray nameArray) { |
| 129 | WorkSource ws = {}; |
| 130 | Parcel* parcel = nativeGetParcelData(env, wsParcel); |
| 131 | |
| 132 | status_t err = ws.readFromParcel(parcel); |
| 133 | if (err != OK) { |
| 134 | ALOGE("WorkSource writeToParcel failed %d", err); |
| 135 | } |
| 136 | |
| 137 | // Now we have a native WorkSource object, verify it. |
| 138 | if (uidArray != nullptr) { |
| 139 | jint *ptr = env->GetIntArrayElements(uidArray, 0); |
| 140 | for (jint i = 0; i < env->GetArrayLength(uidArray); i++) { |
| 141 | if (ws.getUids().at(i) != static_cast<int32_t>(ptr[i])) { |
| 142 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 143 | StringPrintf("WorkSource uid not equal %d %d", |
| 144 | ws.getUids().at(i), static_cast<int32_t>(ptr[i])).c_str()); |
| 145 | } |
| 146 | } |
| 147 | } else { |
| 148 | if (ws.getUids().size() != 0) { |
| 149 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 150 | StringPrintf("WorkSource parcel size not 0").c_str()); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (nameArray != nullptr) { |
| 155 | std::vector<std::optional<String16>> namesVec; |
| 156 | for (jint i = 0; i < env->GetArrayLength(nameArray); i++) { |
| 157 | jstring string = (jstring) (env->GetObjectArrayElement(nameArray, i)); |
| 158 | const char *rawString = env->GetStringUTFChars(string, 0); |
| 159 | if (String16(rawString) != ws.getNames()->at(i)) { |
| 160 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 161 | StringPrintf("WorkSource uid not equal %s", rawString).c_str()); |
| 162 | } |
| 163 | } |
| 164 | } else { |
| 165 | if (ws.getNames() != std::nullopt) { |
| 166 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 167 | StringPrintf("WorkSource parcel name not empty").c_str()); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static jobject nativeObtainPowerSaveStateParcel(JNIEnv* env, jobject /* obj */, |
| 173 | jboolean batterySaverEnabled, jboolean globalBatterySaverEnabled, |
| 174 | jint locationMode, jint soundTriggerMode, jfloat brightnessFactor) { |
| 175 | PowerSaveState ps = PowerSaveState(static_cast<bool>(batterySaverEnabled), |
| 176 | static_cast<bool>(globalBatterySaverEnabled), |
| 177 | static_cast<LocationMode>(locationMode), |
| 178 | static_cast<SoundTriggerMode>(soundTriggerMode), |
| 179 | static_cast<float>(brightnessFactor)); |
| 180 | jobject psParcel = nativeObtainParcel(env); |
| 181 | Parcel* parcel = nativeGetParcelData(env, psParcel); |
| 182 | status_t err = ps.writeToParcel(parcel); |
| 183 | if (err != OK) { |
| 184 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 185 | StringPrintf("WorkSource writeToParcel failed %d", err).c_str()); |
| 186 | } |
| 187 | parcel->setDataPosition(0); |
| 188 | return psParcel; |
| 189 | } |
| 190 | |
| 191 | static void nativeUnparcelAndVerifyPowerSaveState(JNIEnv* env, jobject /* obj */, jobject psParcel, |
| 192 | jboolean batterySaverEnabled, jboolean globalBatterySaverEnabled, |
| 193 | jint locationMode, jint soundTriggerMode, jfloat brightnessFactor) { |
| 194 | PowerSaveState ps = {}; |
| 195 | Parcel* parcel = nativeGetParcelData(env, psParcel); |
| 196 | status_t err = ps.readFromParcel(parcel); |
| 197 | if (err != OK) { |
| 198 | ALOGE("WorkSource writeToParcel failed %d", err); |
| 199 | } |
| 200 | // Now we have a native PowerSaveState object, verify it. |
| 201 | PowerSaveState psOrig = PowerSaveState(static_cast<bool>(batterySaverEnabled), |
| 202 | static_cast<bool>(globalBatterySaverEnabled), |
| 203 | static_cast<LocationMode>(locationMode), |
| 204 | static_cast<SoundTriggerMode>(soundTriggerMode), |
| 205 | static_cast<float>(brightnessFactor)); |
| 206 | if (ps == psOrig) { |
| 207 | return; |
| 208 | } else { |
| 209 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 210 | "PowerSaveState not equal with origin"); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static jobject nativeObtainBSPConfigParcel(JNIEnv* env, jobject /* obj */, |
| 215 | jobject bsObj, jobjectArray keyArray, jobjectArray valueArray) { |
| 216 | std::vector<std::pair<String16, String16>> deviceSpecificSettings; |
| 217 | for (jint i = 0; i < env->GetArrayLength(keyArray); i++) { |
| 218 | jstring keyString = (jstring) (env->GetObjectArrayElement(keyArray, i)); |
| 219 | jstring valueString = (jstring) (env->GetObjectArrayElement(valueArray, i)); |
| 220 | deviceSpecificSettings.push_back({String16(env->GetStringUTFChars(keyString, 0)), |
| 221 | String16(env->GetStringUTFChars(valueString, 0))}); |
| 222 | } |
| 223 | |
| 224 | BatterySaverPolicyConfig bs = BatterySaverPolicyConfig( |
| 225 | env->GetFloatField(bsObj, gBSPCFieldIds.adjustBrightnessFactor), |
| 226 | env->GetBooleanField(bsObj, gBSPCFieldIds.advertiseIsEnabled), |
| 227 | env->GetBooleanField(bsObj, gBSPCFieldIds.deferFullBackup), |
| 228 | env->GetBooleanField(bsObj, gBSPCFieldIds.deferKeyValueBackup), |
| 229 | deviceSpecificSettings, |
| 230 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableAnimation), |
| 231 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableAod), |
| 232 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableLaunchBoost), |
| 233 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableOptionalSensors), |
| 234 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableVibration), |
| 235 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableAdjustBrightness), |
| 236 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableDataSaver), |
| 237 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableFirewall), |
| 238 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableNightMode), |
| 239 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableQuickDoze), |
| 240 | env->GetBooleanField(bsObj, gBSPCFieldIds.forceAllAppsStandby), |
| 241 | env->GetBooleanField(bsObj, gBSPCFieldIds.forceBackgroundCheck), |
| 242 | static_cast<LocationMode>(env->GetIntField(bsObj, gBSPCFieldIds.locationMode)), |
| 243 | static_cast<SoundTriggerMode>(env->GetIntField(bsObj, gBSPCFieldIds.soundTriggerMode))); |
| 244 | |
| 245 | jobject bsParcel = nativeObtainParcel(env); |
| 246 | Parcel* parcel = nativeGetParcelData(env, bsParcel); |
| 247 | status_t err = bs.writeToParcel(parcel); |
| 248 | if (err != OK) { |
| 249 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 250 | StringPrintf("WorkSource writeToParcel failed %d", err).c_str()); |
| 251 | } |
| 252 | parcel->setDataPosition(0); |
| 253 | return bsParcel; |
| 254 | } |
| 255 | |
| 256 | static void nativeUnparcelAndVerifyBSPConfig(JNIEnv* env, jobject /* obj */, |
| 257 | jobject bsParcel, jobject bsObj, jobjectArray keyArray, jobjectArray valueArray) { |
| 258 | BatterySaverPolicyConfig bs = {}; |
| 259 | Parcel* parcel = nativeGetParcelData(env, bsParcel); |
| 260 | status_t err = bs.readFromParcel(parcel); |
| 261 | if (err != OK) { |
| 262 | ALOGE("WorkSource writeToParcel failed %d", err); |
| 263 | } |
| 264 | |
| 265 | // Get the device settings from Java |
| 266 | std::vector<std::pair<String16, String16>> deviceSpecificSettings; |
| 267 | for (jint i = 0; i < env->GetArrayLength(keyArray); i++) { |
| 268 | jstring keyString = (jstring) (env->GetObjectArrayElement(keyArray, i)); |
| 269 | jstring valueString = (jstring) (env->GetObjectArrayElement(valueArray, i)); |
| 270 | deviceSpecificSettings.push_back({String16(env->GetStringUTFChars(keyString, 0)), |
| 271 | String16(env->GetStringUTFChars(valueString, 0))}); |
| 272 | } |
| 273 | // Now we have a native BatterySaverPolicyConfig object, verify it. |
| 274 | BatterySaverPolicyConfig bsOrig = BatterySaverPolicyConfig( |
| 275 | env->GetFloatField(bsObj, gBSPCFieldIds.adjustBrightnessFactor), |
| 276 | env->GetBooleanField(bsObj, gBSPCFieldIds.advertiseIsEnabled), |
| 277 | env->GetBooleanField(bsObj, gBSPCFieldIds.deferFullBackup), |
| 278 | env->GetBooleanField(bsObj, gBSPCFieldIds.deferKeyValueBackup), |
| 279 | deviceSpecificSettings, |
| 280 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableAnimation), |
| 281 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableAod), |
| 282 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableLaunchBoost), |
| 283 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableOptionalSensors), |
| 284 | env->GetBooleanField(bsObj, gBSPCFieldIds.disableVibration), |
| 285 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableAdjustBrightness), |
| 286 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableDataSaver), |
| 287 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableFirewall), |
| 288 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableNightMode), |
| 289 | env->GetBooleanField(bsObj, gBSPCFieldIds.enableQuickDoze), |
| 290 | env->GetBooleanField(bsObj, gBSPCFieldIds.forceAllAppsStandby), |
| 291 | env->GetBooleanField(bsObj, gBSPCFieldIds.forceBackgroundCheck), |
| 292 | static_cast<LocationMode>(env->GetIntField(bsObj, gBSPCFieldIds.locationMode)), |
| 293 | static_cast<SoundTriggerMode>(env->GetIntField(bsObj, gBSPCFieldIds.soundTriggerMode))); |
| 294 | |
| 295 | if (bs == bsOrig) { |
| 296 | return; |
| 297 | } else { |
| 298 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 299 | "BatterySaverPolicyConfig not equal with origin"); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) |
| 304 | { |
| 305 | JNIEnv* env; |
| 306 | const JNINativeMethod methodTable[] = { |
| 307 | /* name, signature, funcPtr */ |
| 308 | { "nativeObtainWorkSourceParcel", "([I[Ljava/lang/String;)Landroid/os/Parcel;", |
| 309 | (void*) nativeObtainWorkSourceParcel }, |
| 310 | { "nativeUnparcelAndVerifyWorkSource", "(Landroid/os/Parcel;[I[Ljava/lang/String;)V", |
| 311 | (void*) nativeUnparcelAndVerifyWorkSource }, |
| 312 | { "nativeObtainPowerSaveStateParcel", "(ZZIIF)Landroid/os/Parcel;", |
| 313 | (void*) nativeObtainPowerSaveStateParcel }, |
| 314 | { "nativeUnparcelAndVerifyPowerSaveState", "(Landroid/os/Parcel;ZZIIF)V", |
| 315 | (void*) nativeUnparcelAndVerifyPowerSaveState }, |
| 316 | { "nativeObtainBSPConfigParcel", |
| 317 | "(Landroid/os/BatterySaverPolicyConfig;" |
| 318 | "[Ljava/lang/String;[Ljava/lang/String;)Landroid/os/Parcel;", |
| 319 | (void*) nativeObtainBSPConfigParcel }, |
| 320 | { "nativeUnparcelAndVerifyBSPConfig", |
| 321 | "(Landroid/os/Parcel;Landroid/os/BatterySaverPolicyConfig;" |
| 322 | "[Ljava/lang/String;[Ljava/lang/String;)V", |
| 323 | (void*) nativeUnparcelAndVerifyBSPConfig }, |
| 324 | }; |
| 325 | |
| 326 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 327 | return JNI_ERR; |
| 328 | } |
| 329 | |
| 330 | jclass bspcClazz; |
| 331 | FIND_CLASS(gParcelClazz, "android/os/Parcel"); |
| 332 | GET_FIELD_ID(gParcelDataFieldID, gParcelClazz, "mNativePtr", "J"); |
| 333 | GET_STATIC_METHOD_ID(gParcelObtainMethodID, gParcelClazz, "obtain", "()Landroid/os/Parcel;"); |
| 334 | FIND_CLASS(bspcClazz, "android/os/BatterySaverPolicyConfig"); |
| 335 | GET_FIELD_ID(gBSPCFieldIds.adjustBrightnessFactor, bspcClazz, "mAdjustBrightnessFactor", "F"); |
| 336 | GET_FIELD_ID(gBSPCFieldIds.advertiseIsEnabled, bspcClazz, "mAdvertiseIsEnabled", "Z"); |
| 337 | GET_FIELD_ID(gBSPCFieldIds.deferFullBackup, bspcClazz, "mDeferFullBackup", "Z"); |
| 338 | GET_FIELD_ID(gBSPCFieldIds.deferKeyValueBackup, bspcClazz, "mDeferKeyValueBackup", "Z"); |
| 339 | GET_FIELD_ID(gBSPCFieldIds.deviceSpecificSettings, bspcClazz, "mDeviceSpecificSettings", |
| 340 | "Ljava/util/Map;"); |
| 341 | GET_FIELD_ID(gBSPCFieldIds.disableAnimation, bspcClazz, "mDisableAnimation", "Z"); |
| 342 | GET_FIELD_ID(gBSPCFieldIds.disableAod, bspcClazz, "mDisableAod", "Z"); |
| 343 | GET_FIELD_ID(gBSPCFieldIds.disableLaunchBoost, bspcClazz, "mDisableLaunchBoost", "Z"); |
| 344 | GET_FIELD_ID(gBSPCFieldIds.disableOptionalSensors, bspcClazz, "mDisableOptionalSensors", "Z"); |
| 345 | GET_FIELD_ID(gBSPCFieldIds.disableVibration, bspcClazz, "mDisableVibration", "Z"); |
| 346 | GET_FIELD_ID(gBSPCFieldIds.enableAdjustBrightness, bspcClazz, "mEnableAdjustBrightness", "Z"); |
| 347 | GET_FIELD_ID(gBSPCFieldIds.enableDataSaver, bspcClazz, "mEnableDataSaver", "Z"); |
| 348 | GET_FIELD_ID(gBSPCFieldIds.enableFirewall, bspcClazz, "mEnableFirewall", "Z"); |
| 349 | GET_FIELD_ID(gBSPCFieldIds.enableNightMode, bspcClazz, "mEnableNightMode", "Z"); |
| 350 | GET_FIELD_ID(gBSPCFieldIds.enableQuickDoze, bspcClazz, "mEnableQuickDoze", "Z"); |
| 351 | GET_FIELD_ID(gBSPCFieldIds.forceAllAppsStandby, bspcClazz, "mForceAllAppsStandby", "Z"); |
| 352 | GET_FIELD_ID(gBSPCFieldIds.forceBackgroundCheck, bspcClazz, "mForceBackgroundCheck", "Z"); |
| 353 | GET_FIELD_ID(gBSPCFieldIds.locationMode, bspcClazz, "mLocationMode", "I"); |
| 354 | GET_FIELD_ID(gBSPCFieldIds.soundTriggerMode, bspcClazz, "mSoundTriggerMode", "I"); |
| 355 | |
| 356 | jniRegisterNativeMethods(env, "android/os/PowerManagerTest", methodTable, |
| 357 | sizeof(methodTable) / sizeof(JNINativeMethod)); |
| 358 | return JNI_VERSION_1_6; |
| 359 | } |
| 360 | |
| 361 | } /* namespace android */ |