Skip to content

Commit

Permalink
refactor: Lint Kotlin, C and C++ code (#1610)
Browse files Browse the repository at this point in the history
Test Linting Github Action Super Linter, to uniform source code:
https://github.com/marketplace/actions/super-linter
  • Loading branch information
Gustl22 committed Aug 20, 2023
1 parent 1a3de1a commit 0539466
Show file tree
Hide file tree
Showing 40 changed files with 1,798 additions and 1,766 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Defines the Chromium style for automatic reformatting.
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: Chromium
Standard: c++17
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[{*.kt,*.kts}]
max_line_length = 120
insert_final_newline = true
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_continuation_indent_size = 4
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_allow_trailing_comma = true
ij_kotlin_name_count_to_use_star_import = 999
ij_kotlin_name_count_to_use_star_import_for_members = 999
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ jobs:
- run: melos run analyze -- ${{ inputs.fatal_warnings && '--fatal-infos' || '--no-fatal-warnings --no-fatal-infos' }}
- run: melos run test

- name: Lint Code Base
uses: super-linter/super-linter@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: main
VALIDATE_KOTLIN_ANDROID: true
VALIDATE_CLANG_FORMAT: true

web:
runs-on: ubuntu-latest
timeout-minutes: 30
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ package xyz.luan.audioplayers.example

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}
class MainActivity : FlutterActivity()
21 changes: 12 additions & 9 deletions packages/audioplayers/example/linux/my_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static void my_application_activate(GApplication* application) {
gtk_widget_show(GTK_WIDGET(window));

g_autoptr(FlDartProject) project = fl_dart_project_new();
fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments);
fl_dart_project_set_dart_entrypoint_arguments(
project, self->dart_entrypoint_arguments);

FlView* view = fl_view_new(project);
gtk_widget_show(GTK_WIDGET(view));
Expand All @@ -63,16 +64,18 @@ static void my_application_activate(GApplication* application) {
}

// Implements GApplication::local_command_line.
static gboolean my_application_local_command_line(GApplication* application, gchar*** arguments, int* exit_status) {
static gboolean my_application_local_command_line(GApplication* application,
gchar*** arguments,
int* exit_status) {
MyApplication* self = MY_APPLICATION(application);
// Strip out the first argument as it is the binary name.
self->dart_entrypoint_arguments = g_strdupv(*arguments + 1);

g_autoptr(GError) error = nullptr;
if (!g_application_register(application, nullptr, &error)) {
g_warning("Failed to register: %s", error->message);
*exit_status = 1;
return TRUE;
g_warning("Failed to register: %s", error->message);
*exit_status = 1;
return TRUE;
}

g_application_activate(application);
Expand All @@ -90,15 +93,15 @@ static void my_application_dispose(GObject* object) {

static void my_application_class_init(MyApplicationClass* klass) {
G_APPLICATION_CLASS(klass)->activate = my_application_activate;
G_APPLICATION_CLASS(klass)->local_command_line = my_application_local_command_line;
G_APPLICATION_CLASS(klass)->local_command_line =
my_application_local_command_line;
G_OBJECT_CLASS(klass)->dispose = my_application_dispose;
}

static void my_application_init(MyApplication* self) {}

MyApplication* my_application_new() {
return MY_APPLICATION(g_object_new(my_application_get_type(),
"application-id", APPLICATION_ID,
"flags", G_APPLICATION_NON_UNIQUE,
nullptr));
"application-id", APPLICATION_ID, "flags",
G_APPLICATION_NON_UNIQUE, nullptr));
}
5 changes: 4 additions & 1 deletion packages/audioplayers/example/linux/my_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

#include <gtk/gtk.h>

G_DECLARE_FINAL_TYPE(MyApplication, my_application, MY, APPLICATION,
G_DECLARE_FINAL_TYPE(MyApplication,
my_application,
MY,
APPLICATION,
GtkApplication)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ void FlutterWindow::OnDestroy() {
}

LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
FlutterWindow::MessageHandler(HWND hwnd,
UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Give Flutter, including plugins, an opportunity to handle window messages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class FlutterWindow : public Win32Window {
// Win32Window:
bool OnCreate() override;
void OnDestroy() override;
LRESULT MessageHandler(HWND window, UINT const message, WPARAM const wparam,
LRESULT MessageHandler(HWND window,
UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept override;

private:
Expand Down
9 changes: 5 additions & 4 deletions packages/audioplayers/example/windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include "flutter_window.h"
#include "utils.h"

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {
int APIENTRY wWinMain(_In_ HINSTANCE instance,
_In_opt_ HINSTANCE prev,
_In_ wchar_t* command_line,
_In_ int show_command) {
// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
Expand All @@ -19,8 +21,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,

flutter::DartProject project(L"data");

std::vector<std::string> command_line_arguments =
GetCommandLineArguments();
std::vector<std::string> command_line_arguments = GetCommandLineArguments();

project.set_dart_entrypoint_arguments(std::move(command_line_arguments));

Expand Down
10 changes: 5 additions & 5 deletions packages/audioplayers/example/windows/runner/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Microsoft Visual C++ generated include file.
// Used by Runner.rc
//
#define IDI_APP_ICON 101
#define IDI_APP_ICON 101

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
11 changes: 5 additions & 6 deletions packages/audioplayers/example/windows/runner/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

void CreateAndAttachConsole() {
if (::AllocConsole()) {
FILE *unused;
FILE* unused;
if (freopen_s(&unused, "CONOUT$", "w", stdout)) {
_dup2(_fileno(stdout), 1);
}
Expand Down Expand Up @@ -45,17 +45,16 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
if (utf16_string == nullptr) {
return std::string();
}
int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr);
int target_length =
::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1,
nullptr, 0, nullptr, nullptr);
if (target_length == 0) {
return std::string();
}
std::string utf8_string;
utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(),
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string, -1, utf8_string.data(),
target_length, nullptr, nullptr);
if (converted_length == 0) {
return std::string();
Expand Down
2 changes: 1 addition & 1 deletion packages/audioplayers_android/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ android {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

lint {
disable 'InvalidPackage'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package xyz.luan.audioplayers

import android.annotation.SuppressLint
import android.media.AudioAttributes
import android.media.AudioAttributes.*
import android.media.AudioAttributes.Builder
import android.media.AudioAttributes.CONTENT_TYPE_MUSIC
import android.media.AudioAttributes.USAGE_MEDIA
import android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE
import android.media.AudioAttributes.USAGE_VOICE_COMMUNICATION
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Build
import androidx.annotation.RequiresApi
import java.util.Objects
import java.util.*

data class AudioContextAndroid(
val isSpeakerphoneOn: Boolean,
Expand Down Expand Up @@ -55,11 +59,11 @@ data class AudioContextAndroid(

override fun hashCode() = Objects.hash(isSpeakerphoneOn, stayAwake, contentType, usageType, audioFocus, audioMode)

override fun equals(other: Any?) = (other is AudioContextAndroid)
&& isSpeakerphoneOn == other.isSpeakerphoneOn
&& stayAwake == other.stayAwake
&& contentType == other.contentType
&& usageType == other.usageType
&& audioFocus == other.audioFocus
&& audioMode == other.audioMode
override fun equals(other: Any?) = (other is AudioContextAndroid) &&
isSpeakerphoneOn == other.isSpeakerphoneOn &&
stayAwake == other.stayAwake &&
contentType == other.contentType &&
usageType == other.usageType &&
audioFocus == other.audioFocus &&
audioMode == other.audioMode
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xyz.luan.audioplayers


import android.content.Context
import android.media.AudioManager
import android.os.Build
Expand Down Expand Up @@ -232,7 +231,7 @@ class AudioplayersPlugin : FlutterPlugin, IUpdateCallback {
handler.post {
player.eventHandler.success(
"audio.onDuration",
hashMapOf("value" to (player.getDuration() ?: 0))
hashMapOf("value" to (player.getDuration() ?: 0)),
)
}
}
Expand Down Expand Up @@ -265,7 +264,8 @@ class AudioplayersPlugin : FlutterPlugin, IUpdateCallback {
handler.post {
player.eventHandler.success("audio.onSeekComplete")
player.eventHandler.success(
"audio.onCurrentPosition", hashMapOf("value" to (player.getCurrentPosition() ?: 0))
"audio.onCurrentPosition",
hashMapOf("value" to (player.getCurrentPosition() ?: 0)),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.annotation.RequiresApi

@RequiresApi(Build.VERSION_CODES.M)
class ByteDataSource(
private val data: ByteArray
private val data: ByteArray,
) : MediaDataSource() {
@Synchronized
override fun getSize(): Long = data.size.toLong()
Expand All @@ -32,5 +32,4 @@ class ByteDataSource(
}
return remainingSize.toInt()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package xyz.luan.audioplayers

enum class PlayerMode {
MEDIA_PLAYER, LOW_LATENCY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package xyz.luan.audioplayers

enum class ReleaseMode {
RELEASE, LOOP, STOP
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FocusManager(

@RequiresApi(Build.VERSION_CODES.O)
private fun newRequestAudioFocus(andThen: () -> Unit) {
val audioFocus = context.audioFocus ?: return andThen()
val audioFocus = context.audioFocus

val audioFocusRequest = AudioFocusRequest.Builder(audioFocus)
.setAudioAttributes(context.buildAttributes())
Expand All @@ -56,7 +56,7 @@ class FocusManager(

@Deprecated("Use requestAudioFocus instead")
private fun oldRequestAudioFocus(andThen: () -> Unit) {
val audioFocus = context.audioFocus ?: return andThen()
val audioFocus = context.audioFocus
audioFocusChangeListener = AudioManager.OnAudioFocusChangeListener { handleFocusResult(it, andThen) }
@Suppress("DEPRECATION")
val result = audioManager.requestAudioFocus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import xyz.luan.audioplayers.source.Source
import xyz.luan.audioplayers.source.UrlSource
import java.util.Collections.synchronizedMap

/// Value should not exceed 32
/** Value should not exceed 32 */
// TODO(luan): make this configurable
private const val MAX_STREAMS = 32

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package xyz.luan.audioplayers.player
import android.content.Context
import android.media.AudioManager
import android.media.MediaPlayer
import kotlin.math.min
import xyz.luan.audioplayers.*
import xyz.luan.audioplayers.AudioContextAndroid
import xyz.luan.audioplayers.AudioplayersPlugin
import xyz.luan.audioplayers.EventHandler
import xyz.luan.audioplayers.PlayerMode
import xyz.luan.audioplayers.PlayerMode.LOW_LATENCY
import xyz.luan.audioplayers.PlayerMode.MEDIA_PLAYER
import xyz.luan.audioplayers.ReleaseMode
import xyz.luan.audioplayers.source.Source
import kotlin.math.min

// For some reason this cannot be accessed from MediaPlayer.MEDIA_ERROR_SYSTEM
private const val MEDIA_ERROR_SYSTEM = -2147483648
Expand Down Expand Up @@ -137,8 +141,9 @@ class WrappedPlayer internal constructor(
if (context == audioContext) {
return
}
if (context.audioFocus != AudioManager.AUDIOFOCUS_NONE
&& audioContext.audioFocus == AudioManager.AUDIOFOCUS_NONE) {
if (context.audioFocus != AudioManager.AUDIOFOCUS_NONE &&
audioContext.audioFocus == AudioManager.AUDIOFOCUS_NONE
) {
focusManager.handleStop()
}
this.context = audioContext.copy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import xyz.luan.audioplayers.player.SoundPoolPlayer
@RequiresApi(Build.VERSION_CODES.M)
data class BytesSource(
val dataSource: ByteDataSource,
): Source {
constructor(bytes: ByteArray): this(ByteDataSource(bytes))
) : Source {
constructor(bytes: ByteArray) : this(ByteDataSource(bytes))

override fun setForMediaPlayer(mediaPlayer: MediaPlayer) {
mediaPlayer.setDataSource(dataSource)
Expand All @@ -19,4 +19,4 @@ data class BytesSource(
override fun setForSoundPool(soundPoolPlayer: SoundPoolPlayer) {
error("Bytes sources are not supported on LOW_LATENCY mode yet.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ import xyz.luan.audioplayers.player.SoundPoolPlayer
interface Source {
fun setForMediaPlayer(mediaPlayer: MediaPlayer)
fun setForSoundPool(soundPoolPlayer: SoundPoolPlayer)
}
}
Loading

0 comments on commit 0539466

Please sign in to comment.