From c40bf676cec81ba1c6aaa0821a12078502065028 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Tue, 2 Mar 2021 15:02:55 +0900 Subject: [PATCH] Use PlatformException instead of plugin-specific exception types (#43) --- packages/path_provider/CHANGELOG.md | 5 ++ packages/path_provider/README.md | 2 +- packages/path_provider/lib/src/storage.dart | 28 +++----- packages/path_provider/pubspec.yaml | 2 +- packages/url_launcher/CHANGELOG.md | 6 ++ packages/url_launcher/README.md | 9 ++- .../lib/{ => src}/app_control.dart | 67 ++++++++++--------- .../url_launcher/lib/url_launcher_tizen.dart | 3 +- packages/url_launcher/pubspec.yaml | 2 +- 9 files changed, 67 insertions(+), 57 deletions(-) rename packages/url_launcher/lib/{ => src}/app_control.dart (70%) diff --git a/packages/path_provider/CHANGELOG.md b/packages/path_provider/CHANGELOG.md index 139edeb84..94a53b682 100644 --- a/packages/path_provider/CHANGELOG.md +++ b/packages/path_provider/CHANGELOG.md @@ -8,3 +8,8 @@ * Update platform interface to the latest (1.0.4) * Remove permission_handler_tizen dependency temporarily * Migrate to Tizen 4.0 + +## 1.0.2 + +* Use `PlatformException` instead of a plugin-specific exception type + (`StorageException`) diff --git a/packages/path_provider/README.md b/packages/path_provider/README.md index a9b41c773..400c36a41 100644 --- a/packages/path_provider/README.md +++ b/packages/path_provider/README.md @@ -9,7 +9,7 @@ This package is not an _endorsed_ implementation of `path_provider`. Therefore, ```yaml dependencies: path_provider: ^1.6.27 - path_provider_tizen: ^1.0.1 + path_provider_tizen: ^1.0.2 ``` Then you can import `path_provider` in your Dart code: diff --git a/packages/path_provider/lib/src/storage.dart b/packages/path_provider/lib/src/storage.dart index f8ddc4ec3..fe51fb5eb 100644 --- a/packages/path_provider/lib/src/storage.dart +++ b/packages/path_provider/lib/src/storage.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'dart:ffi'; import 'package:ffi/ffi.dart'; +import 'package:flutter/services.dart'; // Native function signatures typedef storage_get_directory = Int32 Function( @@ -26,16 +27,6 @@ enum StorageDirectoryType { system_ringtones, } -class StorageException { - StorageException(this.returnCode, this.message); - - final int returnCode; - final String message; - - @override - String toString() => '$message ($returnCode)'; -} - Storage _storageInstance; Storage get storage => _storageInstance ??= Storage(); @@ -60,8 +51,10 @@ class Storage { final int ret = _storageForeachDeviceSupported( Pointer.fromFunction(_deviceSupportedCallback, 0), nullptr); if (ret != 0) { - throw StorageException( - ret, 'Failed to execute storage_foreach_device_supported.'); + throw PlatformException( + code: '$ret', + message: 'Failed to execute storage_foreach_device_supported.', + ); } } @@ -96,11 +89,12 @@ class Storage { }) async { final Pointer> path = allocate(); try { - final int result = - _storageGetDirectory(await storageId, type.index, path); - if (result != 0) { - throw StorageException( - result, 'Failed to execute storage_get_directory.'); + final int ret = _storageGetDirectory(await storageId, type.index, path); + if (ret != 0) { + throw PlatformException( + code: '$ret', + message: 'Failed to execute storage_get_directory.', + ); } return Utf8.fromUtf8(path.value); } finally { diff --git a/packages/path_provider/pubspec.yaml b/packages/path_provider/pubspec.yaml index 702dde909..17e067703 100644 --- a/packages/path_provider/pubspec.yaml +++ b/packages/path_provider/pubspec.yaml @@ -1,6 +1,6 @@ name: path_provider_tizen description: Tizen implementation of the path_provider plugin -version: 1.0.1 +version: 1.0.2 homepage: https://github.com/flutter-tizen/plugins flutter: diff --git a/packages/url_launcher/CHANGELOG.md b/packages/url_launcher/CHANGELOG.md index 0699526cd..efe12ef7e 100644 --- a/packages/url_launcher/CHANGELOG.md +++ b/packages/url_launcher/CHANGELOG.md @@ -11,3 +11,9 @@ * Update url_launcher to 5.7.10 * Update platform interface to 1.0.9 * Increase the minimum framework requirement to 1.22.0 + +## 1.0.3 + +* Use `PlatformException` instead of a plugin-specific exception type + (`AppControlException`) +* Move `app_control.dart` to the implementation directory diff --git a/packages/url_launcher/README.md b/packages/url_launcher/README.md index 0001a2f79..c751445e9 100644 --- a/packages/url_launcher/README.md +++ b/packages/url_launcher/README.md @@ -9,7 +9,7 @@ This package is not an _endorsed_ implementation of `url_launcher`. Therefore, y ```yaml dependencies: url_launcher: ^5.7.10 - url_launcher_tizen: ^1.0.2 + url_launcher_tizen: ^1.0.3 ``` Then you can import `url_launcher` in your Dart code: @@ -20,8 +20,6 @@ import 'package:url_launcher/url_launcher.dart'; For detailed usage, see https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher#usage. -An `AppControlException` is raised if no application on the device can launch the requested URL. - ## Required privileges To use this plugin in a Tizen application, the application manager privilege is required. Add below lines under the `` section in your `tizen-manifest.xml` file. @@ -33,3 +31,8 @@ To use this plugin in a Tizen application, the application manager privilege is ``` For details, see [Security and API Privileges](https://docs.tizen.org/application/dotnet/tutorials/sec-privileges). + +## Notes + +- A `PlatformException` is raised if no application on the device can open the provided URL. +- The `launch` method's optional parameters (e.g. `useWebView`) are not currently supported on Tizen. diff --git a/packages/url_launcher/lib/app_control.dart b/packages/url_launcher/lib/src/app_control.dart similarity index 70% rename from packages/url_launcher/lib/app_control.dart rename to packages/url_launcher/lib/src/app_control.dart index 9e3e0c0d0..c66ad7c4a 100644 --- a/packages/url_launcher/lib/app_control.dart +++ b/packages/url_launcher/lib/src/app_control.dart @@ -4,6 +4,7 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; +import 'package:flutter/services.dart'; // Native function signatures typedef app_control_create = Int32 Function(Pointer>); @@ -38,32 +39,24 @@ const int TIZEN_ERROR_IO_ERROR = -5; class _AppControl extends Struct {} -/// A class representing non-zero errors from the platform. -class AppControlException { - const AppControlException(this.returnCode, this.message); - - final int returnCode; - final String message; - - // Constants from [app_control_error_e] in `app_control.h` - static const Map errorCodes = { - TIZEN_ERROR_NONE: 'APP_CONTROL_ERROR_NONE', - TIZEN_ERROR_INVALID_PARAMETER: 'APP_CONTROL_ERROR_INVALID_PARAMETER', - TIZEN_ERROR_OUT_OF_MEMORY: 'APP_CONTROL_ERROR_OUT_OF_MEMORY', - TIZEN_ERROR_APPLICATION | 0x21: 'APP_CONTROL_ERROR_APP_NOT_FOUND', - TIZEN_ERROR_KEY_NOT_AVAILABLE: 'APP_CONTROL_ERROR_KEY_NOT_FOUND', - TIZEN_ERROR_KEY_REJECTED: 'APP_CONTROL_ERROR_KEY_REJECTED', - TIZEN_ERROR_APPLICATION | 0x22: 'APP_CONTROL_ERROR_INVALID_DATA_TYPE', - TIZEN_ERROR_APPLICATION | 0x23: 'APP_CONTROL_ERROR_LAUNCH_REJECTED', - TIZEN_ERROR_PERMISSION_DENIED: 'APP_CONTROL_ERROR_PERMISSION_DENIED', - TIZEN_ERROR_APPLICATION | 0x24: 'APP_CONTROL_ERROR_LAUNCH_FAILED', - TIZEN_ERROR_TIMED_OUT: 'APP_CONTROL_ERROR_TIMED_OUT', - TIZEN_ERROR_IO_ERROR: 'APP_CONTROL_ERROR_IO_ERROR', - }; - - @override - String toString() => '$message (${errorCodes[returnCode] ?? returnCode})'; -} +// Constants from [app_control_error_e] in `app_control.h` +const Map errorCodes = { + TIZEN_ERROR_NONE: 'APP_CONTROL_ERROR_NONE', + TIZEN_ERROR_INVALID_PARAMETER: 'APP_CONTROL_ERROR_INVALID_PARAMETER', + TIZEN_ERROR_OUT_OF_MEMORY: 'APP_CONTROL_ERROR_OUT_OF_MEMORY', + TIZEN_ERROR_APPLICATION | 0x21: 'APP_CONTROL_ERROR_APP_NOT_FOUND', + TIZEN_ERROR_KEY_NOT_AVAILABLE: 'APP_CONTROL_ERROR_KEY_NOT_FOUND', + TIZEN_ERROR_KEY_REJECTED: 'APP_CONTROL_ERROR_KEY_REJECTED', + TIZEN_ERROR_APPLICATION | 0x22: 'APP_CONTROL_ERROR_INVALID_DATA_TYPE', + TIZEN_ERROR_APPLICATION | 0x23: 'APP_CONTROL_ERROR_LAUNCH_REJECTED', + TIZEN_ERROR_PERMISSION_DENIED: 'APP_CONTROL_ERROR_PERMISSION_DENIED', + TIZEN_ERROR_APPLICATION | 0x24: 'APP_CONTROL_ERROR_LAUNCH_FAILED', + TIZEN_ERROR_TIMED_OUT: 'APP_CONTROL_ERROR_TIMED_OUT', + TIZEN_ERROR_IO_ERROR: 'APP_CONTROL_ERROR_IO_ERROR', +}; + +String getErrorCode(int returnCode) => + errorCodes.containsKey(returnCode) ? errorCodes[returnCode] : '$returnCode'; /// A wrapper class of the native App Control API. /// Not all functions or values are supported. @@ -109,7 +102,10 @@ class AppControl { free(pHandle); if (ret != 0) { - throw AppControlException(ret, 'Failed to execute app_control_create.'); + throw PlatformException( + code: getErrorCode(ret), + message: 'Failed to execute app_control_create.', + ); } } @@ -118,8 +114,10 @@ class AppControl { final int ret = _setOperation(_handle, Utf8.toUtf8(operation)); if (ret != 0) { - throw AppControlException( - ret, 'Failed to execute app_control_set_operation.'); + throw PlatformException( + code: getErrorCode(ret), + message: 'Failed to execute app_control_set_operation.', + ); } } @@ -128,7 +126,10 @@ class AppControl { final int ret = _setUri(_handle, Utf8.toUtf8(uri)); if (ret != 0) { - throw AppControlException(ret, 'Failed to execute app_control_set_uri.'); + throw PlatformException( + code: getErrorCode(ret), + message: 'Failed to execute app_control_set_uri.', + ); } } @@ -137,8 +138,10 @@ class AppControl { final int ret = _sendLaunchRequest(_handle, nullptr, nullptr); if (ret != 0) { - throw AppControlException( - ret, 'Failed to execute app_control_send_launch_request.'); + throw PlatformException( + code: getErrorCode(ret), + message: 'Failed to execute app_control_send_launch_request.', + ); } } diff --git a/packages/url_launcher/lib/url_launcher_tizen.dart b/packages/url_launcher/lib/url_launcher_tizen.dart index 13f4f26cf..bf9dcef4e 100644 --- a/packages/url_launcher/lib/url_launcher_tizen.dart +++ b/packages/url_launcher/lib/url_launcher_tizen.dart @@ -8,7 +8,7 @@ import 'package:meta/meta.dart'; import 'package:url_launcher_platform_interface/link.dart'; import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; -import 'app_control.dart'; +import 'src/app_control.dart'; class UrlLauncherPlugin extends UrlLauncherPlatform { static final Set _supportedSchemes = { @@ -35,7 +35,6 @@ class UrlLauncherPlugin extends UrlLauncherPlatform { @override Future launch( String url, { - // None of these options are used in Tizen. @required bool useSafariVC, @required bool useWebView, @required bool enableJavaScript, diff --git a/packages/url_launcher/pubspec.yaml b/packages/url_launcher/pubspec.yaml index 23e8b2296..a1970afb4 100644 --- a/packages/url_launcher/pubspec.yaml +++ b/packages/url_launcher/pubspec.yaml @@ -1,7 +1,7 @@ name: url_launcher_tizen description: Tizen implementation of the url_launcher plugin homepage: https://github.com/flutter-tizen/plugins -version: 1.0.2 +version: 1.0.3 flutter: plugin: