Skip to content

Commit

Permalink
hotfix: add permission handling for external storage and update log f…
Browse files Browse the repository at this point in the history
…ile naming
  • Loading branch information
Dr-Blank committed Oct 3, 2024
1 parent 5d2b9fd commit 33c57da
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 3 deletions.
4 changes: 4 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
android:label="Vaani"
android:name="${applicationName}"
android:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true"
android:icon="@mipmap/ic_launcher">
<!-- android:name=".MainActivity" -->
<activity
Expand Down
3 changes: 1 addition & 2 deletions lib/features/logging/providers/logs_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:vaani/features/logging/core/logger.dart';

part 'logs_provider.g.dart';

@riverpod
Expand Down Expand Up @@ -43,7 +42,7 @@ Future<String> generateZipFilePath() async {
}

String generateZipFileName() {
return 'vaani-${DateTime.now().toIso8601String()}.zip';
return 'vaani-${DateTime.now().microsecondsSinceEpoch}.zip';
}

Level parseLevel(String level) {
Expand Down
44 changes: 44 additions & 0 deletions lib/features/logging/view/logs_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:share_plus/share_plus.dart';
import 'package:vaani/features/logging/providers/logs_provider.dart';
import 'package:vaani/main.dart';
import 'package:vaani/settings/metadata/metadata_provider.dart';

class LogsPage extends HookConsumerWidget {
const LogsPage({super.key});
Expand Down Expand Up @@ -81,19 +83,61 @@ class LogsPage extends HookConsumerWidget {
icon: const Icon(Icons.download),
onPressed: () async {
appLogger.info('Preparing logs for download');

if (Platform.isAndroid) {
final androidVersion =
await ref.watch(deviceSdkVersionProvider.future);

if ((int.parse(androidVersion)) > 29) {
final status = await Permission.manageExternalStorage.status;
if (!status.isGranted) {
appLogger
.info('Requesting manageExternalStorage permission');
final newStatus =
await Permission.manageExternalStorage.request();
if (!newStatus.isGranted) {
appLogger
.warning('manageExternalStorage permission denied');
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Storage permission denied'),
),
);
return;
}
}
} else {
final status = await Permission.storage.status;
if (!status.isGranted) {
appLogger.info('Requesting storage permission');
final newStatus = await Permission.storage.request();
if (!newStatus.isGranted) {
appLogger.warning('Storage permission denied');
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Storage permission denied'),
),
);
return;
}
}
}
}
final zipLogFilePath =
await ref.read(logsProvider.notifier).getZipFilePath();

// save to folder
String? outputFile = await FilePicker.platform.saveFile(
dialogTitle: 'Please select an output file:',
fileName: zipLogFilePath.split('/').last,
bytes: await File(zipLogFilePath).readAsBytes(),
);
if (outputFile != null) {
try {
final file = File(outputFile);
final zipFile = File(zipLogFilePath);
await zipFile.copy(file.path);
appLogger.info('File saved to: $outputFile');
} catch (e) {
appLogger.severe('Error saving file: $e');
}
Expand Down
5 changes: 4 additions & 1 deletion lib/shared/widgets/add_new_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class AddNewServer extends HookConsumerWidget {
controller: controller,
keyboardType: TextInputType.url,
autofillHints: const [AutofillHints.url],
textInputAction: TextInputAction.next,
textInputAction: TextInputAction.done,
onFieldSubmitted: (_) {
onPressed?.call();
},
decoration: InputDecoration(
labelText: 'Server URI',
labelStyle: TextStyle(
Expand Down
48 changes: 48 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,54 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.0"
permission_handler:
dependency: "direct main"
description:
name: permission_handler
sha256: "18bf33f7fefbd812f37e72091a15575e72d5318854877e0e4035a24ac1113ecb"
url: "https://pub.dev"
source: hosted
version: "11.3.1"
permission_handler_android:
dependency: transitive
description:
name: permission_handler_android
sha256: "76e4ab092c1b240d31177bb64d2b0bea43f43d0e23541ec866151b9f7b2490fa"
url: "https://pub.dev"
source: hosted
version: "12.0.12"
permission_handler_apple:
dependency: transitive
description:
name: permission_handler_apple
sha256: e6f6d73b12438ef13e648c4ae56bd106ec60d17e90a59c4545db6781229082a0
url: "https://pub.dev"
source: hosted
version: "9.4.5"
permission_handler_html:
dependency: transitive
description:
name: permission_handler_html
sha256: af26edbbb1f2674af65a8f4b56e1a6f526156bc273d0e65dd8075fab51c78851
url: "https://pub.dev"
source: hosted
version: "0.1.3+2"
permission_handler_platform_interface:
dependency: transitive
description:
name: permission_handler_platform_interface
sha256: e9c8eadee926c4532d0305dff94b85bf961f16759c3af791486613152af4b4f9
url: "https://pub.dev"
source: hosted
version: "4.2.3"
permission_handler_windows:
dependency: transitive
description:
name: permission_handler_windows
sha256: "1a790728016f79a41216d88672dbc5df30e686e811ad4e698bfc51f76ad91f1e"
url: "https://pub.dev"
source: hosted
version: "0.2.1"
petitparser:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ dependencies:
package_info_plus: ^8.0.0
path: ^1.9.0
path_provider: ^2.1.0
permission_handler: ^11.3.1
riverpod_annotation: ^2.3.5
scroll_loop_auto_scroll: ^0.0.5
sensors_plus: ^6.0.1
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <isar_flutter_libs/isar_flutter_libs_plugin.h>
#include <media_kit_libs_windows_audio/media_kit_libs_windows_audio_plugin_c_api.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <share_plus/share_plus_windows_plugin_c_api.h>
#include <url_launcher_windows/url_launcher_windows.h>

Expand All @@ -16,6 +17,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("IsarFlutterLibsPlugin"));
MediaKitLibsWindowsAudioPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("MediaKitLibsWindowsAudioPluginCApi"));
PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
SharePlusWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("SharePlusWindowsPluginCApi"));
UrlLauncherWindowsRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
list(APPEND FLUTTER_PLUGIN_LIST
isar_flutter_libs
media_kit_libs_windows_audio
permission_handler_windows
share_plus
url_launcher_windows
)
Expand Down

0 comments on commit 33c57da

Please sign in to comment.