mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Revert "Temporarily remove device sync"
This reverts commit da018fc64d.
This commit is contained in:
@@ -122,6 +122,7 @@ dependencies {
|
||||
implementation("androidx.legacy:legacy-preference-v14:1.0.0")
|
||||
implementation("androidx.legacy:legacy-support-v4:1.0.0")
|
||||
implementation("com.google.android.material:material:1.3.0")
|
||||
implementation("com.google.zxing:core:3.4.1")
|
||||
implementation("com.opencsv:opencsv:5.4")
|
||||
implementation(project(":uhabits-core"))
|
||||
kapt("com.google.dagger:dagger-compiler:$daggerVersion")
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.sync
|
||||
|
||||
import androidx.test.filters.MediumTest
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.mock.MockEngine
|
||||
import io.ktor.client.engine.mock.MockRequestHandleScope
|
||||
import io.ktor.client.engine.mock.respond
|
||||
import io.ktor.client.engine.mock.respondError
|
||||
import io.ktor.client.engine.mock.respondOk
|
||||
import io.ktor.client.features.json.JsonFeature
|
||||
import io.ktor.client.request.HttpRequestData
|
||||
import io.ktor.client.request.HttpResponseData
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.fullPath
|
||||
import io.ktor.http.headersOf
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.isoron.uhabits.BaseAndroidTest
|
||||
import org.isoron.uhabits.core.sync.AbstractSyncServer
|
||||
import org.isoron.uhabits.core.sync.GetDataVersionResponse
|
||||
import org.isoron.uhabits.core.sync.KeyNotFoundException
|
||||
import org.isoron.uhabits.core.sync.RegisterReponse
|
||||
import org.isoron.uhabits.core.sync.ServiceUnavailable
|
||||
import org.isoron.uhabits.core.sync.SyncData
|
||||
import org.junit.Test
|
||||
|
||||
@MediumTest
|
||||
class RemoteSyncServerTest : BaseAndroidTest() {
|
||||
|
||||
private val mapper = ObjectMapper()
|
||||
val data = SyncData(1, "Hello world")
|
||||
|
||||
@Test
|
||||
fun when_register_succeeds_should_return_key() = runBlocking {
|
||||
val server = server("/register") {
|
||||
respondWithJson(RegisterReponse("ABCDEF"))
|
||||
}
|
||||
assertEquals("ABCDEF", server.register())
|
||||
}
|
||||
|
||||
@Test(expected = ServiceUnavailable::class)
|
||||
fun when_register_fails_should_raise_correct_exception() = runBlocking {
|
||||
val server = server("/register") {
|
||||
respondError(HttpStatusCode.ServiceUnavailable)
|
||||
}
|
||||
server.register()
|
||||
return@runBlocking
|
||||
}
|
||||
|
||||
@Test
|
||||
fun when_get_data_version_succeeds_should_return_version() = runBlocking {
|
||||
server("/db/ABC/version") {
|
||||
respondWithJson(GetDataVersionResponse(5))
|
||||
}.apply {
|
||||
assertEquals(5, getDataVersion("ABC"))
|
||||
}
|
||||
return@runBlocking
|
||||
}
|
||||
|
||||
@Test(expected = ServiceUnavailable::class)
|
||||
fun when_get_data_version_with_server_error_should_raise_exception() = runBlocking {
|
||||
server("/db/ABC/version") {
|
||||
respondError(HttpStatusCode.InternalServerError)
|
||||
}.apply {
|
||||
getDataVersion("ABC")
|
||||
}
|
||||
return@runBlocking
|
||||
}
|
||||
|
||||
@Test(expected = KeyNotFoundException::class)
|
||||
fun when_get_data_version_with_invalid_key_should_raise_exception() = runBlocking {
|
||||
server("/db/ABC/version") {
|
||||
respondError(HttpStatusCode.NotFound)
|
||||
}.apply {
|
||||
getDataVersion("ABC")
|
||||
}
|
||||
return@runBlocking
|
||||
}
|
||||
|
||||
@Test
|
||||
fun when_get_data_succeeds_should_return_data() = runBlocking {
|
||||
server("/db/ABC") {
|
||||
respondWithJson(data)
|
||||
}.apply {
|
||||
assertEquals(data, getData("ABC"))
|
||||
}
|
||||
return@runBlocking
|
||||
}
|
||||
|
||||
@Test(expected = KeyNotFoundException::class)
|
||||
fun when_get_data_with_invalid_key_should_raise_exception() = runBlocking {
|
||||
server("/db/ABC") {
|
||||
respondError(HttpStatusCode.NotFound)
|
||||
}.apply {
|
||||
getData("ABC")
|
||||
}
|
||||
return@runBlocking
|
||||
}
|
||||
|
||||
@Test
|
||||
fun when_put_succeeds_should_not_raise_exceptions() = runBlocking {
|
||||
server("/db/ABC") {
|
||||
respondOk()
|
||||
}.apply {
|
||||
put("ABC", data)
|
||||
}
|
||||
return@runBlocking
|
||||
}
|
||||
|
||||
private fun server(
|
||||
expectedPath: String,
|
||||
action: MockRequestHandleScope.(HttpRequestData) -> HttpResponseData
|
||||
): AbstractSyncServer {
|
||||
return RemoteSyncServer(
|
||||
httpClient = HttpClient(MockEngine) {
|
||||
install(JsonFeature)
|
||||
engine {
|
||||
addHandler { request ->
|
||||
when (request.url.fullPath) {
|
||||
expectedPath -> action(request)
|
||||
else -> error("unexpected call: ${request.url.fullPath}")
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
preferences = prefs
|
||||
)
|
||||
}
|
||||
|
||||
private fun MockRequestHandleScope.respondWithJson(content: Any) =
|
||||
respond(
|
||||
mapper.writeValueAsBytes(content),
|
||||
headers = headersOf("Content-Type" to listOf("application/json"))
|
||||
)
|
||||
}
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:name=".HabitsApplication"
|
||||
@@ -40,6 +42,14 @@
|
||||
android:value=".activities.habits.list.ListHabitsActivity" />
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".activities.sync.SyncActivity"
|
||||
android:exported="true">
|
||||
<meta-data
|
||||
android:name="android.support.PARENT_ACTIVITY"
|
||||
android:value=".activities.settings.SettingsActivity" />
|
||||
</activity>
|
||||
|
||||
<meta-data
|
||||
android:name="com.google.android.backup.api_key"
|
||||
android:value="AEdPqrEAAAAI6aeWncbnMNo8E5GWeZ44dlc5cQ7tCROwFhOtiw" />
|
||||
@@ -49,6 +59,16 @@
|
||||
android:exported="true"
|
||||
android:label="@string/main_activity_title"
|
||||
android:launchMode="singleTop">
|
||||
<tools:validation testUrl="https://loophabits.org/sync/123" />
|
||||
<intent-filter android:autoVerify="true">
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
<data
|
||||
android:scheme="https"
|
||||
android:host="loophabits.org"
|
||||
android:pathPrefix="/sync" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity-alias
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.isoron.uhabits.activities.common.dialogs
|
||||
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.core.ui.callbacks.OnConfirmedCallback
|
||||
import org.isoron.uhabits.inject.ActivityContext
|
||||
|
||||
class ConfirmSyncKeyDialog(
|
||||
@ActivityContext context: Context,
|
||||
callback: OnConfirmedCallback
|
||||
) : AlertDialog(context) {
|
||||
init {
|
||||
setTitle(R.string.device_sync)
|
||||
val res = context.resources
|
||||
setMessage(res.getString(R.string.sync_confirm))
|
||||
setButton(
|
||||
BUTTON_POSITIVE,
|
||||
res.getString(R.string.yes)
|
||||
) { dialog: DialogInterface?, which: Int -> callback.onConfirmed() }
|
||||
setButton(
|
||||
BUTTON_NEGATIVE,
|
||||
res.getString(R.string.no)
|
||||
) { dialog: DialogInterface?, which: Int -> }
|
||||
}
|
||||
}
|
||||
@@ -26,10 +26,12 @@ import android.view.MenuItem
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import org.isoron.uhabits.BaseExceptionHandler
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
import org.isoron.uhabits.activities.habits.list.views.HabitCardListAdapter
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.sync.SyncManager
|
||||
import org.isoron.uhabits.core.tasks.TaskRunner
|
||||
import org.isoron.uhabits.core.ui.ThemeSwitcher.Companion.THEME_DARK
|
||||
import org.isoron.uhabits.core.utils.MidnightTimer
|
||||
@@ -47,6 +49,7 @@ class ListHabitsActivity : AppCompatActivity() {
|
||||
lateinit var screen: ListHabitsScreen
|
||||
lateinit var prefs: Preferences
|
||||
lateinit var midnightTimer: MidnightTimer
|
||||
lateinit var syncManager: SyncManager
|
||||
private val scope = CoroutineScope(Dispatchers.Main)
|
||||
|
||||
private lateinit var menu: ListHabitsMenu
|
||||
@@ -63,6 +66,7 @@ class ListHabitsActivity : AppCompatActivity() {
|
||||
component.themeSwitcher.apply()
|
||||
|
||||
prefs = appComponent.preferences
|
||||
syncManager = appComponent.syncManager
|
||||
pureBlack = prefs.isPureBlackEnabled
|
||||
midnightTimer = appComponent.midnightTimer
|
||||
rootView = component.listHabitsRootView
|
||||
@@ -79,6 +83,9 @@ class ListHabitsActivity : AppCompatActivity() {
|
||||
midnightTimer.onPause()
|
||||
screen.onDettached()
|
||||
adapter.cancelRefresh()
|
||||
scope.launch {
|
||||
syncManager.onPause()
|
||||
}
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
@@ -87,6 +94,9 @@ class ListHabitsActivity : AppCompatActivity() {
|
||||
screen.onAttached()
|
||||
rootView.postInvalidate()
|
||||
midnightTimer.onResume()
|
||||
scope.launch {
|
||||
syncManager.onResume()
|
||||
}
|
||||
taskRunner.run {
|
||||
AutoBackup(this@ListHabitsActivity).run()
|
||||
}
|
||||
|
||||
@@ -22,11 +22,13 @@ package org.isoron.uhabits.activities.habits.list
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import dagger.Lazy
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.common.dialogs.ColorPickerDialogFactory
|
||||
import org.isoron.uhabits.activities.common.dialogs.ConfirmDeleteDialog
|
||||
import org.isoron.uhabits.activities.common.dialogs.ConfirmSyncKeyDialog
|
||||
import org.isoron.uhabits.activities.common.dialogs.NumberPickerFactory
|
||||
import org.isoron.uhabits.activities.habits.edit.HabitTypeDialog
|
||||
import org.isoron.uhabits.activities.habits.list.views.HabitCardListAdapter
|
||||
@@ -51,6 +53,8 @@ import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.FILE_NOT_RECOGNIZED
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.IMPORT_FAILED
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.IMPORT_SUCCESSFUL
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.SYNC_ENABLED
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsBehavior.Message.SYNC_KEY_ALREADY_INSTALLED
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsMenuBehavior
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.ListHabitsSelectionMenuBehavior
|
||||
import org.isoron.uhabits.inject.ActivityContext
|
||||
@@ -99,6 +103,14 @@ class ListHabitsScreen
|
||||
|
||||
fun onAttached() {
|
||||
commandRunner.addListener(this)
|
||||
if (activity.intent.action == "android.intent.action.VIEW") {
|
||||
val uri = activity.intent.data!!.toString()
|
||||
val parts = uri.replace(Regex("^.*sync/"), "").split("#")
|
||||
val syncKey = parts[0]
|
||||
val encKey = parts[1]
|
||||
Log.i("ListHabitsScreen", "sync: $syncKey enc: $encKey")
|
||||
behavior.get().onSyncKeyOffer(syncKey, encKey)
|
||||
}
|
||||
}
|
||||
|
||||
fun onDettached() {
|
||||
@@ -196,6 +208,8 @@ class ListHabitsScreen
|
||||
DATABASE_REPAIRED -> R.string.database_repaired
|
||||
COULD_NOT_GENERATE_BUG_REPORT -> R.string.bug_report_failed
|
||||
FILE_NOT_RECOGNIZED -> R.string.file_not_recognized
|
||||
SYNC_ENABLED -> R.string.sync_enabled
|
||||
SYNC_KEY_ALREADY_INSTALLED -> R.string.sync_key_already_installed
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -230,6 +244,10 @@ class ListHabitsScreen
|
||||
numberPickerFactory.create(value, unit, callback).show()
|
||||
}
|
||||
|
||||
override fun showConfirmInstallSyncKey(callback: OnConfirmedCallback) {
|
||||
ConfirmSyncKeyDialog(activity, callback).show()
|
||||
}
|
||||
|
||||
private fun getExecuteString(command: Command): String? {
|
||||
when (command) {
|
||||
is ArchiveHabitsCommand -> {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
package org.isoron.uhabits.activities.settings
|
||||
|
||||
import android.app.backup.BackupManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
|
||||
@@ -27,6 +28,7 @@ import android.os.Build.VERSION
|
||||
import android.os.Bundle
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import androidx.preference.CheckBoxPreference
|
||||
import androidx.preference.ListPreference
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceCategory
|
||||
@@ -41,6 +43,7 @@ import org.isoron.uhabits.activities.habits.list.RESULT_REPAIR_DB
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.ui.NotificationTray
|
||||
import org.isoron.uhabits.core.utils.DateUtils.Companion.getLongWeekdayNames
|
||||
import org.isoron.uhabits.intents.IntentFactory
|
||||
import org.isoron.uhabits.notifications.AndroidNotificationTray.Companion.createAndroidNotificationChannel
|
||||
import org.isoron.uhabits.notifications.RingtoneManager
|
||||
import org.isoron.uhabits.widgets.WidgetUpdater
|
||||
@@ -97,6 +100,13 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
|
||||
intent.putExtra(Settings.EXTRA_CHANNEL_ID, NotificationTray.REMINDERS_CHANNEL_ID)
|
||||
startActivity(intent)
|
||||
return true
|
||||
} else if (key == "pref_sync_enabled_dummy") {
|
||||
if (prefs.isSyncEnabled) {
|
||||
prefs.disableSync()
|
||||
} else {
|
||||
val context: Context? = activity
|
||||
context!!.startActivity(IntentFactory().startSyncActivity(context))
|
||||
}
|
||||
}
|
||||
return super.onPreferenceTreeClick(preference)
|
||||
}
|
||||
@@ -111,6 +121,7 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
|
||||
devCategory.isVisible = false
|
||||
}
|
||||
updateWeekdayPreference()
|
||||
updateSyncPreferences()
|
||||
|
||||
if (VERSION.SDK_INT < Build.VERSION_CODES.O)
|
||||
findPreference("reminderCustomize").isVisible = false
|
||||
@@ -119,6 +130,12 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateSyncPreferences() {
|
||||
findPreference("pref_sync_display").isVisible = prefs.isSyncEnabled
|
||||
(findPreference("pref_sync_enabled_dummy") as CheckBoxPreference).isChecked =
|
||||
prefs.isSyncEnabled
|
||||
}
|
||||
|
||||
private fun updateWeekdayPreference() {
|
||||
val weekdayPref = findPreference("pref_first_weekday") as ListPreference
|
||||
val currentFirstWeekday = prefs.firstWeekday.daysSinceSunday + 1
|
||||
@@ -140,6 +157,7 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis
|
||||
}
|
||||
BackupManager.dataChanged("org.isoron.uhabits")
|
||||
updateWeekdayPreference()
|
||||
updateSyncPreferences()
|
||||
}
|
||||
|
||||
private fun setResultOnPreferenceClick(key: String, result: Int) {
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.activities.sync
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.text.Html
|
||||
import android.view.View
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.qrcode.QRCodeWriter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.invoke
|
||||
import kotlinx.coroutines.launch
|
||||
import org.isoron.uhabits.HabitsApplication
|
||||
import org.isoron.uhabits.R
|
||||
import org.isoron.uhabits.activities.AndroidThemeSwitcher
|
||||
import org.isoron.uhabits.core.models.PaletteColor
|
||||
import org.isoron.uhabits.core.ui.screens.sync.SyncBehavior
|
||||
import org.isoron.uhabits.databinding.ActivitySyncBinding
|
||||
import org.isoron.uhabits.sync.RemoteSyncServer
|
||||
import org.isoron.uhabits.utils.InterfaceUtils.getFontAwesome
|
||||
import org.isoron.uhabits.utils.setupToolbar
|
||||
import org.isoron.uhabits.utils.showMessage
|
||||
|
||||
class SyncActivity : AppCompatActivity(), SyncBehavior.Screen {
|
||||
|
||||
private lateinit var binding: ActivitySyncBinding
|
||||
private lateinit var behavior: SyncBehavior
|
||||
|
||||
private val scope = CoroutineScope(Dispatchers.Main)
|
||||
|
||||
override fun onCreate(savedInstance: Bundle?) {
|
||||
super.onCreate(savedInstance)
|
||||
val component = (application as HabitsApplication).component
|
||||
val preferences = component.preferences
|
||||
val server = RemoteSyncServer(preferences = preferences)
|
||||
AndroidThemeSwitcher(this, component.preferences).apply()
|
||||
|
||||
behavior = SyncBehavior(this, preferences, server, component.logging)
|
||||
binding = ActivitySyncBinding.inflate(layoutInflater)
|
||||
binding.errorIcon.typeface = getFontAwesome(this)
|
||||
binding.root.setupToolbar(
|
||||
toolbar = binding.toolbar,
|
||||
color = PaletteColor(11),
|
||||
title = resources.getString(R.string.device_sync),
|
||||
)
|
||||
binding.syncLink.setOnClickListener { copyToClipboard() }
|
||||
binding.instructions.text = Html.fromHtml(resources.getString(R.string.sync_instructions))
|
||||
setContentView(binding.root)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
scope.launch {
|
||||
behavior.onResume()
|
||||
}
|
||||
}
|
||||
|
||||
private fun copyToClipboard() {
|
||||
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText("Loop Sync Link", binding.syncLink.text))
|
||||
showMessage(resources.getString(R.string.copied_to_the_clipboard))
|
||||
}
|
||||
|
||||
suspend fun generateQR(msg: String): Bitmap = Dispatchers.IO {
|
||||
val writer = QRCodeWriter()
|
||||
val matrix = writer.encode(msg, BarcodeFormat.QR_CODE, 1024, 1024)
|
||||
val height = matrix.height
|
||||
val width = matrix.width
|
||||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
|
||||
val bgColor = Color.WHITE
|
||||
val fgColor = Color.BLACK
|
||||
for (x in 0 until width) {
|
||||
for (y in 0 until height) {
|
||||
val color = if (matrix.get(x, y)) fgColor else bgColor
|
||||
bitmap.setPixel(x, y, color)
|
||||
}
|
||||
}
|
||||
return@IO bitmap
|
||||
}
|
||||
|
||||
suspend fun showQR(msg: String) {
|
||||
binding.progress.visibility = View.GONE
|
||||
binding.qrCode.visibility = View.VISIBLE
|
||||
binding.qrCode.setImageBitmap(generateQR(msg))
|
||||
}
|
||||
|
||||
override suspend fun showLoadingScreen() {
|
||||
binding.qrCode.visibility = View.GONE
|
||||
binding.progress.visibility = View.VISIBLE
|
||||
binding.errorPanel.visibility = View.GONE
|
||||
}
|
||||
|
||||
override suspend fun showErrorScreen() {
|
||||
binding.qrCode.visibility = View.GONE
|
||||
binding.progress.visibility = View.GONE
|
||||
binding.errorPanel.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
override suspend fun showLink(link: String) {
|
||||
binding.qrCode.visibility = View.GONE
|
||||
binding.progress.visibility = View.VISIBLE
|
||||
binding.errorPanel.visibility = View.GONE
|
||||
binding.syncLink.text = link
|
||||
showQR(link)
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import org.isoron.uhabits.core.models.ModelFactory
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.preferences.WidgetPreferences
|
||||
import org.isoron.uhabits.core.reminders.ReminderScheduler
|
||||
import org.isoron.uhabits.core.sync.SyncManager
|
||||
import org.isoron.uhabits.core.tasks.TaskRunner
|
||||
import org.isoron.uhabits.core.ui.NotificationTray
|
||||
import org.isoron.uhabits.core.ui.screens.habits.list.HabitCardListCache
|
||||
@@ -63,4 +64,5 @@ interface HabitsApplicationComponent {
|
||||
val taskRunner: TaskRunner
|
||||
val widgetPreferences: WidgetPreferences
|
||||
val widgetUpdater: WidgetUpdater
|
||||
val syncManager: SyncManager
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
package org.isoron.uhabits.inject
|
||||
|
||||
import android.content.Context
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import org.isoron.uhabits.core.AppScope
|
||||
@@ -33,6 +34,8 @@ import org.isoron.uhabits.core.models.sqlite.SQLiteHabitList
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.preferences.WidgetPreferences
|
||||
import org.isoron.uhabits.core.reminders.ReminderScheduler
|
||||
import org.isoron.uhabits.core.sync.AbstractSyncServer
|
||||
import org.isoron.uhabits.core.sync.NetworkManager
|
||||
import org.isoron.uhabits.core.tasks.TaskRunner
|
||||
import org.isoron.uhabits.core.ui.NotificationTray
|
||||
import org.isoron.uhabits.database.AndroidDatabase
|
||||
@@ -41,6 +44,8 @@ import org.isoron.uhabits.intents.IntentScheduler
|
||||
import org.isoron.uhabits.io.AndroidLogging
|
||||
import org.isoron.uhabits.notifications.AndroidNotificationTray
|
||||
import org.isoron.uhabits.preferences.SharedPreferencesStorage
|
||||
import org.isoron.uhabits.sync.AndroidNetworkManager
|
||||
import org.isoron.uhabits.sync.RemoteSyncServer
|
||||
import org.isoron.uhabits.utils.DatabaseUtils
|
||||
import java.io.File
|
||||
|
||||
@@ -109,6 +114,18 @@ class HabitsModule(dbFile: File) {
|
||||
return AndroidLogging()
|
||||
}
|
||||
|
||||
@Provides
|
||||
@AppScope
|
||||
fun getNetworkManager(@AppContext context: Context): NetworkManager {
|
||||
return AndroidNetworkManager(context)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@AppScope
|
||||
fun getSyncServer(preferences: Preferences): AbstractSyncServer {
|
||||
return RemoteSyncServer(preferences)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@AppScope
|
||||
fun getDatabase(): Database {
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.isoron.uhabits.activities.habits.edit.EditHabitActivity
|
||||
import org.isoron.uhabits.activities.habits.show.ShowHabitActivity
|
||||
import org.isoron.uhabits.activities.intro.IntroActivity
|
||||
import org.isoron.uhabits.activities.settings.SettingsActivity
|
||||
import org.isoron.uhabits.activities.sync.SyncActivity
|
||||
import org.isoron.uhabits.core.models.Habit
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -100,4 +101,8 @@ class IntentFactory
|
||||
intent.putExtra("habitType", habitType)
|
||||
return intent
|
||||
}
|
||||
|
||||
fun startSyncActivity(context: Context): Intent {
|
||||
return Intent(context, SyncActivity::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.sync
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkRequest
|
||||
import org.isoron.uhabits.core.sync.NetworkManager
|
||||
|
||||
class AndroidNetworkManager(
|
||||
val context: Context,
|
||||
) : NetworkManager, ConnectivityManager.NetworkCallback() {
|
||||
|
||||
val listeners = mutableListOf<NetworkManager.Listener>()
|
||||
var connected = false
|
||||
|
||||
init {
|
||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
cm.registerNetworkCallback(NetworkRequest.Builder().build(), this)
|
||||
}
|
||||
|
||||
override fun addListener(listener: NetworkManager.Listener) {
|
||||
if (connected) listener.onNetworkAvailable()
|
||||
else listener.onNetworkLost()
|
||||
listeners.add(listener)
|
||||
}
|
||||
|
||||
override fun remoteListener(listener: NetworkManager.Listener) {
|
||||
listeners.remove(listener)
|
||||
}
|
||||
|
||||
override fun onAvailable(network: Network) {
|
||||
connected = true
|
||||
for (l in listeners) l.onNetworkAvailable()
|
||||
}
|
||||
|
||||
override fun onLost(network: Network) {
|
||||
connected = false
|
||||
for (l in listeners) l.onNetworkLost()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.isoron.uhabits.sync
|
||||
|
||||
import android.util.Log
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.android.Android
|
||||
import io.ktor.client.features.ClientRequestException
|
||||
import io.ktor.client.features.ServerResponseException
|
||||
import io.ktor.client.features.json.JsonFeature
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.put
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.invoke
|
||||
import org.isoron.uhabits.core.preferences.Preferences
|
||||
import org.isoron.uhabits.core.sync.AbstractSyncServer
|
||||
import org.isoron.uhabits.core.sync.EditConflictException
|
||||
import org.isoron.uhabits.core.sync.GetDataVersionResponse
|
||||
import org.isoron.uhabits.core.sync.KeyNotFoundException
|
||||
import org.isoron.uhabits.core.sync.RegisterReponse
|
||||
import org.isoron.uhabits.core.sync.ServiceUnavailable
|
||||
import org.isoron.uhabits.core.sync.SyncData
|
||||
|
||||
class RemoteSyncServer(
|
||||
private val preferences: Preferences,
|
||||
private val httpClient: HttpClient = HttpClient(Android) {
|
||||
install(JsonFeature)
|
||||
}
|
||||
) : AbstractSyncServer {
|
||||
|
||||
override suspend fun register(): String = Dispatchers.IO {
|
||||
try {
|
||||
val url = "${preferences.syncBaseURL}/register"
|
||||
Log.i("RemoteSyncServer", "POST $url")
|
||||
val response: RegisterReponse = httpClient.post(url)
|
||||
return@IO response.key
|
||||
} catch (e: ServerResponseException) {
|
||||
throw ServiceUnavailable()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun put(key: String, newData: SyncData) = Dispatchers.IO {
|
||||
try {
|
||||
val url = "${preferences.syncBaseURL}/db/$key"
|
||||
Log.i("RemoteSyncServer", "PUT $url")
|
||||
val response: String = httpClient.put(url) {
|
||||
header("Content-Type", "application/json")
|
||||
body = newData
|
||||
}
|
||||
} catch (e: ServerResponseException) {
|
||||
throw ServiceUnavailable()
|
||||
} catch (e: ClientRequestException) {
|
||||
Log.w("RemoteSyncServer", "ClientRequestException", e)
|
||||
if (e.message!!.contains("409")) throw EditConflictException()
|
||||
if (e.message!!.contains("404")) throw KeyNotFoundException()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getData(key: String): SyncData = Dispatchers.IO {
|
||||
try {
|
||||
val url = "${preferences.syncBaseURL}/db/$key"
|
||||
Log.i("RemoteSyncServer", "GET $url")
|
||||
return@IO httpClient.get<SyncData>(url)
|
||||
} catch (e: ServerResponseException) {
|
||||
throw ServiceUnavailable()
|
||||
} catch (e: ClientRequestException) {
|
||||
Log.w("RemoteSyncServer", "ClientRequestException", e)
|
||||
throw KeyNotFoundException()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getDataVersion(key: String): Long = Dispatchers.IO {
|
||||
try {
|
||||
val url = "${preferences.syncBaseURL}/db/$key/version"
|
||||
Log.i("RemoteSyncServer", "GET $url")
|
||||
val response: GetDataVersionResponse = httpClient.get(url)
|
||||
return@IO response.version
|
||||
} catch (e: ServerResponseException) {
|
||||
throw ServiceUnavailable()
|
||||
} catch (e: ClientRequestException) {
|
||||
Log.w("RemoteSyncServer", "ClientRequestException", e)
|
||||
throw KeyNotFoundException()
|
||||
}
|
||||
}
|
||||
}
|
||||
147
uhabits-android/src/main/res/layout/activity_sync.xml
Normal file
147
uhabits-android/src/main/res/layout/activity_sync.xml
Normal file
@@ -0,0 +1,147 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
~
|
||||
~ This file is part of Loop Habit Tracker.
|
||||
~
|
||||
~ Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
~ it under the terms of the GNU General Public License as published by the
|
||||
~ Free Software Foundation, either version 3 of the License, or (at your
|
||||
~ option) any later version.
|
||||
~
|
||||
~ Loop Habit Tracker 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 General Public License for
|
||||
~ more details.
|
||||
~
|
||||
~ You should have received a copy of the GNU General Public License along
|
||||
~ with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
-->
|
||||
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?attr/contrast0"
|
||||
android:fitsSystemWindows="true"
|
||||
android:orientation="vertical"
|
||||
tools:context=".activities.habits.edit.EditHabitActivity">
|
||||
|
||||
<com.google.android.material.appbar.AppBarLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<androidx.appcompat.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/colorPrimary"
|
||||
android:elevation="2dp"
|
||||
android:gravity="end"
|
||||
android:minHeight="?attr/actionBarSize"
|
||||
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
|
||||
app:title="@string/device_sync"
|
||||
app:titleTextColor="@color/white">
|
||||
|
||||
</androidx.appcompat.widget.Toolbar>
|
||||
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_behavior="@string/appbar_scrolling_view_behavior">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingLeft="4dp"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingRight="4dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:lineSpacingExtra="4sp"
|
||||
android:padding="8dp"
|
||||
android:paddingBottom="16dp"
|
||||
android:textSize="@dimen/regularTextSize"
|
||||
android:id="@+id/instructions"
|
||||
/>
|
||||
|
||||
<!-- Sync Link (QR) -->
|
||||
<FrameLayout style="@style/FormOuterBox">
|
||||
|
||||
<LinearLayout
|
||||
style="@style/FormInnerBox">
|
||||
<TextView
|
||||
style="@style/FormLabel"
|
||||
android:translationZ="0.01dp"
|
||||
android:text="@string/sync_link_qr" />
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/errorPanel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/errorIcon"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="40dp"
|
||||
android:layout_margin="40dp"
|
||||
android:text="@string/fa_exclamation_circle" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Error generating code. Please try again later."
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="200dp"
|
||||
android:layout_gravity="center"
|
||||
android:indeterminate="true"
|
||||
/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/qrCode"
|
||||
android:layout_margin="10dp"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="200dp"
|
||||
android:layout_gravity="center"
|
||||
/>
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
||||
|
||||
<!-- Sync Link -->
|
||||
<FrameLayout style="@style/FormOuterBox">
|
||||
|
||||
<LinearLayout style="@style/FormInnerBox">
|
||||
|
||||
<TextView
|
||||
style="@style/FormLabel"
|
||||
android:text="@string/sync_link" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/syncLink"
|
||||
style="@style/FormInput"
|
||||
android:singleLine="true"
|
||||
android:text=""
|
||||
android:background="@drawable/ripple"
|
||||
android:textSize="@dimen/smallTextSize"
|
||||
android:layout_margin="8dp"
|
||||
/>
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
@@ -221,6 +221,18 @@
|
||||
<string name="decrement">Decrement</string>
|
||||
<string name="pref_skip_title">Enable skip days</string>
|
||||
<string name="pref_skip_description">Toggle twice to add a skip instead of a checkmark. Skips keep your score unchanged and don\'t break your streak.</string>
|
||||
<string name="device_sync">Device sync</string>
|
||||
<string name="pref_sync_summary">When enabled, an encrypted copy of your data will be uploaded to our servers. See privacy policy.</string>
|
||||
<string name="pref_sync_title">Sync data across devices</string>
|
||||
<string name="display_sync_code">Show device sync instructions</string>
|
||||
<string name="sync_instructions"><![CDATA[<b>Instructions:</b><br/>1. Install Loop in your second device.<br/>2. Open the link below in your second device.<br/><b>Important:</b> Do not not make this information public. It gives anyone access to your data.]]></string>
|
||||
<string name="sync_link">Sync link</string>
|
||||
<string name="sync_link_qr">Sync link (QR code)</string>
|
||||
<string name="password">Password</string>
|
||||
<string name="copied_to_the_clipboard">Copied to the clipboard</string>
|
||||
<string name="sync_confirm"><![CDATA[Are you trying to enable device sync?\n\nThis feature allows you to sync your data across multiple devices. When enabled, an encrypted copy of your data will be uploaded to our servers.]]></string>
|
||||
<string name="sync_enabled">Device sync enabled</string>
|
||||
<string name="sync_key_already_installed">Sync key already installed</string>
|
||||
<string name="pref_unknown_title">Show question marks for missing data</string>
|
||||
<string name="pref_unknown_description">Differentiate days without data from actual lapses. To enter a lapse, toggle twice.</string>
|
||||
<string name="you_are_now_a_developer">You are now a developer</string>
|
||||
|
||||
@@ -114,6 +114,30 @@
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="deviceSync"
|
||||
android:title="@string/device_sync" >
|
||||
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
android:key="pref_sync_enabled_dummy"
|
||||
android:summary="@string/pref_sync_summary"
|
||||
android:title="@string/pref_sync_title"
|
||||
app:iconSpaceReserved="false" />
|
||||
|
||||
<Preference
|
||||
android:key="pref_sync_display"
|
||||
android:title="@string/display_sync_code"
|
||||
app:iconSpaceReserved="false">
|
||||
<intent
|
||||
android:action="android.intent.action.VIEW"
|
||||
android:targetClass="org.isoron.uhabits.activities.sync.SyncActivity"
|
||||
android:targetPackage="org.isoron.uhabits" />
|
||||
</Preference>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
|
||||
<PreferenceCategory
|
||||
android:key="databaseCategory"
|
||||
android:title="@string/database">
|
||||
|
||||
Reference in New Issue
Block a user