1/*
2 * Copyright (c) 2022-2023 Huawei Device 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
16import { Constants, ScreenManager, UserFileManagerAccess, MediaObserver, Log } from '@ohos/common';
17import display from '@ohos.display';
18import Extension from '@ohos.app.ability.ServiceExtensionAbility';
19import Window from '@ohos.window';
20import dialogRequest from '@ohos.app.ability.dialogRequest';
21import type common from '@ohos.app.ability.common';
22import type Want from '@ohos.app.ability.Want';
23
24const TAG: string = 'ServiceExtAbility';
25
26export default class ServiceExtAbility extends Extension {
27  private windowClass;
28
29  onCreate(want: Want): void {
30    Log.info(TAG, 'ServiceExtAbility want param:' + JSON.stringify(want));
31    AppStorage.setOrCreate('windowClass', null);
32    AppStorage.setOrCreate('photosAbilityContext', this.context);
33    AppStorage.setOrCreate(Constants.SCREEN_SIDEBAR, false);
34    AppStorage.setOrCreate('deviceType', Constants.PC_DEVICE_TYPE);
35  }
36
37  onReconnect(want: Want): void {
38    Log.info(TAG, 'onReconnect windowClass : ' + this.windowClass);
39    if (this.windowClass === null || this.windowClass === undefined) {
40      return;
41    }
42    try {
43      Log.info(TAG, 'test start1');
44      this.windowClass.destroyWindow((err): void => {
45        if (err.code) {
46          Log.info(TAG, 'Failed to destroy the window. Cause:' + JSON.stringify(err));
47          return;
48        }
49        Log.info(TAG, 'Succeeded in destroying the window.');
50        this.windowClass = null;
51      });
52      Log.info(TAG, 'test done1');
53    } catch (e) {
54      Log.error(TAG, 'fail1 ' + JSON.stringify(e));
55    }
56  }
57
58
59  onRequest(want: Want, startId: number): void {
60    if (want.action !== Constants.ACTION_DELETE_DATA) {
61      return;
62    }
63    UserFileManagerAccess.getInstance().onCreate(AppStorage.get<common.UIAbilityContext>('photosAbilityContext'));
64    MediaObserver.getInstance().registerForAllPhotos();
65    MediaObserver.getInstance().registerForAllAlbums();
66    let wantParam: { [key: string]: Object } = want.parameters;
67    let uris: any = wantParam?.uris;
68    let appName: string = wantParam?.appName as string;
69    Log.info(TAG, 'get delete data : ' + JSON.stringify(wantParam));
70    if (!uris?.length) {
71      return;
72    }
73    AppStorage.setOrCreate('uris', uris);
74    AppStorage.setOrCreate('appName', appName);
75    this.windowClass = AppStorage.get('windowClassg');
76    let config: Window.Configuration = {
77      name: 'DeleteDialog ' + appName + Math.random(), windowType: Window.WindowType.TYPE_DIALOG, ctx: this.context
78    };
79    try {
80      Window.createWindow(config, (err, data): void => { //创建模态窗口
81        if (err.code) {
82          Log.info(TAG, 'Failed to create the window. Cause: ' + JSON.stringify(err));
83          return;
84        }
85        this.windowClass = data;
86        Log.info(TAG, 'Success ded in creating the window. Data: ');
87        this.bindDialogTarget(want);
88      });
89    } catch (exception) {
90      Log.info(TAG, 'Failed to create the window. Cause: ' + JSON.stringify(exception));
91    }
92  }
93
94  private bindDialogTarget(want: Want): void {
95    try {
96      let requestInfo: dialogRequest.RequestInfo = dialogRequest.getRequestInfo(want); //从Want中获取请求方的RequestInfo
97      Log.info(TAG, 'requestInfo param:' + JSON.stringify(requestInfo));
98
99      let requestCallback: dialogRequest.RequestCallback = dialogRequest.getRequestCallback(want);
100      AppStorage.setOrCreate('requestCallback', requestCallback);
101
102      this.windowClass.bindDialogTarget(requestInfo, (): void => { //绑定模态窗口与目标窗口
103        Log.info(TAG, 'Dialog Window Need Destroy.');
104      }, (err): void => {
105        if (err.code) {
106          Log.info(TAG, 'Failed to bind dialog target. Cause:' + JSON.stringify(err));
107          return;
108        }
109        Log.info(TAG, 'Succeeded in binding dialog target.');
110        this.setContentUI();
111      });
112    } catch (exception) {
113      Log.error(TAG, 'Failed to bind dialog target. Cause:' + JSON.stringify(exception));
114    }
115  }
116
117  private setContentUI(): void {
118    try {
119      this.windowClass.setUIContent('pages/ResourceDeletePage', (err): void => {
120        if (err.code) {
121          Log.info(TAG, 'Failed to load the content. Cause:' + JSON.stringify(err));
122          return;
123        }
124        Log.info(TAG, 'Succeeded in loading the content.');
125        display.getDefaultDisplay().then((data): void => {
126          Log.info(TAG, 'Succeeded in loading the content.' + data.width + ',  ' + data.height);
127          ScreenManager.getInstance().setWinWidth(px2vp(data.width));
128          this.windowClass.resetSize(data.width, data.height); //设置窗口全屏
129          this.windowClass.setBackgroundColor('#00000000'); //设置窗口背景透明
130          this.windowClass.show();
131        }).catch((err): void => {
132          Log.error(TAG, 'getDefaultDisplay fail: ' + JSON.stringify(err));
133        });
134      });
135    } catch (exception) {
136      Log.error(TAG, 'Failed to load the content. Cause:' + JSON.stringify(exception));
137    }
138  }
139
140  onDisconnect(want: Want): void {
141    Log.info(TAG, `onDisconnect, want: ${want.abilityName}`);
142  }
143
144  onDestroy(): void {
145    Log.info(TAG, 'onDestroy');
146  }
147}