├── .gitignore ├── .idea ├── gradle.xml ├── modules.xml └── runConfigurations.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── claudiodegio │ │ └── dbsync │ │ └── sample │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── slf4j-android.properties │ ├── java │ │ └── com │ │ │ └── claudiodegio │ │ │ └── dbsync │ │ │ └── sample │ │ │ ├── BaseActivity.java │ │ │ ├── BaseMainDbActivity.java │ │ │ ├── DbSyncApplication.java │ │ │ ├── MainActivity.java │ │ │ ├── SAFUtils.java │ │ │ ├── Utility.java │ │ │ ├── core │ │ │ └── TableViewerFragment.java │ │ │ ├── db1 │ │ │ ├── Db1Callback.java │ │ │ ├── InsertNameActivity.java │ │ │ └── MainDb1Activity.java │ │ │ ├── db2 │ │ │ ├── CategoriesActivity.java │ │ │ ├── Db2Callback.java │ │ │ ├── InsertCategoryActivity.java │ │ │ ├── InsertNameActivity.java │ │ │ ├── MainDb2Activity.java │ │ │ └── NamesActivity.java │ │ │ ├── db3 │ │ │ ├── Db3Callback.java │ │ │ ├── InsertNameActivity.java │ │ │ └── MainDb3Activity.java │ │ │ ├── db4 │ │ │ ├── ArticlesActivity.java │ │ │ ├── CategoriesActivity.java │ │ │ ├── Db4Callback.java │ │ │ ├── InsertArticleActivity.java │ │ │ ├── InsertCategoryActivity.java │ │ │ └── MainDb4Activity.java │ │ │ └── db5 │ │ │ ├── Db5Callback.java │ │ │ ├── InsertNameActivity.java │ │ │ └── MainDb5Activity.java │ └── res │ │ ├── layout │ │ ├── activity_fragment.xml │ │ ├── activity_insert_article.xml │ │ ├── activity_insert_category.xml │ │ ├── activity_insert_name.xml │ │ ├── activity_insert_name_db3.xml │ │ ├── activity_main.xml │ │ ├── activity_main_db1.xml │ │ ├── activity_main_db2.xml │ │ ├── activity_main_db3.xml │ │ ├── activity_main_db4.xml │ │ ├── activity_main_db5.xml │ │ ├── activity_test_gdrive_activity.xml │ │ ├── fragment_table_view.xml │ │ ├── view_actions.xml │ │ └── view_status.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── raw │ │ └── db_20170114_184802_3200_records.json │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── claudiodegio │ └── dbsync │ └── sample │ └── ExampleUnitTest.java ├── build.gradle ├── db_gen.json ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── icone.txt ├── lib ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── claudiodegio │ │ └── dbsync │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── claudiodegio │ │ │ └── dbsync │ │ │ ├── DBSync.java │ │ │ ├── SyncResult.java │ │ │ ├── SyncStatus.java │ │ │ ├── TableToSync.java │ │ │ ├── core │ │ │ ├── Database.java │ │ │ ├── DatabaseReader.java │ │ │ ├── DatabaseWriter.java │ │ │ ├── JoinTable.java │ │ │ ├── Record.java │ │ │ ├── RecordChanged.java │ │ │ ├── RecordSyncResult.java │ │ │ ├── SqlLiteManager.java │ │ │ ├── SqlLiteUtility.java │ │ │ ├── Table.java │ │ │ ├── Utility.java │ │ │ ├── Value.java │ │ │ └── ValueMetadata.java │ │ │ ├── exception │ │ │ ├── SyncBuildException.java │ │ │ └── SyncException.java │ │ │ ├── json │ │ │ ├── JSonConverter.java │ │ │ ├── JSonConverterFactory.java │ │ │ ├── JSonDatabaseReader.java │ │ │ ├── JSonDatabaseWriter.java │ │ │ ├── JSonLongConverter.java │ │ │ └── JSonStringConverter.java │ │ │ └── provider │ │ │ ├── CloudProvider.java │ │ │ └── GDriveCloudProvider.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── claudiodegio │ └── dbsync │ └── ExampleUnitTest.java ├── settings.gradle └── test ├── .gitignore ├── build.gradle └── src └── main ├── groovy └── com │ └── example │ ├── DatabaseGenerator.groovy │ ├── db_20170114_002331_9_records.json │ └── db_20170114_184802_3200_records.json └── java └── com └── example ├── JSonDatabaseWriter.java ├── JsonDatabaseReader.java └── Main.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | .idea/ 42 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## DbSync 2 | 3 | Small library for sync android SqlLite database to cloud storage (for now only GDrive) 4 | 5 | ## Motivation 6 | 7 | There is not solution to sync SqLite database between device without the of custom 8 | server application or use of paid online cloud service. My solution it's base 9 | a json file on GDrive cloud account of user with no server involved, so all 10 | data are private to user e non saved on server. 11 | 12 | ## How work 13 | 14 | The library write a file on GDrive called cloud file like: 15 | ```json 16 | { 17 | "name" : "db2.db", 18 | "formatVersion" : 1, 19 | "schemaVersion" : 1, 20 | "tableCount" : 2, 21 | "tables" : [ { 22 | "name" : "name", 23 | "recordsCount" : 1, 24 | "records" : [ { 25 | "NAME" : "myname", 26 | "SEND_TIME" : 1487887189513, 27 | "CLOUD_ID" : "c2bd71ee-3f1b-4dca-b478-ebecaaa1f835" 28 | } ] 29 | }, { 30 | "name" : "category", 31 | "recordsCount" : 2, 32 | "records" : [ { 33 | "NAME" : "cat1", 34 | "SEND_TIME" : 1488038770858, 35 | "CLOUD_ID" : "6f2a8bcc-f1ee-4f14-8987-9f4275205793" 36 | }, { 37 | "NAME" : "musica", 38 | "SEND_TIME" : 1487887376113, 39 | "CLOUD_ID" : "52a292b5-8f38-4f30-b13f-7f86bbff49a6" 40 | } ] 41 | } ] 42 | } 43 | ``` 44 | 45 | The library download the file from cloud storage and update the local database using the cloud id used 46 | to connect a record in 2 different device and and the send time use to understand is the record 47 | it's to update. 48 | The solution try to update only the record who are been update from last sync time, for 49 | performance reason don't update all database, after the sync process it generates the cloud id (uuid) 50 | and rewrite the json cloud file and upload to file storage. 51 | 52 | Note: with GDrive you can share between more user 53 | 54 | Your table need to have two columns for syn SEND_TIME long and CLOUD_ID TEXT unique 55 | 56 | ## Features 57 | 58 | The library support: 59 | 60 | * Sync of 1 o more table 61 | * Sync join table (updating the correct id) 62 | * Support more match rule (default use CLOUD_ID counted with uiid faction) 63 | * Support for ignore selected column 64 | * Support for filter on what to sync (usefull for multi account application but different sync file) 65 | 66 | Not supported yet 67 | * Other cloud storage 68 | * Support for images and file 69 | * Delete elements (to solve use a state column or column whit deleted flag) 70 | 71 | # Usage 72 | 73 | Simple example usage of library 74 | 75 | #### 1 Table 76 | ```java 77 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 78 | .setSyncFileByDriveId(mDriveId) 79 | .setGoogleApiClient(mGoogleApiClient) 80 | .build(); 81 | 82 | DBSync dbSync = new DBSync.Builder(this.getBaseContext()) 83 | .setCloudProvider(gDriveProvider) 84 | .setSQLiteDatabase(mDB) 85 | .setDataBaseName("db1") 86 | .addTable(new TableToSync.Builder("name").build()) 87 | .build(); 88 | ``` 89 | #### 2 Table 90 | ```java 91 | 92 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 93 | .setSyncFileByDriveId(mDriveId) 94 | .setGoogleApiClient(mGoogleApiClient) 95 | .build(); 96 | 97 | DBSync dbSync = new DBSync.Builder(this.getBaseContext()) 98 | .setCloudProvider(gDriveProvider) 99 | .setSQLiteDatabase(mDb) 100 | .setDataBaseName("db2") 101 | .addTable(new TableToSync.Builder("name").build()) 102 | .addTable(new TableToSync.Builder("category").build()) 103 | .build(); 104 | ``` 105 | 106 | #### 1 Table whit filter 107 | ```java 108 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 109 | .setSyncFileByDriveId(mDriveId) 110 | .setGoogleApiClient(mGoogleApiClient) 111 | .build(); 112 | 113 | TableToSync tableName = new TableToSync.Builder("name") 114 | .setFilter("FILTER = 0") // send data with apply FILTER = 0 before send 115 | .build(); 116 | 117 | DBSync dbSync = new DBSync.Builder(this.getBaseContext()) 118 | .setCloudProvider(gDriveProvider) 119 | .setSQLiteDatabase(mDB) 120 | .setDataBaseName("db3") 121 | .addTable(tableName) 122 | .build(); 123 | ``` 124 | 125 | #### 2 Table whit join 126 | ```java 127 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 128 | .setSyncFileByDriveId(mDriveId) 129 | .setGoogleApiClient(mGoogleApiClient) 130 | .build(); 131 | 132 | // Keep the order 133 | TableToSync tableCategory = new TableToSync.Builder("CATEGORY") 134 | .build(); 135 | 136 | TableToSync tableArticle = new TableToSync.Builder("ARTICLE") 137 | .addJoinTable(tableCategory, "CATEGORY_ID") 138 | .build(); 139 | 140 | DBSync dbSync = new DBSync.Builder(this.getBaseContext()) 141 | .setCloudProvider(gDriveProvider) 142 | .setSQLiteDatabase(mDB) 143 | .setDataBaseName("db4") 144 | .addTable(tableCategory) 145 | .addTable(tableArticle) 146 | .build(); 147 | ``` 148 | 149 | ### How sync 150 | 151 | To start sync process 152 | 153 | ```java 154 | dbSync.sync(); 155 | ``` 156 | 157 | ### How Insert/Update Data 158 | 159 | Every time you insert or update a record simple set SEND_TIME to null 160 | 161 | # Installation 162 | **Add the dependencies to your gradle file:** 163 | ```javascript 164 | allprojects { 165 | repositories { 166 | ... 167 | maven { url 'https://jitpack.io' } 168 | } 169 | } 170 | dependencies { 171 | compile 'com.github.claudiodegio:dbsync:X.Y.Z' 172 | } 173 | ``` 174 | 175 | ## License 176 | 177 | Copyright 2020 Claudio Degioanni 178 | 179 | Licensed under the Apache License, Version 2.0 (the "License"); 180 | you may not use this file except in compliance with the License. 181 | You may obtain a copy of the License at 182 | 183 | http://www.apache.org/licenses/LICENSE-2.0 184 | 185 | Unless required by applicable law or agreed to in writing, software 186 | distributed under the License is distributed on an "AS IS" BASIS, 187 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 188 | See the License for the specific language governing permissions and 189 | limitations under the License. -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion "28.0.3" 6 | defaultConfig { 7 | applicationId "com.claudiodegio.dbsync.sample" 8 | minSdkVersion 19 9 | targetSdkVersion 28 10 | versionCode 4 11 | versionName "2.0.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | multiDexEnabled true 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | 23 | compileOptions { 24 | sourceCompatibility JavaVersion.VERSION_1_8 25 | targetCompatibility JavaVersion.VERSION_1_8 26 | } 27 | } 28 | 29 | 30 | dependencies { 31 | implementation project(":lib") 32 | 33 | implementation 'androidx.appcompat:appcompat:1.1.0' 34 | implementation 'com.google.android.material:material:1.1.0' 35 | implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0' 36 | 37 | implementation 'com.jakewharton:butterknife:10.2.0' 38 | annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0' 39 | 40 | implementation 'com.github.claudiodegio:sfl4j-android:1.0.0' 41 | implementation 'im.dino:dbinspector:4.0.0' 42 | implementation 'de.codecrafters.tableview:tableview:2.4.2' 43 | implementation 'commons-io:commons-io:2.6' 44 | 45 | implementation "androidx.sqlite:sqlite:2.1.0" 46 | implementation "androidx.sqlite:sqlite-framework:2.1.0" 47 | 48 | // Migration to google drive api rest 49 | implementation 'com.google.android.gms:play-services-auth:17.0.0' 50 | implementation 'com.google.http-client:google-http-client-gson:1.28.0' 51 | implementation('com.google.api-client:google-api-client-android:1.28.0') { 52 | exclude group: 'org.apache.httpcomponents' 53 | } 54 | implementation('com.google.apis:google-api-services-drive:v3-rev152-1.25.0') { 55 | exclude group: 'org.apache.httpcomponents' 56 | } 57 | 58 | implementation 'androidx.multidex:multidex:2.0.1' 59 | 60 | } 61 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\IDE\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/claudiodegio/dbsync/sample/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.claudiodegio.dbsync.sample", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 18 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 33 | 34 | 35 | 39 | 40 | 41 | 45 | 46 | 47 | 51 | 52 | 53 | 57 | 58 | 59 | 63 | 64 | 65 | 69 | 70 | 71 | 75 | 76 | 77 | 81 | 82 | 83 | 84 | 85 | 89 | 90 | 91 | 95 | 96 | 97 | 101 | 102 | 103 | 107 | 108 | 109 | 113 | 114 | 115 | 119 | 120 | 121 | 125 | 126 | 127 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /app/src/main/assets/slf4j-android.properties: -------------------------------------------------------------------------------- 1 | # slf4j-android.properties 2 | # ROOT level 3 | # ERROR,WARN,INFO,DEBUG,TRACE,NONE,ALL 4 | LEVEL=ALL -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample; 2 | 3 | import android.os.Bundle; 4 | import androidx.annotation.Nullable; 5 | import androidx.appcompat.app.AppCompatActivity; 6 | 7 | 8 | public abstract class BaseActivity extends AppCompatActivity { 9 | 10 | protected DbSyncApplication app; 11 | 12 | @Override 13 | protected void onCreate(@Nullable Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | app = (DbSyncApplication) getApplication(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/DbSyncApplication.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample; 2 | 3 | import androidx.multidex.MultiDexApplication; 4 | import androidx.sqlite.db.SupportSQLiteOpenHelper; 5 | import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory; 6 | 7 | import com.claudiodegio.dbsync.sample.db1.Db1Callback; 8 | import com.claudiodegio.dbsync.sample.db2.Db2Callback; 9 | import com.claudiodegio.dbsync.sample.db3.Db3Callback; 10 | import com.claudiodegio.dbsync.sample.db4.Db4Callback; 11 | import com.claudiodegio.dbsync.sample.db5.Db5Callback; 12 | 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | import org.slf4j.impl.StaticLoggerBinder; 16 | 17 | 18 | public class DbSyncApplication extends MultiDexApplication { 19 | static private Logger log; 20 | 21 | public SupportSQLiteOpenHelper db1OpenHelper; 22 | public SupportSQLiteOpenHelper db2OpenHelper; 23 | public SupportSQLiteOpenHelper db3OpenHelper; 24 | public SupportSQLiteOpenHelper db4OpenHelper; 25 | public SupportSQLiteOpenHelper db5OpenHelper; 26 | 27 | @Override 28 | public void onCreate() { 29 | SupportSQLiteOpenHelper.Configuration configuration; 30 | super.onCreate(); 31 | 32 | StaticLoggerBinder.init(this); 33 | log = LoggerFactory.getLogger(DbSyncApplication.class); 34 | 35 | log.info("onCreate"); 36 | 37 | 38 | configuration = SupportSQLiteOpenHelper.Configuration.builder(this) 39 | .name("db1") 40 | .callback(new Db1Callback()) 41 | .build(); 42 | db1OpenHelper = new FrameworkSQLiteOpenHelperFactory() 43 | .create(configuration); 44 | db1OpenHelper.getReadableDatabase(); 45 | 46 | configuration = SupportSQLiteOpenHelper.Configuration.builder(this) 47 | .name("db2") 48 | .callback(new Db2Callback()) 49 | .build(); 50 | db2OpenHelper = new FrameworkSQLiteOpenHelperFactory() 51 | .create(configuration); 52 | db2OpenHelper.getReadableDatabase(); 53 | 54 | configuration = SupportSQLiteOpenHelper.Configuration.builder(this) 55 | .name("db3") 56 | .callback(new Db3Callback()) 57 | .build(); 58 | db3OpenHelper = new FrameworkSQLiteOpenHelperFactory() 59 | .create(configuration); 60 | db3OpenHelper.getReadableDatabase(); 61 | 62 | configuration = SupportSQLiteOpenHelper.Configuration.builder(this) 63 | .name("db4") 64 | .callback(new Db4Callback()) 65 | .build(); 66 | db4OpenHelper = new FrameworkSQLiteOpenHelperFactory() 67 | .create(configuration); 68 | db4OpenHelper.getReadableDatabase(); 69 | 70 | 71 | configuration = SupportSQLiteOpenHelper.Configuration.builder(this) 72 | .name("db5") 73 | .callback(new Db5Callback()) 74 | .build(); 75 | db5OpenHelper = new FrameworkSQLiteOpenHelperFactory() 76 | .create(configuration); 77 | db5OpenHelper.getReadableDatabase(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | 6 | import androidx.appcompat.app.AppCompatActivity; 7 | import androidx.appcompat.widget.Toolbar; 8 | 9 | import android.view.Menu; 10 | import android.view.MenuItem; 11 | 12 | 13 | import com.claudiodegio.dbsync.sample.db1.MainDb1Activity; 14 | import com.claudiodegio.dbsync.sample.db2.MainDb2Activity; 15 | import com.claudiodegio.dbsync.sample.db3.MainDb3Activity; 16 | import com.claudiodegio.dbsync.sample.db4.MainDb4Activity; 17 | import com.claudiodegio.dbsync.sample.db5.MainDb5Activity; 18 | 19 | import org.slf4j.Logger; 20 | import org.slf4j.LoggerFactory; 21 | 22 | import butterknife.ButterKnife; 23 | import butterknife.OnClick; 24 | import im.dino.dbinspector.activities.DbInspectorActivity; 25 | 26 | public class MainActivity extends AppCompatActivity { 27 | 28 | static final private Logger log = LoggerFactory.getLogger(MainActivity.class); 29 | 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_main); 35 | ButterKnife.bind(this); 36 | } 37 | 38 | @Override 39 | public boolean onCreateOptionsMenu(Menu menu) { 40 | // Inflate the menu; this adds items to the action bar if it is present. 41 | getMenuInflater().inflate(R.menu.menu_main, menu); 42 | return true; 43 | } 44 | 45 | @Override 46 | public boolean onOptionsItemSelected(MenuItem item) { 47 | // Handle action bar item clicks here. The action bar will 48 | // automatically handle clicks on the Home/Up button, so long 49 | // as you specify a parent activity in AndroidManifest.xml. 50 | int id = item.getItemId(); 51 | 52 | //noinspection SimplifiableIfStatement 53 | if (id == R.id.action_settings) { 54 | return true; 55 | } 56 | 57 | return super.onOptionsItemSelected(item); 58 | } 59 | 60 | 61 | 62 | @OnClick(R.id.btToDbManager) 63 | public void goToDBManager(){ 64 | 65 | startActivity(new Intent(this, DbInspectorActivity.class)); 66 | } 67 | 68 | @OnClick(R.id.btDb1) 69 | public void goToDB1(){ 70 | startActivity(new Intent(this, MainDb1Activity.class)); 71 | } 72 | 73 | @OnClick(R.id.btDb2) 74 | public void goToDB2(){ 75 | startActivity(new Intent(this, MainDb2Activity.class)); 76 | } 77 | 78 | @OnClick(R.id.btDb3) 79 | public void goToDB3(){ 80 | startActivity(new Intent(this, MainDb3Activity.class)); 81 | } 82 | 83 | 84 | @OnClick(R.id.btDb4) 85 | public void goToDB4(){ 86 | startActivity(new Intent(this, MainDb4Activity.class)); 87 | } 88 | 89 | @OnClick(R.id.btDb5) 90 | public void goToDB5(){ 91 | startActivity(new Intent(this, MainDb5Activity.class)); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/SAFUtils.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample; 2 | 3 | import android.content.Intent; 4 | 5 | public class SAFUtils { 6 | 7 | /** 8 | * Returns an {@link Intent} for opening the Storage Access Framework file picker. 9 | */ 10 | static public Intent createAddFilePickerIntent(String fileName) { 11 | Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); 12 | intent.addCategory(Intent.CATEGORY_OPENABLE); 13 | intent.putExtra(Intent.EXTRA_TITLE, fileName); 14 | intent.setType("text/plain"); 15 | 16 | return intent; 17 | } 18 | 19 | 20 | static public Intent createSelectFilePickerIntent() { 21 | Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 22 | intent.addCategory(Intent.CATEGORY_OPENABLE); 23 | intent.setType("text/plain"); 24 | 25 | return intent; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/Utility.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample; 2 | 3 | import java.util.Date; 4 | import java.text.SimpleDateFormat; 5 | import java.util.TimeZone; 6 | 7 | /** 8 | * Created by claud on 20/02/2017. 9 | */ 10 | 11 | public class Utility { 12 | 13 | 14 | final static String formatDateTimeNoTimeZone(Date date){ 15 | SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS"); 16 | dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0")); 17 | return dateFormat.format(date); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/core/TableViewerFragment.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.core; 2 | 3 | import android.app.Fragment; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.Button; 12 | import android.widget.TextView; 13 | 14 | import com.claudiodegio.dbsync.sample.R; 15 | 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | import java.io.File; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | import butterknife.BindView; 24 | import butterknife.ButterKnife; 25 | import butterknife.OnClick; 26 | import de.codecrafters.tableview.TableDataAdapter; 27 | import de.codecrafters.tableview.TableHeaderAdapter; 28 | import de.codecrafters.tableview.TableView; 29 | import de.codecrafters.tableview.listeners.TableDataClickListener; 30 | import de.codecrafters.tableview.model.TableColumnDpWidthModel; 31 | import de.codecrafters.tableview.toolkit.SimpleTableDataAdapter; 32 | import de.codecrafters.tableview.toolkit.SimpleTableHeaderAdapter; 33 | 34 | 35 | public class TableViewerFragment extends Fragment implements TableDataClickListener { 36 | 37 | static final private Logger log = LoggerFactory.getLogger(TableViewerFragment.class); 38 | 39 | final private static int PAGE_SIZE = 5; 40 | 41 | @BindView(R.id.tvTable) 42 | TableView mTableView; 43 | 44 | @BindView(R.id.tvPage) 45 | TextView mTVPage; 46 | 47 | String mDbName; 48 | String mTableName; 49 | 50 | @BindView(R.id.btNext) 51 | Button btNext; 52 | @BindView(R.id.btPrev) 53 | Button btPrev; 54 | 55 | SQLiteDatabase mDB; 56 | 57 | private int mCurrentPage = 0; 58 | private int mNumPages = 0; 59 | 60 | private OnEditListener mOnItemClicked; 61 | 62 | public static TableViewerFragment newInstance(String dbName, String tableName) { 63 | 64 | Bundle args = new Bundle(); 65 | 66 | args.putString("db_name", dbName); 67 | args.putString("table_name", tableName); 68 | 69 | TableViewerFragment fragment = new TableViewerFragment(); 70 | fragment.setArguments(args); 71 | return fragment; 72 | } 73 | 74 | 75 | @Override 76 | public void onCreate(Bundle savedInstanceState) { 77 | super.onCreate(savedInstanceState); 78 | 79 | mDbName = getArguments().getString("db_name"); 80 | mTableName = getArguments().getString("table_name"); 81 | 82 | openDatabase(); 83 | } 84 | 85 | @Nullable 86 | @Override 87 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 88 | 89 | View view = inflater.inflate(R.layout.fragment_table_view, container, false); 90 | 91 | ButterKnife.bind(this, view); 92 | 93 | mTableView.addDataClickListener(this); 94 | return view; 95 | } 96 | 97 | @Override 98 | public void onStart() { 99 | super.onStart(); 100 | 101 | initHeader(); 102 | countPages(); 103 | mCurrentPage = 1; 104 | showPage(mCurrentPage); 105 | } 106 | 107 | @Override 108 | public void onDestroy() { 109 | super.onDestroy(); 110 | 111 | if (mDB != null) { 112 | mDB.close(); 113 | } 114 | } 115 | 116 | private void openDatabase(){ 117 | log.info("open database {}", mDbName); 118 | 119 | File dbFile = new File("/data/data/com.claudiodegio.dbsync.sample/databases", mDbName); 120 | 121 | mDB = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY); 122 | } 123 | 124 | private void initHeader(){ 125 | TableHeaderAdapter tableHeaderAdapter; 126 | Cursor cursor = mDB.query(mTableName, null, " 1 = 0", null, null, null, null); 127 | 128 | tableHeaderAdapter = new SimpleTableHeaderAdapter(getActivity(), cursor.getColumnNames()); 129 | 130 | mTableView.setHeaderAdapter(tableHeaderAdapter); 131 | cursor.close(); 132 | 133 | int headerCount = cursor.getColumnNames().length; 134 | 135 | TableColumnDpWidthModel model = new TableColumnDpWidthModel(getActivity(), headerCount, 200); 136 | model.setColumnWidth(0, 50); 137 | 138 | mTableView.setColumnModel(model); 139 | } 140 | 141 | private void showPage(int page){ 142 | log.info("Show Page {}", page); 143 | 144 | String sqlSelect = "SELECT * FROM " + mTableName + " LIMIT " + (page - 1) * PAGE_SIZE + ", " + PAGE_SIZE; 145 | 146 | Cursor selectCursor = mDB.rawQuery(sqlSelect, null); 147 | 148 | List list = new ArrayList<>(); 149 | 150 | while (selectCursor.moveToNext()) { 151 | String [] records = new String[selectCursor.getColumnCount()]; 152 | 153 | for (int i = 0; i < selectCursor.getColumnCount(); ++i) { 154 | if (!selectCursor.isNull(i)) { 155 | records[i] = selectCursor.getString(i); 156 | } else { 157 | records[i] = "[NULL]"; 158 | } 159 | } 160 | 161 | list.add(records); 162 | } 163 | 164 | TableDataAdapter tableDataAdapter = new SimpleTableDataAdapter(getActivity(), list); 165 | mTableView.setDataAdapter(tableDataAdapter); 166 | 167 | selectCursor.close(); 168 | 169 | updatePage(page); 170 | updateButton(page); 171 | } 172 | 173 | private void countPages(){ 174 | String sqlCount = "SELECT COUNT(*) FROM " + mTableName; 175 | 176 | Cursor countCursor = mDB.rawQuery(sqlCount, null); 177 | 178 | countCursor.moveToFirst(); 179 | 180 | int count = countCursor.getInt(0); 181 | countCursor.close(); 182 | 183 | mNumPages = (int) Math.ceil((float)count / (float)PAGE_SIZE); 184 | 185 | log.info("Found {} records, pages {}", count, mNumPages); 186 | } 187 | 188 | private void updatePage(int page){ 189 | mTVPage.setText( page + "/" + mNumPages); 190 | } 191 | 192 | private void updateButton(int page){ 193 | if (mNumPages == 1) { 194 | btNext.setEnabled(false); 195 | btPrev.setEnabled(false); 196 | } else { 197 | if (page == 1) { 198 | btNext.setEnabled(true); 199 | btPrev.setEnabled(false); 200 | } else if (page == mNumPages){ 201 | btNext.setEnabled(false); 202 | btPrev.setEnabled(true); 203 | } else { 204 | btNext.setEnabled(true); 205 | btPrev.setEnabled(true); 206 | } 207 | } 208 | } 209 | 210 | @OnClick(R.id.btNext) 211 | public void buttonNext(){ 212 | mCurrentPage++; 213 | showPage(mCurrentPage); 214 | } 215 | 216 | @OnClick(R.id.btPrev) 217 | public void buttonPrev(){ 218 | mCurrentPage--; 219 | showPage(mCurrentPage); 220 | } 221 | 222 | @OnClick(R.id.btAdd) 223 | public void buttonAdd(){ 224 | if (mOnItemClicked != null) { 225 | mOnItemClicked.onAdd(); 226 | } 227 | } 228 | 229 | public void reload(){ 230 | countPages(); 231 | mCurrentPage = 1; 232 | showPage(mCurrentPage); 233 | } 234 | 235 | @Override 236 | public void onDataClicked(int rowIndex, Object clickedData) { 237 | String [] data = (String[]) clickedData; 238 | 239 | long id = Long.parseLong(data[0]); 240 | 241 | if (mOnItemClicked != null) { 242 | mOnItemClicked.onItemEdit(id, data); 243 | } 244 | } 245 | 246 | @OnClick(R.id.btAdd) 247 | public void onBtAdd(){ 248 | 249 | } 250 | 251 | public interface OnEditListener { 252 | void onItemEdit(long id, String [] data ); 253 | void onAdd(); 254 | } 255 | 256 | 257 | public void setOnItemClicked(OnEditListener mOnItemClicked) { 258 | this.mOnItemClicked = mOnItemClicked; 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db1/Db1Callback.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db1; 2 | 3 | 4 | import androidx.annotation.NonNull; 5 | import androidx.sqlite.db.SupportSQLiteDatabase; 6 | import androidx.sqlite.db.SupportSQLiteOpenHelper; 7 | 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | public class Db1Callback extends SupportSQLiteOpenHelper.Callback { 12 | 13 | static final private Logger log = LoggerFactory.getLogger(Db1Callback.class); 14 | 15 | public static final int DATABASE_VERSION = 1; 16 | 17 | private static final String DDL = "CREATE TABLE name (\n" + 18 | " _id INTEGER PRIMARY KEY,\n" + 19 | " NAME TEXT,\n" + 20 | " SEND_TIME INTEGER,\n" + 21 | " CLOUD_ID TEXT UNIQUE);"; 22 | 23 | public Db1Callback() { 24 | super(DATABASE_VERSION); 25 | } 26 | 27 | @Override 28 | public void onCreate(@NonNull SupportSQLiteDatabase db) { 29 | log.info("onCreate"); 30 | db.execSQL(DDL); 31 | } 32 | 33 | @Override 34 | public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) { 35 | onCreate(db); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db1/InsertNameActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db1; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import androidx.appcompat.app.AppCompatActivity; 9 | import androidx.sqlite.db.SupportSQLiteDatabase; 10 | 11 | import android.view.View; 12 | import android.widget.Button; 13 | import android.widget.CheckBox; 14 | import android.widget.EditText; 15 | 16 | import com.claudiodegio.dbsync.sample.BaseActivity; 17 | import com.claudiodegio.dbsync.sample.DbSyncApplication; 18 | import com.claudiodegio.dbsync.sample.R; 19 | 20 | import butterknife.BindView; 21 | import butterknife.ButterKnife; 22 | import butterknife.OnClick; 23 | 24 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE; 25 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE; 26 | 27 | public class InsertNameActivity extends BaseActivity { 28 | 29 | @BindView(R.id.etName) 30 | EditText mETName; 31 | 32 | @BindView(R.id.btUpdate) 33 | Button btUpdate; 34 | 35 | @BindView(R.id.btInsert) 36 | Button btInsert; 37 | 38 | @BindView(R.id.cbNull) 39 | CheckBox mCheckBoxNull; 40 | private long mId = -1; 41 | 42 | SupportSQLiteDatabase mDB; 43 | 44 | @Override 45 | protected void onCreate(@Nullable Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(R.layout.activity_insert_name); 48 | app = (DbSyncApplication) getApplication(); 49 | mDB = app.db1OpenHelper.getWritableDatabase(); 50 | ButterKnife.bind(this); 51 | 52 | mId = getIntent().getLongExtra("ID", -1); 53 | 54 | } 55 | 56 | 57 | @OnClick(R.id.btInsert) 58 | public void insertName(){ 59 | 60 | mDB = app.db1OpenHelper.getWritableDatabase(); 61 | 62 | ContentValues contentValues = new ContentValues(); 63 | 64 | if (mCheckBoxNull.isChecked()) { 65 | contentValues.put("NAME", (String)null); 66 | } else { 67 | contentValues.put("NAME", mETName.getEditableText().toString()); 68 | } 69 | contentValues.putNull("SEND_TIME"); 70 | 71 | mDB.insert("name", CONFLICT_REPLACE, contentValues); 72 | 73 | finish(); 74 | } 75 | 76 | @OnClick(R.id.btUpdate) 77 | public void updateName(){ 78 | 79 | ContentValues contentValues = new ContentValues(); 80 | 81 | if (mCheckBoxNull.isChecked()) { 82 | contentValues.put("NAME", (String)null); 83 | } else { 84 | contentValues.put("NAME", mETName.getEditableText().toString()); 85 | } 86 | contentValues.putNull("SEND_TIME"); 87 | 88 | mDB.update("name", CONFLICT_NONE, contentValues, "_id = ? ", new String[] {Long.toString(mId)}); 89 | 90 | finish(); 91 | } 92 | 93 | @Override 94 | protected void onStart() { 95 | super.onStart(); 96 | loadById(); 97 | } 98 | 99 | public void loadById() { 100 | Cursor cur; 101 | 102 | if (mId != -1) { 103 | cur = mDB.query("SELECT * FROM name WHERE _id = ?", new String[] {Long.toString(mId)}); 104 | cur.moveToFirst(); 105 | mETName.setText(cur.getString(1)); 106 | cur.close(); 107 | btInsert.setVisibility(View.GONE); 108 | btUpdate.setVisibility(View.VISIBLE); 109 | } else { 110 | btInsert.setVisibility(View.VISIBLE); 111 | btUpdate.setVisibility(View.GONE); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db1/MainDb1Activity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db1; 2 | 3 | 4 | import android.app.FragmentManager; 5 | import android.app.FragmentTransaction; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import androidx.annotation.Nullable; 9 | import android.util.Log; 10 | 11 | import com.claudiodegio.dbsync.provider.CloudProvider; 12 | import com.claudiodegio.dbsync.DBSync; 13 | import com.claudiodegio.dbsync.provider.GDriveCloudProvider; 14 | import com.claudiodegio.dbsync.TableToSync; 15 | import com.claudiodegio.dbsync.sample.BaseMainDbActivity; 16 | import com.claudiodegio.dbsync.sample.R; 17 | import com.claudiodegio.dbsync.sample.core.TableViewerFragment; 18 | 19 | public class MainDb1Activity extends BaseMainDbActivity implements TableViewerFragment.OnEditListener { 20 | 21 | private final static String TAG = "MainDb5Activity"; 22 | 23 | TableViewerFragment mFragment; 24 | 25 | @Override 26 | protected void onCreate(@Nullable Bundle savedInstanceState) { 27 | setContentView(R.layout.activity_main_db1); 28 | super.onCreate(savedInstanceState); 29 | 30 | 31 | mFragment = TableViewerFragment.newInstance("db1", "name"); 32 | mFragment.setOnItemClicked(this); 33 | 34 | FragmentManager fm = getFragmentManager(); 35 | FragmentTransaction ft = fm.beginTransaction(); 36 | 37 | ft.add(R.id.flFragment, mFragment, "TAG").commit(); 38 | } 39 | 40 | @Override 41 | public void onItemEdit(long id, String[] data) { 42 | Intent intent = new Intent(this, InsertNameActivity.class); 43 | intent.putExtra("ID", id); 44 | startActivity(intent); 45 | } 46 | 47 | @Override 48 | public void onAdd() { 49 | startActivity(new Intent(this, InsertNameActivity.class)); 50 | } 51 | 52 | @Override 53 | public void onPostSync() { 54 | mFragment.reload(); 55 | } 56 | 57 | @Override 58 | public void onPostSelectFile() { 59 | Log.d(TAG, "onPostSelectFile"); 60 | 61 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 62 | .setDriveID(driveId) 63 | .setDriveService(googleDriveService) 64 | .build(); 65 | 66 | dbSync = new DBSync.Builder(this.getBaseContext()) 67 | .setCloudProvider(gDriveProvider) 68 | .setSQLiteDatabase(app.db1OpenHelper.getWritableDatabase()) 69 | .setDataBaseName(app.db1OpenHelper.getDatabaseName()) 70 | .addTable(new TableToSync.Builder("name").build()) 71 | .setSchemaVersion(2) 72 | .build(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db2/CategoriesActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db2; 2 | 3 | import android.app.FragmentManager; 4 | import android.app.FragmentTransaction; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | 9 | import com.claudiodegio.dbsync.sample.BaseActivity; 10 | import com.claudiodegio.dbsync.sample.R; 11 | import com.claudiodegio.dbsync.sample.core.TableViewerFragment; 12 | 13 | public class CategoriesActivity extends BaseActivity implements TableViewerFragment.OnEditListener { 14 | 15 | TableViewerFragment mFragment; 16 | 17 | @Override 18 | protected void onCreate(@Nullable Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_fragment); 21 | 22 | mFragment = TableViewerFragment.newInstance("db2", "CATEGORY"); 23 | mFragment.setOnItemClicked(this); 24 | 25 | FragmentManager fm = getFragmentManager(); 26 | FragmentTransaction ft = fm.beginTransaction(); 27 | 28 | ft.add(R.id.flFragment, mFragment, "TAG").commit(); 29 | } 30 | 31 | @Override 32 | public void onItemEdit(long id, String[] data) { 33 | Intent intent = new Intent(this, InsertCategoryActivity.class); 34 | intent.putExtra("ID", id); 35 | startActivity(intent); 36 | } 37 | 38 | @Override 39 | public void onAdd() { 40 | startActivity(new Intent(this, InsertCategoryActivity.class)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db2/Db2Callback.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db2; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.sqlite.db.SupportSQLiteDatabase; 5 | import androidx.sqlite.db.SupportSQLiteOpenHelper; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | public class Db2Callback extends SupportSQLiteOpenHelper.Callback { 11 | 12 | static final private Logger log = LoggerFactory.getLogger(Db2Callback.class); 13 | 14 | public static final int DATABASE_VERSION = 1; 15 | 16 | private static final String DDL_1 = "CREATE TABLE name (\n" + 17 | " _id INTEGER PRIMARY KEY,\n" + 18 | " NAME TEXT,\n" + 19 | " SEND_TIME INTEGER,\n" + 20 | " CLOUD_ID TEXT UNIQUE);"; 21 | 22 | private static final String DDL_2 = "CREATE TABLE CATEGORY (\n" + 23 | " _id INTEGER PRIMARY KEY,\n" + 24 | " NAME TEXT,\n" + 25 | " SEND_TIME INTEGER,\n" + 26 | " CLOUD_ID TEXT UNIQUE);"; 27 | 28 | public Db2Callback() { 29 | super(DATABASE_VERSION); 30 | } 31 | 32 | 33 | @Override 34 | public void onCreate(@NonNull SupportSQLiteDatabase db) { 35 | log.info("onCreate"); 36 | db.execSQL(DDL_1); 37 | db.execSQL(DDL_2); 38 | } 39 | 40 | @Override 41 | public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) { 42 | onCreate(db); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db2/InsertCategoryActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db2; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import androidx.sqlite.db.SupportSQLiteDatabase; 9 | 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.CheckBox; 13 | import android.widget.EditText; 14 | 15 | import com.claudiodegio.dbsync.sample.BaseActivity; 16 | import com.claudiodegio.dbsync.sample.DbSyncApplication; 17 | import com.claudiodegio.dbsync.sample.R; 18 | 19 | import butterknife.BindView; 20 | import butterknife.ButterKnife; 21 | import butterknife.OnClick; 22 | 23 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE; 24 | 25 | public class InsertCategoryActivity extends BaseActivity { 26 | 27 | @BindView(R.id.etName) 28 | EditText mETName; 29 | 30 | @BindView(R.id.btUpdate) 31 | Button btUpdate; 32 | 33 | @BindView(R.id.btInsert) 34 | Button btInsert; 35 | 36 | @BindView(R.id.cbNull) 37 | CheckBox mCheckBoxNull; 38 | private long mId = -1; 39 | 40 | SupportSQLiteDatabase mDB; 41 | 42 | @Override 43 | protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_insert_category); 46 | app = (DbSyncApplication) getApplication(); 47 | mDB = app.db2OpenHelper.getWritableDatabase(); 48 | ButterKnife.bind(this); 49 | 50 | mId = getIntent().getLongExtra("ID", -1); 51 | } 52 | 53 | @OnClick(R.id.btInsert) 54 | public void insertName(){ 55 | 56 | ContentValues contentValues = new ContentValues(); 57 | 58 | if (mCheckBoxNull.isChecked()) { 59 | contentValues.put("NAME", (String)null); 60 | } else { 61 | contentValues.put("NAME", mETName.getEditableText().toString()); 62 | } 63 | contentValues.putNull("SEND_TIME"); 64 | 65 | mDB.insert("category", CONFLICT_NONE, contentValues); 66 | 67 | finish(); 68 | } 69 | 70 | @OnClick(R.id.btUpdate) 71 | public void updateName(){ 72 | 73 | ContentValues contentValues = new ContentValues(); 74 | 75 | if (mCheckBoxNull.isChecked()) { 76 | contentValues.put("NAME", (String)null); 77 | } else { 78 | contentValues.put("NAME", mETName.getEditableText().toString()); 79 | } 80 | contentValues.putNull("SEND_TIME"); 81 | 82 | mDB.update("category",CONFLICT_NONE, contentValues, "_id = ? ", new String[] {Long.toString(mId)}); 83 | 84 | finish(); 85 | } 86 | 87 | @Override 88 | protected void onStart() { 89 | super.onStart(); 90 | loadById(); 91 | } 92 | 93 | public void loadById() { 94 | Cursor cur; 95 | 96 | if (mId != -1) { 97 | cur = mDB.query("SELECT name FROM category WHERE _id = ?", new String[] {Long.toString(mId)}); 98 | cur.moveToFirst(); 99 | mETName.setText(cur.getString(1)); 100 | cur.close(); 101 | btInsert.setVisibility(View.GONE); 102 | btUpdate.setVisibility(View.VISIBLE); 103 | } else { 104 | btInsert.setVisibility(View.VISIBLE); 105 | btUpdate.setVisibility(View.GONE); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db2/InsertNameActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db2; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import androidx.sqlite.db.SupportSQLiteDatabase; 9 | 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.CheckBox; 13 | import android.widget.EditText; 14 | 15 | import com.claudiodegio.dbsync.sample.BaseActivity; 16 | import com.claudiodegio.dbsync.sample.DbSyncApplication; 17 | import com.claudiodegio.dbsync.sample.R; 18 | 19 | import butterknife.BindView; 20 | import butterknife.ButterKnife; 21 | import butterknife.OnClick; 22 | 23 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE; 24 | 25 | public class InsertNameActivity extends BaseActivity { 26 | 27 | @BindView(R.id.etName) 28 | EditText mETName; 29 | 30 | @BindView(R.id.btUpdate) 31 | Button btUpdate; 32 | 33 | @BindView(R.id.btInsert) 34 | Button btInsert; 35 | 36 | @BindView(R.id.cbNull) 37 | CheckBox mCheckBoxNull; 38 | private long mId = -1; 39 | 40 | SupportSQLiteDatabase mDB; 41 | 42 | @Override 43 | protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_insert_name); 46 | app = (DbSyncApplication) getApplication(); 47 | mDB = app.db2OpenHelper.getWritableDatabase(); 48 | ButterKnife.bind(this); 49 | 50 | mId = getIntent().getLongExtra("ID", -1); 51 | 52 | } 53 | 54 | 55 | @OnClick(R.id.btInsert) 56 | public void insertName(){ 57 | 58 | ContentValues contentValues = new ContentValues(); 59 | 60 | if (mCheckBoxNull.isChecked()) { 61 | contentValues.put("NAME", (String)null); 62 | } else { 63 | contentValues.put("NAME", mETName.getEditableText().toString()); 64 | } 65 | contentValues.putNull("SEND_TIME"); 66 | 67 | mDB.insert("name", CONFLICT_NONE, contentValues); 68 | 69 | finish(); 70 | } 71 | 72 | @OnClick(R.id.btUpdate) 73 | public void updateName(){ 74 | 75 | ContentValues contentValues = new ContentValues(); 76 | 77 | if (mCheckBoxNull.isChecked()) { 78 | contentValues.put("NAME", (String)null); 79 | } else { 80 | contentValues.put("NAME", mETName.getEditableText().toString()); 81 | } 82 | contentValues.putNull("SEND_TIME"); 83 | 84 | mDB.update("name", CONFLICT_NONE, contentValues, "_id = ? ", new String[] {Long.toString(mId)}); 85 | 86 | finish(); 87 | } 88 | 89 | @Override 90 | protected void onStart() { 91 | super.onStart(); 92 | loadById(); 93 | } 94 | 95 | public void loadById() { 96 | Cursor cur; 97 | 98 | if (mId != -1) { 99 | cur = mDB.query("SELECT * FROM name WHERE _id = ?", new String[] {Long.toString(mId)}); 100 | cur.moveToFirst(); 101 | mETName.setText(cur.getString(1)); 102 | cur.close(); 103 | btInsert.setVisibility(View.GONE); 104 | btUpdate.setVisibility(View.VISIBLE); 105 | } else { 106 | btInsert.setVisibility(View.VISIBLE); 107 | btUpdate.setVisibility(View.GONE); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db2/MainDb2Activity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db2; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import androidx.annotation.Nullable; 7 | 8 | import com.claudiodegio.dbsync.provider.CloudProvider; 9 | import com.claudiodegio.dbsync.DBSync; 10 | import com.claudiodegio.dbsync.provider.GDriveCloudProvider; 11 | import com.claudiodegio.dbsync.TableToSync; 12 | import com.claudiodegio.dbsync.sample.BaseMainDbActivity; 13 | import com.claudiodegio.dbsync.sample.R; 14 | 15 | import butterknife.OnClick; 16 | 17 | public class MainDb2Activity extends BaseMainDbActivity { 18 | 19 | private final static String TAG = "MainDb2Activity"; 20 | 21 | 22 | @Override 23 | protected void onCreate(@Nullable Bundle savedInstanceState) { 24 | setContentView(R.layout.activity_main_db2); 25 | super.onCreate(savedInstanceState); 26 | } 27 | 28 | @Override 29 | public void onPostSync() { 30 | } 31 | 32 | @Override 33 | public void onPostSelectFile() { 34 | 35 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 36 | .setDriveID(driveId) 37 | .setDriveService(googleDriveService) 38 | .build(); 39 | 40 | dbSync = new DBSync.Builder(this.getBaseContext()) 41 | .setCloudProvider(gDriveProvider) 42 | .setSQLiteDatabase(app.db2OpenHelper.getWritableDatabase()) 43 | .setDataBaseName(app.db2OpenHelper.getDatabaseName()) 44 | .addTable(new TableToSync.Builder("name").build()) 45 | .addTable(new TableToSync.Builder("category").build()) 46 | .setSchemaVersion(1) 47 | .build(); 48 | } 49 | 50 | 51 | @OnClick(R.id.btName) 52 | public void goToNames(){ 53 | startActivity(new Intent(this, NamesActivity.class)); 54 | } 55 | 56 | 57 | @OnClick(R.id.btCat) 58 | public void goToCats(){ 59 | startActivity(new Intent(this, CategoriesActivity.class)); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db2/NamesActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db2; 2 | 3 | import android.app.FragmentManager; 4 | import android.app.FragmentTransaction; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | 9 | import com.claudiodegio.dbsync.sample.BaseActivity; 10 | import com.claudiodegio.dbsync.sample.R; 11 | import com.claudiodegio.dbsync.sample.core.TableViewerFragment; 12 | 13 | public class NamesActivity extends BaseActivity implements TableViewerFragment.OnEditListener { 14 | 15 | TableViewerFragment mFragment; 16 | 17 | @Override 18 | protected void onCreate(@Nullable Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_fragment); 21 | 22 | mFragment = TableViewerFragment.newInstance("db2", "name"); 23 | mFragment.setOnItemClicked(this); 24 | 25 | FragmentManager fm = getFragmentManager(); 26 | FragmentTransaction ft = fm.beginTransaction(); 27 | 28 | ft.add(R.id.flFragment, mFragment, "TAG").commit(); 29 | } 30 | 31 | @Override 32 | public void onItemEdit(long id, String[] data) { 33 | Intent intent = new Intent(this, InsertNameActivity.class); 34 | intent.putExtra("ID", id); 35 | startActivity(intent); 36 | } 37 | 38 | @Override 39 | public void onAdd() { 40 | startActivity(new Intent(this, InsertNameActivity.class)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db3/Db3Callback.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db3; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.sqlite.db.SupportSQLiteDatabase; 5 | import androidx.sqlite.db.SupportSQLiteOpenHelper; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | public class Db3Callback extends SupportSQLiteOpenHelper.Callback { 11 | 12 | static final private Logger log = LoggerFactory.getLogger(Db3Callback.class); 13 | 14 | public static final int DATABASE_VERSION = 1; 15 | 16 | private static final String DDL = "CREATE TABLE name (\n" + 17 | " _id INTEGER PRIMARY KEY,\n" + 18 | " NAME TEXT,\n" + 19 | " SEND_TIME INTEGER,\n" + 20 | " FILTER INTEGER,\n" + 21 | " CLOUD_ID TEXT UNIQUE);"; 22 | 23 | public Db3Callback() { 24 | super(DATABASE_VERSION); 25 | } 26 | 27 | 28 | @Override 29 | public void onCreate(@NonNull SupportSQLiteDatabase db) { 30 | log.info("onCreate"); 31 | db.execSQL(DDL); 32 | } 33 | 34 | @Override 35 | public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) { 36 | onCreate(db); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db3/InsertNameActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db3; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import androidx.sqlite.db.SupportSQLiteDatabase; 9 | 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.CheckBox; 13 | import android.widget.EditText; 14 | 15 | import com.claudiodegio.dbsync.sample.BaseActivity; 16 | import com.claudiodegio.dbsync.sample.DbSyncApplication; 17 | import com.claudiodegio.dbsync.sample.R; 18 | 19 | import butterknife.BindView; 20 | import butterknife.ButterKnife; 21 | import butterknife.OnClick; 22 | 23 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE; 24 | 25 | public class InsertNameActivity extends BaseActivity { 26 | 27 | @BindView(R.id.etName) 28 | EditText mETName; 29 | 30 | @BindView(R.id.etFilter) 31 | EditText mETFilter; 32 | 33 | @BindView(R.id.btUpdate) 34 | Button btUpdate; 35 | 36 | @BindView(R.id.btInsert) 37 | Button btInsert; 38 | 39 | @BindView(R.id.cbNull) 40 | CheckBox mCheckBoxNull; 41 | private long mId = -1; 42 | 43 | SupportSQLiteDatabase mDB; 44 | 45 | @Override 46 | protected void onCreate(@Nullable Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_insert_name_db3); 49 | app = (DbSyncApplication) getApplication(); 50 | mDB = app.db3OpenHelper.getWritableDatabase(); 51 | ButterKnife.bind(this); 52 | 53 | mId = getIntent().getLongExtra("ID", -1); 54 | 55 | } 56 | 57 | 58 | @OnClick(R.id.btInsert) 59 | public void insertName(){ 60 | 61 | ContentValues contentValues = new ContentValues(); 62 | 63 | if (mCheckBoxNull.isChecked()) { 64 | contentValues.put("NAME", (String)null); 65 | } else { 66 | contentValues.put("NAME", mETName.getEditableText().toString()); 67 | } 68 | contentValues.put("FILTER", Integer.parseInt(mETFilter.getEditableText().toString())); 69 | contentValues.putNull("SEND_TIME"); 70 | 71 | mDB.insert("name", CONFLICT_NONE, contentValues); 72 | 73 | finish(); 74 | } 75 | 76 | @OnClick(R.id.btUpdate) 77 | public void updateName(){ 78 | 79 | ContentValues contentValues = new ContentValues(); 80 | 81 | if (mCheckBoxNull.isChecked()) { 82 | contentValues.put("NAME", (String)null); 83 | } else { 84 | contentValues.put("NAME", mETName.getEditableText().toString()); 85 | } 86 | 87 | contentValues.put("FILTER", Integer.parseInt(mETFilter.getEditableText().toString())); 88 | contentValues.putNull("SEND_TIME"); 89 | 90 | mDB.update("name", CONFLICT_NONE, contentValues, "_id = ? ", new String[] {Long.toString(mId)}); 91 | 92 | finish(); 93 | } 94 | 95 | @Override 96 | protected void onStart() { 97 | super.onStart(); 98 | loadById(); 99 | } 100 | 101 | public void loadById() { 102 | Cursor cur; 103 | 104 | if (mId != -1) { 105 | cur = mDB.query("SELECT * FROM name WHERE _id = ?", new String[] {Long.toString(mId)}); 106 | cur.moveToFirst(); 107 | mETName.setText(cur.getString(1)); 108 | cur.close(); 109 | btInsert.setVisibility(View.GONE); 110 | btUpdate.setVisibility(View.VISIBLE); 111 | } else { 112 | btInsert.setVisibility(View.VISIBLE); 113 | btUpdate.setVisibility(View.GONE); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db3/MainDb3Activity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db3; 2 | 3 | 4 | import android.app.FragmentManager; 5 | import android.app.FragmentTransaction; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import androidx.annotation.Nullable; 9 | 10 | import com.claudiodegio.dbsync.provider.CloudProvider; 11 | import com.claudiodegio.dbsync.DBSync; 12 | import com.claudiodegio.dbsync.provider.GDriveCloudProvider; 13 | import com.claudiodegio.dbsync.TableToSync; 14 | import com.claudiodegio.dbsync.sample.BaseMainDbActivity; 15 | import com.claudiodegio.dbsync.sample.R; 16 | import com.claudiodegio.dbsync.sample.core.TableViewerFragment; 17 | 18 | public class MainDb3Activity extends BaseMainDbActivity implements TableViewerFragment.OnEditListener { 19 | 20 | private final static String TAG = "MainDb3Activity"; 21 | 22 | TableViewerFragment mFragment; 23 | 24 | @Override 25 | protected void onCreate(@Nullable Bundle savedInstanceState) { 26 | setContentView(R.layout.activity_main_db3); 27 | super.onCreate(savedInstanceState); 28 | 29 | 30 | mFragment = TableViewerFragment.newInstance("db3", "name"); 31 | mFragment.setOnItemClicked(this); 32 | 33 | FragmentManager fm = getFragmentManager(); 34 | FragmentTransaction ft = fm.beginTransaction(); 35 | 36 | ft.add(R.id.flFragment, mFragment, "TAG").commit(); 37 | } 38 | 39 | @Override 40 | public void onItemEdit(long id, String[] data) { 41 | Intent intent = new Intent(this, InsertNameActivity.class); 42 | intent.putExtra("ID", id); 43 | startActivity(intent); 44 | } 45 | 46 | 47 | @Override 48 | public void onAdd() { 49 | startActivity(new Intent(this, InsertNameActivity.class)); 50 | } 51 | 52 | @Override 53 | public void onPostSync() { 54 | mFragment.reload(); 55 | } 56 | 57 | @Override 58 | public void onPostSelectFile() { 59 | 60 | 61 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 62 | .setDriveID(driveId) 63 | .setDriveService(googleDriveService) 64 | .build(); 65 | 66 | TableToSync tableName = new TableToSync.Builder("name") 67 | .setFilter("FILTER = 0") 68 | .build(); 69 | 70 | dbSync = new DBSync.Builder(this.getBaseContext()) 71 | .setCloudProvider(gDriveProvider) 72 | .setSQLiteDatabase(app.db3OpenHelper.getWritableDatabase()) 73 | .setDataBaseName(app.db3OpenHelper.getDatabaseName()) 74 | .addTable(tableName) 75 | .setSchemaVersion(2) 76 | .build(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db4/ArticlesActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db4; 2 | 3 | import android.app.FragmentManager; 4 | import android.app.FragmentTransaction; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | 9 | import com.claudiodegio.dbsync.sample.BaseActivity; 10 | import com.claudiodegio.dbsync.sample.R; 11 | import com.claudiodegio.dbsync.sample.core.TableViewerFragment; 12 | 13 | 14 | public class ArticlesActivity extends BaseActivity implements TableViewerFragment.OnEditListener { 15 | 16 | TableViewerFragment mFragment; 17 | 18 | @Override 19 | protected void onCreate(@Nullable Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_fragment); 22 | 23 | mFragment = TableViewerFragment.newInstance("db4", "article"); 24 | 25 | mFragment.setOnItemClicked(this); 26 | 27 | FragmentManager fm = getFragmentManager(); 28 | FragmentTransaction ft = fm.beginTransaction(); 29 | 30 | ft.add(R.id.flFragment, mFragment, "TAG").commit(); 31 | } 32 | 33 | @Override 34 | public void onItemEdit(long id, String[] data) { 35 | Intent intent = new Intent(this, InsertArticleActivity.class); 36 | 37 | intent.putExtra("ID", id); 38 | startActivity(intent); 39 | } 40 | 41 | @Override 42 | public void onAdd() { 43 | startActivity(new Intent(this, InsertArticleActivity.class)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db4/CategoriesActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db4; 2 | 3 | import android.app.FragmentManager; 4 | import android.app.FragmentTransaction; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | 9 | import com.claudiodegio.dbsync.sample.BaseActivity; 10 | import com.claudiodegio.dbsync.sample.R; 11 | import com.claudiodegio.dbsync.sample.core.TableViewerFragment; 12 | 13 | 14 | public class CategoriesActivity extends BaseActivity implements TableViewerFragment.OnEditListener { 15 | 16 | TableViewerFragment mFragment; 17 | 18 | @Override 19 | protected void onCreate(@Nullable Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_fragment); 22 | 23 | mFragment = TableViewerFragment.newInstance("db4", "CATEGORY"); 24 | 25 | mFragment.setOnItemClicked(this); 26 | 27 | FragmentManager fm = getFragmentManager(); 28 | FragmentTransaction ft = fm.beginTransaction(); 29 | 30 | ft.add(R.id.flFragment, mFragment, "TAG").commit(); 31 | } 32 | 33 | @Override 34 | public void onItemEdit(long id, String[] data) { 35 | Intent intent = new Intent(this, InsertCategoryActivity.class); 36 | 37 | intent.putExtra("ID", id); 38 | startActivity(intent); 39 | } 40 | 41 | @Override 42 | public void onAdd() { 43 | startActivity(new Intent(this, InsertCategoryActivity.class)); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db4/Db4Callback.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db4; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.sqlite.db.SupportSQLiteDatabase; 5 | import androidx.sqlite.db.SupportSQLiteOpenHelper; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | public class Db4Callback extends SupportSQLiteOpenHelper.Callback { 11 | 12 | static final private Logger log = LoggerFactory.getLogger(Db4Callback.class); 13 | 14 | public static final int DATABASE_VERSION = 1; 15 | 16 | private static final String DDL_CATEGORY = "CREATE TABLE category (\n" + 17 | " _id INTEGER PRIMARY KEY,\n" + 18 | " NAME TEXT,\n" + 19 | " SEND_TIME INTEGER,\n" + 20 | " CLOUD_ID TEXT UNIQUE);"; 21 | 22 | private static final String DDL_ARTICLE = "CREATE TABLE article (\n" + 23 | " _id INTEGER PRIMARY KEY,\n" + 24 | " NAME TEXT,\n" + 25 | " SEND_TIME INTEGER,\n" + 26 | " CLOUD_ID TEXT UNIQUE,\n" + 27 | " CATEGORY_ID INTEGER REFERENCES category (_id));"; 28 | 29 | public Db4Callback() { 30 | super(DATABASE_VERSION); 31 | } 32 | 33 | 34 | @Override 35 | public void onCreate(@NonNull SupportSQLiteDatabase db) { 36 | log.info("onCreate"); 37 | db.execSQL(DDL_CATEGORY); 38 | db.execSQL(DDL_ARTICLE); 39 | } 40 | 41 | @Override 42 | public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) { 43 | onCreate(db); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db4/InsertArticleActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db4; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import androidx.sqlite.db.SupportSQLiteDatabase; 9 | 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.CheckBox; 13 | import android.widget.EditText; 14 | 15 | import com.claudiodegio.dbsync.sample.BaseActivity; 16 | import com.claudiodegio.dbsync.sample.DbSyncApplication; 17 | import com.claudiodegio.dbsync.sample.R; 18 | 19 | import butterknife.BindView; 20 | import butterknife.ButterKnife; 21 | import butterknife.OnClick; 22 | 23 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE; 24 | 25 | public class InsertArticleActivity extends BaseActivity { 26 | 27 | @BindView(R.id.etName) 28 | EditText mETName; 29 | 30 | @BindView(R.id.etCategoryId) 31 | EditText mETCategoryId; 32 | 33 | @BindView(R.id.btUpdate) 34 | Button btUpdate; 35 | 36 | @BindView(R.id.btInsert) 37 | Button btInsert; 38 | 39 | @BindView(R.id.cbNull) 40 | CheckBox mCheckBoxNull; 41 | 42 | @BindView(R.id.cbNullCategory) 43 | CheckBox mCheckBoxNullCategoryId; 44 | private long mId = -1; 45 | 46 | SupportSQLiteDatabase mDB; 47 | 48 | @Override 49 | protected void onCreate(@Nullable Bundle savedInstanceState) { 50 | super.onCreate(savedInstanceState); 51 | setContentView(R.layout.activity_insert_article); 52 | app = (DbSyncApplication) getApplication(); 53 | mDB = app.db4OpenHelper.getWritableDatabase(); 54 | 55 | ButterKnife.bind(this); 56 | 57 | mId = getIntent().getLongExtra("ID", -1); 58 | 59 | } 60 | 61 | 62 | @OnClick(R.id.btInsert) 63 | public void insertName(){ 64 | 65 | mDB.insert("article", CONFLICT_NONE, buildContentValue()); 66 | 67 | finish(); 68 | } 69 | 70 | @OnClick(R.id.btUpdate) 71 | public void updateName(){ 72 | 73 | mDB.update("article", CONFLICT_NONE, buildContentValue(), "_id = ? ", new String[] {Long.toString(mId)}); 74 | 75 | finish(); 76 | } 77 | 78 | private ContentValues buildContentValue(){ 79 | 80 | ContentValues contentValues = new ContentValues(); 81 | 82 | if (mCheckBoxNull.isChecked()) { 83 | contentValues.putNull("NAME"); 84 | } else { 85 | contentValues.put("NAME", mETName.getEditableText().toString()); 86 | } 87 | 88 | String categoryID = mETCategoryId.getEditableText().toString(); 89 | 90 | if (mCheckBoxNullCategoryId.isChecked() || categoryID.isEmpty()) { 91 | contentValues.putNull("CATEGORY_ID"); 92 | } else { 93 | contentValues.put("CATEGORY_ID", Integer.parseInt(categoryID)); 94 | } 95 | contentValues.putNull("SEND_TIME"); 96 | 97 | return contentValues; 98 | } 99 | 100 | @Override 101 | protected void onStart() { 102 | super.onStart(); 103 | loadById(); 104 | } 105 | 106 | public void loadById() { 107 | Cursor cur; 108 | 109 | if (mId != -1) { 110 | cur = mDB.query("SELECT * FROM article WHERE _id = ?", new String[] {Long.toString(mId)}); 111 | cur.moveToFirst(); 112 | mETName.setText(cur.getString(1)); 113 | 114 | if (!cur.isNull(4)){ 115 | mETCategoryId.setText(cur.getString(4)); 116 | } 117 | 118 | cur.close(); 119 | btInsert.setVisibility(View.GONE); 120 | btUpdate.setVisibility(View.VISIBLE); 121 | } else { 122 | btInsert.setVisibility(View.VISIBLE); 123 | btUpdate.setVisibility(View.GONE); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db4/InsertCategoryActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db4; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import androidx.sqlite.db.SupportSQLiteDatabase; 9 | 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.CheckBox; 13 | import android.widget.EditText; 14 | 15 | import com.claudiodegio.dbsync.sample.BaseActivity; 16 | import com.claudiodegio.dbsync.sample.DbSyncApplication; 17 | import com.claudiodegio.dbsync.sample.R; 18 | 19 | import butterknife.BindView; 20 | import butterknife.ButterKnife; 21 | import butterknife.OnClick; 22 | 23 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE; 24 | 25 | public class InsertCategoryActivity extends BaseActivity { 26 | 27 | @BindView(R.id.etName) 28 | EditText mETName; 29 | 30 | @BindView(R.id.btUpdate) 31 | Button btUpdate; 32 | 33 | @BindView(R.id.btInsert) 34 | Button btInsert; 35 | 36 | @BindView(R.id.cbNull) 37 | CheckBox mCheckBoxNull; 38 | private long mId = -1; 39 | 40 | SupportSQLiteDatabase mDB; 41 | 42 | @Override 43 | protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_insert_category); 46 | app = (DbSyncApplication) getApplication(); 47 | mDB = app.db4OpenHelper.getWritableDatabase(); 48 | 49 | ButterKnife.bind(this); 50 | 51 | mId = getIntent().getLongExtra("ID", -1); 52 | } 53 | 54 | @OnClick(R.id.btInsert) 55 | public void insertName(){ 56 | 57 | mDB.insert("category", CONFLICT_NONE, buildContentValue()); 58 | 59 | finish(); 60 | } 61 | 62 | @OnClick(R.id.btUpdate) 63 | public void updateName(){ 64 | 65 | mDB.update("category", CONFLICT_NONE, buildContentValue(), "_id = ? ", new String[] {Long.toString(mId)}); 66 | 67 | finish(); 68 | } 69 | 70 | private ContentValues buildContentValue(){ 71 | ContentValues contentValues = new ContentValues(); 72 | 73 | if (mCheckBoxNull.isChecked()) { 74 | contentValues.putNull("NAME"); 75 | } else { 76 | contentValues.put("NAME", mETName.getEditableText().toString()); 77 | } 78 | contentValues.putNull("SEND_TIME"); 79 | 80 | return contentValues; 81 | 82 | } 83 | 84 | @Override 85 | protected void onStart() { 86 | super.onStart(); 87 | loadById(); 88 | } 89 | 90 | public void loadById() { 91 | Cursor cur; 92 | 93 | if (mId != -1) { 94 | cur = mDB.query("SELECT name FROM category WHERE _id = ?", new String[] {Long.toString(mId)}); 95 | cur.moveToFirst(); 96 | mETName.setText(cur.getString(1)); 97 | cur.close(); 98 | btInsert.setVisibility(View.GONE); 99 | btUpdate.setVisibility(View.VISIBLE); 100 | } else { 101 | btInsert.setVisibility(View.VISIBLE); 102 | btUpdate.setVisibility(View.GONE); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db4/MainDb4Activity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db4; 2 | 3 | 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import androidx.annotation.Nullable; 7 | 8 | import com.claudiodegio.dbsync.provider.CloudProvider; 9 | import com.claudiodegio.dbsync.DBSync; 10 | import com.claudiodegio.dbsync.provider.GDriveCloudProvider; 11 | import com.claudiodegio.dbsync.TableToSync; 12 | import com.claudiodegio.dbsync.sample.BaseMainDbActivity; 13 | import com.claudiodegio.dbsync.sample.R; 14 | 15 | import butterknife.OnClick; 16 | 17 | public class MainDb4Activity extends BaseMainDbActivity { 18 | 19 | private final static String TAG = "MainDb4Activity"; 20 | 21 | @Override 22 | protected void onCreate(@Nullable Bundle savedInstanceState) { 23 | setContentView(R.layout.activity_main_db4); 24 | super.onCreate(savedInstanceState); 25 | } 26 | 27 | @Override 28 | public void onPostSync() { 29 | } 30 | 31 | @Override 32 | public void onPostSelectFile() { 33 | 34 | 35 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 36 | .setDriveID(driveId) 37 | .setDriveService(googleDriveService) 38 | .build(); 39 | 40 | TableToSync tableCategory = new TableToSync.Builder("CATEGORY") 41 | .build(); 42 | 43 | TableToSync tableArticle = new TableToSync.Builder("ARTICLE") 44 | .addJoinTable(tableCategory, "CATEGORY_ID") 45 | .build(); 46 | 47 | dbSync = new DBSync.Builder(this.getBaseContext()) 48 | .setCloudProvider(gDriveProvider) 49 | .setSQLiteDatabase(app.db4OpenHelper.getWritableDatabase()) 50 | .setDataBaseName(app.db4OpenHelper.getDatabaseName()) 51 | .addTable(tableCategory) 52 | .addTable(tableArticle) 53 | .setSchemaVersion(1) 54 | .build(); 55 | 56 | } 57 | 58 | 59 | @OnClick(R.id.btArticle) 60 | public void goToNames(){ 61 | startActivity(new Intent(this, ArticlesActivity.class)); 62 | } 63 | 64 | 65 | @OnClick(R.id.btCat) 66 | public void goToCats(){ 67 | startActivity(new Intent(this, CategoriesActivity.class)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db5/Db5Callback.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db5; 2 | 3 | import androidx.annotation.NonNull; 4 | import androidx.sqlite.db.SupportSQLiteDatabase; 5 | import androidx.sqlite.db.SupportSQLiteOpenHelper; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | public class Db5Callback extends SupportSQLiteOpenHelper.Callback { 11 | 12 | static final private Logger log = LoggerFactory.getLogger(Db5Callback.class); 13 | 14 | public static final int DATABASE_VERSION = 1; 15 | 16 | private static final String DDL = "CREATE TABLE category (\n" + 17 | " _id INTEGER PRIMARY KEY,\n" + 18 | " NAME TEXT,\n" + 19 | " SEND_TIME INTEGER,\n" + 20 | " CLOUD_ID TEXT UNIQUE);"; 21 | 22 | public Db5Callback() { 23 | super(DATABASE_VERSION); 24 | } 25 | 26 | @Override 27 | public void onCreate(@NonNull SupportSQLiteDatabase db) { 28 | log.info("onCreate"); 29 | db.execSQL(DDL); 30 | } 31 | 32 | @Override 33 | public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) { 34 | onCreate(db); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db5/InsertNameActivity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db5; 2 | 3 | import android.content.ContentValues; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.os.Bundle; 7 | import androidx.annotation.Nullable; 8 | import androidx.sqlite.db.SupportSQLiteDatabase; 9 | 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.CheckBox; 13 | import android.widget.EditText; 14 | 15 | import com.claudiodegio.dbsync.sample.BaseActivity; 16 | import com.claudiodegio.dbsync.sample.DbSyncApplication; 17 | import com.claudiodegio.dbsync.sample.R; 18 | 19 | import butterknife.BindView; 20 | import butterknife.ButterKnife; 21 | import butterknife.OnClick; 22 | 23 | import static android.database.sqlite.SQLiteDatabase.CONFLICT_NONE; 24 | 25 | public class InsertNameActivity extends BaseActivity { 26 | 27 | @BindView(R.id.etName) 28 | EditText mETName; 29 | 30 | @BindView(R.id.btUpdate) 31 | Button btUpdate; 32 | 33 | @BindView(R.id.btInsert) 34 | Button btInsert; 35 | 36 | @BindView(R.id.cbNull) 37 | CheckBox mCheckBoxNull; 38 | private long mId = -1; 39 | 40 | SupportSQLiteDatabase mDB; 41 | 42 | @Override 43 | protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_insert_name); 46 | app = (DbSyncApplication) getApplication(); 47 | mDB = app.db5OpenHelper.getWritableDatabase(); 48 | ButterKnife.bind(this); 49 | 50 | mId = getIntent().getLongExtra("ID", -1); 51 | 52 | } 53 | 54 | 55 | @OnClick(R.id.btInsert) 56 | public void insertName(){ 57 | 58 | ContentValues contentValues = new ContentValues(); 59 | 60 | if (mCheckBoxNull.isChecked()) { 61 | contentValues.put("NAME", (String)null); 62 | } else { 63 | contentValues.put("NAME", mETName.getEditableText().toString()); 64 | } 65 | contentValues.putNull("SEND_TIME"); 66 | 67 | mDB.insert("category", CONFLICT_NONE, contentValues); 68 | 69 | finish(); 70 | } 71 | 72 | @OnClick(R.id.btUpdate) 73 | public void updateName(){ 74 | 75 | ContentValues contentValues = new ContentValues(); 76 | 77 | if (mCheckBoxNull.isChecked()) { 78 | contentValues.put("NAME", (String)null); 79 | } else { 80 | contentValues.put("NAME", mETName.getEditableText().toString()); 81 | } 82 | contentValues.putNull("SEND_TIME"); 83 | 84 | mDB.update("category", CONFLICT_NONE, contentValues, "_id = ? ", new String[] {Long.toString(mId)}); 85 | 86 | finish(); 87 | } 88 | 89 | @Override 90 | protected void onStart() { 91 | super.onStart(); 92 | loadById(); 93 | } 94 | 95 | public void loadById() { 96 | Cursor cur; 97 | 98 | if (mId != -1) { 99 | cur = mDB.query("SELECT * FROM name WHERE _id = ?", new String[] {Long.toString(mId)}); 100 | cur.moveToFirst(); 101 | mETName.setText(cur.getString(1)); 102 | cur.close(); 103 | btInsert.setVisibility(View.GONE); 104 | btUpdate.setVisibility(View.VISIBLE); 105 | } else { 106 | btInsert.setVisibility(View.VISIBLE); 107 | btUpdate.setVisibility(View.GONE); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /app/src/main/java/com/claudiodegio/dbsync/sample/db5/MainDb5Activity.java: -------------------------------------------------------------------------------- 1 | package com.claudiodegio.dbsync.sample.db5; 2 | 3 | 4 | import android.app.FragmentManager; 5 | import android.app.FragmentTransaction; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import androidx.annotation.Nullable; 9 | import android.util.Log; 10 | 11 | import com.claudiodegio.dbsync.provider.CloudProvider; 12 | import com.claudiodegio.dbsync.DBSync; 13 | import com.claudiodegio.dbsync.provider.GDriveCloudProvider; 14 | import com.claudiodegio.dbsync.TableToSync; 15 | import com.claudiodegio.dbsync.sample.BaseMainDbActivity; 16 | import com.claudiodegio.dbsync.sample.R; 17 | import com.claudiodegio.dbsync.sample.core.TableViewerFragment; 18 | 19 | public class MainDb5Activity extends BaseMainDbActivity implements TableViewerFragment.OnEditListener { 20 | 21 | private final static String TAG = "MainDb5Activity"; 22 | 23 | TableViewerFragment mFragment; 24 | 25 | @Override 26 | protected void onCreate(@Nullable Bundle savedInstanceState) { 27 | setContentView(R.layout.activity_main_db5); 28 | super.onCreate(savedInstanceState); 29 | 30 | 31 | mFragment = TableViewerFragment.newInstance("db5", "category"); 32 | mFragment.setOnItemClicked(this); 33 | 34 | FragmentManager fm = getFragmentManager(); 35 | FragmentTransaction ft = fm.beginTransaction(); 36 | 37 | ft.add(R.id.flFragment, mFragment, "TAG").commit(); 38 | } 39 | 40 | @Override 41 | public void onItemEdit(long id, String[] data) { 42 | Intent intent = new Intent(this, InsertNameActivity.class); 43 | intent.putExtra("ID", id); 44 | startActivity(intent); 45 | } 46 | 47 | @Override 48 | public void onAdd() { 49 | startActivity(new Intent(this, InsertNameActivity.class)); 50 | } 51 | 52 | @Override 53 | public void onPostSync() { 54 | mFragment.reload(); 55 | } 56 | 57 | @Override 58 | public void onPostSelectFile() { 59 | Log.d(TAG, "onPostSelectFile"); 60 | 61 | CloudProvider gDriveProvider = new GDriveCloudProvider.Builder(this.getBaseContext()) 62 | .setDriveID(driveId) 63 | .setDriveService(googleDriveService) 64 | .build(); 65 | TableToSync tableToSync = new TableToSync.Builder("category") 66 | .addMatchRule("NAME = :NAME") 67 | .build(); 68 | 69 | dbSync = new DBSync.Builder(this.getBaseContext()) 70 | .setCloudProvider(gDriveProvider) 71 | .setSQLiteDatabase(app.db5OpenHelper.getWritableDatabase()) 72 | .setDataBaseName(app.db5OpenHelper.getDatabaseName()) 73 | .addTable(tableToSync) 74 | .setSchemaVersion(2) 75 | .build(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_insert_article.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 | 18 | 19 | 25 | 26 | 31 | 32 |