!147 安全控件添加弹窗

Merge pull request !147 from zhouyan/security
This commit is contained in:
openharmony_ci 2023-07-24 11:28:18 +00:00 committed by Gitee
commit b8448b238e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 383 additions and 6 deletions

View File

@ -0,0 +1,92 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import extension from '@ohos.app.ability.ServiceExtensionAbility';
import window from '@ohos.window';
import display from '@ohos.display';
import deviceInfo from '@ohos.deviceInfo';
const TAG = 'PermissionManager_Log:';
const BG_COLOR = '#00000000';
let bottomPopoverTypes = ['default', 'phone'];
export default class SecurityExtensionAbility extends extension {
/**
* Lifecycle function, called back when a service extension is started for initialization.
*/
onCreate(want): void {
console.info(TAG + 'SecurityExtensionAbility onCreate, ability name is ' + want.abilityName);
globalThis.securityContext = this.context;
globalThis.windowNum = 0;
}
/**
* Lifecycle function, called back when a service extension is started or recall.
*/
onRequest(want, startId): void {
console.info(TAG + 'SecurityExtensionAbility onRequest. start id is ' + startId);
console.info(TAG + 'want: ' + JSON.stringify(want));
try {
let dis = display.getDefaultDisplaySync();
let navigationBarRect = {
left: 0,
top: 0,
width: dis.width,
height: dis.height
};
let isVertical = dis.width > dis.height ? false : true;
globalThis.isBottomPopover = (bottomPopoverTypes.includes(deviceInfo.deviceType) && isVertical) ? true : false;
this.createWindow('SecurityDialog' + startId, window.WindowType.TYPE_DIALOG, navigationBarRect, want);
} catch (exception) {
console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
};
}
/**
* Lifecycle function, called back before a service extension is destroyed.
*/
onDestroy(): void {
console.info(TAG + 'SecurityExtensionAbility onDestroy.');
}
private async createWindow(name: string, windowType, rect, want): Promise<void> {
console.info(TAG + 'create securitywindow');
try {
const win = await window.createWindow({ ctx: globalThis.securityContext, name, windowType });
let storage: LocalStorage = new LocalStorage({
'want': want,
'win': win
});
await win.bindDialogTarget(want.parameters['ohos.ability.params.token'].value, () => {
win.destroyWindow();
globalThis.windowNum --;
if (globalThis.windowNum === 0) {
this.context.terminateSelf();
}
});
await win.moveWindowTo(rect.left, rect.top);
await win.resize(rect.width, rect.height);
await win.loadContent('pages/securityDialog', storage);
await win.setWindowBackgroundColor(BG_COLOR);
await win.showWindow();
await win.setWindowLayoutFullScreen(true);
globalThis.windowNum ++;
} catch {
console.info(TAG + 'window create failed!');
}
}
};

View File

@ -274,10 +274,26 @@ export default class Constants {
static RECORD_PADDING_BOTTOM = '20%'
//////////////security_dialog///////////////
// total
static SECURITY_TOTAL_MARGIN_LEFT = 24;
static SECURITY_TOTAL_MARGIN_RIGHT = 24;
// icon
static SECURITY_ICON_MARGIN_TOP = 24;
static SECURITY_ICON_MARGIN_BOTTOM = 16;
static SECURITY_ICON_WIDTH = 24;
static SECURITY_ICON_HEIGHT = 24;
// content-descriptor
static SECURITY_DESCRIPTOR_DEVIDER_MARGIN_TOP = 8;
//button
static SECURITY_BUTTON_MARGIN_TOP = 8;
static SECURITY_BUTTON_ROW_HEIGHT = 56;
static SECURITY_BUTTON_HEIGHT = 40;
////////////////////////////////////////////
// icon of dialog
static DIALOG_ICON_WIDTH = 24;
static DIALOG_ICON_HEIGHT = 24;

View File

@ -0,0 +1,189 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import bundleManager from '@ohos.bundle.bundleManager';
import Constants from '../common/utils/constant';
import window from '@ohos.window';
import { Log } from '../common/utils/utils';
let storage = LocalStorage.GetShared();
let want: any = '';
let win: any = '';
interface param {
'icon': Resource,
'label': Resource
};
@Entry(storage)
@Component
struct SecurityDialog {
@LocalStorageLink('want') want: Object = {};
@LocalStorageLink('win') win: Object = {};
securityDialogController: CustomDialogController = new CustomDialogController({
builder: CustomSecurityDialog(),
alignment: DialogAlignment.Bottom,
customStyle: true
})
build() {}
aboutToAppear() {
this.securityDialogController.open();
want = this.want;
win = this.win;
}
}
@CustomDialog
struct CustomSecurityDialog {
controller: CustomDialogController;
descriptor: string = ''
@State appName: string = 'ToBeInstead';
@State index: number = 0;
@State naviHeight: number = 0
securityParams : Array<param> = [
{
'icon': $r('app.media.ic_public_gps'),
'label': $r('app.string.location_desc'),
},
{
'icon': $r('app.media.ic_public_folder'),
'label': $r('app.string.media_files_desc')
}
]
GetAppName() {
let bundleName: string = want.parameters['ohos.aafwk.param.callerBundleName'];
bundleManager.getApplicationInfo(bundleName, bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT)
.then(data => {
globalThis.securityContext.resourceManager.getStringValue(data.labelResource)
.then(data => {
this.appName = data;
})
.catch(error => {
Log.error('getStringValue failed. err is ' + JSON.stringify(error));
});
})
.catch(error => {
Log.error('getApplicationInfo failed. err is ' + JSON.stringify(error));
});
}
destruction() {
win.destroy();
globalThis.windowNum --;
if (globalThis.windowNum == 0) {
globalThis.extensionContext.terminateSelf();
}
}
getAvoidWindow() {
let type = window.AvoidAreaType.TYPE_SYSTEM;
try {
let avoidArea = win.getWindowAvoidArea(type);
Log.info('avoidArea: ' + JSON.stringify(avoidArea));
this.naviHeight = avoidArea.bottomRect.height;
} catch (exception) {
Log.error('Failed to obtain the area. Cause:' + JSON.stringify(exception));
}
}
build() {
GridRow({
columns: {
xs: Constants.XS_COLUMNS, sm: Constants.SM_COLUMNS, md: Constants.MD_COLUMNS, lg: Constants.LG_COLUMNS
},
gutter: Constants.DIALOG_GUTTER
}) {
GridCol({
span: {
xs: Constants.XS_SPAN, sm: Constants.SM_SPAN, md: Constants.DIALOG_MD_SPAN, lg: Constants.DIALOG_LG_SPAN
},
offset: {
xs: Constants.XS_OFFSET,
sm: Constants.SM_OFFSET,
md: Constants.DIALOG_MD_OFFSET,
lg: Constants.DIALOG_LG_OFFSET
}
}) {
Flex({
justifyContent: FlexAlign.Center,
alignItems: globalThis.isBottomPopover ? ItemAlign.End : ItemAlign.Center
}) {
Row() {
Column() {
Image(this.securityParams[this.index].icon) // icon
.width(Constants.SECURITY_ICON_WIDTH)
.height(Constants.SECURITY_ICON_HEIGHT)
.fillColor($r('sys.color.ohos_id_color_text_primary'))
.margin({
top: Constants.SECURITY_ICON_MARGIN_TOP,
bottom: Constants.SECURITY_ICON_MARGIN_BOTTOM
})
Column() { // content
Text($r('app.string.Temporarily_authorize') + this.appName + $r('app.string.To_access')
+ this.securityParams[this.index].label)
.textAlign(TextAlign.Center)
.lineHeight($r('sys.float.ohos_id_text_line_space_l'))
.fontColor($r('sys.color.ohos_id_color_text_primary'))
.fontSize($r('sys.float.ohos_id_text_size_body1'))
.fontWeight(FontWeight.Medium)
Text($r('app.string.only_can_desc') + '' + this.securityParams[this.index].label
+ $r('app.string.only_after_desc') + '' + $r('app.string.prevent_desc'))
.textAlign(TextAlign.Center)
.lineHeight($r('sys.float.ohos_id_text_line_space_l'))
.fontColor($r('sys.color.ohos_id_color_text_secondary'))
.fontSize($r('sys.float.ohos_id_text_size_body2'))
.margin({
top: Constants.SECURITY_DESCRIPTOR_DEVIDER_MARGIN_TOP,
})
}
Column() {
Button($r('app.string.Got_it')) // button
.fontSize($r('sys.float.ohos_id_text_size_button1'))
.fontWeight(FontWeight.Medium)
.fontColor($r('sys.color.ohos_id_color_text_primary_activated'))
.backgroundColor($r('sys.color.ohos_id_color_dialog_bg'))
.height(Constants.SECURITY_BUTTON_HEIGHT)
.width(Constants.FULL_WIDTH)
.onClick(() => {
this.controller.close();
this.destruction();
})
}
.margin({ top: Constants.SECURITY_BUTTON_MARGIN_TOP })
.height(Constants.SECURITY_BUTTON_ROW_HEIGHT)
}.margin({ left: Constants.SECURITY_TOTAL_MARGIN_LEFT, right: Constants.SECURITY_TOTAL_MARGIN_RIGHT })
}
.margin({ bottom: Constants.DIALOG_MARGIN_BOTTOM })
.backgroundColor($r('sys.color.ohos_id_color_dialog_bg'))
.borderRadius(Constants.DIALOG_PRIVACY_BORDER_RADIUS)
}
}
}.margin({ left: globalThis.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN,
right: globalThis.isBottomPopover ? Constants.DIALOG_MARGIN_VERTICAL : Constants.DIALOG_MARGIN,
bottom: globalThis.isBottomPopover ? this.naviHeight : 0})
}
aboutToAppear() {
Log.info('onAboutToAppear.');
this.getAvoidWindow();
this.GetAppName();
this.index = want.parameters['ohos.user.security.type'];
}
}

View File

@ -61,7 +61,14 @@
"srcEntrance": "./ets/GlobalExtAbility/GlobalExtAbility.ts",
"type": "service",
"visible": true
}
},
{
"icon": "$media:icon",
"name": "com.ohos.permissionmanager.SecurityExtAbility",
"srcEntrance": "./ets/SecurityExtAbility/SecurityExtAbility.ts",
"type": "service",
"visible": true
}
],
"requestPermissions": [
{

View File

@ -375,6 +375,42 @@
{
"name": "SpecificAbility_label",
"value": "label"
},
{
"name": "Temporarily_authorize",
"value": "Temporarily authorize "
},
{
"name": "To_access",
"value": " to access "
},
{
"name": "location_desc",
"value": "location information"
},
{
"name": "media_files_desc",
"value": "media and files"
},
{
"name": "location_button_desc",
"value": "This button has passed the system security authentication. "
},
{
"name": "only_can_desc",
"value": "The app is allowed to temporarily access your "
},
{
"name": "only_after_desc",
"value": " only after you touch this button. "
},
{
"name": "prevent_desc",
"value": "This prevents your private data from being continuously obtained by the app."
},
{
"name": "Got_it",
"value": "OK"
}
]
}

View File

@ -9,6 +9,7 @@
"pages/authority-tertiary-groups",
"pages/permission-access-record",
"pages/dialogPlus",
"pages/globalSwitch"
"pages/globalSwitch",
"pages/securityDialog"
]
}

View File

@ -375,6 +375,42 @@
{
"name": "SpecificAbility_label",
"value": "label"
},
{
"name": "Temporarily_authorize",
"value": "临时授权"
},
{
"name": "To_access",
"value": "访问"
},
{
"name": "location_desc",
"value": "位置信息"
},
{
"name": "media_files_desc",
"value": "媒体和文件"
},
{
"name": "location_button_desc",
"value": "此按钮通过系统安全认证,"
},
{
"name": "only_can_desc",
"value": "仅允许应用在您点击此按钮时,临时访问"
},
{
"name": "only_after_desc",
"value": ""
},
{
"name": "prevent_desc",
"value": "以减少您的隐私数据被应用持续获取。"
},
{
"name": "Got_it",
"value": "知道了"
}
]
}