diff --git a/data/DistributedPictures/README.md b/data/DistributedPictures/README.md new file mode 100644 index 0000000000000000000000000000000000000000..190271fac9a79c8800ef8cbec1ae288e63108ee3 --- /dev/null +++ b/data/DistributedPictures/README.md @@ -0,0 +1,9 @@ +# Distributed File Sharing + +- This sample demonstrates how to share files between multiple remote devices through a distributed shared path. + + Available capabilities include obtaining the distributed shared path, enabling sharing \(copying images to the distributed shared path\), + + reading and displaying images in the distributed shared path, and stopping sharing \(deleting all images in the distributed shared path\). + + diff --git a/data/DistributedPictures/README_zh.md b/data/DistributedPictures/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..51f68c2985db4747b1224143ff3d0f78ff7e61f4 --- /dev/null +++ b/data/DistributedPictures/README_zh.md @@ -0,0 +1,9 @@ +# 分布式文件共享 + +- 本示例演示了通过分布式共享路径策略,从而实现多远程设备间的文件共享。 + + 包括获取当前分布式共享路径、开启共享(将图片拷贝至分布式共享路径)、 + + 读取分布式路径下图片,并显示、停止共享(将分布式共享路径下图片删掉)。 + + diff --git a/data/DistributedPictures/build.gradle b/data/DistributedPictures/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..e76a14f743baa5377849b43abf29dd0fe29b6478 --- /dev/null +++ b/data/DistributedPictures/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } +} +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.4.2' + } +} +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } +} \ No newline at end of file diff --git a/data/DistributedPictures/entry/build.gradle b/data/DistributedPictures/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..594f9016e51cd0afef06b965647a56a947055f21 --- /dev/null +++ b/data/DistributedPictures/entry/build.gradle @@ -0,0 +1,19 @@ +apply plugin: 'com.huawei.ohos.hap' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/data/DistributedPictures/entry/src/main/config.json b/data/DistributedPictures/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..96b31437a4c748f167d7345d9e96a12a2895b415 --- /dev/null +++ b/data/DistributedPictures/entry/src/main/config.json @@ -0,0 +1,53 @@ +{ + "app": { + "bundleName": "ohos.samples.distributedpictures", + "version": { + "code": 1000000, + "name": "1.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.distributedpictures", + "name": ".MainAbility", + "reqCapabilities": [ + "video_support" + ], + "deviceType": [ + "default" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree":false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "portrait", + "formsEnabled": false, + "name": ".MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.DISTRIBUTED_DATASYNC" + } + ] + } +} \ No newline at end of file diff --git a/data/DistributedPictures/entry/src/main/java/ohos/samples/distributedpictures/MainAbility.java b/data/DistributedPictures/entry/src/main/java/ohos/samples/distributedpictures/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..29f80b4024469e4bc8c520a7e4c36cd5beb4ddb3 --- /dev/null +++ b/data/DistributedPictures/entry/src/main/java/ohos/samples/distributedpictures/MainAbility.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 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. + */ + +package ohos.samples.distributedpictures; + +import ohos.samples.distributedpictures.slice.MainAbilitySlice; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.bundle.IBundleManager; +import ohos.security.SystemPermission; + +/** + * MainAbility + */ +public class MainAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + requestPermission(); + super.setMainRoute(MainAbilitySlice.class.getName()); + } + + private void requestPermission() { + if (verifySelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) { + requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC}, 0); + } + } +} \ No newline at end of file diff --git a/data/DistributedPictures/entry/src/main/java/ohos/samples/distributedpictures/slice/MainAbilitySlice.java b/data/DistributedPictures/entry/src/main/java/ohos/samples/distributedpictures/slice/MainAbilitySlice.java new file mode 100644 index 0000000000000000000000000000000000000000..cf34ae3d6bc2bf5878a5dbbb39d8dd856532b50f --- /dev/null +++ b/data/DistributedPictures/entry/src/main/java/ohos/samples/distributedpictures/slice/MainAbilitySlice.java @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2021 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. + */ + +package ohos.samples.distributedpictures.slice; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Component; +import ohos.agp.components.Image; +import ohos.agp.window.dialog.ToastDialog; +import ohos.app.Context; +import ohos.data.distributed.file.DistFile; +import ohos.global.resource.RawFileEntry; +import ohos.global.resource.Resource; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.media.image.ImageSource; +import ohos.media.image.PixelMap; +import ohos.media.image.SourceDataMalformedException; +import ohos.media.image.common.PixelFormat; +import ohos.media.image.common.Rect; +import ohos.media.image.common.Size; +import ohos.samples.distributedpictures.ResourceTable; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * MainAbilitySlice + */ +public class MainAbilitySlice extends AbilitySlice { + private static final String TAG = MainAbilitySlice.class.getSimpleName(); + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG); + + private static final int CACHE_SIZE = 256 * 1024; + + private Image remoteImage; + + private String distributedFile; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_main_ability_slice); + initComponents(); + initDistributedFile(); + } + + private void initDistributedFile() { + distributedFile = this.getDistributedDir().getPath() + "/icon.png"; + HiLog.info(LABEL_LOG, "%{public}s", "distributedFile :" + distributedFile); + } + + private void initComponents() { + remoteImage = (Image) findComponentById(ResourceTable.Id_remote_image); + Component shareButton = findComponentById(ResourceTable.Id_save_to_distributed_dir); + Component deleteButton = findComponentById(ResourceTable.Id_delete_distributed_dir); + Component refreshButton = findComponentById(ResourceTable.Id_read_from_distributed_dir); + shareButton.setClickedListener(component -> copyPicToDistributedDir()); + deleteButton.setClickedListener(component -> deleteDistributedDir()); + refreshButton.setClickedListener(component -> readToDistributedDir()); + } + + private void readToDistributedDir() { + try { + File file = new File(distributedFile); + if (!file.exists()) { + showTip(this, "No pictures exists in the distributedDir"); + remoteImage.setPixelMap(null); + return; + } + ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); + ImageSource imageSource = ImageSource.create(distributedFile, srcOpts); + ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions(); + decodingOpts.desiredSize = new Size(0, 0); + decodingOpts.desiredRegion = new Rect(0, 0, 0, 0); + decodingOpts.desiredPixelFormat = PixelFormat.ARGB_8888; + PixelMap pixelMap = imageSource.createPixelmap(decodingOpts); + remoteImage.setPixelMap(pixelMap); + } catch (SourceDataMalformedException e) { + HiLog.error(LABEL_LOG, "%{public}s", "readToDistributedDir SourceDataMalformedException "); + } + } + + private void deleteDistributedDir() { + DistFile file = new DistFile(distributedFile); + if (file.exists() && file.isFile()) { + boolean result = file.delete(); + showTip(this, "delete :" + (result ? "success" : "fail")); + remoteImage.setPixelMap(null); + } else { + showTip(this, "No pictures exists in the distributedDir"); + } + } + + private void copyPicToDistributedDir() { + writeToDistributedDir("entry/resources/rawfile/icon.png", distributedFile); + File file = new File(distributedFile); + if (file.exists()) { + showTip(this, "shared success"); + } + } + + private void showTip(Context context, String msg) { + ToastDialog toastDialog = new ToastDialog(context); + toastDialog.setAutoClosable(false); + toastDialog.setContentText(msg); + toastDialog.show(); + } + + private void writeToDistributedDir(String rawFilePathString, String targetFilePath) { + RawFileEntry rawFileEntry = getResourceManager().getRawFileEntry(rawFilePathString); + try (FileOutputStream output = new FileOutputStream(new File(targetFilePath))) { + Resource resource = rawFileEntry.openRawFile(); + byte[] cache = new byte[CACHE_SIZE]; + int len = resource.read(cache); + while (len != -1) { + output.write(cache, 0, len); + len = resource.read(cache); + } + } catch (IOException e) { + HiLog.info(LABEL_LOG, "%{public}s", "writeToDisk IOException "); + } + } +} diff --git a/data/DistributedPictures/entry/src/main/resources/base/element/string.json b/data/DistributedPictures/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e05de56594a0dd7bd1b8afd510ad015d6d220d82 --- /dev/null +++ b/data/DistributedPictures/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "DistributedPictures" + }, + { + "name": "mainability_description", + "value": "hap sample empty page" + } + ] +} \ No newline at end of file diff --git a/data/DistributedPictures/entry/src/main/resources/base/graphic/bg.xml b/data/DistributedPictures/entry/src/main/resources/base/graphic/bg.xml new file mode 100644 index 0000000000000000000000000000000000000000..05f01b1d1d19dbd46040aff485b183320a3dfae3 --- /dev/null +++ b/data/DistributedPictures/entry/src/main/resources/base/graphic/bg.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/data/DistributedPictures/entry/src/main/resources/base/layout/main_ability_slice.xml b/data/DistributedPictures/entry/src/main/resources/base/layout/main_ability_slice.xml new file mode 100644 index 0000000000000000000000000000000000000000..65e082e822934a79251fda077e28ac1cd8d60202 --- /dev/null +++ b/data/DistributedPictures/entry/src/main/resources/base/layout/main_ability_slice.xml @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + +