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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tests/proguard-project.txt
*.iml
build
/gradle.properties

.attach_pid*
fastlane/Fastfile
*.hprof

5 changes: 5 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ android {
versionName "1"
}
}

testOptions {
unitTests.returnDefaultValues = true
}
}

// adapt structure from Eclipse to Gradle/Android Studio expectations;
Expand Down
1 change: 1 addition & 0 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ test-pattern: # Configure exclusions for test sources
- 'ForEachOnRange'
- 'FunctionMaxLength'
- 'TooGenericExceptionCaught'
- 'TooGenericExceptionThrown'
- 'InstanceOfCheckForException'

build:
Expand Down
2 changes: 1 addition & 1 deletion scripts/analysis/findbugs-results.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
412
410
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.LogHistoryActivity"/>
<activity android:name=".ui.activity.LogsActivity"/>

<activity android:name="com.nextcloud.client.errorhandling.ShowErrorActivity"
android:theme="@style/Theme.ownCloud.Toolbar"
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/nextcloud/client/core/AsyncRunner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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
Comment thread
ezaquarii marked this conversation as resolved.

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

/**
* This interface allows to post background tasks that report results via callbacks invoked on main thread.
*
* 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
}
65 changes: 65 additions & 0 deletions src/main/java/com/nextcloud/client/core/AsyncRunnerImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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
Comment thread
ezaquarii marked this conversation as resolved.

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

internal class AsyncRunnerImpl(private val uiThreadHandler: Handler, corePoolSize: Int) : AsyncRunner {

private class Task<T>(
private val handler: Handler,
private val callable: () -> 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 = callable.invoke()
if (!cancelled.get()) {
handler.post {
onSuccess?.invoke(result)
}
}
} catch (t: Throwable) {
if (!cancelled.get()) {
handler.post { onError?.invoke(t) }
}
}
}

override fun cancel() {
cancelled.set(true)
}
}

private val executor = ScheduledThreadPoolExecutor(corePoolSize)

override fun <T> post(block: () -> T, onResult: OnResultCallback<T>?, onError: OnErrorCallback?): Cancellable {
val task = Task(uiThreadHandler, block, onResult, onError)
executor.execute(task)
return task
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/nextcloud/client/core/Cancellable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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

interface Cancellable {
fun cancel()
}
29 changes: 29 additions & 0 deletions src/main/java/com/nextcloud/client/core/Clock.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.Date
import java.util.TimeZone

interface Clock {
val currentTime: Long
val currentDate: Date
val tz: TimeZone
}
38 changes: 38 additions & 0 deletions src/main/java/com/nextcloud/client/core/ClockImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.Date
import java.util.TimeZone

class ClockImpl : Clock {
override val currentTime: Long
get() {
return System.currentTimeMillis()
}

override val currentDate: Date
get() {
return Date(currentTime)
}

override val tz: TimeZone
get() = TimeZone.getDefault()
}
42 changes: 42 additions & 0 deletions src/main/java/com/nextcloud/client/di/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;

import com.nextcloud.client.account.CurrentAccountProvider;
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.Clock;
import com.nextcloud.client.core.ClockImpl;
import com.nextcloud.client.device.DeviceInfo;
import com.nextcloud.client.logger.FileLogHandler;
import com.nextcloud.client.logger.Logger;
import com.nextcloud.client.logger.LoggerImpl;
import com.nextcloud.client.logger.LogsRepository;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.ui.activities.data.activities.ActivitiesRepository;
Expand All @@ -40,6 +49,10 @@
import com.owncloud.android.ui.activities.data.files.FilesServiceApiImpl;
import com.owncloud.android.ui.activities.data.files.RemoteFilesRepository;

import java.io.File;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

Expand Down Expand Up @@ -104,4 +117,33 @@ UploadsStorageManager uploadsStorageManager(Context context,
DeviceInfo deviceInfo() {
return new DeviceInfo();
}

@Provides
@Singleton
Clock clock() {
return new ClockImpl();
}

@Provides
@Singleton
Logger logger(Context context, Clock clock) {
File logDir = new File(context.getFilesDir(), "logs");
FileLogHandler handler = new FileLogHandler(logDir, "log.txt", 1024*1024);
LoggerImpl logger = new LoggerImpl(clock, handler, new Handler(), 1000);
logger.start();
return logger;
}

@Provides
@Singleton
LogsRepository logsRepository(Logger logger) {
return (LogsRepository)logger;
}

@Provides
@Singleton
AsyncRunner asyncRunner() {
Handler uiHandler = new Handler();
return new AsyncRunnerImpl(uiHandler, 4);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/nextcloud/client/di/ComponentsModule.java
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.LogHistoryActivity;
import com.owncloud.android.ui.activity.LogsActivity;
import com.owncloud.android.ui.activity.ManageAccountsActivity;
import com.owncloud.android.ui.activity.ManageSpaceActivity;
import com.owncloud.android.ui.activity.NotificationsActivity;
Expand Down Expand Up @@ -101,7 +101,7 @@ abstract class ComponentsModule {
@ContributesAndroidInjector abstract FilePickerActivity filePickerActivity();
@ContributesAndroidInjector abstract FirstRunActivity firstRunActivity();
@ContributesAndroidInjector abstract FolderPickerActivity folderPickerActivity();
@ContributesAndroidInjector abstract LogHistoryActivity logHistoryActivity();
@ContributesAndroidInjector abstract LogsActivity logsActivity();
@ContributesAndroidInjector abstract ManageAccountsActivity manageAccountsActivity();
@ContributesAndroidInjector abstract ManageSpaceActivity manageSpaceActivity();
@ContributesAndroidInjector abstract NotificationsActivity notificationsActivity();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/nextcloud/client/di/ViewModelModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package com.nextcloud.client.di
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.nextcloud.client.etm.EtmViewModel
import com.nextcloud.client.logger.ui.LogsViewModel
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
Expand All @@ -33,6 +34,11 @@ abstract class ViewModelModule {
@ViewModelKey(EtmViewModel::class)
abstract fun etmViewModel(vm: EtmViewModel): ViewModel

@Binds
@IntoMap
@ViewModelKey(LogsViewModel::class)
abstract fun logsViewModel(vm: LogsViewModel): ViewModel

@Binds
abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
}
Loading