Showing
3 changed files
with
267 additions
and
0 deletions
| 1 | +import { BasicMessageChannel, BinaryMessenger, ByteBuffer, Log, MessageCodec } from '@ohos/flutter_ohos'; | ||
| 2 | +import { Reply } from '@ohos/flutter_ohos/src/main/ets/plugin/common/BasicMessageChannel'; | ||
| 3 | +import StandardMessageCodec from '@ohos/flutter_ohos/src/main/ets/plugin/common/StandardMessageCodec'; | ||
| 4 | +import ArrayList from '@ohos.util.ArrayList'; | ||
| 5 | +import { WakelockApi } from './Wakelock'; | ||
| 6 | + | ||
| 7 | +const TAG = "wakelock.Messages"; | ||
| 8 | + | ||
| 9 | +export class ToggleMessage { | ||
| 10 | + enable: Boolean = false; | ||
| 11 | + | ||
| 12 | + toMap(): Map<String, ESObject> { | ||
| 13 | + let toMapResult: Map<String, ESObject> = new Map<String, ESObject>(); | ||
| 14 | + toMapResult.set("enable", this.enable); | ||
| 15 | + return toMapResult; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + static fromMap(map: Map<String, ESObject>): ToggleMessage { | ||
| 19 | + let fromMapResult = new ToggleMessage(); | ||
| 20 | + let enable: Boolean = map.get("enable") ?? false; | ||
| 21 | + fromMapResult.enable = enable; | ||
| 22 | + return fromMapResult; | ||
| 23 | + } | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +export class IsEnabledMessage { | ||
| 27 | + enabled: Boolean = false; | ||
| 28 | + | ||
| 29 | + toMap(): Map<String, ESObject> { | ||
| 30 | + let toMapResult: Map<String, ESObject> = new Map<String, ESObject>(); | ||
| 31 | + toMapResult.set("enabled", this.enabled); | ||
| 32 | + return toMapResult; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + static fromMap(map: Map<String, ESObject>): IsEnabledMessage { | ||
| 36 | + let fromMapResult = new IsEnabledMessage(); | ||
| 37 | + let enabled: Boolean = map.get("enabled") ?? false; | ||
| 38 | + fromMapResult.enabled = enabled; | ||
| 39 | + return fromMapResult; | ||
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +class WakelockApiCodec extends StandardMessageCodec { | ||
| 44 | + static INSTANCE: WakelockApiCodec = new WakelockApiCodec(); | ||
| 45 | + | ||
| 46 | + private constructor() { | ||
| 47 | + super(); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + readValueOfType(type: number, buffer: ByteBuffer): ESObject { | ||
| 51 | + // Log.i(TAG, "readValueOfType type=" + type + ", buffer=" + buffer); | ||
| 52 | + switch (type) { | ||
| 53 | + case 128: | ||
| 54 | + return IsEnabledMessage.fromMap(this.readValue(buffer) as Map<String, ESObject>); | ||
| 55 | + | ||
| 56 | + case 129: | ||
| 57 | + return ToggleMessage.fromMap(this.readValue(buffer) as Map<String, ESObject>); | ||
| 58 | + | ||
| 59 | + default: | ||
| 60 | + return super.readValueOfType(type, buffer); | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + writeValue(stream: ByteBuffer, value: ESObject): ESObject { | ||
| 65 | + if (value instanceof IsEnabledMessage) { | ||
| 66 | + stream.writeUint8(128); | ||
| 67 | + this.writeValue(stream, value.toMap()); | ||
| 68 | + } else if (value instanceof ToggleMessage) { | ||
| 69 | + stream.writeUint8(129); | ||
| 70 | + this.writeValue(stream, value.toMap()); | ||
| 71 | + } else { | ||
| 72 | + super.writeValue(stream, value); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +export class Messages { | ||
| 78 | + static getCodec(): MessageCodec<ESObject> { | ||
| 79 | + return WakelockApiCodec.INSTANCE; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + static setup(binaryMessenger: BinaryMessenger, api: WakelockApi): void { | ||
| 83 | + { | ||
| 84 | + const channel: BasicMessageChannel<ESObject> = new BasicMessageChannel<ESObject>( | ||
| 85 | + binaryMessenger, "dev.flutter.pigeon.WakelockApi.toggle", Messages.getCodec()); | ||
| 86 | + if (api != null) { | ||
| 87 | + channel.setMessageHandler({ | ||
| 88 | + onMessage(message: ESObject, reply: Reply<ESObject>): void { | ||
| 89 | + // Log.d(TAG, 'onMessage reply:' + reply) | ||
| 90 | + let wrapped: Map<String, ESObject> = new Map<String, ESObject>(); | ||
| 91 | + try { | ||
| 92 | + let args: ArrayList<ESObject> = message as ArrayList<ESObject>; | ||
| 93 | + let msgArg: ToggleMessage = args[0] as ToggleMessage; | ||
| 94 | + | ||
| 95 | + Log.i(TAG, "received msgArg=" + JSON.stringify(msgArg)); | ||
| 96 | + if (msgArg == null) { | ||
| 97 | + throw new Error("msgArg unexpectedly null."); | ||
| 98 | + } | ||
| 99 | + api.toggle(msgArg).then(()=> { | ||
| 100 | + Log.i(TAG, "toggle success"); | ||
| 101 | + wrapped.set("result", null); | ||
| 102 | + reply.reply(wrapped); | ||
| 103 | + }).catch((err: ESObject) => { | ||
| 104 | + Log.i(TAG, "toggle failed: " + JSON.stringify(err)); | ||
| 105 | + wrapped.set("error", Messages.wrapError(err)); | ||
| 106 | + reply.reply(wrapped); | ||
| 107 | + }); | ||
| 108 | + } catch (err) { | ||
| 109 | + Log.i(TAG, "toggle catch err: " + JSON.stringify(err)); | ||
| 110 | + wrapped.set("error", Messages.wrapError(err)); | ||
| 111 | + reply.reply(wrapped); | ||
| 112 | + } | ||
| 113 | + } | ||
| 114 | + }); | ||
| 115 | + } else { | ||
| 116 | + channel.setMessageHandler(null); | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + { | ||
| 120 | + const channel: BasicMessageChannel<ESObject> = new BasicMessageChannel<ESObject>( | ||
| 121 | + binaryMessenger, "dev.flutter.pigeon.WakelockApi.isEnabled", Messages.getCodec()); | ||
| 122 | + if (api != null) { | ||
| 123 | + channel.setMessageHandler({ | ||
| 124 | + onMessage(message: ESObject, reply: Reply<ESObject>): void { | ||
| 125 | + // Log.d(TAG, 'onMessage reply:' + reply) | ||
| 126 | + let wrapped: Map<String, ESObject> = new Map<String, ESObject>(); | ||
| 127 | + try { | ||
| 128 | + let output: IsEnabledMessage = api.isEnabled(); | ||
| 129 | + wrapped.set("result", output); | ||
| 130 | + } catch (err) { | ||
| 131 | + wrapped.set("error", Messages.wrapError(err)) | ||
| 132 | + } | ||
| 133 | + reply.reply(wrapped) | ||
| 134 | + } | ||
| 135 | + }); | ||
| 136 | + } else { | ||
| 137 | + channel.setMessageHandler(null); | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + static wrapError(error: Error): Map<String, ESObject> { | ||
| 143 | + let errorMap: Map<String, ESObject> = new Map(); | ||
| 144 | + errorMap.set("message", error.message); | ||
| 145 | + errorMap.set("code", error.name); | ||
| 146 | + errorMap.set("details", null); | ||
| 147 | + return errorMap; | ||
| 148 | + } | ||
| 149 | +} |
| 1 | +import { Log } from '@ohos/flutter_ohos'; | ||
| 2 | +import { IsEnabledMessage, ToggleMessage } from './Messages'; | ||
| 3 | +import common from '@ohos.app.ability.common'; | ||
| 4 | +import window from '@ohos.window'; | ||
| 5 | + | ||
| 6 | +const TAG = "Wakelock.ohos"; | ||
| 7 | + | ||
| 8 | +export interface WakelockApi { | ||
| 9 | + toggle(msg: ToggleMessage): Promise<void>; | ||
| 10 | + | ||
| 11 | + isEnabled(): IsEnabledMessage; | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +export class Wakelock implements WakelockApi { | ||
| 15 | + private enabled: boolean = false; | ||
| 16 | + context?: common.UIAbilityContext; | ||
| 17 | + | ||
| 18 | + constructor(context: common.UIAbilityContext) { | ||
| 19 | + this.context = context; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + toggle(message: ToggleMessage): Promise<void> { | ||
| 23 | + if (!this.context) { | ||
| 24 | + throw new NoAbilityError(); | ||
| 25 | + } | ||
| 26 | + return new Promise<void>((resolve, reject) => { | ||
| 27 | + window.getLastWindow(this.context).then((data) => { | ||
| 28 | + return Promise.resolve(data); | ||
| 29 | + }).then((windowClass: window.Window) => { | ||
| 30 | + let isKeepScreenOn: boolean = message.enable as boolean; | ||
| 31 | + Log.i(TAG, "message=" + message.enable); | ||
| 32 | + Log.i(TAG, "this.enabled=" + this.enabled); | ||
| 33 | + Log.i(TAG, "isKeepScreenOn=" + isKeepScreenOn); | ||
| 34 | + windowClass.setWindowKeepScreenOn(isKeepScreenOn).then(() => { | ||
| 35 | + Log.i(TAG, "setWindowKeepScreenOn success"); | ||
| 36 | + this.enabled = isKeepScreenOn; | ||
| 37 | + resolve(); | ||
| 38 | + }).catch((err: ESObject) => { | ||
| 39 | + this.enabled = false; | ||
| 40 | + reject(err); | ||
| 41 | + Log.e(TAG, "setWindowKeepScreenOn error: " + JSON.stringify(err)); | ||
| 42 | + }) | ||
| 43 | + }).catch((err: ESObject) => { | ||
| 44 | + this.enabled = false; | ||
| 45 | + reject(err); | ||
| 46 | + Log.i(TAG, "setWindowKeepScreenOn other error: " + JSON.stringify(err)); | ||
| 47 | + }) | ||
| 48 | + }) | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + isEnabled(): IsEnabledMessage { | ||
| 52 | + if (this.context == null) { | ||
| 53 | + throw new NoAbilityError() | ||
| 54 | + } | ||
| 55 | + let msg = new IsEnabledMessage(); | ||
| 56 | + msg.enabled = this.enabled; | ||
| 57 | + return msg; | ||
| 58 | + } | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +export class NoAbilityError extends Error { | ||
| 62 | + constructor() { | ||
| 63 | + super("wakelock requires a foreground Ability.") | ||
| 64 | + } | ||
| 65 | +} |
| 1 | +/* | ||
| 2 | +* Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd. | ||
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | +* you may not use this file except in compliance with the License. | ||
| 5 | +* You may obtain a copy of the License at | ||
| 6 | +* | ||
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | +* | ||
| 9 | +* Unless required by applicable law or agreed to in writing, software | ||
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | +* See the License for the specific language governing permissions and | ||
| 13 | +* limitations under the License. | ||
| 14 | +*/ | ||
| 15 | + | ||
| 16 | +import AbilityAware from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/ability/AbilityAware'; | ||
| 17 | +import { AbilityPluginBinding } from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/ability/AbilityPluginBinding'; | ||
| 18 | +import { | ||
| 19 | + FlutterPlugin, | ||
| 20 | + FlutterPluginBinding | ||
| 21 | +} from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin'; | ||
| 22 | +import { Messages } from './Messages'; | ||
| 23 | +import { Wakelock } from './Wakelock'; | ||
| 24 | + | ||
| 25 | +const TAG = "WakelockPlugin" | ||
| 26 | + | ||
| 27 | +export class WakelockPlugin implements FlutterPlugin, AbilityAware { | ||
| 28 | + private pluginBinding : FlutterPluginBinding | null = null; | ||
| 29 | + private wakelock : Wakelock | null = null; | ||
| 30 | + | ||
| 31 | + getUniqueClassName(): string { | ||
| 32 | + return "WakelockPlugin" | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + onAttachedToAbility(binding: AbilityPluginBinding): void { | ||
| 36 | + this.wakelock = new Wakelock(binding.getAbility().context); | ||
| 37 | + if (this.pluginBinding != null) { | ||
| 38 | + Messages.setup(this.pluginBinding.getBinaryMessenger(), this.wakelock); | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + onDetachedFromAbility(): void { | ||
| 43 | + this.wakelock = null; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + onAttachedToEngine(binding: FlutterPluginBinding): void { | ||
| 47 | + this.pluginBinding = binding; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + onDetachedFromEngine(binding: FlutterPluginBinding): void { | ||
| 51 | + this.pluginBinding = null; | ||
| 52 | + } | ||
| 53 | +} |
-
Please register or login to post a comment