This commit is contained in:
2025-10-22 04:04:02 +08:00
parent 9554e5e53d
commit c720f924d5
40 changed files with 925 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
try {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
} catch (err) {
hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err));
}
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
}

View File

@@ -0,0 +1,16 @@
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit';
const DOMAIN = 0x0000;
export default class EntryBackupAbility extends BackupExtensionAbility {
async onBackup() {
hilog.info(DOMAIN, 'testTag', 'onBackup ok');
await Promise.resolve();
}
async onRestore(bundleVersion: BundleVersion) {
hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion));
await Promise.resolve();
}
}

View File

@@ -0,0 +1,64 @@
import { EnterpriseAdminExtensionAbility } from '@kit.MDMKit';
import { Want } from '@kit.AbilityKit';
import { restrictions } from '@kit.MDMKit';
export default class EnterpriseAdminAbility extends EnterpriseAdminExtensionAbility {
// 设备管理应用激活回调方法,应用可在此回调函数中进行初始化策略设置
onAdminEnabled(): void {
console.info("onAdminEnabled");
try {
// 默认启用相机限制功能(即禁用相机)
// 注意在实际应用中需要传入正确的admin参数
// restrictions.setRestriction(admin, "camera", true);
console.info("Camera would be disabled by default on admin enabled");
} catch (error) {
console.error(`Failed to set default camera restriction: ${error}`);
}
}
// 设备管理应用解除激活回调方法,应用可在此回调函数中通知企业管理员设备已脱管
onAdminDisabled(): void {
console.info("onAdminDisabled");
try {
// 解除激活时,移除相机限制
// restrictions.setRestriction(admin, "camera", false);
console.info("Camera restriction would be removed on admin disabled");
} catch (error) {
console.error(`Failed to remove camera restriction: ${error}`);
}
}
// 应用安装回调方法,应用可在此回调函数中进行事件上报,通知企业管理员
onBundleAdded(bundleName: string): void {
console.info("EnterpriseAdminAbility onBundleAdded bundleName:" + bundleName);
}
// 应用卸载回调方法,应用可在此回调函数中进行事件上报,通知企业管理员
onBundleRemoved(bundleName: string): void {
console.info("EnterpriseAdminAbility onBundleRemoved bundleName" + bundleName);
}
// 控制相机功能
setCameraDisabled(admin: Want, disabled: boolean): void {
try {
// 在实际应用中应使用正确的API
// restrictions.setRestriction(admin, "camera", disabled);
console.info(`Camera restriction would be set to ${disabled}`);
} catch (error) {
console.error(`Failed to set camera restriction: ${error}`);
}
}
// 获取相机当前状态
isCameraDisabled(admin: Want): boolean {
try {
// 在实际应用中应使用正确的API
// const disabled = restrictions.isRestrictionSet(admin, "camera");
console.info(`Camera restriction status would be checked`);
return false;
} catch (error) {
console.error(`Failed to get camera restriction: ${error}`);
return false;
}
}
};

View File

@@ -0,0 +1,162 @@
// Index.ets
import { BusinessError } from '@kit.BasicServicesKit';
import { Want } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State message: string = 'MDM相机控制';
@State cameraStatus: string = '未知';
@State isAdminActive: boolean = false;
aboutToAppear(): void {
// 初始化时检查管理权限状态
this.checkAdminStatus();
}
// 检查设备管理权限是否已激活
checkAdminStatus(): void {
try {
// 在实际应用中,这里需要检查当前应用是否已被激活为设备管理器
// 由于API限制这里仅作演示
console.info('Checking admin status');
// this.isAdminActive = deviceManager.isAdminActive();
} catch (error) {
console.error(`Failed to check admin status: ${error}`);
}
}
// 激活设备管理应用
activateAdmin(): void {
console.info('Attempting to activate admin');
// 实际应用中需要实现完整的激活流程
// 通常会跳转到系统设置页面请求激活
this.isAdminActive = true;
this.cameraStatus = '已启用';
}
// 切换相机状态
toggleCamera(): void {
if (!this.isAdminActive) {
console.warn('Admin is not active, cannot toggle camera');
this.message = '请先激活管理应用';
return;
}
console.info('Toggling camera status');
// 在实际应用中这里会调用EnterpriseAdminExtensionAbility中的方法
// 来控制相机的启用/禁用状态
if (this.cameraStatus === '已启用') {
// 禁用相机
try {
// 这里应该调用EnterpriseAdminExtensionAbility中的方法
// restrictions.setRestriction(..., "camera", true);
this.cameraStatus = '已禁用';
this.message = '相机已禁用';
} catch (error) {
console.error(`Failed to disable camera: ${error}`);
this.message = '禁用相机失败';
}
} else {
// 启用相机
try {
// 这里应该调用EnterpriseAdminExtensionAbility中的方法
// restrictions.setRestriction(..., "camera", false);
this.cameraStatus = '已启用';
this.message = '相机已启用';
} catch (error) {
console.error(`Failed to enable camera: ${error}`);
this.message = '启用相机失败';
}
}
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(40)
.fontWeight(FontWeight.Bold)
.textAlign(TextAlign.Center)
// 显示管理权限状态
Text('管理权限: ' + (this.isAdminActive ? '已激活' : '未激活'))
.fontSize(20)
.fontColor(this.isAdminActive ? Color.Green : Color.Red)
.margin({ top: 20 })
// 显示相机状态
Text('相机状态: ' + this.cameraStatus)
.fontSize(20)
.fontColor(this.cameraStatus === '已启用' ? Color.Green : Color.Red)
.margin({ top: 10 })
// 切换相机状态按钮
Button() {
Text('切换相机状态')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 30
})
.backgroundColor('#0D9FFB')
.width('80%')
.height('8%')
.onClick(() => {
this.toggleCamera();
})
// 激活管理应用按钮
Button() {
Text('激活管理应用')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20
})
.backgroundColor('#4CAF50')
.width('80%')
.height('8%')
.onClick(() => {
this.activateAdmin();
})
// 添加按钮以响应用户onClick事件
Button() {
Text('Next')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 30
})
.backgroundColor('#0D9FFB')
.width('40%')
.height('8%')
// 跳转按钮绑定onClick事件单击时跳转到第二页
.onClick(() => {
console.info(`Succeeded in clicking the 'Next' button.`)
// 获取UIContext
let uiContext: UIContext = this.getUIContext();
let router = uiContext.getRouter();
// 跳转到第二页
router.pushUrl({ url: 'pages/Second' }).then(() => {
console.info('Succeeded in jumping to the second page.')
}).catch((err: BusinessError) => {
console.error(`Failed to jump to the second page. Code is ${err.code}, message is ${err.message}`)
})
})
}
.width('100%')
.padding(20)
}
.height('100%')
.backgroundColor('#f0f0f0')
}
}

View File

@@ -0,0 +1,68 @@
// Second.ets
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Second {
@State message: string = 'MDM相机控制说明';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(40)
.fontWeight(FontWeight.Bold)
.textAlign(TextAlign.Center)
// 添加说明文本
Text('此应用为设备管理应用,可用于控制设备的相机功能。')
.fontSize(20)
.margin({ top: 30 })
.textAlign(TextAlign.Center)
Text('功能说明:\n\n' +
'1. 激活管理应用:获取设备管理权限\n' +
'2. 切换相机状态:启用或禁用设备相机\n' +
'3. 相机被禁用后,所有应用都无法使用相机功能')
.fontSize(18)
.margin({ top: 20 })
.textAlign(TextAlign.Start)
.layoutWeight(1)
Button() {
Text('Back')
.fontSize(25)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 20,
bottom: 30
})
.backgroundColor('#0D9FFB')
.width('40%')
.height('8%')
// 返回按钮绑定onClick事件单击按钮时返回到第一页
.onClick(() => {
console.info(`Succeeded in clicking the 'Back' button.`)
// 获取UIContext
let uiContext: UIContext = this.getUIContext();
let router = uiContext.getRouter();
try {
// 返回第一页
router.back()
console.info('Succeeded in returning to the first page.')
} catch (err) {
let code = (err as BusinessError).code;
let message = (err as BusinessError).message;
console.error(`Failed to return to the first page. Code is ${code}, message is ${message}`)
}
})
}
.width('100%')
.padding(20)
}
.height('100%')
.backgroundColor('#f0f0f0')
}
}

View File

@@ -0,0 +1,82 @@
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"requestPermissions": [
{
"name": "ohos.permission.ENTERPRISE_MANAGE_RESTRICTIONS",
"reason": "$string:module_desc",
"usedScene": {
"abilities": [
"EnterpriseAdminAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN",
"reason": "$string:module_desc",
"usedScene": {
"abilities": [
"EnterpriseAdminAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.ENTERPRISE_GET_DEVICE_INFO",
"reason": "$string:module_desc",
"usedScene": {
"abilities": [
"EnterpriseAdminAbility"
],
"when": "always"
}
}
],
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"ohos.want.action.home"
]
}
]
}
],
"extensionAbilities": [
{
"name": "EnterpriseAdminAbility",
"srcEntry": "./ets/extensionability/EnterpriseAdminExtensionAbility.ets",
"type": "enterpriseAdmin",
"exported": true,
"metadata": [
{
"name": "ohos.extension.backup",
"resource": "$profile:backup_config"
}
]
}
]
}
}

View File

@@ -0,0 +1,8 @@
{
"color": [
{
"name": "start_window_background",
"value": "#FFFFFF"
}
]
}

View File

@@ -0,0 +1,8 @@
{
"float": [
{
"name": "page_text_font_size",
"value": "50fp"
}
]
}

View File

@@ -0,0 +1,16 @@
{
"string": [
{
"name": "module_desc",
"value": "MDM设备管理模块用于控制设备相机功能"
},
{
"name": "EntryAbility_desc",
"value": "MDM相机控制应用"
},
{
"name": "EntryAbility_label",
"value": "相机控制"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

@@ -0,0 +1,7 @@
{
"layered-image":
{
"background" : "$media:background",
"foreground" : "$media:foreground"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@@ -0,0 +1,3 @@
{
"allowToBackupRestore": true
}

View File

@@ -0,0 +1,6 @@
{
"src": [
"pages/Index",
"pages/Second"
]
}

View File

@@ -0,0 +1,8 @@
{
"color": [
{
"name": "start_window_background",
"value": "#000000"
}
]
}