• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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 { MediaDataManager } from '../data/MediaDataManager';
17import { BigDataConstants, Constants as commonConstants, DataStoreUtil, Log, ReportToBigDataUtil } from '@ohos/common';
18import formBindingData from '@ohos.application.formBindingData';
19import { Constants } from '../common/Constants';
20import formProvider from '@ohos.application.formProvider';
21import type Want from '@ohos.app.ability.Want';
22import common from '@ohos.app.ability.common';
23
24const TAG: string = 'formA_The_FormController';
25
26export interface FormListener {
27  onDeleteForm(formId: string): void;
28
29  onUpdateFormData(formId: string): void;
30
31  onEvent(formId: string): void;
32
33  onCallback(formId: string): void;
34
35  onShowForm(formId: string): void;
36
37  updateFormData(formId: string, vars?: string[]): void;
38}
39
40export class FormController implements FormListener {
41  private static readonly MSG_ROUTER_PHOTOS = 'routerPhotos';
42  mediaDataManager: MediaDataManager;
43  private formId: string;
44  private callback: Function = null;
45  private indexValue: number = 0;
46
47  constructor(formId: string, operationMode: number, callback?: Function) {
48    this.formId = formId;
49    this.callback = callback;
50    this.mediaDataManager = new MediaDataManager(operationMode, this);
51    if (operationMode === Constants.PHOTOS_FORM_OPERATION_MODE_DESTROY) {
52      this.onDeleteForm(formId);
53    } else {
54      this.mediaDataManager.initData(formId);
55    }
56  }
57
58  bindFormData(formId: string): formBindingData.FormBindingData {
59    let fd: number = this.mediaDataManager.getCurrentFd();
60    Log.info(TAG, `bindFormData start formId: ${formId}  fd:${fd}`);
61    let image: string = this.imageHashCode(fd, formId);
62    let dataObj: object = {
63      fd: fd !== -1,
64      image0: 'memory://' + image,
65      image1: 'memory://' + image,
66      indexValue: this.indexValue,
67      albumName: this.mediaDataManager.getCurrentAlbumName(),
68      currentIndex: this.mediaDataManager.getCurrentIndex(),
69      isShow: this.mediaDataManager.getIsShowAlbumName(),
70      formImages: JSON.parse(`{ "${image}": ${fd} }`),
71      uri: this.mediaDataManager.getMediaData().currentUri !==
72        '' ? commonConstants.ACTION_URI_FORM_ABILITY : commonConstants.ACTION_URI_FORM_ABILITY_NONE,
73      albumUri: `${this.mediaDataManager.getMediaData().albumUri}`,
74      currentUri: this.mediaDataManager.getMediaData().currentUri
75    };
76    Log.debug(TAG, `bindFormData, createFormBindingData dataObj2.data: ${JSON.stringify(dataObj)}`);
77    let obj: formBindingData.FormBindingData = formBindingData.createFormBindingData(JSON.stringify(dataObj));
78    Log.debug(TAG, `bindFormData end, createFormBindingData obj2.data: ${JSON.stringify(obj.data)}`);
79    return obj;
80  }
81
82  imageHashCode(fd: number, formId: string) {
83    let mediaData = this.mediaDataManager.getMediaData();
84    return 'image_' + fd + '_formId_' + formId + '_uri_' + mediaData.currentUri + '_orientation_' + mediaData.orientation;
85  }
86
87  async updateFormData(formId: string, vars: string[]) {
88    Log.debug(TAG, `updateFormData formId: ${JSON.stringify(formId)}`);
89    let dataStore = DataStoreUtil.getInstance();
90    await dataStore.init();
91    this.indexValue = await dataStore.getData(Constants.FA_INDEX_VALUE, 0);
92    let formObj: formBindingData.FormBindingData = this.bindFormData(formId);
93    Log.debug(TAG, `updateFormData obj: ${JSON.stringify(formObj)}`);
94    formProvider.updateForm(formId, formObj)
95      .then(() => {
96        this.onCallback();
97        this.onDestroy();
98      }).catch((error) => {
99        Log.error(TAG, `updateForm failed. Cause: ${JSON.stringify(error)}`);
100        let msg = { 'err': JSON.stringify(error) };
101        ReportToBigDataUtil.errEventReport(BigDataConstants.SET_FA_CARD_IS_NAME_ERROR, msg);
102        this.mediaDataManager.closeFd();
103      });
104  }
105
106  onDestroy() {
107    Log.info(TAG, 'onDestroy start!');
108    this.mediaDataManager.closeFd();
109    this.callback = null;
110    Log.info(TAG, 'onDestroy done end!');
111  }
112
113  async onUpdateFormData(formId: string) {
114    Log.debug(TAG, `onUpdateFormData formId: ${formId}`);
115    let dataStore = DataStoreUtil.getInstance();
116    await dataStore.init();
117    let temp = await dataStore.getData(Constants.FA_INDEX_VALUE, 0);
118    this.indexValue = (temp + Constants.NUMBER_1) % Constants.NUMBER_2;
119    await dataStore.putData(Constants.FA_INDEX_VALUE, this.indexValue);
120    await dataStore.flush();
121    this.mediaDataManager.setNextIndex();
122  }
123
124  routerPhotoBrowser() {
125    Log.debug(TAG, 'routerPhotoBrowser start!');
126    let param: Want = {
127      'bundleName': 'com.ohos.photos',
128      'abilityName': 'com.ohos.photos.MainAbility',
129      'parameters': {
130        'uri': (this.mediaDataManager.getMediaData().currentUri !== '') ?
131          commonConstants.ACTION_URI_FORM_ABILITY : commonConstants.ACTION_URI_FORM_ABILITY_NONE,
132        'albumUri': `${this.mediaDataManager.getMediaData().albumUri}`,
133        'displayName': `${this.mediaDataManager.getMediaData().displayName}`,
134        'currentUri': this.mediaDataManager.getMediaData().currentUri
135      }
136    };
137    Log.debug(TAG, `routerPhotoBrowser parm ${JSON.stringify(param)}`);
138
139    let context: common.UIAbilityContext = AppStorage.get<common.UIAbilityContext>('formContext');
140    context.startAbility(param).then((): void => {
141      AppStorage.delete(Constants.FROM_CONTROLLER_MANAGER);
142    })
143
144    this.onDestroy();
145    Log.debug(TAG, 'routerPhotoBrowser end!');
146  }
147
148  onTriggerFormEvent(formId: string, message): void {
149    Log.debug(TAG, `onTriggerFormEvent ${formId} ${message}`);
150    let msgObj = JSON.parse(message);
151    let param = msgObj['params'];
152    let msg = param['message'];
153    Log.debug(TAG, `onTriggerFormEvent ${param} ${msg}`);
154    if (msg == FormController.MSG_ROUTER_PHOTOS) {
155      this.routerPhotoBrowser();
156    }
157  }
158
159  onEvent(formId: string): void {
160    Log.debug(TAG, 'onEvent start!');
161    if (this.callback != null) {
162      if (this.mediaDataManager.getUpdateTag()) {
163        this.mediaDataManager.setUpdateTag(false)
164        Log.debug(TAG, `updateFormData formId: ${JSON.stringify(formId)}`);
165        let formObj: formBindingData.FormBindingData = this.bindFormData(formId);
166        Log.debug(TAG, `updateFormData obj: ${JSON.stringify(formObj)}`);
167        formProvider.updateForm(formId, formObj).then(() => {
168          Log.info(TAG, 'updateFormData begin');
169          this.onTriggerFormEvent(formId, this.callback.call(this.callback));
170        }).catch((error) => {
171          this.onTriggerFormEvent(formId, this.callback.call(this.callback));
172        });
173      } else {
174        this.onTriggerFormEvent(formId, this.callback.call(this.callback));
175      }
176    }
177    Log.debug(TAG, 'onEvent end!');
178  }
179
180  onCallback(): void {
181    Log.debug(TAG, 'onCallback start!');
182    if (this.callback != null) {
183      this.callback.call(this.callback);
184    }
185    Log.debug(TAG, 'onCallback end!');
186  }
187
188  onDeleteForm(formId: string): void {
189    this.mediaDataManager.storageDelete();
190  }
191
192  async onShowForm(formId: string) {
193    Log.info(TAG, 'onShowForm start!');
194    ReportToBigDataUtil.report(BigDataConstants.SET_FA_CARD_IS_NAME_ID, null);
195    let isShowName = AppStorage.Get(Constants.FROM_PLAYBACK_ISSHOWNAME);
196    let isShowKey = Constants.FROM_PLAYBACK_ISSHOWNAME;
197    let dataStore = DataStoreUtil.getInstance();
198    await dataStore.init();
199    await dataStore.putData(isShowKey, isShowName ? 1 : 0);
200    await dataStore.flush();
201    this.updateFormData(formId, []);
202  }
203}