Skip to content

Commit

Permalink
src: Add auto backup support
Browse files Browse the repository at this point in the history
Runs getNotes() upon restore finish
  • Loading branch information
argraur committed May 29, 2020
1 parent 7d64f1a commit 9229c46
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 153 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<application
android:name=".App"
android:allowBackup="true"
android:fullBackupContent="@xml/backup"
android:fullBackupOnly="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/me/argraur/notes/helpers/BackupAgent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2020 Arseniy Graur
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.argraur.notes.helpers

import android.app.backup.BackupAgent
import android.app.backup.BackupDataInput
import android.app.backup.BackupDataOutput
import android.os.ParcelFileDescriptor
import android.util.Log

class BackupAgent: BackupAgent() {
/**
* Send log message when backup started
* @see BackupAgent.onBackup
*/
override fun onBackup(
oldState: ParcelFileDescriptor?,
data: BackupDataOutput?,
newState: ParcelFileDescriptor?
) {
Log.d(javaClass.simpleName, "Backing up!")
}

/**
* Send log message when restore finished
* @see BackupAgent.onRestore
*/
override fun onRestore(
data: BackupDataInput?,
appVersionCode: Int,
newState: ParcelFileDescriptor?
) {
Log.d(javaClass.simpleName, "Restoring from backup!")
}

/**
* Updates notes when restore finished.
* @see BackupAgent.onRestoreFinished
*/
override fun onRestoreFinished() {
super.onRestoreFinished()
NoteManager.getInstance(null).getNotes()
}
}
6 changes: 5 additions & 1 deletion app/src/main/java/me/argraur/notes/helpers/NoteManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package me.argraur.notes.helpers

import android.app.backup.BackupAgent
import android.app.backup.BackupDataInput
import android.app.backup.BackupDataOutput
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteException
import android.os.ParcelFileDescriptor
import android.provider.BaseColumns
import me.argraur.notes.entities.Note
import me.argraur.notes.interfaces.Subject
Expand Down Expand Up @@ -84,7 +88,7 @@ class NoteManager(context: Context): Subject {
* Creates array of Note types based on title, value, color and creation time
* If current notes array doesn't match new one, update current and notify observers
*/
private fun getNotes() {
fun getNotes() {
val db = dbHelper.readableDatabase
val cursor = db.query(
Note.Companion.Entry.TABLE_NAME,
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/res/xml/backup.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2020 Arseniy Graur
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<full-backup-content xmlns:android="http://schemas.android.com/apk/res/android">
<include domain="database" path="." requireFlags="clientSideEncryption" />
</full-backup-content>
3 changes: 3 additions & 0 deletions docs/alltypes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ <h5><a href="../me.argraur.notes/-app/index.html">me.argraur.notes.App</a></h5>
<p>Creates NoteManager instance</p>
</tr>
<tr>
<h5><a href="../me.argraur.notes.helpers/-backup-agent/index.html">me.argraur.notes.helpers.BackupAgent</a></h5>
</tr>
<tr>
<h5><a href="../me.argraur.notes.helpers/-database-helper/index.html">me.argraur.notes.helpers.DatabaseHelper</a></h5>
</tr>
<tr>
Expand Down
336 changes: 185 additions & 151 deletions docs/index-outline.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<HEAD>
<meta charset="UTF-8">
<title>app</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="../style.css">
</HEAD>
<BODY>
<a href="./index.html">app</a><br/>
Expand Down
14 changes: 14 additions & 0 deletions docs/me.argraur.notes.helpers/-backup-agent/-init-.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>BackupAgent.<init> - app</title>
<link rel="stylesheet" href="../../../style.css">
</HEAD>
<BODY>
<a href="../../index.html">app</a>&nbsp;/&nbsp;<a href="../index.html">me.argraur.notes.helpers</a>&nbsp;/&nbsp;<a href="index.html">BackupAgent</a>&nbsp;/&nbsp;<a href="./-init-.html">&lt;init&gt;</a><br/>
<br/>
<h1>&lt;init&gt;</h1>
<a name="me.argraur.notes.helpers.BackupAgent$&lt;init&gt;()"></a>
<code><span class="identifier">BackupAgent</span><span class="symbol">(</span><span class="symbol">)</span></code>
</BODY>
</HTML>
54 changes: 54 additions & 0 deletions docs/me.argraur.notes.helpers/-backup-agent/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>BackupAgent - app</title>
<link rel="stylesheet" href="../../../style.css">
</HEAD>
<BODY>
<a href="../../index.html">app</a>&nbsp;/&nbsp;<a href="../index.html">me.argraur.notes.helpers</a>&nbsp;/&nbsp;<a href="./index.html">BackupAgent</a><br/>
<br/>
<h1>BackupAgent</h1>
<code><span class="keyword">class </span><span class="identifier">BackupAgent</span>&nbsp;<span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/app/backup/BackupAgent.html"><span class="identifier">BackupAgent</span></a></code>
<h3>Constructors</h3>
<table>
<tbody>
<tr>
<td>
<h4><a href="-init-.html">&lt;init&gt;</a></h4>
</td>
<td>
<code><span class="identifier">BackupAgent</span><span class="symbol">(</span><span class="symbol">)</span></code></td>
</tr>
</tbody>
</table>
<h3>Functions</h3>
<table>
<tbody>
<tr>
<td>
<h4><a href="on-backup.html">onBackup</a></h4>
</td>
<td>
<p>Send log message when backup started</p>
<code><span class="keyword">fun </span><span class="identifier">onBackup</span><span class="symbol">(</span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)/oldState">oldState</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/os/ParcelFileDescriptor.html"><span class="identifier">ParcelFileDescriptor</span></a><span class="symbol">?</span><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)/data">data</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/app/backup/BackupDataOutput.html"><span class="identifier">BackupDataOutput</span></a><span class="symbol">?</span><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)/newState">newState</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/os/ParcelFileDescriptor.html"><span class="identifier">ParcelFileDescriptor</span></a><span class="symbol">?</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code></td>
</tr>
<tr>
<td>
<h4><a href="on-restore.html">onRestore</a></h4>
</td>
<td>
<p>Send log message when restore finished</p>
<code><span class="keyword">fun </span><span class="identifier">onRestore</span><span class="symbol">(</span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onRestore(android.app.backup.BackupDataInput, kotlin.Int, android.os.ParcelFileDescriptor)/data">data</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/app/backup/BackupDataInput.html"><span class="identifier">BackupDataInput</span></a><span class="symbol">?</span><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onRestore(android.app.backup.BackupDataInput, kotlin.Int, android.os.ParcelFileDescriptor)/appVersionCode">appVersionCode</span><span class="symbol">:</span>&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onRestore(android.app.backup.BackupDataInput, kotlin.Int, android.os.ParcelFileDescriptor)/newState">newState</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/os/ParcelFileDescriptor.html"><span class="identifier">ParcelFileDescriptor</span></a><span class="symbol">?</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code></td>
</tr>
<tr>
<td>
<h4><a href="on-restore-finished.html">onRestoreFinished</a></h4>
</td>
<td>
<p>Updates notes when restore finished.</p>
<code><span class="keyword">fun </span><span class="identifier">onRestoreFinished</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code></td>
</tr>
</tbody>
</table>
</BODY>
</HTML>
18 changes: 18 additions & 0 deletions docs/me.argraur.notes.helpers/-backup-agent/on-backup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>BackupAgent.onBackup - app</title>
<link rel="stylesheet" href="../../../style.css">
</HEAD>
<BODY>
<a href="../../index.html">app</a>&nbsp;/&nbsp;<a href="../index.html">me.argraur.notes.helpers</a>&nbsp;/&nbsp;<a href="index.html">BackupAgent</a>&nbsp;/&nbsp;<a href="./on-backup.html">onBackup</a><br/>
<br/>
<h1>onBackup</h1>
<a name="me.argraur.notes.helpers.BackupAgent$onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)"></a>
<code><span class="keyword">fun </span><span class="identifier">onBackup</span><span class="symbol">(</span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)/oldState">oldState</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/os/ParcelFileDescriptor.html"><span class="identifier">ParcelFileDescriptor</span></a><span class="symbol">?</span><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)/data">data</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/app/backup/BackupDataOutput.html"><span class="identifier">BackupDataOutput</span></a><span class="symbol">?</span><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)/newState">newState</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/os/ParcelFileDescriptor.html"><span class="identifier">ParcelFileDescriptor</span></a><span class="symbol">?</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code>
<p>Send log message when backup started</p>
<p><strong>See Also</strong><br/>
<p><a href="https://developer.android.com/reference/android/app/backup/BackupAgent.html#onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor)">BackupAgent.onBackup</a></p>
</p>
</BODY>
</HTML>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>BackupAgent.onRestoreFinished - app</title>
<link rel="stylesheet" href="../../../style.css">
</HEAD>
<BODY>
<a href="../../index.html">app</a>&nbsp;/&nbsp;<a href="../index.html">me.argraur.notes.helpers</a>&nbsp;/&nbsp;<a href="index.html">BackupAgent</a>&nbsp;/&nbsp;<a href="./on-restore-finished.html">onRestoreFinished</a><br/>
<br/>
<h1>onRestoreFinished</h1>
<a name="me.argraur.notes.helpers.BackupAgent$onRestoreFinished()"></a>
<code><span class="keyword">fun </span><span class="identifier">onRestoreFinished</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code>
<p>Updates notes when restore finished.</p>
<p><strong>See Also</strong><br/>
<p><a href="https://developer.android.com/reference/android/app/backup/BackupAgent.html#onRestoreFinished()">BackupAgent.onRestoreFinished</a></p>
</p>
</BODY>
</HTML>
18 changes: 18 additions & 0 deletions docs/me.argraur.notes.helpers/-backup-agent/on-restore.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>BackupAgent.onRestore - app</title>
<link rel="stylesheet" href="../../../style.css">
</HEAD>
<BODY>
<a href="../../index.html">app</a>&nbsp;/&nbsp;<a href="../index.html">me.argraur.notes.helpers</a>&nbsp;/&nbsp;<a href="index.html">BackupAgent</a>&nbsp;/&nbsp;<a href="./on-restore.html">onRestore</a><br/>
<br/>
<h1>onRestore</h1>
<a name="me.argraur.notes.helpers.BackupAgent$onRestore(android.app.backup.BackupDataInput, kotlin.Int, android.os.ParcelFileDescriptor)"></a>
<code><span class="keyword">fun </span><span class="identifier">onRestore</span><span class="symbol">(</span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onRestore(android.app.backup.BackupDataInput, kotlin.Int, android.os.ParcelFileDescriptor)/data">data</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/app/backup/BackupDataInput.html"><span class="identifier">BackupDataInput</span></a><span class="symbol">?</span><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onRestore(android.app.backup.BackupDataInput, kotlin.Int, android.os.ParcelFileDescriptor)/appVersionCode">appVersionCode</span><span class="symbol">:</span>&nbsp;<a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html"><span class="identifier">Int</span></a><span class="symbol">, </span><span class="identifier" id="me.argraur.notes.helpers.BackupAgent$onRestore(android.app.backup.BackupDataInput, kotlin.Int, android.os.ParcelFileDescriptor)/newState">newState</span><span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/os/ParcelFileDescriptor.html"><span class="identifier">ParcelFileDescriptor</span></a><span class="symbol">?</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code>
<p>Send log message when restore finished</p>
<p><strong>See Also</strong><br/>
<p><a href="https://developer.android.com/reference/android/app/backup/BackupAgent.html#onRestore(android.app.backup.BackupDataInput, int, android.os.ParcelFileDescriptor)">BackupAgent.onRestore</a></p>
</p>
</BODY>
</HTML>
17 changes: 17 additions & 0 deletions docs/me.argraur.notes.helpers/-note-manager/get-notes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<HTML>
<HEAD>
<meta charset="UTF-8">
<title>NoteManager.getNotes - app</title>
<link rel="stylesheet" href="../../../style.css">
</HEAD>
<BODY>
<a href="../../index.html">app</a>&nbsp;/&nbsp;<a href="../index.html">me.argraur.notes.helpers</a>&nbsp;/&nbsp;<a href="index.html">NoteManager</a>&nbsp;/&nbsp;<a href="./get-notes.html">getNotes</a><br/>
<br/>
<h1>getNotes</h1>
<a name="me.argraur.notes.helpers.NoteManager$getNotes()"></a>
<code><span class="keyword">fun </span><span class="identifier">getNotes</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code>
<p>Retrieves all notes from database
Creates array of Note types based on title, value, color and creation time
If current notes array doesn't match new one, update current and notify observers</p>
</BODY>
</HTML>
10 changes: 10 additions & 0 deletions docs/me.argraur.notes.helpers/-note-manager/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ <h4><a href="delete-note.html">deleteNote</a></h4>
</tr>
<tr>
<td>
<h4><a href="get-notes.html">getNotes</a></h4>
</td>
<td>
<p>Retrieves all notes from database
Creates array of Note types based on title, value, color and creation time
If current notes array doesn't match new one, update current and notify observers</p>
<code><span class="keyword">fun </span><span class="identifier">getNotes</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html"><span class="identifier">Unit</span></a></code></td>
</tr>
<tr>
<td>
<h4><a href="notify-observer.html">notifyObserver</a></h4>
</td>
<td>
Expand Down
7 changes: 7 additions & 0 deletions docs/me.argraur.notes.helpers/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ <h3>Types</h3>
<tbody>
<tr>
<td>
<h4><a href="-backup-agent/index.html">BackupAgent</a></h4>
</td>
<td>
<code><span class="keyword">class </span><span class="identifier">BackupAgent</span>&nbsp;<span class="symbol">:</span>&nbsp;<a href="https://developer.android.com/reference/android/app/backup/BackupAgent.html"><span class="identifier">BackupAgent</span></a></code></td>
</tr>
<tr>
<td>
<h4><a href="-database-helper/index.html">DatabaseHelper</a></h4>
</td>
<td>
Expand Down

0 comments on commit 9229c46

Please sign in to comment.