Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ trim_trailing_whitespace=false

[.drone.yml]
indent_size=2

[*.{kt,kts}]
# IDE does not follow this Ktlint rule strictly, but the default ordering is pretty good anyway, so let's ditch it
disabled_rules=import-ordering
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

dataBinding {
enabled true
}
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion scripts/analysis/lint-results.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DO NOT TOUCH; GENERATED BY DRONE
<span class="mdl-layout-title">Lint Report: 59 warnings</span>
<span class="mdl-layout-title">Lint Report: 58 warnings</span>
8 changes: 8 additions & 0 deletions spotbugs-filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<Bug pattern="IICU_INCORRECT_INTERNAL_CLASS_USE" />
</Match>

<!-- Data bindings autogenerated classes -->
<Match>
<Or>
<Class name="~.*BindingImpl"/>
<Class name="~.*\.DataBinderMapperImpl"/>
</Or>
</Match>

<Bug pattern="PATH_TRAVERSAL_IN" />
<Bug pattern="ANDROID_EXTERNAL_FILE_ACCESS" />
<Bug pattern="BAS_BLOATED_ASSIGNMENT_SCOPE" />
Expand Down
2 changes: 1 addition & 1 deletion src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
<activity android:name=".ui.activity.ConflictsResolveActivity"/>
<activity android:name=".ui.activity.ErrorsWhileCopyingHandlerActivity"/>

<activity android:name=".ui.activity.LogsActivity"/>
<activity android:name="com.nextcloud.client.logger.ui.LogsActivity"/>

<activity android:name="com.nextcloud.client.errorhandling.ShowErrorActivity"
android:theme="@style/Theme.ownCloud.Toolbar"
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/nextcloud/client/core/AsyncRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package com.nextcloud.client.core

typealias TaskBody<T> = () -> T
typealias OnResultCallback<T> = (T) -> Unit
typealias OnErrorCallback = (Throwable) -> Unit

Expand All @@ -29,5 +28,5 @@ typealias OnErrorCallback = (Throwable) -> Unit
* It is provided as an alternative for heavy, platform specific and virtually untestable [android.os.AsyncTask]
*/
interface AsyncRunner {
fun <T> post(block: () -> T, onResult: OnResultCallback<T>? = null, onError: OnErrorCallback? = null): Cancellable
fun <T> post(task: () -> T, onResult: OnResultCallback<T>? = null, onError: OnErrorCallback? = null): Cancellable
}
65 changes: 0 additions & 65 deletions src/main/java/com/nextcloud/client/core/AsyncRunnerImpl.kt

This file was deleted.

76 changes: 76 additions & 0 deletions src/main/java/com/nextcloud/client/core/ManualAsyncRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.core

import java.util.ArrayDeque

/**
* This async runner is suitable for tests, where manual simulation of
* asynchronous operations is desirable.
*/
class ManualAsyncRunner : AsyncRunner {

private val queue: ArrayDeque<Task<*>> = ArrayDeque()

override fun <T> post(task: () -> T, onResult: OnResultCallback<T>?, onError: OnErrorCallback?): Cancellable {
val taskWrapper = Task(
postResult = { it.run() },
taskBody = task,
onSuccess = onResult,
onError = onError
)
queue.push(taskWrapper)
return taskWrapper
}

val size: Int get() = queue.size
val isEmpty: Boolean get() = queue.size == 0

/**
* Run all enqueued tasks until queue is empty. This will run also tasks
* enqueued by task callbacks.
*
* @param maximum max number of tasks to run to avoid infinite loopss
* @return number of executed tasks
*/
fun runAll(maximum: Int = 100): Int {
var c = 0
while (queue.size > 0) {
val t = queue.remove()
t.run()
c++
if (c > maximum) {
throw IllegalStateException("Maximum number of tasks run. Are you in infinite loop?")
}
}
return c
}

/**
* Run one pending task
*
* @return true if task has been run
*/
fun runOne(): Boolean {
val t = queue.pollFirst()
t?.run()
return t != null
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/nextcloud/client/core/Task.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.core

import java.util.concurrent.atomic.AtomicBoolean

/**
* This is a wrapper for a function run in background.
*
* Runs task function and posts result if task is not cancelled.
*/
internal class Task<T>(
private val postResult: (Runnable) -> Unit,
private val taskBody: () -> T,
private val onSuccess: OnResultCallback<T>?,
private val onError: OnErrorCallback?
) : Runnable, Cancellable {

private val cancelled = AtomicBoolean(false)

override fun run() {
@Suppress("TooGenericExceptionCaught") // this is exactly what we want here
try {
val result = taskBody.invoke()
if (!cancelled.get()) {
postResult.invoke(Runnable {
onSuccess?.invoke(result)
})
}
} catch (t: Throwable) {
if (!cancelled.get()) {
postResult(Runnable { onError?.invoke(t) })
}
}
}

override fun cancel() {
cancelled.set(true)
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/nextcloud/client/core/ThreadPoolAsyncRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Nextcloud Android client application
*
* @author Chris Narkiewicz
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.core

import android.os.Handler
import java.util.concurrent.ScheduledThreadPoolExecutor

/**
* This async runner uses [java.util.concurrent.ScheduledThreadPoolExecutor] to run tasks
* asynchronously.
*
* Tasks are run on multi-threaded pool. If serialized execution is desired, set [corePoolSize] to 1.
*/
internal class ThreadPoolAsyncRunner(private val uiThreadHandler: Handler, corePoolSize: Int) : AsyncRunner {

private val executor = ScheduledThreadPoolExecutor(corePoolSize)

override fun <T> post(task: () -> T, onResult: OnResultCallback<T>?, onError: OnErrorCallback?): Cancellable {
val taskWrapper = Task(this::postResult, task, onResult, onError)
executor.execute(taskWrapper)
return taskWrapper
}

private fun postResult(r: Runnable) {
uiThreadHandler.post(r)
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/nextcloud/client/di/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.account.UserAccountManagerImpl;
import com.nextcloud.client.core.AsyncRunner;
import com.nextcloud.client.core.AsyncRunnerImpl;
import com.nextcloud.client.core.ThreadPoolAsyncRunner;
import com.nextcloud.client.core.Clock;
import com.nextcloud.client.core.ClockImpl;
import com.nextcloud.client.device.DeviceInfo;
Expand Down Expand Up @@ -144,6 +144,6 @@ LogsRepository logsRepository(Logger logger) {
@Singleton
AsyncRunner asyncRunner() {
Handler uiHandler = new Handler();
return new AsyncRunnerImpl(uiHandler, 4);
return new ThreadPoolAsyncRunner(uiHandler, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.activity.FilePickerActivity;
import com.owncloud.android.ui.activity.FolderPickerActivity;
import com.owncloud.android.ui.activity.LogsActivity;
import com.nextcloud.client.logger.ui.LogsActivity;
import com.owncloud.android.ui.activity.ManageAccountsActivity;
import com.owncloud.android.ui.activity.ManageSpaceActivity;
import com.owncloud.android.ui.activity.NotificationsActivity;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/nextcloud/client/logger/FileLogHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import java.nio.charset.Charset
*/
internal class FileLogHandler(private val logDir: File, private val logFilename: String, private val maxSize: Long) {

data class RawLogs(val lines: List<String>, val logSize: Long)

companion object {
const val ROTATED_LOGS_COUNT = 3
}
Expand Down Expand Up @@ -111,21 +113,23 @@ internal class FileLogHandler(private val logDir: File, private val logFilename:
}
}

fun loadLogFiles(rotated: Int = ROTATED_LOGS_COUNT): List<String> {
fun loadLogFiles(rotated: Int = ROTATED_LOGS_COUNT): RawLogs {
if (rotated < 0) {
throw IllegalArgumentException("Negative index")
}
val allLines = mutableListOf<String>()
var size = 0L
for (i in 0..Math.min(rotated, rotationList.size - 1)) {
val file = File(logDir, rotationList[i])
if (!file.exists()) continue
try {
val rotatedLines = file.readLines(charset = Charsets.UTF_8)
allLines.addAll(rotatedLines)
val lines = file.readLines(Charsets.UTF_8)
allLines.addAll(lines)
size += file.length()
} catch (ex: IOException) {
// ignore failing file
}
}
return allLines
return RawLogs(lines = allLines, logSize = size)
}
}
Loading