目录

Workspace Configuration

Since API version 6.0.0(20), the capability of setting workspace information and profile photos is supported.

Since API version 6.0.2(22), the capability of setting and querying workspace policies is supported.

Since API version 6.1.0(23), the capability of setting the local name and status bar icon of a workspace is supported.

Use Cases

Enterprise Space Kit provides the capability of customizing workspace display attributes for enterprise apps. Enterprises can set the domain information, profile photo, local name, and status bar icon of the workspace to meet their personalized requirements.

API Description

For details about these APIs, please refer to the API Reference.

API Description
setWorkspaceInfo(workspaceId: number, domainInfo: WorkspaceDomainInfo): Promise<void> Sets workspace information.
setWorkspaceProfilePhoto(workspaceId: number, photo: string): Promise<void> Sets the workspace profile photo.
setWorkspaceLocalName(localName: string, workspaceId?: number): Promise<void> Sets the local name of a workspace.
setWorkspaceStatusBarIcon(icon: StatusBarIcon, workspaceId?: number): Promise<void> Sets the status bar icon for a workspace.
setWorkspacePolicy(key: string, value: number, workspaceId?: number): Promise<void> Sets a workspace policy.
getWorkspacePolicy(key: string, workspaceId?: number): Promise<number> Queries workspace policies and returns the result.

Development Procedure

  1. Import the dependent modules of the workspace configuration API.
import { spaceManager } from '@kit.EnterpriseSpaceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { ErrCode } from '../../common/ErrCode';
  1. Encapsulate the workspace configuration API.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

export class WorkspaceConfigurationApi {
  static async setWorkspacePolicy(workspaceId: number, key: string,
    value: spaceManager.LockdownModePolicy): Promise<number> {
    try {
      await spaceManager.setWorkspacePolicy(key, value, workspaceId);
      hilog.info(DOMAIN, TAG, 'Succeeded in setting workspace policy.');
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to set workspace policy. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }

  static async getWorkspacePolicy(workspaceId: number, key: string): Promise<number | undefined> {
    try {
      const value: number = await spaceManager.getWorkspacePolicy(key, workspaceId);
      hilog.info(DOMAIN, TAG, `Succeeded in getting workspace policy. value: ${value}`);
      return value;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to get workspace policy. Code: ${err.code}, message: ${err.message}`);
      return undefined;
    }
  }

  static async setWorkspaceInfo(workspaceId: number, domainInfo: spaceManager.WorkspaceDomainInfo): Promise<number> {
    try {
      await spaceManager.setWorkspaceInfo(workspaceId, domainInfo);
      hilog.info(DOMAIN, TAG, 'Succeeded in setting workspace info');
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to set workspace info. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }

  static async setWorkspaceProfilePhoto(workspaceId: number, photo: string): Promise<number> {
    try {
      await spaceManager.setWorkspaceProfilePhoto(workspaceId, photo);
      hilog.info(DOMAIN, TAG, 'Succeeded in setting workspace profile photo');
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to set workspace profile photo. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }

  static async setWorkspaceStatusBarIcon(workspaceId: number, icons: spaceManager.StatusBarIcon): Promise<number> {
    try {
      await spaceManager.setWorkspaceStatusBarIcon(icons, workspaceId);
      hilog.info(DOMAIN, TAG, `Succeeded in setting workspace status bar icon`);
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to set workspace status bar icon. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }

  static async setWorkspaceLocalName(workspaceId: number, localName: string): Promise<number> {
    try {
      await spaceManager.setWorkspaceLocalName(localName, workspaceId);
      hilog.info(DOMAIN, TAG, 'Succeeded in setting workspace local name');
      return ErrCode.OK;
    } catch (err) {
      hilog.error(DOMAIN, TAG, `Failed to set workspace local name. Code: ${err.code}, message: ${err.message}`);
      return ErrCode.ERR;
    }
  }
}
  1. Import the dependencies related to the implementation of workspace configuration service.
import { router } from '@kit.ArkUI';
import { spaceManager } from '@kit.EnterpriseSpaceKit';
import { image } from '@kit.ImageKit';
import { util } from '@kit.ArkTS';
import { resourceManager } from '@kit.LocalizationKit';
import { ErrCode } from '../../common/ErrCode';
import { WorkspaceConfigurationApi } from '../api/WorkspaceConfigurationApi'
import { hilog } from '@kit.PerformanceAnalysisKit';
  1. Implement the workspace configuration service.
const TAG = '[Sample_SpaceManagerSample]';
const DOMAIN = 0xF811;

@Entry
@Component
struct WorkspaceConfigurationPage {
  async setWorkspacePolicy() {
    const key: string = 'lockdown'; // Passed by the user.
    const value: spaceManager.LockdownModePolicy = spaceManager.LockdownModePolicy.OFF;
    const workspaceId: number = 100; // Passed by the user.

    if (await WorkspaceConfigurationApi.setWorkspacePolicy(workspaceId, key, value) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to set workspace policy!');
      return;
    }
    // Process the post-logic.
  }

  async getWorkspacePolicy() {
    const key: string = 'lockdown'; // Passed by the user.
    const workspaceId: number = 100; // Passed by the user.
    const value: number | undefined = await WorkspaceConfigurationApi.getWorkspacePolicy(workspaceId, key);
    if (value === undefined) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to get workspace policy!');
      return;
    }
    // Process the post-logic.
  }

  async setWorkspaceLocalName() {
    const localName: string = 'localName'; // Passed by the user.
    const workspaceId: number = 100; // Passed by the user.
    if (await WorkspaceConfigurationApi.setWorkspaceLocalName(workspaceId, localName) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to set workspace local name!');
      return;
    }
    // Process the post-logic.
  }

  async setWorkspaceStatusBarIcon() {
    const context: Context | undefined = this.getUIContext().getHostContext();
    if (!context) {
      hilog.error(DOMAIN, TAG, 'get host context fail!');
      return;
    }
    const resourceMgr: resourceManager.ResourceManager = context.resourceManager;

    // Create the white pixelMap using the CustomWhite.jpg image in the rawfile folder.
    let whiteFileData = await resourceMgr.getRawFd('CustomWhite.jpg');
    const whiteImageSource: image.ImageSource = image.createImageSource(whiteFileData);
    const whitePixelMap: image.PixelMap = await whiteImageSource.createPixelMap();

    // Create the black pixelMap using the CustomBlack.jpg image in the rawfile folder.
    let blackFileData = await resourceMgr.getRawFd('CustomBlack.jpg');
    const blackImageSource: image.ImageSource = image.createImageSource(blackFileData);
    const blackPixelMap: image.PixelMap = await blackImageSource.createPixelMap();

    // Build icon information.
    const icons: spaceManager.StatusBarIcon = {
      white: whitePixelMap,
      black: blackPixelMap
    };
    const workspaceId: number = 100;
    if (await WorkspaceConfigurationApi.setWorkspaceStatusBarIcon(workspaceId, icons) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to set workspace status bar icon!');
      return;
    }
    // Process the post-logic.
  }

  async setWorkspaceInfo() {
    const workspaceId: number = 100; // Passed by the user.
    const domainInfo: spaceManager.WorkspaceDomainInfo = {
      domain: 'test1',
      workspaceName: 'test2',
      accountId: 'test3',
      isAuthenticated: false,
      serverConfigId: 'test4',
      enterpriseWorkspaceName: 'default' // Enterprise workspace name, which is configured by the user.
    };
    if (await WorkspaceConfigurationApi.setWorkspaceInfo(workspaceId, domainInfo) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to set workspace info!');
      return;
    }
    // Process the post-logic.
  }

  async setWorkspaceProfilePhoto() {
    const workspaceId: number = 100;
    const context: Context | undefined = this.getUIContext().getHostContext();
    if (!context) {
      hilog.error(DOMAIN, TAG, 'get context fail!');
      return;
    }
    const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
    const photoData = await resourceMgr.getRawFileContent('CustomWhite.jpg');
    const base64Helper = new util.Base64Helper();
    const base64Img: string = base64Helper.encodeToStringSync(photoData);
    const photo: string = JSON.stringify({ type: 0, defaultImg: `data:image/jpeg;base64,${base64Img}` });
    if (await WorkspaceConfigurationApi.setWorkspaceProfilePhoto(workspaceId, photo) !== ErrCode.OK) {
      // Handle exceptions.
      hilog.error(DOMAIN, TAG, 'Failed to set workspace profile photo!');
      return;
    }
    // Process the post-logic.
  }

  build() {
    Column() {
      Row() {
        Button($r('app.string.setWorkspacePolicy'))
          .buttonCommonStyle()
          .onClick(() => {
            this.setWorkspacePolicy();
          })
      }

      Row() {
        Button($r('app.string.getWorkspacePolicy'))
          .buttonCommonStyle()
          .onClick(() => {
            this.getWorkspacePolicy();
          })
      }

      Row() {
        Button($r('app.string.setWorkspaceLocalName'))
          .buttonCommonStyle()
          .onClick(() => {
            this.setWorkspaceLocalName();
          })
      }

      Row() {
        Button($r('app.string.setWorkspaceStatusBarIcon'))
          .buttonCommonStyle()
          .onClick(() => {
            this.setWorkspaceStatusBarIcon();
          })
      }

      Row() {
        Button($r('app.string.setWorkspaceInfo'))
          .buttonCommonStyle()
          .onClick(() => {
            this.setWorkspaceInfo();
          })
      }

      Row() {
        Button($r('app.string.setWorkspaceProfilePhoto'))
          .buttonCommonStyle()
          .onClick(() => {
            this.setWorkspaceProfilePhoto();
          })
      }

      Row() {
        Button($r('app.string.back'))
          .buttonCommonStyle()
          .onClick(() => {
            router.back();
          })
      }
    }
  }
}

@Extend(Button)
function buttonCommonStyle() {
  .width(200)
  .height(50)
  .backgroundColor('#6366F1')
  .fontColor('#FFFFFF')
  .fontSize(14)
  .margin({ left: 20, bottom: 5 })
}