├── .gitignore
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── sixthsolution
│ │ └── lpisyncadapterapp
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── sixthsolution
│ │ │ └── lpisyncadapterapp
│ │ │ ├── CustomLoginActivity.java
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── layout
│ │ ├── activity_custom_login.xml
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── sixthsolution
│ └── lpisyncadapterapp
│ └── ExampleUnitTest.java
├── build.gradle
├── easy-apple-syncadapter
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── sixthsolution
│ │ └── lpisyncadapter
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── sixthsolution
│ │ │ └── lpisyncadapter
│ │ │ ├── App.java
│ │ │ ├── GlobalConstant.java
│ │ │ ├── authenticator
│ │ │ ├── AuthServerHandler.java
│ │ │ ├── AuthServerHandlerImpl.java
│ │ │ ├── BaseLoginActivity.java
│ │ │ ├── ICalAuthenticator.java
│ │ │ ├── ICalAuthenticatorService.java
│ │ │ ├── LoginActivity.java
│ │ │ ├── SignInException.java
│ │ │ └── crypto
│ │ │ │ ├── Crypto.java
│ │ │ │ └── KeyManager.java
│ │ │ ├── download
│ │ │ └── EventDownloader.java
│ │ │ ├── entitiy
│ │ │ ├── BaseCalendarData.java
│ │ │ ├── BaseCollectionInfo.java
│ │ │ ├── CalendarData.java
│ │ │ └── CollectionInfo.java
│ │ │ ├── exceptions
│ │ │ └── InvalidAccountException.java
│ │ │ ├── resource
│ │ │ ├── LocalCalendar.java
│ │ │ ├── LocalCollection.java
│ │ │ ├── LocalEvent.java
│ │ │ ├── LocalResource.java
│ │ │ └── LocalTask.java
│ │ │ ├── syncadapter
│ │ │ ├── BaseSyncAdapter.java
│ │ │ ├── ICalSyncAdapter.java
│ │ │ ├── ICalSyncService.java
│ │ │ ├── SyncManager.java
│ │ │ ├── SyncServerHandler.java
│ │ │ └── SyncServerHandlerImpl.java
│ │ │ └── util
│ │ │ ├── AccountSettings.java
│ │ │ ├── ArrayUtils.java
│ │ │ ├── DavResourceFinder.java
│ │ │ ├── DavUtils.java
│ │ │ ├── HttpClient.java
│ │ │ ├── MemoryCookieStore.java
│ │ │ ├── SSLSocketFactoryCompat.java
│ │ │ └── Util.java
│ ├── lombok.config
│ └── res
│ │ ├── drawable
│ │ └── ical_icon.png
│ │ ├── layout
│ │ └── activity_login.xml
│ │ ├── values
│ │ └── strings.xml
│ │ └── xml
│ │ ├── authenticator.xml
│ │ └── sync_adapter.xml
│ └── test
│ └── java
│ └── com
│ └── sixthsolution
│ └── lpisyncadapter
│ └── ExampleUnitTest.java
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | .gradle
3 | /local.properties
4 | .DS_Store
5 | /build
6 | # Android Studio
7 | .idea
8 | *.iml
9 | *.class
10 | *.apk
11 | /captures
12 |
13 | ## Android Studio and Intellij and Android in general
14 | *.ipr
15 | *.iws
16 | out/
17 | com_crashlytics_export_strings.xml
18 | OkHttpClient/build
19 | lib/build
--------------------------------------------------------------------------------
/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.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Easy Apple Sync Adapter is an Android Library for syncing with **apple** calendar service.
2 |
3 | Performing authentication and full duplex sync with **apple caldav** server.
4 |
5 | _This library is based on [DavDroid](https://gitlab.com/bitfireAT/davdroid), and borrows many code from them.
6 | we just simplify the process of reusing the library_
7 |
8 | ## Features
9 | * Easy to use.
10 | * Powerful encryption for passwords.
11 |
12 | ## Installation
13 | 1) Configure your top-level `build.gradle` to include our repository
14 | ```groovy
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | maven { url "http://dl.bintray.com/6thsolution/public-maven" }
19 | }
20 | }
21 | ```
22 | Then config your app-level `build.gradle` to include the library as dependency:
23 | ``` groovy
24 | compile 'com.sixthsolution:easyapplesyncadapter:1.0.0-beta1'
25 | ```
26 |
27 | 2) config:
28 |
29 | **Authenticator config** : Add Authenticator service to your manifest:
30 | ```xml
31 |
35 |
36 |
37 |
38 |
39 |
43 |
47 |
51 |
52 | ```
53 | There is a few metadata that you can pass your parameters to the service via them:
54 |
55 | 1) `android.accounts.AccountAuthenticator`
56 | The authenticator config file, something like this:
57 | ```xml
58 |
64 |
65 | ```
66 | 2) `android:accountType` parameter must be the same value you passed via `unique_authentication_type` metadata.
67 | this must be **unique**, otherwise your app may not work. so use something package specific.
68 |
69 | 3) `login_activity_class`
70 | You can pass a custom login activity with custom ui for login process.
71 | The value must be _complete path with package name_ like: `com.sixthsolution.applecalendar.CustomLoginActivity`.
72 | Your activity must extend **BaseLoginActivity** and your layout must have some view that is necessary for login process.
73 | two _EditText_ with exact id as `user_name` and `password` . and one _Button_ with id `signin_button` and
74 | After calling _setContentView()_ in _onCreate()_ you must call **init()**.
75 |
76 | **SyncAdapter config** :
77 | You also need to add sync adapter service to your manifest:
78 | ```xml
79 |
83 |
84 |
85 |
86 |
90 |
91 | ```
92 | 1) `sync_adapter` file is something like this:
93 | ```xml
94 |
101 |
102 | ```
103 | 2) `android:contentAuthority="com.android.calendar"` always must be it. (do NOT change it except when you need to).
104 | 3) `android:accountType` must be the same value you set for authenticator config.
105 |
106 | ## Usage
107 | * **Add new account**:
108 | You must call `AccountManager#addAccount` for adding new account. it automatically you the config and open
109 | `LoginActivity` and handle add progress by itself.
110 | ```java
111 | accountManager.addAccount(AUTHTOKEN_TYPE_FULL_ACCESS, AUTHTOKEN_TYPE_FULL_ACCESS, null, null, this,
112 | new AccountManagerCallback() {
113 | @Override
114 | public void run(AccountManagerFuture future) {
115 | try {
116 | Bundle bnd = future.getResult();
117 | } catch (Exception e) {
118 | e.printStackTrace();
119 | }
120 | }
121 | }, null);
122 | ```
123 | * **Get list of available accounts**:
124 | ```java
125 | AccountManager accountManager = AccountManager.get(this);
126 | Account availableAccounts[] = accountManager.getAccountsByType(AUTHTOKEN_TYPE_FULL_ACCESS);
127 | ```
128 | * **Requesting manual sync**:
129 | ```java
130 | Bundle params = new Bundle();
131 | params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
132 | params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
133 | ContentResolver.requestSync(account, GlobalConstant.AUTHORITY, params);
134 | ```
135 | * **Listening for sync finish**:
136 | if you wana know when the sync finish or show progress during progress use a `ContentObserver` :
137 | ```java
138 | ContentObserver contentObserver = new ContentObserver(new Handler()) {
139 | @Override
140 | public void onChange(boolean selfChange, Uri uri) {
141 | super.onChange(selfChange, uri);
142 | showMessage("Sync Finished");
143 | }
144 | };
145 | ```
146 | ```java
147 | @Override
148 | protected void onCreate(Bundle savedInstanceState) {
149 | super.onCreate(savedInstanceState);
150 | setContentView(R.layout.activity_main);
151 | getContentResolver().registerContentObserver(GlobalConstant.CONTENT_URI, true, contentObserver);
152 | }
153 | ```
154 | do NOT forget to unregister it:
155 | ```java
156 | @Override
157 | protected void onDestroy() {
158 | super.onDestroy();
159 | getContentResolver().unregisterContentObserver(contentObserver);
160 | }
161 | ```
162 | * **Get list of Calendars associated with account**:
163 | ```java
164 | LocalCalendar[] localCalendars = (LocalCalendar[]) LocalCalendar.find(account,
165 | // get contentProviderClient for your authority
166 | getContentResolver().acquireContentProviderClient(GlobalConstant.AUTHORITY),
167 | LocalCalendar.Factory.INSTANCE,
168 | null,
169 | null);
170 | ```
171 | * **Get list of events associated with LocalCalendar**:
172 | You can get list of all resource by:
173 | ```java
174 | LocalResource[] localResources = localCalendar.getAll();
175 | ```
176 | Then cast if to `LocalEvent`:
177 | ```java
178 | (LocalEvent) localResources[i]
179 | ```
180 |
181 | ## License
182 | ```
183 | Copyright 2016-2017 6thSolution Technologies Inc.
184 |
185 | Licensed under the Apache License, Version 2.0 (the "License");
186 | you may not use this file except in compliance with the License.
187 | You may obtain a copy of the License at
188 |
189 | http://www.apache.org/licenses/LICENSE-2.0
190 |
191 | Unless required by applicable law or agreed to in writing, software
192 | distributed under the License is distributed on an "AS IS" BASIS,
193 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
194 | See the License for the specific language governing permissions and
195 | limitations under the License.
196 | ```
197 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'com.neenbedankt.android-apt'
3 |
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion "25.0.2"
7 | defaultConfig {
8 | applicationId "com.sixthsolution.lpisyncadapterapp"
9 | minSdkVersion 16
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | compile fileTree(include: ['*.jar'], dir: 'libs')
25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26 | exclude group: 'com.android.support', module: 'support-annotations'
27 | })
28 | compile 'com.android.support:appcompat-v7:25.3.1'
29 | compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9'
30 | testCompile 'junit:junit:4.12'
31 | compile project(':easy-apple-syncadapter')
32 |
33 | //for runtime permission
34 | compile 'com.github.hotchemi:permissionsdispatcher:2.2.1'
35 | apt 'com.github.hotchemi:permissionsdispatcher-processor:2.2.1'
36 | }
37 |
--------------------------------------------------------------------------------
/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 H:\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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/sixthsolution/lpisyncadapterapp/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapterapp;
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.assertEquals;
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.sixthsolution.lpisyncadapterapp", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
37 |
38 |
39 |
40 |
41 |
45 |
49 |
53 |
54 |
55 |
59 |
60 |
61 |
62 |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sixthsolution/lpisyncadapterapp/CustomLoginActivity.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapterapp;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.sixthsolution.lpisyncadapter.authenticator.BaseLoginActivity;
6 |
7 |
8 | /**
9 | * @author mehdok (mehdok@gmail.com) on 3/15/2017.
10 | */
11 |
12 | public class CustomLoginActivity extends BaseLoginActivity {
13 |
14 |
15 | @Override
16 | protected void onCreate(Bundle icicle) {
17 | super.onCreate(icicle);
18 |
19 | setContentView(R.layout.activity_custom_login);
20 | init();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sixthsolution/lpisyncadapterapp/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapterapp;
2 |
3 | import android.Manifest;
4 | import android.accounts.Account;
5 | import android.accounts.AccountManager;
6 | import android.accounts.AccountManagerCallback;
7 | import android.accounts.AccountManagerFuture;
8 | import android.app.AlertDialog;
9 | import android.content.ContentResolver;
10 | import android.content.DialogInterface;
11 | import android.database.ContentObserver;
12 | import android.net.Uri;
13 | import android.os.Bundle;
14 | import android.os.Handler;
15 | import android.support.annotation.NonNull;
16 | import android.support.v7.app.AppCompatActivity;
17 | import android.util.Log;
18 | import android.view.View;
19 | import android.widget.ArrayAdapter;
20 | import android.widget.Toast;
21 |
22 | import com.sixthsolution.lpisyncadapter.GlobalConstant;
23 | import com.sixthsolution.lpisyncadapter.authenticator.crypto.Crypto;
24 | import com.sixthsolution.lpisyncadapter.resource.LocalCalendar;
25 | import com.sixthsolution.lpisyncadapter.resource.LocalEvent;
26 | import com.sixthsolution.lpisyncadapter.resource.LocalResource;
27 |
28 | import net.fortuna.ical4j.model.DateList;
29 | import net.fortuna.ical4j.model.Dur;
30 | import net.fortuna.ical4j.model.TimeZone;
31 | import net.fortuna.ical4j.model.component.VAlarm;
32 | import net.fortuna.ical4j.model.parameter.Value;
33 | import net.fortuna.ical4j.model.property.Attendee;
34 | import net.fortuna.ical4j.model.property.DtEnd;
35 | import net.fortuna.ical4j.model.property.DtStart;
36 | import net.fortuna.ical4j.model.property.ExDate;
37 | import net.fortuna.ical4j.model.property.Organizer;
38 | import net.fortuna.ical4j.model.property.RRule;
39 | import net.fortuna.ical4j.model.property.RecurrenceId;
40 | import net.fortuna.ical4j.model.property.Status;
41 |
42 | import java.io.FileNotFoundException;
43 | import java.net.URI;
44 |
45 | import at.bitfire.ical4android.CalendarStorageException;
46 | import at.bitfire.ical4android.DateUtils;
47 | import at.bitfire.ical4android.Event;
48 | import permissions.dispatcher.NeedsPermission;
49 | import permissions.dispatcher.RuntimePermissions;
50 |
51 | import static com.sixthsolution.lpisyncadapter.GlobalConstant.AUTHTOKEN_TYPE_FULL_ACCESS;
52 | import static com.sixthsolution.lpisyncadapterapp.MainActivity.ActionType.CREATE_EVENT;
53 | import static com.sixthsolution.lpisyncadapterapp.MainActivity.ActionType.GET_TOKEN;
54 | import static com.sixthsolution.lpisyncadapterapp.MainActivity.ActionType.SHOW_CALENDARS;
55 | import static com.sixthsolution.lpisyncadapterapp.MainActivity.ActionType.SHOW_EVENTS;
56 | import static com.sixthsolution.lpisyncadapterapp.MainActivity.ActionType.SYNC;
57 | import static com.sixthsolution.lpisyncadapterapp.MainActivityPermissionsDispatcher.showAccountPickerWithCheck;
58 |
59 | @RuntimePermissions
60 | public class MainActivity extends AppCompatActivity {
61 | private AccountManager accountManager;
62 |
63 | public enum ActionType {
64 | GET_TOKEN,
65 | SYNC,
66 | SHOW_CALENDARS,
67 | SHOW_EVENTS,
68 | CREATE_EVENT
69 | }
70 |
71 | ContentObserver contentObserver = new ContentObserver(new Handler()) {
72 | @Override
73 | public void onChange(boolean selfChange, Uri uri) {
74 | super.onChange(selfChange, uri);
75 |
76 | showMessage("Sync Finished");
77 | }
78 | };
79 |
80 | @Override
81 | protected void onCreate(Bundle savedInstanceState) {
82 | super.onCreate(savedInstanceState);
83 | setContentView(R.layout.activity_main);
84 | accountManager = AccountManager.get(this);
85 |
86 | findViewById(R.id.get_token).setOnClickListener(new View.OnClickListener() {
87 | @Override
88 | public void onClick(View v) {
89 | showAccountPickerWithCheck(MainActivity.this, AUTHTOKEN_TYPE_FULL_ACCESS, GET_TOKEN);
90 | }
91 | });
92 | findViewById(R.id.add_account).setOnClickListener(new View.OnClickListener() {
93 | @Override
94 | public void onClick(View v) {
95 | addNewAccount();
96 | }
97 | });
98 | findViewById(R.id.sync).setOnClickListener(new View.OnClickListener() {
99 | @Override
100 | public void onClick(View v) {
101 | showAccountPickerWithCheck(MainActivity.this, AUTHTOKEN_TYPE_FULL_ACCESS, SYNC);
102 | }
103 | });
104 | findViewById(R.id.show_calendars).setOnClickListener(new View.OnClickListener() {
105 | @Override
106 | public void onClick(View v) {
107 | showAccountPickerWithCheck(MainActivity.this, AUTHTOKEN_TYPE_FULL_ACCESS, SHOW_CALENDARS);
108 | }
109 | });
110 | findViewById(R.id.show_events).setOnClickListener(new View.OnClickListener() {
111 | @Override
112 | public void onClick(View v) {
113 | showAccountPickerWithCheck(MainActivity.this, AUTHTOKEN_TYPE_FULL_ACCESS, SHOW_EVENTS);
114 | }
115 | });
116 | findViewById(R.id.create_event).setOnClickListener(new View.OnClickListener() {
117 | @Override
118 | public void onClick(View v) {
119 | showAccountPickerWithCheck(MainActivity.this, AUTHTOKEN_TYPE_FULL_ACCESS, CREATE_EVENT);
120 | }
121 | });
122 |
123 | syncAutomatically();
124 |
125 | // register a content observer to notify when th sync is finished
126 | getContentResolver().registerContentObserver(GlobalConstant.CONTENT_URI, true, contentObserver);
127 | }
128 |
129 | public void addNewAccount() {
130 | final AccountManagerFuture future =
131 | accountManager.addAccount(AUTHTOKEN_TYPE_FULL_ACCESS, AUTHTOKEN_TYPE_FULL_ACCESS, null, null, this,
132 | new AccountManagerCallback() {
133 | @Override
134 | public void run(AccountManagerFuture future) {
135 | try {
136 | Bundle bnd = future.getResult();
137 | showMessage("AddNewAccount Bundle is " + bnd);
138 | } catch (Exception e) {
139 | e.printStackTrace();
140 | showMessage(e.getMessage());
141 | }
142 | }
143 | }, null);
144 | accountManager.addAccount(AUTHTOKEN_TYPE_FULL_ACCESS, AUTHTOKEN_TYPE_FULL_ACCESS, null, null, this,
145 | new AccountManagerCallback() {
146 | @Override
147 | public void run(AccountManagerFuture future) {
148 | try {
149 | Bundle bnd = future.getResult();
150 | showMessage("AddNewAccount Bundle is " + bnd);
151 | } catch (Exception e) {
152 | e.printStackTrace();
153 | showMessage(e.getMessage());
154 | }
155 | }
156 | }, null);
157 | }
158 |
159 | @NeedsPermission({Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR})
160 | public void showAccountPicker(final String authTokenType, final ActionType actionType) {
161 | // get list of available account
162 | final Account availableAccounts[] = accountManager.getAccountsByType(AUTHTOKEN_TYPE_FULL_ACCESS);
163 |
164 | if (availableAccounts.length == 0) {
165 | showMessage("No accounts");
166 | } else {
167 | String name[] = new String[availableAccounts.length];
168 | for (int i = 0; i < availableAccounts.length; i++) {
169 | name[i] = availableAccounts[i].name;
170 | }
171 |
172 | // Account picker
173 | AlertDialog mAlertDialog = new AlertDialog.Builder(this).setTitle("Pick Account")
174 | .setAdapter(
175 | new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_1, name),
176 | new DialogInterface.OnClickListener() {
177 | @Override
178 | public void onClick(DialogInterface dialog, int which) {
179 | // invalidateAuthToken(availableAccounts[which], authTokenType);
180 |
181 | switch (actionType) {
182 | case GET_TOKEN:
183 | getExistingAccountAuthToken(availableAccounts[which], authTokenType);
184 | break;
185 | case SYNC:
186 | sync(availableAccounts[which]);
187 | break;
188 | case SHOW_CALENDARS:
189 | showCalendars(availableAccounts[which], SHOW_EVENTS);
190 | break;
191 | case SHOW_EVENTS:
192 | showCalendars(availableAccounts[which], SHOW_EVENTS);
193 | break;
194 | case CREATE_EVENT:
195 | showCalendars(availableAccounts[which], CREATE_EVENT);
196 | }
197 | }
198 | })
199 | .create();
200 | mAlertDialog.show();
201 | }
202 | }
203 |
204 | private void invalidateAuthToken(final Account account, String authTokenType) {
205 | final AccountManagerFuture future =
206 | accountManager.getAuthToken(account, authTokenType, null, this, null, null);
207 |
208 | new Thread(new Runnable() {
209 | @Override
210 | public void run() {
211 | try {
212 | Bundle bnd = future.getResult();
213 |
214 | final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
215 | accountManager.invalidateAuthToken(account.type, authtoken);
216 | Log.d("MainActivity", account.name + " invalidated");
217 | } catch (Exception e) {
218 | e.printStackTrace();
219 | showMessage(e.getMessage());
220 | }
221 | }
222 | }).start();
223 | }
224 |
225 | private void getExistingAccountAuthToken(Account account, String authTokenType) {
226 | final AccountManagerFuture future =
227 | accountManager.getAuthToken(account, authTokenType, null, this, null, null);
228 |
229 | new Thread(new Runnable() {
230 | @Override
231 | public void run() {
232 | try {
233 | Bundle bnd = future.getResult();
234 |
235 | String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN, "UTF-8");
236 | authtoken = Crypto.armorDecrypt(authtoken, MainActivity.this);
237 | showMessage(bnd.toString() + "\n encrypted user id: " + authtoken);
238 | } catch (Exception e) {
239 | e.printStackTrace();
240 | showMessage(e.getMessage());
241 | }
242 | }
243 | }).start();
244 | }
245 |
246 | private void showMessage(final String str) {
247 | runOnUiThread(new Runnable() {
248 | @Override
249 | public void run() {
250 | Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
251 | }
252 | });
253 | }
254 |
255 | /**
256 | * The {@link ContentResolver#setSyncAutomatically} and {@link ContentResolver#addPeriodicSync}
257 | * must call programmatically
258 | */
259 | private void syncAutomatically() {
260 | //get all account related to our app
261 | final Account availableAccounts[] = accountManager.getAccountsByType(AUTHTOKEN_TYPE_FULL_ACCESS);
262 |
263 | //set syncable status for all of them
264 | for (Account account : availableAccounts) {
265 | ContentResolver.setIsSyncable(account, GlobalConstant.AUTHORITY, 1);
266 | ContentResolver.setSyncAutomatically(account, GlobalConstant.AUTHORITY, true);
267 | ContentResolver.addPeriodicSync(account, GlobalConstant.AUTHORITY, new Bundle(), 2 * 60 * 60);
268 | }
269 | }
270 |
271 | private void sync(Account account) {
272 | Bundle params = new Bundle();
273 | params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
274 | params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
275 | ContentResolver.requestSync(account, GlobalConstant.AUTHORITY, params);
276 | }
277 |
278 | private void showCalendars(Account account, final ActionType actionType) {
279 | try {
280 | //get list of local calendar associated to the account
281 | final LocalCalendar[] localCalendars =
282 | (LocalCalendar[]) LocalCalendar.find(account,
283 | // get contentProviderClient for our authority
284 | getContentResolver().acquireContentProviderClient(
285 | GlobalConstant.AUTHORITY),
286 | LocalCalendar.Factory.INSTANCE,
287 | null,
288 | null);
289 |
290 | if (localCalendars.length == 0) {
291 | showMessage("No calendar with this account");
292 | } else {
293 | String name[] = new String[localCalendars.length];
294 | for (int i = 0; i < localCalendars.length; i++) {
295 | name[i] = localCalendars[i].getDisplayName();
296 | }
297 |
298 | AlertDialog mAlertDialog = new AlertDialog.Builder(this).setTitle("Pick Calendar")
299 | .setAdapter(
300 | new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_1,
301 | name),
302 | new DialogInterface.OnClickListener() {
303 | @Override
304 | public void onClick(DialogInterface dialog, int which) {
305 | switch (actionType) {
306 | case SHOW_EVENTS:
307 | showEvents(localCalendars[which]);
308 | break;
309 | case CREATE_EVENT:
310 | createTestEvent(localCalendars[which]);
311 | break;
312 | }
313 |
314 | }
315 | })
316 | .create();
317 | mAlertDialog.show();
318 | }
319 | } catch (CalendarStorageException e) {
320 | e.printStackTrace();
321 | showMessage(e.getMessage());
322 | }
323 | }
324 |
325 | private void showEvents(LocalCalendar localCalendar) {
326 | try {
327 | // get list of local calendar associated with selected calendar
328 | LocalResource[] localResources = localCalendar.getAll();
329 | if (localResources.length == 0) {
330 | showMessage("No event in this calendar");
331 | } else {
332 | String name[] = new String[localResources.length];
333 | for (int i = 0; i < localResources.length; i++) {
334 | name[i] = ((LocalEvent) localResources[i]).getEvent().toString();
335 | }
336 |
337 | AlertDialog mAlertDialog = new AlertDialog.Builder(this).setTitle("See events")
338 | .setAdapter(
339 | new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_1,
340 | name),
341 | null)
342 | .create();
343 | mAlertDialog.show();
344 | }
345 | } catch (CalendarStorageException | FileNotFoundException e) {
346 | e.printStackTrace();
347 | showMessage(e.getMessage());
348 | }
349 | }
350 |
351 | private void createTestEvent(LocalCalendar calendar) {
352 | try {
353 | TimeZone tzVienna = DateUtils.tzRegistry.getTimeZone("Europe/Vienna");
354 |
355 | Event event = new Event();
356 | event.uid = "sample1@testAddEvent";
357 | event.summary = "Sample event";
358 | event.description = "Sample event with date/time";
359 | event.location = "Sample location";
360 | event.dtStart = new DtStart("20150501T120000", tzVienna);
361 | event.dtEnd = new DtEnd("20150501T130000", tzVienna);
362 | event.organizer = new Organizer(new URI("mailto:organizer@example.com"));
363 | event.rRule = new RRule("FREQ=DAILY;COUNT=10");
364 | event.forPublic = false;
365 | event.status = Status.VEVENT_CONFIRMED;
366 |
367 | // set an alarm one day, two hours, three minutes and four seconds before begin of event
368 | event.alarms.add(new VAlarm(new Dur(-1, -2, -3, -4)));
369 |
370 | // add two attendees
371 | event.attendees.add(new Attendee(new URI("mailto:user1@example.com")));
372 | event.attendees.add(new Attendee(new URI("mailto:user2@example.com")));
373 |
374 | // add exception with alarm and attendee
375 | Event exception = new Event();
376 | exception.recurrenceId = new RecurrenceId("20150502T120000", tzVienna);
377 | exception.summary = "Exception for sample event";
378 | exception.dtStart = new DtStart("20150502T140000", tzVienna);
379 | exception.dtEnd = new DtEnd("20150502T150000", tzVienna);
380 | exception.alarms.add(new VAlarm(new Dur(-2, -3, -4, -5)));
381 | exception.attendees.add(new Attendee(new URI("mailto:only.here@today")));
382 | event.exceptions.add(exception);
383 |
384 | // add EXDATE
385 | event.exDates.add(new ExDate(new DateList("20150502T120000", Value.DATE_TIME, tzVienna)));
386 | // add to calendar
387 | Uri uri = new LocalEvent(calendar, event, null, null).add();
388 |
389 | showMessage("Event created: " + uri);
390 | showEvents(calendar);
391 | } catch (Exception e) {
392 | e.printStackTrace();
393 | showMessage(e.toString());
394 | }
395 | }
396 |
397 | @Override
398 | protected void onDestroy() {
399 | super.onDestroy();
400 | getContentResolver().unregisterContentObserver(contentObserver);
401 | }
402 |
403 | @Override
404 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
405 | @NonNull int[] grantResults) {
406 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
407 | MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
408 | }
409 | }
410 |
411 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_custom_login.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
23 |
24 |
31 |
32 |
38 |
39 |
48 |
49 |
54 |
55 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
23 |
24 |
30 |
31 |
37 |
38 |
44 |
45 |
51 |
52 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LpiSyncAdapterApp
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/sixthsolution/lpisyncadapterapp/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapterapp;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.assertEquals;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.3'
9 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
10 | classpath 'com.novoda:bintray-release:0.5.0'
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | jcenter()
19 | maven { url "http://dl.bintray.com/6thsolution/public-maven" }
20 | }
21 | }
22 |
23 | task clean(type: Delete) {
24 | delete rootProject.buildDir
25 | }
26 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.novoda.bintray-release'
3 |
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion "25.0.2"
7 |
8 | defaultConfig {
9 | minSdkVersion 16
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | buildConfigField "long", "buildTime", System.currentTimeMillis() + "L"
17 | buildConfigField "boolean", "customCerts", "true"
18 | }
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | lintOptions {
27 | checkReleaseBuilds false
28 | abortOnError false
29 | }
30 | }
31 |
32 | dependencies {
33 | compile fileTree(dir: 'libs', include: ['*.jar'])
34 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
35 | exclude group: 'com.android.support', module: 'support-annotations'
36 | })
37 | compile 'com.android.support:appcompat-v7:25.3.1'
38 | compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
39 | provided 'org.projectlombok:lombok:1.16.12'
40 | compile 'dnsjava:dnsjava:2.1.7'
41 | compile 'com.jakewharton.timber:timber:4.1.2'
42 | compile 'commons-io:commons-io:2.5'
43 | compile 'org.apache.commons:commons-lang3:3.4'
44 | compile 'org.apache.commons:commons-collections4:4.1'
45 | compile 'com.google.code.gson:gson:2.7'
46 | compile 'at.bitfire.cert4android:cert4android:1.0.0-beta1'
47 | compile 'at.bitfire.dav4android:dav4android:1.0.0-beta1'
48 | compile 'at.bitfire.ical4android:ical4android:1.0.0-beta1'
49 | testCompile 'junit:junit:4.12'
50 | }
51 |
52 | publish {
53 | userOrg = '6thsolution'
54 | groupId = 'com.sixthsolution'
55 | uploadName = 'easyapplesyncadapter'
56 | repoName = 'public-maven'
57 | artifactId = 'easyapplesyncadapter'
58 | publishVersion = '1.0.0-beta1'
59 | desc = 'Easy Apple Sync Adapter is an Android Library for syncing with apple calendar service.'
60 | website = 'https://github.com/6thsolution/EasyAppleSyncAdapter'
61 | repository = 'https://github.com/6thsolution/EasyAppleSyncAdapter.git'
62 | licences = ['Apache-2.0']
63 | }
64 |
65 | tasks.withType(Javadoc).all { enabled = false }
66 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/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 H:\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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/androidTest/java/com/sixthsolution/lpisyncadapter/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter;
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.assertEquals;
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.sixthsolution.lipsyncadapter.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 |
23 |
24 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/App.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter;
2 |
3 | import android.app.Application;
4 |
5 | import com.sixthsolution.lpisyncadapter.util.SSLSocketFactoryCompat;
6 |
7 | import net.fortuna.ical4j.util.UidGenerator;
8 |
9 | import java.util.logging.Logger;
10 |
11 | import javax.net.ssl.HostnameVerifier;
12 |
13 | import at.bitfire.cert4android.CustomCertManager;
14 | import okhttp3.internal.tls.OkHostnameVerifier;
15 | import timber.log.Timber;
16 |
17 | /**
18 | * @author mehdok (mehdok@gmail.com) on 3/29/2017.
19 | */
20 |
21 | public class App extends Application {
22 | public static SSLSocketFactoryCompat sslSocketFactoryCompat;
23 | public static HostnameVerifier hostnameVerifier;
24 | public CustomCertManager certManager;
25 | public static UidGenerator uidGenerator;
26 |
27 | // comment this if you don't need any logs
28 | static {
29 | at.bitfire.dav4android.Constants.log = Logger.getLogger("davdroid.dav4android");
30 | at.bitfire.cert4android.Constants.log = Logger.getLogger("davdroid.cert4android");
31 | }
32 |
33 | @Override
34 | public void onCreate() {
35 | super.onCreate();
36 | reinitCertManager();
37 | uidGenerator = new UidGenerator(null, android.provider.Settings.Secure.getString(getContentResolver(),
38 | android.provider.Settings.Secure.ANDROID_ID));
39 |
40 | Timber.plant(new Timber.DebugTree());
41 | }
42 |
43 | public void reinitCertManager() {
44 | if (BuildConfig.customCerts) {
45 | if (certManager != null) {
46 | certManager.close();
47 | }
48 |
49 | certManager = new CustomCertManager(this, true);
50 | sslSocketFactoryCompat = new SSLSocketFactoryCompat(certManager);
51 | hostnameVerifier = certManager.hostnameVerifier(OkHostnameVerifier.INSTANCE);
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/GlobalConstant.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter;
2 |
3 | import android.net.Uri;
4 |
5 | /**
6 | * @author mehdok (mehdok@gmail.com) on 3/18/2017.
7 | */
8 |
9 | public class GlobalConstant {
10 | public static final Uri BASE_URL = Uri.parse("https://p01-caldav.icloud.com");
11 |
12 | public final static String ACCOUNT_TYPE = "ACCOUNT_TYPE";
13 | public final static String AUTH_TYPE = "AUTH_TYPE";
14 | public final static String IS_ADDING_NEW_ACCOUNT = "IS_ADDING_ACCOUNT";
15 | public final static String ACCOUNT_NAME = "ACCOUNT_NAME";
16 | public final static String PARAM_USER_PASS = "USER_PASS";
17 | public final static String PARAM_PRINCIPAL = "USER_PRINCIPAL";
18 |
19 | // authentication unique key, this must be different for every app
20 | public static String AUTHTOKEN_TYPE_FULL_ACCESS = "com.sixthsolution.lpisyncadapter.ical_access";
21 |
22 | // content provider unique authority, this must be unique for every app
23 | public static String AUTHORITY = "com.android.calendar";
24 | public static Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/ical");
25 | }
26 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/AuthServerHandler.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator;
2 |
3 | import java.security.InvalidAlgorithmParameterException;
4 | import java.security.NoSuchAlgorithmException;
5 |
6 | import javax.crypto.BadPaddingException;
7 | import javax.crypto.IllegalBlockSizeException;
8 | import javax.crypto.NoSuchPaddingException;
9 |
10 | /**
11 | * @author mehdok (mehdok@gmail.com) on 3/9/2017.
12 | */
13 |
14 | public interface AuthServerHandler {
15 |
16 | /**
17 | * Getting the username and password and return the iCal user id
18 | *
19 | * @param user iCal username
20 | * @param pass iCal password
21 | * @return The user id of iCal
22 | * @throws Exception, throws various Exceptions such as {@link NoSuchAlgorithmException},
23 | * {@link NoSuchPaddingException}, {@link IllegalBlockSizeException},
24 | * {@link BadPaddingException}, {@link InvalidAlgorithmParameterException}
25 | * or general Exception
26 | */
27 | String userSignIn(final String user, final String pass) throws Exception;
28 | }
29 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/AuthServerHandlerImpl.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator;
2 |
3 | import java.io.IOException;
4 | import java.net.URI;
5 | import java.util.concurrent.TimeUnit;
6 | import java.util.logging.Level;
7 | import java.util.logging.Logger;
8 |
9 | import at.bitfire.dav4android.BasicDigestAuthHandler;
10 | import at.bitfire.dav4android.DavResource;
11 | import at.bitfire.dav4android.UrlUtils;
12 | import at.bitfire.dav4android.exception.DavException;
13 | import at.bitfire.dav4android.exception.HttpException;
14 | import at.bitfire.dav4android.property.CurrentUserPrincipal;
15 | import okhttp3.HttpUrl;
16 | import okhttp3.OkHttpClient;
17 |
18 | import static com.sixthsolution.lpisyncadapter.GlobalConstant.BASE_URL;
19 |
20 | /**
21 | * This class is responsible for handling network communication and parsing iCal user id
22 | * at the end It will use {@link BasicDigestAuthHandler} and {@link DavResource} to find and parse iCal user id
23 | *
24 | * @author mehdok (mehdok@gmail.com) on 3/9/2017.
25 | */
26 |
27 | public class AuthServerHandlerImpl implements AuthServerHandler {
28 |
29 | @Override
30 | public String userSignIn(String user, String pass) throws Exception {
31 | try {
32 | URI uri = new URI(BASE_URL.getScheme(),
33 | null,
34 | BASE_URL.getHost(),
35 | BASE_URL.getPort(),
36 | BASE_URL.getEncodedPath(),
37 | null,
38 | null);
39 | return getICalID(uri, user, pass);
40 | } catch (Exception e) {
41 | e.printStackTrace();
42 | }
43 |
44 | return null;
45 | }
46 |
47 | /**
48 | * @param uri the base {@link URI} of ical caldav server
49 | * @param user username
50 | * @param pass password
51 | * @return the ical User id
52 | * @throws DavException
53 | * @throws IOException
54 | * @throws HttpException
55 | */
56 | private String getICalID(URI uri, String user, String pass) throws DavException, IOException, HttpException {
57 | OkHttpClient client = new OkHttpClient();
58 | OkHttpClient.Builder builder = client.newBuilder();
59 | builder.connectTimeout(30, TimeUnit.SECONDS);
60 | builder.writeTimeout(30, TimeUnit.SECONDS);
61 | builder.readTimeout(120, TimeUnit.SECONDS);
62 |
63 | // don't allow redirects, because it would break PROPFIND handling
64 | builder.followRedirects(false);
65 |
66 | BasicDigestAuthHandler
67 | authHandler = new BasicDigestAuthHandler(UrlUtils.hostToDomain(null), user, pass);
68 | builder.addNetworkInterceptor(authHandler).authenticator(authHandler);
69 | final HttpUrl baseURL = HttpUrl.get(uri);
70 | Logger log = Logger.getLogger("ICalServerHandler.DavResourceFinder");
71 | log.setLevel(Level.OFF);
72 |
73 | client = builder.build();
74 | DavResource davBase = new DavResource(client, baseURL, log);
75 | davBase.propfind(0, CurrentUserPrincipal.NAME);
76 |
77 | CurrentUserPrincipal currentUserPrincipal =
78 | (CurrentUserPrincipal) davBase.properties.get(CurrentUserPrincipal.NAME);
79 | if (currentUserPrincipal != null && currentUserPrincipal.href != null) {
80 | /*String[] nodeText = currentUserPrincipal.href.split("/");
81 | if (nodeText.length > 1) {
82 | return nodeText[1];
83 | }*/
84 | return currentUserPrincipal.href;
85 | }
86 |
87 | return null;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/BaseLoginActivity.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator;
2 |
3 | import android.accounts.Account;
4 | import android.accounts.AccountAuthenticatorActivity;
5 | import android.accounts.AccountManager;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.SharedPreferences;
9 | import android.os.AsyncTask;
10 | import android.os.Bundle;
11 | import android.view.View;
12 | import android.widget.Button;
13 | import android.widget.EditText;
14 | import android.widget.Toast;
15 |
16 | import com.sixthsolution.lpisyncadapter.GlobalConstant;
17 | import com.sixthsolution.lpisyncadapter.R;
18 | import com.sixthsolution.lpisyncadapter.authenticator.crypto.Crypto;
19 | import com.sixthsolution.lpisyncadapter.authenticator.crypto.KeyManager;
20 |
21 | import java.io.UnsupportedEncodingException;
22 | import java.security.InvalidAlgorithmParameterException;
23 | import java.security.InvalidKeyException;
24 | import java.security.NoSuchAlgorithmException;
25 | import java.util.UUID;
26 |
27 | import javax.crypto.BadPaddingException;
28 | import javax.crypto.IllegalBlockSizeException;
29 | import javax.crypto.NoSuchPaddingException;
30 |
31 | import static com.sixthsolution.lpisyncadapter.GlobalConstant.PARAM_PRINCIPAL;
32 |
33 |
34 | /**
35 | * Every custom login activity must extend this class, and in their layout they must have 2 {@link EditText} with
36 | * Exact id 'user_name' and 'password' and a {@link Button} with id of 'signin_button'.
37 | * After calling {@link #setContentView(int)} in {@link #onCreate(Bundle)} you must call {@link #init()}
38 | *
39 | * @author mehdok (mehdok@gmail.com) on 3/15/2017.
40 | */
41 |
42 | public class BaseLoginActivity extends AccountAuthenticatorActivity {
43 | private final static String SIGNIN_ERROR = "signin_error";
44 |
45 | private EditText userName;
46 | private EditText password;
47 | private Button signIn;
48 | private View progressLayout;
49 |
50 | private AccountManager accountManager;
51 | private AuthServerHandler serverHandler;
52 | private String authTokenType;
53 | private boolean isNewAccount = true;
54 |
55 | @Override
56 | protected void onCreate(Bundle icicle) {
57 | super.onCreate(icicle);
58 | }
59 |
60 | protected void init() {
61 | initCrypto();
62 |
63 | accountManager = AccountManager.get(getBaseContext());
64 | serverHandler = new AuthServerHandlerImpl();
65 |
66 | userName = (EditText) findViewById(R.id.user_name);
67 | password = (EditText) findViewById(R.id.password);
68 | signIn = (Button) findViewById(R.id.signin_button);
69 | progressLayout = findViewById(R.id.progress_layout);
70 |
71 | Intent intent = getIntent();
72 | authTokenType = getIntent().getStringExtra(GlobalConstant.AUTH_TYPE);
73 | if (authTokenType == null) {
74 | authTokenType = GlobalConstant.AUTHTOKEN_TYPE_FULL_ACCESS;
75 | }
76 |
77 | isNewAccount = intent.getBooleanExtra(GlobalConstant.IS_ADDING_NEW_ACCOUNT, true);
78 | if (!isNewAccount) {
79 | // existing account
80 | String accountName = getIntent().getStringExtra(GlobalConstant.ACCOUNT_NAME);
81 | userName.setText(accountName);
82 | }
83 |
84 | signIn.setOnClickListener(new View.OnClickListener() {
85 | @Override
86 | public void onClick(View v) {
87 | login();
88 | }
89 | });
90 | }
91 |
92 | /**
93 | * In first run we must create a random keys and save it for further use
94 | */
95 | private void initCrypto() {
96 | String pref_name = "ical_authenticator_pref";
97 | String first_run = "pref_first_run";
98 |
99 | SharedPreferences sharedPreferences = getSharedPreferences(pref_name, Context.MODE_PRIVATE);
100 |
101 | if (sharedPreferences.getBoolean(first_run, true)) {
102 | // set private key for any encryption, this will run once
103 | KeyManager keyManager = new KeyManager();
104 | keyManager.setId(UUID.randomUUID().toString().substring(0, 32).getBytes(), this);
105 | keyManager.setIv(UUID.randomUUID().toString().substring(0, 16).getBytes(), this);
106 |
107 | sharedPreferences.edit().putBoolean(first_run, false).apply();
108 | }
109 | }
110 |
111 | private void login() {
112 | final String usr = userName.getText().toString();
113 | final String pass = password.getText().toString();
114 | new AsyncTask() {
115 |
116 | @Override
117 | protected void onPreExecute() {
118 | super.onPreExecute();
119 | showProgress(true);
120 | }
121 |
122 | @Override
123 | protected Intent doInBackground(String... params) {
124 | String iCalUserId = null;
125 | Bundle data = new Bundle();
126 | try {
127 | iCalUserId = serverHandler.userSignIn(usr, pass);
128 |
129 | data.putString(AccountManager.KEY_ACCOUNT_NAME, usr);
130 | data.putString(AccountManager.KEY_AUTHTOKEN, iCalUserId);
131 | data.putString(AccountManager.KEY_ACCOUNT_TYPE, authTokenType);
132 | data.putString(GlobalConstant.PARAM_USER_PASS, pass);
133 |
134 | } catch (Exception e) {
135 | e.printStackTrace();
136 | data.putSerializable(SIGNIN_ERROR, e);
137 | }
138 |
139 | final Intent res = new Intent();
140 | res.putExtras(data);
141 | return res;
142 | }
143 |
144 | @Override
145 | protected void onPostExecute(Intent intent) {
146 | try {
147 | passDataToAuthenticator(intent);
148 | } catch (Exception e) {
149 | e.printStackTrace();
150 | Toast.makeText(BaseLoginActivity.this, "Can not connect to server or wrong info",
151 | Toast.LENGTH_SHORT).show();
152 | }
153 | showProgress(false);
154 | }
155 | }.execute();
156 | }
157 |
158 |
159 | /**
160 | * Pass the data from server to authenticator class
161 | *
162 | * @param intent the intent contain data from user and server {@link AccountManager#KEY_ACCOUNT_NAME},
163 | * {@link AccountManager#KEY_AUTHTOKEN}, {@link AccountManager#KEY_ACCOUNT_TYPE} and
164 | * {@link GlobalConstant#PARAM_USER_PASS}
165 | * @throws NoSuchPaddingException
166 | * @throws InvalidAlgorithmParameterException
167 | * @throws NoSuchAlgorithmException
168 | * @throws IllegalBlockSizeException
169 | * @throws BadPaddingException
170 | * @throws InvalidKeyException
171 | * @throws UnsupportedEncodingException
172 | * @throws SignInException
173 | */
174 | private void passDataToAuthenticator(Intent intent)
175 | throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
176 | IllegalBlockSizeException, BadPaddingException, InvalidKeyException, UnsupportedEncodingException,
177 | SignInException {
178 |
179 | if (intent.hasExtra(SIGNIN_ERROR)) throw new SignInException();
180 |
181 | String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
182 | String accountPassword = intent.getStringExtra(GlobalConstant.PARAM_USER_PASS);
183 | accountPassword = Crypto.armorEncrypt(accountPassword.getBytes("UTF-8"), this);
184 |
185 | final Account account = new Account(accountName, authTokenType);
186 |
187 | if (isNewAccount) {
188 | String iCalId = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
189 | String authtoken = intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
190 |
191 | // encrypt user id and pass it to intent
192 | iCalId = Crypto.armorEncrypt(iCalId.getBytes("UTF-8"), this);
193 | intent.putExtra(AccountManager.KEY_AUTHTOKEN, iCalId);
194 |
195 | final Bundle extraData = new Bundle();
196 | extraData.putString(PARAM_PRINCIPAL, iCalId);
197 |
198 | accountManager.addAccountExplicitly(account, accountPassword, extraData);
199 | accountManager.setAuthToken(account, authtoken, iCalId);
200 | } else {
201 | accountManager.setPassword(account, accountPassword);
202 | }
203 |
204 | // encrypt password and pass it to intent
205 | intent.putExtra(GlobalConstant.PARAM_USER_PASS, accountPassword);
206 |
207 |
208 | setAccountAuthenticatorResult(intent.getExtras());
209 | setResult(RESULT_OK, intent);
210 | finish();
211 | }
212 |
213 | private void showProgress(boolean show) {
214 | if (show) {
215 | progressLayout.setVisibility(View.VISIBLE);
216 | } else {
217 | progressLayout.setVisibility(View.GONE);
218 | }
219 | }
220 | }
221 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/ICalAuthenticator.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator;
2 |
3 | import android.accounts.AbstractAccountAuthenticator;
4 | import android.accounts.Account;
5 | import android.accounts.AccountAuthenticatorResponse;
6 | import android.accounts.AccountManager;
7 | import android.accounts.NetworkErrorException;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.os.Bundle;
11 | import android.text.TextUtils;
12 |
13 | import com.sixthsolution.lpisyncadapter.GlobalConstant;
14 | import com.sixthsolution.lpisyncadapter.authenticator.crypto.Crypto;
15 |
16 | import static android.accounts.AccountManager.KEY_BOOLEAN_RESULT;
17 | import static com.sixthsolution.lpisyncadapter.GlobalConstant.IS_ADDING_NEW_ACCOUNT;
18 |
19 | /**
20 | * This class is responsible for getting iCal user id if it is exist or show login screen if there is not one,
21 | * By default this class suppose the Saved (in {@link AccountManager}) password and user id is encrypted via
22 | * {@link Crypto#armorEncrypt(byte[], Context)}, so before saving password and user id just encrypt it via
23 | * {@link Crypto#armorEncrypt(byte[], Context)}
24 | *
25 | * @author mehdok (mehdok@gmail.com) on 3/9/2017.
26 | */
27 |
28 | public class ICalAuthenticator extends AbstractAccountAuthenticator {
29 | private final Context context;
30 | private AuthServerHandler serverHandler;
31 | private Class loginActivity;
32 |
33 | /**
34 | * @param context
35 | * @param loginActivity the custom login activity to run
36 | * @param authType the custom authentication type that you set in your xml file
37 | */
38 | public ICalAuthenticator(Context context, Class loginActivity, String authType) {
39 | this(context, loginActivity);
40 | GlobalConstant.AUTHTOKEN_TYPE_FULL_ACCESS = authType;
41 | }
42 |
43 | /**
44 | * @param context
45 | * @param loginActivity the custom login activity to run
46 | */
47 | public ICalAuthenticator(Context context, Class loginActivity) {
48 | this(context);
49 | this.loginActivity = loginActivity;
50 | }
51 |
52 | public ICalAuthenticator(Context context) {
53 | super(context);
54 | this.context = context;
55 | serverHandler = new AuthServerHandlerImpl();
56 | }
57 |
58 | @Override
59 | public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType,
60 | String[] requiredFeatures, Bundle options) throws NetworkErrorException {
61 | final Intent intent = new Intent(context, loginActivity);
62 | intent.putExtra(GlobalConstant.ACCOUNT_TYPE, accountType);
63 | intent.putExtra(GlobalConstant.AUTH_TYPE, authTokenType);
64 | intent.putExtra(IS_ADDING_NEW_ACCOUNT, true);
65 | intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
66 | final Bundle bundle = new Bundle();
67 | bundle.putParcelable(AccountManager.KEY_INTENT, intent);
68 | return bundle;
69 | }
70 |
71 | @Override
72 | public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
73 | Bundle options) throws NetworkErrorException {
74 | final AccountManager am = AccountManager.get(context);
75 | String authToken = am.peekAuthToken(account, authTokenType);
76 |
77 | // get new token if there is no one
78 | if (TextUtils.isEmpty(authToken)) {
79 | String password = am.getPassword(account);
80 | if (password != null) {
81 | try {
82 | password = Crypto.armorDecrypt(password, context);
83 | authToken = serverHandler.userSignIn(account.name, password);
84 | } catch (Exception e) {
85 | e.printStackTrace();
86 | }
87 | }
88 | }
89 |
90 | // there is new token, return it
91 | if (!TextUtils.isEmpty(authToken)) {
92 | final Bundle result = new Bundle();
93 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
94 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
95 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
96 | return result;
97 | }
98 |
99 | // no token and no password, show login screen
100 | final Intent intent = new Intent(context, loginActivity);
101 | intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
102 | intent.putExtra(GlobalConstant.ACCOUNT_TYPE, account.type);
103 | intent.putExtra(GlobalConstant.ACCOUNT_NAME, account.name);
104 | intent.putExtra(IS_ADDING_NEW_ACCOUNT, false);
105 | final Bundle bundle = new Bundle();
106 | bundle.putParcelable(AccountManager.KEY_INTENT, intent);
107 | return bundle;
108 | }
109 |
110 | @Override
111 | public String getAuthTokenLabel(String authTokenType) {
112 | // we have one account type
113 | return "iCal access";
114 | }
115 |
116 | @Override
117 | public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)
118 | throws NetworkErrorException {
119 | final Bundle result = new Bundle();
120 | result.putBoolean(KEY_BOOLEAN_RESULT, false);
121 | return result;
122 | }
123 |
124 | @Override
125 | public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
126 | // no op
127 | return null;
128 | }
129 |
130 | @Override
131 | public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType,
132 | Bundle options) throws NetworkErrorException {
133 | // no op
134 | return null;
135 | }
136 |
137 | @Override
138 | public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)
139 | throws NetworkErrorException {
140 | // no op
141 | return null;
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/ICalAuthenticatorService.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator;
2 |
3 | import android.app.Service;
4 | import android.content.ComponentName;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.os.Bundle;
8 | import android.os.IBinder;
9 | import android.support.annotation.Nullable;
10 | import android.util.Log;
11 |
12 | import static com.sixthsolution.lpisyncadapter.GlobalConstant.AUTHTOKEN_TYPE_FULL_ACCESS;
13 |
14 | /**
15 | * This service bind to {@link ICalAuthenticator}
16 | *
17 | * @author mehdok (mehdok@gmail.com) on 3/9/2017.
18 | */
19 |
20 | public class ICalAuthenticatorService extends Service {
21 |
22 | @Nullable
23 | @Override
24 | public IBinder onBind(Intent intent) {
25 | Class clazz = getLoginClass();
26 | String authType = getAuthenticationType();
27 |
28 | ICalAuthenticator authenticator =
29 | new ICalAuthenticator(this,
30 | clazz != null ? clazz : LoginActivity.class,
31 | authType != null ? authType : AUTHTOKEN_TYPE_FULL_ACCESS);
32 | return authenticator.getIBinder();
33 | }
34 |
35 | /**
36 | * @return custom login class that passed via meta-data
37 | */
38 | private Class getLoginClass() {
39 | ComponentName service = new ComponentName(this, this.getClass());
40 | try {
41 | Bundle data = getPackageManager().getServiceInfo(service, PackageManager.GET_META_DATA).metaData;
42 | String clazzName = data.getString("login_activity_class");
43 |
44 | if (clazzName != null) {
45 | Log.e("getLoginClass", "clazzName: " + clazzName);
46 | return (Class) Class.forName(clazzName);
47 | } else {
48 | Log.e("getLoginClass", "clazzName is null");
49 | return null;
50 | }
51 | } catch (PackageManager.NameNotFoundException | ClassNotFoundException e) {
52 | e.printStackTrace();
53 | return null;
54 | }
55 | }
56 |
57 | /**
58 | * @return unique authentication type that passed via meta-data
59 | */
60 | private String getAuthenticationType() {
61 | ComponentName service = new ComponentName(this, this.getClass());
62 | try {
63 | Bundle data = getPackageManager().getServiceInfo(service, PackageManager.GET_META_DATA).metaData;
64 | return data.getString("unique_authentication_type");
65 |
66 | } catch (PackageManager.NameNotFoundException e) {
67 | e.printStackTrace();
68 | return null;
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/LoginActivity.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.sixthsolution.lpisyncadapter.R;
6 |
7 |
8 | public class LoginActivity extends BaseLoginActivity {
9 | @Override
10 | protected void onCreate(Bundle savedInstanceState) {
11 | super.onCreate(savedInstanceState);
12 | setContentView(R.layout.activity_login);
13 | init();
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/SignInException.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator;
2 |
3 | /**
4 | * @author mehdok (mehdok@gmail.com) on 3/15/2017.
5 | */
6 |
7 | public class SignInException extends Exception {
8 | }
9 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/crypto/Crypto.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator.crypto;
2 |
3 | import android.content.Context;
4 | import android.util.Base64;
5 |
6 | import java.io.UnsupportedEncodingException;
7 | import java.math.BigInteger;
8 | import java.security.InvalidAlgorithmParameterException;
9 | import java.security.InvalidKeyException;
10 | import java.security.MessageDigest;
11 | import java.security.NoSuchAlgorithmException;
12 |
13 | import javax.crypto.BadPaddingException;
14 | import javax.crypto.Cipher;
15 | import javax.crypto.IllegalBlockSizeException;
16 | import javax.crypto.NoSuchPaddingException;
17 | import javax.crypto.spec.IvParameterSpec;
18 | import javax.crypto.spec.SecretKeySpec;
19 |
20 | /**
21 | * Created by mehdok on 4/10/2016.
22 | */
23 | public class Crypto {
24 | private static final String engine = "AES";
25 | private static final String crypto = "AES/CBC/PKCS5Padding";
26 |
27 | private static byte[] cipher(byte[] data, int mode, Context ctx)
28 | throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
29 | IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
30 | KeyManager km = new KeyManager();
31 | SecretKeySpec sks = new SecretKeySpec(km.getId(ctx), engine);
32 | IvParameterSpec iv = new IvParameterSpec(km.getIv(ctx));
33 | Cipher c = Cipher.getInstance(crypto);
34 | c.init(mode, sks, iv);
35 | return c.doFinal(data);
36 | }
37 |
38 | private static byte[] encrypt(byte[] data, Context ctx) throws InvalidKeyException,
39 | NoSuchAlgorithmException, NoSuchPaddingException,
40 | IllegalBlockSizeException, BadPaddingException,
41 | InvalidAlgorithmParameterException {
42 | return cipher(data, Cipher.ENCRYPT_MODE, ctx);
43 | }
44 |
45 | private static byte[] decrypt(byte[] data, Context ctx) throws InvalidKeyException,
46 | NoSuchAlgorithmException, NoSuchPaddingException,
47 | IllegalBlockSizeException, BadPaddingException,
48 | InvalidAlgorithmParameterException {
49 | return cipher(data, Cipher.DECRYPT_MODE, ctx);
50 | }
51 |
52 | private static String getMD5BASE64(String str) {
53 | try {
54 | String base64 = Base64.encodeToString(str.getBytes("UTF-8"), Base64.NO_WRAP);
55 | return (getMD5EncryptedString(base64));
56 | } catch (UnsupportedEncodingException e) {
57 | return "";
58 | }
59 | }
60 |
61 | public static String armorEncrypt(byte[] data, Context ctx)
62 | throws InvalidKeyException, NoSuchAlgorithmException,
63 | NoSuchPaddingException, IllegalBlockSizeException,
64 | BadPaddingException, InvalidAlgorithmParameterException {
65 | return Base64.encodeToString(encrypt(data, ctx), Base64.DEFAULT);
66 | }
67 |
68 | public static String armorDecrypt(String data, Context ctx)
69 | throws InvalidKeyException, NoSuchAlgorithmException,
70 | NoSuchPaddingException, IllegalBlockSizeException,
71 | BadPaddingException, InvalidAlgorithmParameterException {
72 | return new String(decrypt(Base64.decode(data, Base64.DEFAULT), ctx));
73 | }
74 |
75 | public static String getMD5EncryptedString(String encTarget) {
76 | MessageDigest mdEnc = null;
77 | try {
78 | mdEnc = MessageDigest.getInstance("MD5");
79 | } catch (NoSuchAlgorithmException e) {
80 | System.out.println("Exception while encrypting to md5");
81 | e.printStackTrace();
82 | }
83 | // Encryption algorithm
84 | mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
85 | String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
86 | while (md5.length() < 32) {
87 | md5 = "0" + md5;
88 | }
89 | return md5;
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/authenticator/crypto/KeyManager.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.authenticator.crypto;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 |
6 | import java.io.ByteArrayOutputStream;
7 | import java.io.FileInputStream;
8 | import java.io.FileNotFoundException;
9 | import java.io.FileOutputStream;
10 | import java.io.IOException;
11 |
12 | /**
13 | * Created by mehdok on 4/10/2016.
14 | */
15 | public class KeyManager {
16 | private static final String file1 = "id_value";
17 | private static final String file2 = "iv_value";
18 |
19 | public KeyManager() {
20 | }
21 |
22 | public void setId(byte[] data, Context ctx) {
23 | writer(data, file1, ctx);
24 | }
25 |
26 | public void setIv(byte[] data, Context ctx) {
27 | writer(data, file2, ctx);
28 | }
29 |
30 | public byte[] getId(Context ctx) {
31 | return reader(file1, ctx);
32 | }
33 |
34 | public byte[] getIv(Context ctx) {
35 | return reader(file2, ctx);
36 | }
37 |
38 | private byte[] reader(String file, Context ctx) {
39 | byte[] data = null;
40 | try {
41 | int bytesRead = 0;
42 | FileInputStream fis = ctx.openFileInput(file);
43 | ByteArrayOutputStream bos = new ByteArrayOutputStream();
44 | byte[] b = new byte[1024];
45 | while ((bytesRead = fis.read(b)) != -1) {
46 | bos.write(b, 0, bytesRead);
47 | }
48 | data = bos.toByteArray();
49 | } catch (FileNotFoundException e) {
50 | Log.e("KeyManager", "File not found in getId()");
51 | } catch (IOException e) {
52 | Log.e("KeyManager", "IOException in setId(): " + e.getMessage());
53 | }
54 | return data;
55 | }
56 |
57 | private void writer(byte[] data, String file, Context ctx) {
58 | try {
59 | FileOutputStream fos = ctx.openFileOutput(file,
60 | Context.MODE_PRIVATE);
61 | fos.write(data);
62 | fos.flush();
63 | fos.close();
64 | } catch (FileNotFoundException e) {
65 | Log.e("KeyManager", "File not found in setId()");
66 | } catch (IOException e) {
67 | Log.e("KeyManager", "IOException in setId(): " + e.getMessage());
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/download/EventDownloader.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.download;
2 |
3 |
4 | import com.sixthsolution.lpisyncadapter.resource.LocalCalendar;
5 | import com.sixthsolution.lpisyncadapter.resource.LocalEvent;
6 | import com.sixthsolution.lpisyncadapter.resource.LocalResource;
7 | import com.sixthsolution.lpisyncadapter.util.ArrayUtils;
8 |
9 | import org.apache.commons.io.Charsets;
10 | import org.apache.commons.lang3.StringUtils;
11 |
12 | import java.io.ByteArrayInputStream;
13 | import java.io.IOException;
14 | import java.io.InputStream;
15 | import java.nio.charset.Charset;
16 | import java.util.LinkedList;
17 | import java.util.List;
18 | import java.util.Map;
19 | import java.util.Set;
20 |
21 | import at.bitfire.dav4android.DavCalendar;
22 | import at.bitfire.dav4android.DavResource;
23 | import at.bitfire.dav4android.exception.DavException;
24 | import at.bitfire.dav4android.exception.HttpException;
25 | import at.bitfire.dav4android.property.CalendarData;
26 | import at.bitfire.dav4android.property.GetContentType;
27 | import at.bitfire.dav4android.property.GetETag;
28 | import at.bitfire.ical4android.CalendarStorageException;
29 | import at.bitfire.ical4android.Event;
30 | import at.bitfire.ical4android.InvalidCalendarException;
31 | import lombok.Cleanup;
32 | import okhttp3.HttpUrl;
33 | import okhttp3.MediaType;
34 | import okhttp3.ResponseBody;
35 | import timber.log.Timber;
36 |
37 | /**
38 | * @author mehdok (mehdok@gmail.com) on 4/8/2017.
39 | */
40 |
41 | public class EventDownloader {
42 | private static final int MAX_MULTIGET = 20;
43 |
44 | /**
45 | * Download server event and update local
46 | *
47 | * @param toDownload
48 | * @param calendar
49 | * @param localCalendar
50 | * @param localResources
51 | * @throws DavException
52 | * @throws IOException
53 | * @throws HttpException
54 | * @throws CalendarStorageException
55 | */
56 | public static void downloadEvents(Set toDownload, DavCalendar calendar,
57 | LocalCalendar localCalendar, Map localResources)
58 | throws DavException, IOException, HttpException, CalendarStorageException {
59 | for (DavResource[] bunch : ArrayUtils.partition(toDownload.toArray(new DavResource[toDownload.size()]),
60 | MAX_MULTIGET)) {
61 | // instant cancel
62 | if (Thread.interrupted()) {
63 | return;
64 | }
65 |
66 | if (bunch.length == 1) {
67 | // only one contact, use GET
68 | final DavResource remote = bunch[0];
69 |
70 | ResponseBody body = remote.get("text/calendar");
71 |
72 | // CalDAV servers MUST return ETag on GET [https://tools.ietf.org/html/rfc4791#section-5.3.4]
73 | GetETag eTag = (GetETag) remote.properties.get(GetETag.NAME);
74 | if (eTag == null || StringUtils.isEmpty(eTag.eTag)) {
75 | throw new DavException("Received CalDAV GET response without ETag for " + remote.location);
76 | }
77 |
78 | Charset charset = Charsets.UTF_8;
79 | MediaType contentType = body.contentType();
80 | if (contentType != null) {
81 | charset = contentType.charset(Charsets.UTF_8);
82 | }
83 |
84 | @Cleanup InputStream stream = body.byteStream();
85 | processVEvent(remote.fileName(), eTag.eTag, stream, charset, localCalendar, localResources);
86 | } else {
87 | // multiple contacts, use multi-get
88 | List urls = new LinkedList<>();
89 | for (DavResource remote : bunch)
90 | urls.add(remote.location);
91 |
92 | calendar.multiget(urls.toArray(new HttpUrl[urls.size()]));
93 |
94 | // process multiget results
95 | for (final DavResource remote : calendar.members) {
96 | String eTag;
97 | GetETag getETag = (GetETag) remote.properties.get(GetETag.NAME);
98 | if (getETag != null) {
99 | eTag = getETag.eTag;
100 | } else {
101 | throw new DavException("Received multi-get response without ETag");
102 | }
103 |
104 | Charset charset = Charsets.UTF_8;
105 | GetContentType getContentType = (GetContentType) remote.properties.get(GetContentType.NAME);
106 | if (getContentType != null && getContentType.type != null) {
107 | MediaType type = MediaType.parse(getContentType.type);
108 | if (type != null) {
109 | charset = type.charset(Charsets.UTF_8);
110 | }
111 | }
112 |
113 | CalendarData calendarData = (CalendarData) remote.properties.get(CalendarData.NAME);
114 | if (calendarData == null || calendarData.iCalendar == null) {
115 | throw new DavException("Received multi-get response without address data");
116 | }
117 |
118 | @Cleanup InputStream stream = new ByteArrayInputStream(calendarData.iCalendar.getBytes());
119 | processVEvent(remote.fileName(), eTag, stream, charset, localCalendar, localResources);
120 | }
121 | }
122 | }
123 | }
124 |
125 | private static void processVEvent(String fileName, String eTag, InputStream stream, Charset charset,
126 | LocalCalendar localCalendar, Map localResources)
127 | throws IOException, CalendarStorageException {
128 | Event[] events;
129 | try {
130 | events = Event.fromStream(stream, charset);
131 | } catch (InvalidCalendarException e) {
132 | Timber.w("Received invalid iCalendar, ignoring", e);
133 | return;
134 | }
135 |
136 | if (events.length == 1) {
137 | Event newData = events[0];
138 |
139 | // delete local event, if it exists
140 | LocalEvent localEvent = (LocalEvent) localResources.get(fileName);
141 | if (localEvent != null) {
142 | localEvent.setETag(eTag);
143 | localEvent.update(newData);
144 | } else {
145 | localEvent = new LocalEvent(localCalendar, newData, fileName, eTag);
146 | localEvent.add();
147 | }
148 | } else {
149 | Timber.w(
150 | "Received VCALENDAR with not exactly one VEVENT with UID, but without RECURRENCE-ID; ignoring " +
151 | fileName);
152 | }
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/entitiy/BaseCalendarData.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.entitiy;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author mehdok (mehdok@gmail.com) on 4/6/2017.
7 | */
8 |
9 | public class BaseCalendarData implements Serializable {
10 | }
11 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/entitiy/BaseCollectionInfo.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.entitiy;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * @author mehdok (mehdok@gmail.com) on 4/6/2017.
7 | */
8 |
9 | public class BaseCollectionInfo implements Serializable {
10 | }
11 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/entitiy/CalendarData.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.entitiy;
2 |
3 | import android.content.ContentValues;
4 | import android.database.Cursor;
5 |
6 | import java.util.HashMap;
7 | import java.util.HashSet;
8 | import java.util.Map;
9 | import java.util.Set;
10 |
11 | import okhttp3.HttpUrl;
12 |
13 | /**
14 | * Stub data class
15 | *
16 | * @author mehdok (mehdok@gmail.com) on 3/18/2017.
17 | */
18 |
19 | public class CalendarData extends BaseCalendarData {
20 | private Set homeSets;
21 | private Map collections;
22 |
23 | public CalendarData() {
24 | homeSets = new HashSet<>();
25 | collections = new HashMap<>();
26 | }
27 |
28 | public CalendarData(Set homeSets,
29 | Map collections) {
30 | this.homeSets = homeSets;
31 | this.collections = collections;
32 | }
33 |
34 | /**
35 | * Create CalendarData from cursor
36 | *
37 | * @param cursor
38 | * @return
39 | */
40 | public static CalendarData fromCursor(Cursor cursor) {
41 | return new CalendarData();
42 | }
43 |
44 | /**
45 | * Create Content value for use in Content provider
46 | *
47 | * @return
48 | */
49 | public ContentValues getContentValues() {
50 | ContentValues values = new ContentValues();
51 | return values;
52 | }
53 |
54 | public void addHomeSet(HttpUrl homeSet) {
55 | homeSets.add(homeSet);
56 | }
57 |
58 | public void addCollection(HttpUrl uri, CollectionInfo collectionInfo) {
59 | collections.put(uri, collectionInfo);
60 | }
61 |
62 | public Set getHomeSets() {
63 | return homeSets;
64 | }
65 |
66 | public Map getCollections() {
67 | return collections;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/entitiy/CollectionInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the GNU Public License v3.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.gnu.org/licenses/gpl.html
7 | */
8 |
9 | package com.sixthsolution.lpisyncadapter.entitiy;
10 |
11 | import android.text.TextUtils;
12 |
13 | import com.sixthsolution.lpisyncadapter.util.DavUtils;
14 |
15 | import org.apache.commons.lang3.StringUtils;
16 |
17 | import at.bitfire.dav4android.DavResource;
18 | import at.bitfire.dav4android.Property;
19 | import at.bitfire.dav4android.property.AddressbookDescription;
20 | import at.bitfire.dav4android.property.CalendarColor;
21 | import at.bitfire.dav4android.property.CalendarDescription;
22 | import at.bitfire.dav4android.property.CalendarTimezone;
23 | import at.bitfire.dav4android.property.CurrentUserPrivilegeSet;
24 | import at.bitfire.dav4android.property.DisplayName;
25 | import at.bitfire.dav4android.property.ResourceType;
26 | import at.bitfire.dav4android.property.SupportedAddressData;
27 | import at.bitfire.dav4android.property.SupportedCalendarComponentSet;
28 | import lombok.ToString;
29 |
30 | @ToString
31 | public class CollectionInfo extends BaseCollectionInfo {
32 | public static final int defaultColor = 0xFF8bc34a; // light green 500
33 |
34 | public enum Type {
35 | CALENDAR
36 | }
37 |
38 | public Type type;
39 |
40 | public String url;
41 |
42 | public boolean readOnly;
43 | public String displayName, description;
44 | public Integer color;
45 |
46 | public String timeZone;
47 | public Boolean supportsVEVENT;
48 | public Boolean supportsVTODO;
49 |
50 | public boolean selected;
51 |
52 | // non-persistent properties
53 | public boolean confirmed;
54 |
55 |
56 | public static final Property.Name[] DAV_PROPERTIES = {
57 | ResourceType.NAME,
58 | CurrentUserPrivilegeSet.NAME,
59 | DisplayName.NAME,
60 | AddressbookDescription.NAME, SupportedAddressData.NAME,
61 | CalendarDescription.NAME, CalendarColor.NAME, SupportedCalendarComponentSet.NAME
62 | };
63 |
64 | public static CollectionInfo fromDavResource(DavResource dav) {
65 | CollectionInfo info = new CollectionInfo();
66 | info.url = dav.location.toString();
67 |
68 | ResourceType type = (ResourceType) dav.properties.get(ResourceType.NAME);
69 | if (type != null) {
70 | if (type.types.contains(ResourceType.CALENDAR)) {
71 | info.type = Type.CALENDAR;
72 | }
73 | }
74 |
75 | info.readOnly = false;
76 | CurrentUserPrivilegeSet privilegeSet =
77 | (CurrentUserPrivilegeSet) dav.properties.get(CurrentUserPrivilegeSet.NAME);
78 | if (privilegeSet != null) {
79 | info.readOnly = !privilegeSet.mayWriteContent;
80 | }
81 |
82 | DisplayName displayName = (DisplayName) dav.properties.get(DisplayName.NAME);
83 | if (displayName != null && !StringUtils.isEmpty(displayName.displayName)) {
84 | info.displayName = displayName.displayName;
85 | }
86 |
87 | if (info.type == Type.CALENDAR) {
88 | CalendarDescription calendarDescription =
89 | (CalendarDescription) dav.properties.get(CalendarDescription.NAME);
90 | if (calendarDescription != null) {
91 | info.description = calendarDescription.description;
92 | }
93 |
94 | CalendarColor calendarColor = (CalendarColor) dav.properties.get(CalendarColor.NAME);
95 | if (calendarColor != null) {
96 | info.color = calendarColor.color;
97 | }
98 |
99 | CalendarTimezone timeZone = (CalendarTimezone) dav.properties.get(CalendarTimezone.NAME);
100 | if (timeZone != null) {
101 | info.timeZone = timeZone.vTimeZone;
102 | }
103 |
104 | info.supportsVEVENT = info.supportsVTODO = true;
105 | SupportedCalendarComponentSet supportedCalendarComponentSet =
106 | (SupportedCalendarComponentSet) dav.properties.get(
107 | SupportedCalendarComponentSet.NAME);
108 | if (supportedCalendarComponentSet != null) {
109 | info.supportsVEVENT = supportedCalendarComponentSet.supportsEvents;
110 | info.supportsVTODO = supportedCalendarComponentSet.supportsTasks;
111 | }
112 | }
113 |
114 | return info;
115 | }
116 |
117 | public String getDisplayName() {
118 | return !TextUtils.isEmpty(displayName) ? displayName : DavUtils.lastSegmentOfUrl(url);
119 | }
120 |
121 | public Integer getColor() {
122 | return color != null ? color : defaultColor;
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/exceptions/InvalidAccountException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 – 2016 Ricki Hirner (bitfire web engineering).
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the GNU Public License v3.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.gnu.org/licenses/gpl.html
7 | */
8 |
9 | package com.sixthsolution.lpisyncadapter.exceptions;
10 |
11 | import android.accounts.Account;
12 |
13 | public class InvalidAccountException extends Exception {
14 |
15 | public InvalidAccountException(Account account) {
16 | super("Invalid account: " + account);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/resource/LocalCalendar.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.resource;
2 |
3 | import android.accounts.Account;
4 | import android.content.ContentProviderClient;
5 | import android.content.ContentProviderOperation;
6 | import android.content.ContentUris;
7 | import android.content.ContentValues;
8 | import android.database.Cursor;
9 | import android.net.Uri;
10 | import android.os.RemoteException;
11 | import android.provider.CalendarContract;
12 | import android.support.annotation.NonNull;
13 | import android.text.TextUtils;
14 |
15 | import com.sixthsolution.lpisyncadapter.entitiy.CollectionInfo;
16 |
17 | import net.fortuna.ical4j.model.component.VTimeZone;
18 |
19 | import org.apache.commons.lang3.StringUtils;
20 |
21 | import java.io.FileNotFoundException;
22 | import java.util.LinkedList;
23 | import java.util.List;
24 |
25 | import at.bitfire.ical4android.AndroidCalendar;
26 | import at.bitfire.ical4android.AndroidCalendarFactory;
27 | import at.bitfire.ical4android.BatchOperation;
28 | import at.bitfire.ical4android.CalendarStorageException;
29 | import at.bitfire.ical4android.DateUtils;
30 | import lombok.Cleanup;
31 |
32 | /**
33 | * @author mehdok (mehdok@gmail.com) on 4/8/2017.
34 | */
35 |
36 | public class LocalCalendar extends AndroidCalendar implements LocalCollection {
37 |
38 | public static final String COLUMN_CTAG = CalendarContract.Calendars.CAL_SYNC1;
39 |
40 | static String[] BASE_INFO_COLUMNS = new String[] {
41 | CalendarContract.Events._ID,
42 | CalendarContract.Events._SYNC_ID,
43 | LocalEvent.COLUMN_ETAG
44 | };
45 |
46 | protected LocalCalendar(Account account, ContentProviderClient provider, long id) {
47 | super(account, provider, LocalEvent.Factory.INSTANCE, id);
48 | }
49 |
50 | public static Uri create(@NonNull Account account, @NonNull ContentProviderClient provider, @NonNull
51 | CollectionInfo info) throws
52 | CalendarStorageException {
53 | ContentValues values = valuesFromCollectionInfo(info, true);
54 |
55 | // ACCOUNT_NAME and ACCOUNT_TYPE are required (see docs)! If it's missing, other apps will crash.
56 | values.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
57 | values.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
58 | values.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
59 |
60 | // flag as visible & synchronizable at creation, might be changed by user at any time
61 | values.put(CalendarContract.Calendars.VISIBLE, 0);
62 | values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
63 |
64 | return create(account, provider, values);
65 | }
66 |
67 | public void update(CollectionInfo info, boolean updateColor) throws CalendarStorageException {
68 | update(valuesFromCollectionInfo(info, updateColor));
69 | }
70 |
71 | private static ContentValues valuesFromCollectionInfo(CollectionInfo info, boolean withColor) {
72 | ContentValues values = new ContentValues();
73 | values.put(CalendarContract.Calendars.NAME, info.url);
74 | values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, info.getDisplayName());
75 |
76 | if (withColor) {
77 | values.put(CalendarContract.Calendars.CALENDAR_COLOR, info.getColor());
78 | }
79 |
80 | if (info.readOnly) {
81 | values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
82 | CalendarContract.Calendars.CAL_ACCESS_READ);
83 | } else {
84 | values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
85 | CalendarContract.Calendars.CAL_ACCESS_OWNER);
86 | values.put(CalendarContract.Calendars.CAN_MODIFY_TIME_ZONE, 1);
87 | values.put(CalendarContract.Calendars.CAN_ORGANIZER_RESPOND, 1);
88 | }
89 |
90 | if (!TextUtils.isEmpty(info.timeZone)) {
91 | VTimeZone timeZone = DateUtils.parseVTimeZone(info.timeZone);
92 | if (timeZone != null && timeZone.getTimeZoneId() != null) {
93 | values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE,
94 | DateUtils.findAndroidTimezoneID(timeZone.getTimeZoneId().getValue()));
95 | }
96 | }
97 | values.put(CalendarContract.Calendars.ALLOWED_REMINDERS, CalendarContract.Reminders.METHOD_ALERT);
98 | values.put(CalendarContract.Calendars.ALLOWED_AVAILABILITY, StringUtils.join(
99 | new int[] {CalendarContract.Reminders.AVAILABILITY_TENTATIVE,
100 | CalendarContract.Reminders.AVAILABILITY_FREE,
101 | CalendarContract.Reminders.AVAILABILITY_BUSY},
102 | ","));
103 | values.put(CalendarContract.Calendars.ALLOWED_ATTENDEE_TYPES, StringUtils.join(
104 | new int[] {CalendarContract.Attendees.TYPE_OPTIONAL,
105 | CalendarContract.Attendees.TYPE_REQUIRED,
106 | CalendarContract.Attendees.TYPE_RESOURCE},
107 | ", "));
108 | return values;
109 | }
110 |
111 | @Override
112 | public LocalResource[] getDeleted() throws CalendarStorageException {
113 | return (LocalEvent[]) queryEvents(
114 | CalendarContract.Events.DELETED + "!=0 AND " + CalendarContract.Events.ORIGINAL_ID + " IS NULL",
115 | null);
116 | }
117 |
118 | @Override
119 | public LocalResource[] getWithoutFileName() throws CalendarStorageException {
120 | return (LocalEvent[]) queryEvents(
121 | CalendarContract.Events._SYNC_ID +
122 | " IS NULL AND " +
123 | CalendarContract.Events.ORIGINAL_ID +
124 | " IS NULL", null);
125 | }
126 |
127 | @Override
128 | public LocalResource[] getDirty() throws CalendarStorageException, FileNotFoundException {
129 | List dirty = new LinkedList<>();
130 |
131 | // get dirty events which are required to have an increased SEQUENCE value
132 | for (LocalEvent event : (LocalEvent[]) queryEvents(
133 | CalendarContract.Events.DIRTY + "!=0 AND " + CalendarContract.Events.ORIGINAL_ID + " IS NULL",
134 | null)) {
135 | if (event.getEvent().sequence ==
136 | null) // sequence has not been assigned yet (i.e. this event was just locally created)
137 | {
138 | event.getEvent().sequence = 0;
139 | } else if (event.weAreOrganizer) {
140 | event.getEvent().sequence++;
141 | }
142 | dirty.add(event);
143 | }
144 |
145 | return dirty.toArray(new LocalResource[dirty.size()]);
146 | }
147 |
148 | @Override
149 | public LocalResource[] getAll() throws CalendarStorageException {
150 | return (LocalEvent[]) queryEvents(CalendarContract.Events.ORIGINAL_ID + " IS NULL", null);
151 | }
152 |
153 | @Override
154 | public String getCTag() throws CalendarStorageException {
155 | try {
156 | @Cleanup Cursor
157 | cursor = provider.query(calendarSyncURI(), new String[] {COLUMN_CTAG}, null, null, null);
158 | if (cursor != null && cursor.moveToNext()) {
159 | return cursor.getString(0);
160 | }
161 | } catch (RemoteException e) {
162 | throw new CalendarStorageException("Couldn't read local (last known) CTag", e);
163 | }
164 | return null;
165 | }
166 |
167 | @Override
168 | public void setCTag(String cTag) throws CalendarStorageException {
169 | try {
170 | ContentValues values = new ContentValues(1);
171 | values.put(COLUMN_CTAG, cTag);
172 | provider.update(calendarSyncURI(), values, null, null);
173 | } catch (RemoteException e) {
174 | throw new CalendarStorageException("Couldn't write local (last known) CTag", e);
175 | }
176 | }
177 |
178 | public void processDirtyExceptions() throws CalendarStorageException {
179 | // process deleted exceptions
180 | try {
181 | @Cleanup Cursor cursor = provider.query(
182 | syncAdapterURI(CalendarContract.Events.CONTENT_URI),
183 | new String[] {CalendarContract.Events._ID, CalendarContract.Events.ORIGINAL_ID, LocalEvent.COLUMN_SEQUENCE},
184 | CalendarContract.Events.DELETED +
185 | "!=0 AND " +
186 | CalendarContract.Events.ORIGINAL_ID +
187 | " IS NOT NULL", null,
188 | null);
189 | while (cursor != null && cursor.moveToNext()) {
190 | long id = cursor.getLong(0), // can't be null (by definition)
191 | originalID = cursor.getLong(1); // can't be null (by query)
192 |
193 | // get original event's SEQUENCE
194 | @Cleanup Cursor cursor2 = provider.query(
195 | syncAdapterURI(
196 | ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, originalID)),
197 | new String[] {LocalEvent.COLUMN_SEQUENCE},
198 | null, null, null);
199 | int originalSequence = (cursor2 == null || cursor2.isNull(0)) ? 0 : cursor2.getInt(0);
200 |
201 | BatchOperation batch = new BatchOperation(provider);
202 | // re-schedule original event and set it to DIRTY
203 | batch.enqueue(new BatchOperation.Operation(
204 | ContentProviderOperation.newUpdate(syncAdapterURI(
205 | ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, originalID)))
206 | .withValue(LocalEvent.COLUMN_SEQUENCE, originalSequence + 1)
207 | .withValue(CalendarContract.Events.DIRTY, 1)
208 | ));
209 | // remove exception
210 | batch.enqueue(new BatchOperation.Operation(
211 | ContentProviderOperation.newDelete(syncAdapterURI(
212 | ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, id)))
213 | ));
214 | batch.commit();
215 | }
216 | } catch (RemoteException e) {
217 | throw new CalendarStorageException("Couldn't process locally modified exception", e);
218 | }
219 |
220 | // process dirty exceptions
221 | try {
222 | @Cleanup Cursor cursor = provider.query(
223 | syncAdapterURI(CalendarContract.Events.CONTENT_URI),
224 | new String[] {CalendarContract.Events._ID, CalendarContract.Events.ORIGINAL_ID, LocalEvent.COLUMN_SEQUENCE},
225 | CalendarContract.Events.DIRTY +
226 | "!=0 AND " +
227 | CalendarContract.Events.ORIGINAL_ID +
228 | " IS NOT NULL", null, null);
229 | while (cursor != null && cursor.moveToNext()) {
230 | long id = cursor.getLong(0), // can't be null (by definition)
231 | originalID = cursor.getLong(1); // can't be null (by query)
232 | int sequence = cursor.isNull(2) ? 0 : cursor.getInt(2);
233 |
234 | BatchOperation batch = new BatchOperation(provider);
235 | // original event to DIRTY
236 | batch.enqueue(new BatchOperation.Operation(
237 | ContentProviderOperation.newUpdate(syncAdapterURI(
238 | ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, originalID)))
239 | .withValue(CalendarContract.Events.DIRTY, 1)
240 | ));
241 | // increase SEQUENCE and set DIRTY to 0
242 | batch.enqueue(new BatchOperation.Operation(
243 | ContentProviderOperation.newUpdate(
244 | syncAdapterURI(
245 | ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, id)))
246 | .withValue(LocalEvent.COLUMN_SEQUENCE, sequence + 1)
247 | .withValue(CalendarContract.Events.DIRTY, 0)
248 | ));
249 | batch.commit();
250 | }
251 | } catch (RemoteException e) {
252 | throw new CalendarStorageException("Couldn't process locally modified exception", e);
253 | }
254 | }
255 |
256 | public static class Factory implements AndroidCalendarFactory {
257 | public static final Factory INSTANCE = new Factory();
258 |
259 | @Override
260 | public AndroidCalendar newInstance(Account account, ContentProviderClient provider, long id) {
261 | return new LocalCalendar(account, provider, id);
262 | }
263 |
264 | @Override
265 | public AndroidCalendar[] newArray(int size) {
266 | return new LocalCalendar[size];
267 | }
268 | }
269 |
270 | @Override
271 | protected void populate(ContentValues info) {
272 | super.populate(info);
273 | }
274 |
275 | protected String[] eventBaseInfoColumns() {
276 | return BASE_INFO_COLUMNS;
277 | }
278 | }
279 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/resource/LocalCollection.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.resource;
2 |
3 | import java.io.FileNotFoundException;
4 |
5 | import at.bitfire.ical4android.CalendarStorageException;
6 |
7 | /**
8 | * @author mehdok (mehdok@gmail.com) on 4/8/2017.
9 | */
10 |
11 | public interface LocalCollection {
12 | LocalResource[] getDeleted() throws CalendarStorageException;
13 |
14 | LocalResource[] getWithoutFileName() throws CalendarStorageException;
15 |
16 | LocalResource[] getDirty() throws CalendarStorageException, FileNotFoundException;
17 |
18 | LocalResource[] getAll() throws CalendarStorageException;
19 |
20 | String getCTag() throws CalendarStorageException;
21 |
22 | void setCTag(String cTag) throws CalendarStorageException;
23 | }
24 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/resource/LocalEvent.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.resource;
2 |
3 | import android.content.ContentProviderOperation;
4 | import android.content.ContentValues;
5 | import android.database.Cursor;
6 | import android.os.Build;
7 | import android.os.RemoteException;
8 | import android.provider.CalendarContract;
9 | import android.support.annotation.NonNull;
10 |
11 | import com.sixthsolution.lpisyncadapter.App;
12 |
13 | import at.bitfire.ical4android.AndroidCalendar;
14 | import at.bitfire.ical4android.AndroidEvent;
15 | import at.bitfire.ical4android.AndroidEventFactory;
16 | import at.bitfire.ical4android.CalendarStorageException;
17 | import at.bitfire.ical4android.Event;
18 | import lombok.Cleanup;
19 |
20 | /**
21 | * @author mehdok (mehdok@gmail.com) on 4/8/2017.
22 | */
23 |
24 | public class LocalEvent extends AndroidEvent implements LocalResource {
25 | static final String COLUMN_ETAG = CalendarContract.Events.SYNC_DATA1,
26 | COLUMN_UID = Build.VERSION.SDK_INT >= 17 ? CalendarContract.Events.UID_2445 :
27 | CalendarContract.Events.SYNC_DATA2,
28 | COLUMN_SEQUENCE = CalendarContract.Events.SYNC_DATA3;
29 |
30 | protected String fileName;
31 | protected String eTag;
32 |
33 | public boolean weAreOrganizer = true;
34 |
35 | public LocalEvent(@NonNull AndroidCalendar calendar, Event event, String fileName, String eTag) {
36 | super(calendar, event);
37 | this.fileName = fileName;
38 | this.eTag = eTag;
39 | }
40 |
41 | protected LocalEvent(@NonNull AndroidCalendar calendar, long id, ContentValues baseInfo) {
42 | super(calendar, id, baseInfo);
43 | if (baseInfo != null) {
44 | fileName = baseInfo.getAsString(CalendarContract.Events._SYNC_ID);
45 | eTag = baseInfo.getAsString(COLUMN_ETAG);
46 | }
47 | }
48 |
49 | @Override
50 | protected void populateEvent(ContentValues values) {
51 | super.populateEvent(values);
52 | fileName = values.getAsString(CalendarContract.Events._SYNC_ID);
53 | eTag = values.getAsString(COLUMN_ETAG);
54 | event.uid = values.getAsString(COLUMN_UID);
55 |
56 | event.sequence = values.getAsInteger(COLUMN_SEQUENCE);
57 | if (Build.VERSION.SDK_INT >= 17) {
58 | weAreOrganizer = values.getAsInteger(CalendarContract.Events.IS_ORGANIZER) != 0;
59 | } else {
60 | String organizer = values.getAsString(CalendarContract.Events.ORGANIZER);
61 | weAreOrganizer = organizer == null || organizer.equals(calendar.account.name);
62 | }
63 | }
64 |
65 | @Override
66 | protected void buildEvent(Event recurrence, ContentProviderOperation.Builder builder) {
67 | super.buildEvent(recurrence, builder);
68 |
69 | boolean buildException = recurrence != null;
70 | Event eventToBuild = buildException ? recurrence : event;
71 |
72 | builder.withValue(COLUMN_UID, event.uid)
73 | .withValue(COLUMN_SEQUENCE, eventToBuild.sequence)
74 | .withValue(CalendarContract.Events.DIRTY, 0)
75 | .withValue(CalendarContract.Events.DELETED, 0);
76 |
77 | if (buildException) {
78 | builder.withValue(CalendarContract.Events.ORIGINAL_SYNC_ID, fileName);
79 | } else {
80 | builder.withValue(CalendarContract.Events._SYNC_ID, fileName)
81 | .withValue(COLUMN_ETAG, eTag);
82 | }
83 | }
84 |
85 | @Override
86 | public Long getId() {
87 | return id;
88 | }
89 |
90 | @Override
91 | public String getFileName() {
92 | return fileName;
93 | }
94 |
95 | @Override
96 | public String getETag() {
97 | return eTag;
98 | }
99 |
100 | public void setETag(String eTag) {
101 | this.eTag = eTag;
102 | }
103 |
104 | @Override
105 | public int delete() throws CalendarStorageException {
106 | return super.delete();
107 | }
108 |
109 | public void prepareForUpload() throws CalendarStorageException {
110 | try {
111 | String uid = null;
112 | @Cleanup Cursor
113 | c = calendar.provider.query(eventSyncURI(), new String[] {COLUMN_UID}, null, null, null);
114 | if (c.moveToNext()) {
115 | uid = c.getString(0);
116 | }
117 | if (uid == null) {
118 | uid = App.uidGenerator.generateUid().getValue();
119 | }
120 |
121 | final String newFileName = uid + ".ics";
122 |
123 | ContentValues values = new ContentValues(2);
124 | values.put(CalendarContract.Events._SYNC_ID, newFileName);
125 | values.put(COLUMN_UID, uid);
126 | calendar.provider.update(eventSyncURI(), values, null, null);
127 |
128 | fileName = newFileName;
129 | if (event != null) {
130 | event.uid = uid;
131 | }
132 |
133 | } catch (RemoteException e) {
134 | throw new CalendarStorageException("Couldn't update UID", e);
135 | }
136 | }
137 |
138 | @Override
139 | public void clearDirty(String eTag) throws CalendarStorageException {
140 | try {
141 | ContentValues values = new ContentValues(2);
142 | values.put(CalendarContract.Events.DIRTY, 0);
143 | values.put(COLUMN_ETAG, eTag);
144 | if (event != null) {
145 | values.put(COLUMN_SEQUENCE, event.sequence);
146 | }
147 | calendar.provider.update(eventSyncURI(), values, null, null);
148 |
149 | this.eTag = eTag;
150 | } catch (RemoteException e) {
151 | throw new CalendarStorageException("Couldn't update UID", e);
152 | }
153 | }
154 |
155 | static class Factory implements AndroidEventFactory {
156 | static final Factory INSTANCE = new Factory();
157 |
158 | @Override
159 | public AndroidEvent newInstance(AndroidCalendar calendar, long id, ContentValues baseInfo) {
160 | return new LocalEvent(calendar, id, baseInfo);
161 | }
162 |
163 | @Override
164 | public AndroidEvent newInstance(AndroidCalendar calendar, Event event) {
165 | return new LocalEvent(calendar, event, null, null);
166 | }
167 |
168 | @Override
169 | public AndroidEvent[] newArray(int size) {
170 | return new LocalEvent[size];
171 | }
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/resource/LocalResource.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the GNU Public License v3.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.gnu.org/licenses/gpl.html
7 | */
8 |
9 | package com.sixthsolution.lpisyncadapter.resource;
10 |
11 | import at.bitfire.ical4android.CalendarStorageException;
12 |
13 | public interface LocalResource {
14 |
15 | Long getId();
16 |
17 | String getFileName();
18 |
19 | String getETag();
20 |
21 | int delete() throws CalendarStorageException;
22 |
23 | void prepareForUpload() throws CalendarStorageException;
24 |
25 | void clearDirty(String eTag) throws CalendarStorageException;
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/resource/LocalTask.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.resource;
2 |
3 | /**
4 | * @author mehdok (mehdok@gmail.com) on 4/8/2017.
5 | */
6 |
7 | public class LocalTask implements LocalResource {
8 | @Override
9 | public Long getId() {
10 | return null;
11 | }
12 |
13 | @Override
14 | public String getFileName() {
15 | return null;
16 | }
17 |
18 | @Override
19 | public String getETag() {
20 | return null;
21 | }
22 |
23 | @Override
24 | public int delete() {
25 | return 0;
26 | }
27 |
28 | @Override
29 | public void prepareForUpload() {
30 |
31 | }
32 |
33 | @Override
34 | public void clearDirty(String eTag) {
35 |
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/syncadapter/BaseSyncAdapter.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.syncadapter;
2 |
3 | import android.accounts.Account;
4 | import android.accounts.AccountManager;
5 | import android.content.AbstractThreadedSyncAdapter;
6 | import android.content.ContentProviderClient;
7 | import android.content.Context;
8 | import android.content.SyncResult;
9 | import android.os.Bundle;
10 |
11 | import com.sixthsolution.lpisyncadapter.GlobalConstant;
12 | import com.sixthsolution.lpisyncadapter.entitiy.BaseCalendarData;
13 | import com.sixthsolution.lpisyncadapter.entitiy.BaseCollectionInfo;
14 | import com.sixthsolution.lpisyncadapter.exceptions.InvalidAccountException;
15 | import com.sixthsolution.lpisyncadapter.resource.LocalCalendar;
16 |
17 | import java.util.Map;
18 |
19 | import at.bitfire.ical4android.CalendarStorageException;
20 | import okhttp3.HttpUrl;
21 |
22 | /**
23 | * @author mehdok (mehdok@gmail.com) on 4/6/2017.
24 | */
25 |
26 | public abstract class BaseSyncAdapter
27 | extends AbstractThreadedSyncAdapter {
28 |
29 | protected AccountManager accountManager;
30 | protected SyncServerHandler serverHandler;
31 |
32 | public BaseSyncAdapter(Context context, boolean autoInitialize) {
33 | super(context, autoInitialize);
34 | }
35 |
36 | public BaseSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
37 | super(context, autoInitialize, allowParallelSyncs);
38 | }
39 |
40 | @Override
41 | public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
42 | SyncResult syncResult) {
43 | // required for dav4android (ServiceLoader)
44 | Thread.currentThread().setContextClassLoader(getContext().getClassLoader());
45 |
46 | try {
47 | syncLocalAndRemoteCollections(account, provider);
48 | syncCalendarsEvents(account, provider, extras);
49 |
50 | // notify any registered caller that sync operation is finished
51 | getContext().getContentResolver().notifyChange(GlobalConstant.CONTENT_URI, null, false);
52 | } catch (InvalidAccountException | CalendarStorageException e) {
53 | e.printStackTrace();
54 | }
55 | }
56 |
57 | /**
58 | * Get local and remote collection list and sync them
59 | *
60 | * @param provider
61 | */
62 | protected abstract void syncLocalAndRemoteCollections(Account account, ContentProviderClient provider)
63 | throws CalendarStorageException;
64 |
65 | /**
66 | * @return list of collections that stored in local storage (Android provider)
67 | */
68 | protected abstract LocalCalendar[] getLocalCalendarList(Account account, ContentProviderClient provider)
69 | throws CalendarStorageException;
70 |
71 | /**
72 | * @return list of remote collections by querying server
73 | */
74 | protected abstract T getRemoteCollectionList(Account account);
75 |
76 | /**
77 | * Compare local calendars and remote collections to find the difference then add or remove collections
78 | * from local list
79 | *
80 | * @param account
81 | * @param local
82 | * @param remote
83 | * @param provider
84 | * @throws CalendarStorageException
85 | */
86 | protected abstract void updateLocalCalendars(Account account,
87 | LocalCalendar[] local,
88 | Map remote,
89 | ContentProviderClient provider)
90 | throws CalendarStorageException;
91 |
92 | /**
93 | * Get list of collections from local storage and sync their data with server
94 | *
95 | * @param provider
96 | */
97 | protected abstract void syncCalendarsEvents(Account account, ContentProviderClient provider, Bundle extras)
98 | throws InvalidAccountException, CalendarStorageException;
99 |
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/syncadapter/ICalSyncAdapter.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.syncadapter;
2 |
3 | import android.accounts.Account;
4 | import android.accounts.AccountManager;
5 | import android.content.ContentProviderClient;
6 | import android.content.Context;
7 | import android.content.SyncResult;
8 | import android.os.Bundle;
9 |
10 | import com.sixthsolution.lpisyncadapter.entitiy.CalendarData;
11 | import com.sixthsolution.lpisyncadapter.entitiy.CollectionInfo;
12 | import com.sixthsolution.lpisyncadapter.exceptions.InvalidAccountException;
13 | import com.sixthsolution.lpisyncadapter.resource.LocalCalendar;
14 | import com.sixthsolution.lpisyncadapter.util.HttpClient;
15 |
16 | import java.util.Map;
17 |
18 | import at.bitfire.ical4android.CalendarStorageException;
19 | import okhttp3.HttpUrl;
20 | import okhttp3.OkHttpClient;
21 |
22 | import static com.sixthsolution.lpisyncadapter.util.Util.getHttpLogger;
23 |
24 |
25 | /**
26 | * The syncAdapter, after executing {@link #onPerformSync} it will get user apple id from the authenticator,
27 | * ask the server for any new data, get the local data via {@link ContentProviderClient}, then compare them and
28 | * run queries to update server or local storage.
29 | *
30 | * @author mehdok (mehdok@gmail.com) on 3/18/2017.
31 | */
32 |
33 | public class ICalSyncAdapter extends BaseSyncAdapter {
34 |
35 | public ICalSyncAdapter(Context context, boolean autoInitialize) {
36 | super(context, autoInitialize);
37 | accountManager = AccountManager.get(context);
38 | serverHandler = new SyncServerHandlerImpl();
39 | }
40 |
41 | @Override
42 | public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
43 | SyncResult syncResult) {
44 | super.onPerformSync(account, extras, authority, provider, syncResult);
45 | }
46 |
47 | @Override
48 | protected void syncLocalAndRemoteCollections(Account account, ContentProviderClient provider)
49 | throws CalendarStorageException {
50 | LocalCalendar[] localCalendarData = getLocalCalendarList(account, provider);
51 | CalendarData remoteCalendarData = getRemoteCollectionList(account);
52 | updateLocalCalendars(account, localCalendarData, remoteCalendarData.getCollections(), provider);
53 | }
54 |
55 | @Override
56 | protected LocalCalendar[] getLocalCalendarList(Account account, ContentProviderClient provider)
57 | throws CalendarStorageException {
58 | return (LocalCalendar[]) LocalCalendar.find(account, provider, LocalCalendar.Factory.INSTANCE, null, null);
59 | }
60 |
61 | @Override
62 | protected CalendarData getRemoteCollectionList(Account account) {
63 | return serverHandler.getServerCalendar(getContext(), account);
64 | }
65 |
66 | @Override
67 | protected void updateLocalCalendars(Account account,
68 | LocalCalendar[] local,
69 | Map remote,
70 | ContentProviderClient provider)
71 | throws CalendarStorageException {
72 | // delete obsolete local calendar
73 | for (LocalCalendar calendar : local) {
74 | String url = calendar.getName();
75 | HttpUrl httpUrl = HttpUrl.parse(url);
76 | if (!remote.containsKey(httpUrl)) {
77 | calendar.delete();
78 | } else {
79 | // remote CollectionInfo found for this local collection, update data
80 | CollectionInfo info = remote.get(httpUrl);
81 | calendar.update(info, true);
82 | // we already have a local calendar for this remote collection, don't take into consideration anymore
83 | remote.remove(httpUrl);
84 | }
85 | }
86 |
87 | // create new local calendars
88 | for (HttpUrl url : remote.keySet()) {
89 | CollectionInfo info = remote.get(url);
90 | LocalCalendar.create(account, provider, info);
91 | }
92 | }
93 |
94 | @Override
95 | protected void syncCalendarsEvents(Account account, ContentProviderClient provider, Bundle extras)
96 | throws InvalidAccountException, CalendarStorageException {
97 | LocalCalendar[] localCalendar = getLocalCalendarList(account, provider);
98 |
99 | OkHttpClient httpClient = HttpClient.create(getContext(), account, getHttpLogger());
100 | //for every collection get the data from server
101 | for (LocalCalendar calendar : localCalendar) {
102 | SyncManager syncManager = new SyncManager(httpClient, calendar, extras);
103 | syncManager.performSync();
104 | }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/syncadapter/ICalSyncService.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.syncadapter;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.os.IBinder;
6 | import android.support.annotation.Nullable;
7 |
8 | /**
9 | * A simple bind to sync adapter
10 | *
11 | * @author mehdok (mehdok@gmail.com) on 3/18/2017.
12 | */
13 |
14 | public class ICalSyncService extends Service {
15 | private static final Object syncAdapterLock = new Object();
16 | private static ICalSyncAdapter syncAdapter = null;
17 |
18 | @Override
19 | public void onCreate() {
20 | synchronized (syncAdapterLock) {
21 | if (syncAdapter == null) {
22 | syncAdapter = new ICalSyncAdapter(getApplicationContext(), true);
23 | }
24 | }
25 | }
26 |
27 |
28 | @Nullable
29 | @Override
30 | public IBinder onBind(Intent intent) {
31 | return syncAdapter.getSyncAdapterBinder();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/syncadapter/SyncManager.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.syncadapter;
2 |
3 | import android.content.ContentResolver;
4 | import android.os.Bundle;
5 | import android.text.TextUtils;
6 |
7 | import com.sixthsolution.lpisyncadapter.download.EventDownloader;
8 | import com.sixthsolution.lpisyncadapter.resource.LocalCalendar;
9 | import com.sixthsolution.lpisyncadapter.resource.LocalCollection;
10 | import com.sixthsolution.lpisyncadapter.resource.LocalEvent;
11 | import com.sixthsolution.lpisyncadapter.resource.LocalResource;
12 | import com.sixthsolution.lpisyncadapter.util.DavResourceFinder;
13 |
14 | import java.io.ByteArrayOutputStream;
15 | import java.io.IOException;
16 | import java.util.HashMap;
17 | import java.util.HashSet;
18 | import java.util.Map;
19 | import java.util.Set;
20 |
21 | import at.bitfire.dav4android.DavCalendar;
22 | import at.bitfire.dav4android.DavResource;
23 | import at.bitfire.dav4android.exception.ConflictException;
24 | import at.bitfire.dav4android.exception.DavException;
25 | import at.bitfire.dav4android.exception.HttpException;
26 | import at.bitfire.dav4android.exception.PreconditionFailedException;
27 | import at.bitfire.dav4android.property.GetCTag;
28 | import at.bitfire.dav4android.property.GetETag;
29 | import at.bitfire.ical4android.CalendarStorageException;
30 | import okhttp3.HttpUrl;
31 | import okhttp3.OkHttpClient;
32 | import okhttp3.RequestBody;
33 | import timber.log.Timber;
34 |
35 | /**
36 | * @author mehdok (mehdok@gmail.com) on 4/12/2017.
37 | */
38 |
39 | public class SyncManager {
40 | protected HttpUrl collectionURL;
41 | protected DavResource davCollection;
42 | protected LocalCollection localCollection;
43 | protected OkHttpClient httpClient;
44 | protected Bundle extras;
45 |
46 | /** remote CTag at the time of {@link #getDownloadList(Map, Map)} */
47 | protected String remoteCTag = null;
48 |
49 | public SyncManager(OkHttpClient httpClient, LocalCollection localCollection, Bundle extras) {
50 | this.httpClient = httpClient;
51 | this.localCollection = localCollection;
52 | this.extras = extras;
53 | }
54 |
55 | public void performSync() {
56 | try {
57 | prepare();
58 | queryCapabilities();
59 | processLocallyDeleted();
60 | prepareDirty();
61 | uploadDirty();
62 |
63 | if (checkSyncState()) {
64 | Map localResources = listLocal();
65 |
66 | LocalCalendar calendar = localCalendar();
67 | Map remoteCollectionEvents =
68 | DavResourceFinder.getCollectionEvents(httpClient, calendar.getName(),
69 | true, true, null, null);
70 |
71 | Set toDownload = getDownloadList(remoteCollectionEvents, localResources);
72 |
73 | DavCalendar davCalendar = new DavCalendar(httpClient, HttpUrl.parse(calendar.getName()));
74 | EventDownloader.downloadEvents(toDownload, davCalendar, localCalendar(), localResources);
75 |
76 | saveSyncState();
77 | }
78 |
79 | } catch (DavException | IOException | HttpException | CalendarStorageException e) {
80 | e.printStackTrace();
81 | }
82 | }
83 |
84 | protected boolean prepare() {
85 | collectionURL = HttpUrl.parse(localCalendar().getName());
86 | davCollection = new DavCalendar(httpClient, collectionURL);
87 | return true;
88 | }
89 |
90 | protected void queryCapabilities() throws DavException, IOException, HttpException {
91 | davCollection.propfind(0, GetCTag.NAME);
92 | }
93 |
94 | /**
95 | * Process locally deleted entries (DELETE them on the server as well).
96 | * Checks Thread.interrupted() before each request to allow quick sync cancellation.
97 | */
98 | protected void processLocallyDeleted() throws CalendarStorageException {
99 | // Remove locally deleted entries from server (if they have a name, i.e. if they were uploaded before),
100 | // but only if they don't have changed on the server. Then finally remove them from the local address book.
101 | LocalResource[] localList = localCollection.getDeleted();
102 | for (final LocalResource local : localList) {
103 | if (Thread.interrupted()) {
104 | return;
105 | }
106 |
107 | final String fileName = local.getFileName();
108 | if (!TextUtils.isEmpty(fileName)) {
109 |
110 | final DavResource remote =
111 | new DavResource(httpClient, collectionURL.newBuilder().addPathSegment(fileName).build());
112 | try {
113 | remote.delete(local.getETag());
114 | } catch (IOException | HttpException e) {
115 | Timber.w("Couldn't delete " + fileName + " from server; ignoring (may be downloaded again)");
116 | }
117 | } else {
118 | local.delete();
119 | }
120 | }
121 | }
122 |
123 | protected void prepareDirty() throws CalendarStorageException {
124 | // assign file names and UIDs to new contacts so that we can use the file name as an index
125 | for (final LocalResource local : localCollection.getWithoutFileName()) {
126 | local.prepareForUpload();
127 | }
128 |
129 | localCalendar().processDirtyExceptions();
130 | }
131 |
132 | /**
133 | * Uploads dirty records to the server, using a PUT request for each record.
134 | * Checks Thread.interrupted() before each request to allow quick sync cancellation.
135 | */
136 | protected void uploadDirty() throws IOException, HttpException, CalendarStorageException {
137 | // upload dirty contacts
138 | for (final LocalResource local : localCollection.getDirty()) {
139 | if (Thread.interrupted()) {
140 | return;
141 | }
142 |
143 | final String fileName = local.getFileName();
144 | final DavResource remote =
145 | new DavResource(httpClient, collectionURL.newBuilder().addPathSegment(fileName).build());
146 |
147 | // generate entity to upload (VCard, iCal, whatever)
148 | RequestBody body = prepareUpload(local);
149 |
150 | try {
151 | if (local.getETag() == null) {
152 | remote.put(body, null, true);
153 | } else {
154 | remote.put(body, local.getETag(), false);
155 | }
156 | } catch (ConflictException | PreconditionFailedException e) {
157 | // we can't interact with the user to resolve the conflict, so we treat 409 like 412
158 | Timber.w("Resource has been modified on the server before upload, ignoring", e);
159 | }
160 |
161 | String eTag = null;
162 | GetETag newETag = (GetETag) remote.properties.get(GetETag.NAME);
163 | if (newETag != null) {
164 | eTag = newETag.eTag;
165 | } else {
166 | Timber.d("Didn't receive new ETag after uploading, setting to null");
167 | }
168 |
169 | local.clearDirty(eTag);
170 | }
171 | }
172 |
173 | protected RequestBody prepareUpload(LocalResource resource) throws IOException, CalendarStorageException {
174 | LocalEvent local = (LocalEvent) resource;
175 | ByteArrayOutputStream os = new ByteArrayOutputStream();
176 | local.getEvent().write(os);
177 |
178 | return RequestBody.create(
179 | DavCalendar.MIME_ICALENDAR,
180 | os.toByteArray()
181 | );
182 | }
183 |
184 | protected boolean checkSyncState() throws CalendarStorageException {
185 | // check CTag (ignore on manual sync)
186 | GetCTag getCTag = (GetCTag) davCollection.properties.get(GetCTag.NAME);
187 | if (getCTag != null) {
188 | remoteCTag = getCTag.cTag;
189 | }
190 |
191 | String localCTag = null;
192 | if (extras.containsKey(ContentResolver.SYNC_EXTRAS_MANUAL)) {
193 | Timber.d("Manual sync, ignoring CTag");
194 | } else {
195 | localCTag = localCollection.getCTag();
196 | }
197 |
198 | if (remoteCTag != null && remoteCTag.equals(localCTag)) {
199 | return false;
200 | } else {
201 | return true;
202 | }
203 | }
204 |
205 | protected Map listLocal() throws CalendarStorageException {
206 | // fetch list of local contacts and build hash table to index file name
207 | LocalResource[] localList = localCollection.getAll();
208 | Map localResources = new HashMap<>(localList.length);
209 | for (LocalResource resource : localList) {
210 | localResources.put(resource.getFileName(), resource);
211 | }
212 |
213 | return localResources;
214 | }
215 |
216 | protected Set getDownloadList(Map remoteResources,
217 | Map localResources)
218 | throws DavException, CalendarStorageException {
219 | Set toDownload = new HashSet<>();
220 |
221 | for (String localName : localResources.keySet()) {
222 | final DavResource remote = remoteResources.get(localName);
223 |
224 | if (remote == null) {
225 | final LocalResource local = localResources.get(localName);
226 | local.delete();
227 | } else {
228 | // contact is still on server, check whether it has been updated remotely
229 | GetETag getETag = (GetETag) remote.properties.get(GetETag.NAME);
230 | if (getETag == null || getETag.eTag == null) {
231 | throw new DavException("Server didn't provide ETag");
232 | }
233 | String localETag = localResources.get(localName).getETag(),
234 | remoteETag = getETag.eTag;
235 | if (!remoteETag.equals(localETag)) {
236 | toDownload.add(remote);
237 | }
238 |
239 | // remote entry has been seen, remove from list
240 | remoteResources.remove(localName);
241 | }
242 | }
243 |
244 | // add all unseen (= remotely added) remote contacts
245 | if (!remoteResources.isEmpty()) {
246 | toDownload.addAll(remoteResources.values());
247 | }
248 |
249 | return toDownload;
250 | }
251 |
252 | protected void saveSyncState() throws CalendarStorageException {
253 | /* Save sync state (CTag). It doesn't matter if it has changed during the sync process
254 | (for instance, because another client has uploaded changes), because this will simply
255 | cause all remote entries to be listed at the next sync. */
256 | localCollection.setCTag(remoteCTag);
257 | }
258 |
259 | private LocalCalendar localCalendar() {
260 | return ((LocalCalendar) localCollection);
261 | }
262 |
263 | }
264 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/syncadapter/SyncServerHandler.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.syncadapter;
2 |
3 | import android.accounts.Account;
4 | import android.content.Context;
5 |
6 | import com.sixthsolution.lpisyncadapter.entitiy.CalendarData;
7 |
8 |
9 | /**
10 | * @author mehdok (mehdok@gmail.com) on 3/18/2017.
11 | */
12 |
13 | public interface SyncServerHandler {
14 | CalendarData getServerCalendar(Context context, Account account);
15 | }
16 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/syncadapter/SyncServerHandlerImpl.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.syncadapter;
2 |
3 | import android.accounts.Account;
4 | import android.content.Context;
5 |
6 | import com.sixthsolution.lpisyncadapter.entitiy.CalendarData;
7 | import com.sixthsolution.lpisyncadapter.exceptions.InvalidAccountException;
8 | import com.sixthsolution.lpisyncadapter.util.DavResourceFinder;
9 | import com.sixthsolution.lpisyncadapter.util.HttpClient;
10 |
11 | import java.io.IOException;
12 | import java.security.InvalidAlgorithmParameterException;
13 | import java.security.InvalidKeyException;
14 | import java.security.NoSuchAlgorithmException;
15 |
16 | import javax.crypto.BadPaddingException;
17 | import javax.crypto.IllegalBlockSizeException;
18 | import javax.crypto.NoSuchPaddingException;
19 |
20 | import at.bitfire.dav4android.exception.DavException;
21 | import at.bitfire.dav4android.exception.HttpException;
22 | import okhttp3.OkHttpClient;
23 | import timber.log.Timber;
24 |
25 | import static com.sixthsolution.lpisyncadapter.util.Util.getHttpLogger;
26 |
27 | /**
28 | * @author mehdok (mehdok@gmail.com) on 3/18/2017.
29 | */
30 |
31 | public class SyncServerHandlerImpl implements SyncServerHandler {
32 | @Override
33 | public CalendarData getServerCalendar(Context context, Account account) {
34 | try {
35 | return getCalendarFromServer(context, account);
36 | } catch (Exception e) {
37 | e.printStackTrace();
38 | Timber.e("getServerCalendar", e);
39 | }
40 | return null;
41 | }
42 |
43 | private CalendarData getCalendarFromServer(Context context, Account account)
44 | throws InvalidAccountException, DavException, IOException, HttpException, NoSuchPaddingException,
45 | InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException,
46 | BadPaddingException, InvalidKeyException {
47 |
48 | OkHttpClient httpClient = HttpClient.create(context, account, getHttpLogger());
49 |
50 | return DavResourceFinder.getCalendarsData(context, account, httpClient);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/AccountSettings.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the GNU Public License v3.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.gnu.org/licenses/gpl.html
7 | */
8 | package com.sixthsolution.lpisyncadapter.util;
9 |
10 | import android.accounts.Account;
11 | import android.accounts.AccountManager;
12 | import android.annotation.TargetApi;
13 | import android.content.Context;
14 | import android.os.Build;
15 | import android.support.annotation.NonNull;
16 |
17 | import com.sixthsolution.lpisyncadapter.authenticator.crypto.Crypto;
18 | import com.sixthsolution.lpisyncadapter.exceptions.InvalidAccountException;
19 |
20 | import static com.sixthsolution.lpisyncadapter.GlobalConstant.PARAM_PRINCIPAL;
21 |
22 |
23 | public class AccountSettings {
24 | private final Context context;
25 | private final AccountManager accountManager;
26 | private final Account account;
27 |
28 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
29 | public AccountSettings(@NonNull Context context, @NonNull Account account) throws InvalidAccountException {
30 | this.context = context;
31 | this.account = account;
32 |
33 | accountManager = AccountManager.get(context);
34 | }
35 |
36 | // authentication settings
37 | public String username() {
38 | return account.name;
39 | }
40 |
41 | public String password() {
42 | try {
43 | return Crypto.armorDecrypt(accountManager.getPassword(account), context);
44 | } catch (Exception e) {
45 | e.printStackTrace();
46 | }
47 |
48 | return "";
49 | }
50 |
51 | public String getPrincipal() {
52 | try {
53 | return Crypto.armorDecrypt(accountManager.getUserData(account, PARAM_PRINCIPAL), context);
54 | } catch (Exception e) {
55 | e.printStackTrace();
56 | }
57 |
58 | return "";
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/ArrayUtils.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.util;
2 |
3 | import java.lang.reflect.Array;
4 |
5 | /**
6 | * @author mehdok (mehdok@gmail.com) on 4/8/2017.
7 | */
8 |
9 | public class ArrayUtils {
10 | public static T[][] partition(T[] bigArray, int max) {
11 | int nItems = bigArray.length;
12 | int nPartArrays = (nItems + max - 1) / max;
13 |
14 | T[][] partArrays = (T[][]) Array.newInstance(bigArray.getClass().getComponentType(), nPartArrays, 0);
15 |
16 | // nItems is now the number of remaining items
17 | for (int i = 0; nItems > 0; i++) {
18 | int n = (nItems < max) ? nItems : max;
19 | partArrays[i] = (T[]) Array.newInstance(bigArray.getClass().getComponentType(), n);
20 | System.arraycopy(bigArray, i * max, partArrays[i], 0, n);
21 |
22 | nItems -= n;
23 | }
24 |
25 | return partArrays;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/DavResourceFinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the GNU Public License v3.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.gnu.org/licenses/gpl.html
7 | */
8 | package com.sixthsolution.lpisyncadapter.util;
9 |
10 | import android.accounts.Account;
11 | import android.content.Context;
12 |
13 | import com.sixthsolution.lpisyncadapter.entitiy.CalendarData;
14 | import com.sixthsolution.lpisyncadapter.entitiy.CollectionInfo;
15 | import com.sixthsolution.lpisyncadapter.exceptions.InvalidAccountException;
16 |
17 | import org.apache.commons.collections4.iterators.IteratorChain;
18 | import org.apache.commons.collections4.iterators.SingletonIterator;
19 |
20 | import java.io.IOException;
21 | import java.net.URI;
22 | import java.net.URISyntaxException;
23 | import java.util.Date;
24 | import java.util.HashMap;
25 | import java.util.Iterator;
26 | import java.util.LinkedHashSet;
27 | import java.util.Map;
28 | import java.util.Set;
29 |
30 | import at.bitfire.dav4android.DavCalendar;
31 | import at.bitfire.dav4android.DavResource;
32 | import at.bitfire.dav4android.UrlUtils;
33 | import at.bitfire.dav4android.exception.DavException;
34 | import at.bitfire.dav4android.exception.HttpException;
35 | import at.bitfire.dav4android.property.CalendarHomeSet;
36 | import at.bitfire.dav4android.property.CalendarProxyReadFor;
37 | import at.bitfire.dav4android.property.CalendarProxyWriteFor;
38 | import at.bitfire.dav4android.property.GroupMembership;
39 | import okhttp3.HttpUrl;
40 | import okhttp3.OkHttpClient;
41 | import timber.log.Timber;
42 |
43 | import static com.sixthsolution.lpisyncadapter.GlobalConstant.BASE_URL;
44 |
45 | public class DavResourceFinder {
46 |
47 | public static CalendarData getCalendarsData(Context context, Account account, OkHttpClient httpClient)
48 | throws InvalidAccountException, DavException, IOException, HttpException {
49 | String host = BASE_URL.getHost();
50 | String path = BASE_URL.getEncodedPath();
51 | int port = BASE_URL.getPort();
52 | URI uri = null;
53 | try {
54 | uri = new URI(BASE_URL.getScheme(), null, host, port, path, null, null);
55 | } catch (URISyntaxException e) {
56 | e.printStackTrace();
57 | }
58 |
59 | AccountSettings settings = new AccountSettings(context, account);
60 | DavResource dav = new DavResource(httpClient, HttpUrl.parse(uri.toString() + settings.getPrincipal()));
61 |
62 | // refresh home sets
63 | Set homeSets = getHomeSets(httpClient, dav);
64 | Map collections = getCollections(homeSets, httpClient);
65 |
66 | return new CalendarData(homeSets, collections);
67 | }
68 |
69 | public static Set getHomeSets(OkHttpClient httpClient, DavResource dav)
70 | throws IOException, HttpException, DavException {
71 | Set homeSets = new LinkedHashSet<>();
72 |
73 | queryHomeSets(dav, homeSets);
74 |
75 | CalendarProxyReadFor proxyRead = (CalendarProxyReadFor) dav.properties.get(CalendarProxyReadFor.NAME);
76 | if (proxyRead != null) {
77 | for (String href : proxyRead.principals) {
78 | queryHomeSets(new DavResource(httpClient, dav.location.resolve(href)), homeSets);
79 | }
80 | }
81 |
82 | CalendarProxyWriteFor proxyWrite = (CalendarProxyWriteFor) dav.properties.get(CalendarProxyWriteFor.NAME);
83 | if (proxyWrite != null) {
84 | for (String href : proxyWrite.principals) {
85 | Timber.d("getCalendarFromServer",
86 | "Principal is a read-write proxy for " + href + ", checking for home sets");
87 | queryHomeSets(new DavResource(httpClient, dav.location.resolve(href)), homeSets);
88 | }
89 | }
90 |
91 | // refresh home sets: direct group memberships
92 | GroupMembership groupMembership = (GroupMembership) dav.properties.get(GroupMembership.NAME);
93 | if (groupMembership != null) {
94 | for (String href : groupMembership.hrefs) {
95 | Timber.d("getCalendarFromServer",
96 | "Principal is member of group " + href + ", checking for home sets");
97 | DavResource group = new DavResource(httpClient, dav.location.resolve(href));
98 | try {
99 | queryHomeSets(group, homeSets);
100 | } catch (HttpException | DavException e) {
101 | Timber.e("getCalendarFromServer", "Couldn't query member group ", e);
102 | }
103 | }
104 | }
105 |
106 | return homeSets;
107 | }
108 |
109 | private static void queryHomeSets(DavResource dav, Set homeSets)
110 | throws IOException, HttpException, DavException {
111 | dav.propfind(0, CalendarHomeSet.NAME, CalendarProxyReadFor.NAME, CalendarProxyWriteFor.NAME,
112 | GroupMembership.NAME);
113 | CalendarHomeSet calendarHomeSet = (CalendarHomeSet) dav.properties.get(CalendarHomeSet.NAME);
114 | if (calendarHomeSet != null) {
115 | for (String href : calendarHomeSet.hrefs) {
116 | homeSets.add(UrlUtils.withTrailingSlash(dav.location.resolve(href)));
117 | }
118 | }
119 | }
120 |
121 | public static Map getCollections(Set homeSets, OkHttpClient httpClient) {
122 | Map collections = new HashMap<>();
123 |
124 | // refresh collections (taken from home sets)
125 | for (Iterator itHomeSets = homeSets.iterator(); itHomeSets.hasNext(); ) {
126 | HttpUrl homeSet = itHomeSets.next();
127 | DavResource dav = new DavResource(httpClient, homeSet);
128 | try {
129 | dav.propfind(1, CollectionInfo.DAV_PROPERTIES);
130 | IteratorChain itCollections =
131 | new IteratorChain<>(dav.members.iterator(), new SingletonIterator(dav));
132 | while (itCollections.hasNext()) {
133 | DavResource member = itCollections.next();
134 | CollectionInfo info = CollectionInfo.fromDavResource(member);
135 | info.confirmed = true;
136 |
137 | if (info.type == CollectionInfo.Type.CALENDAR) {
138 | collections.put(member.location, info);
139 | }
140 | }
141 | } catch (HttpException e) {
142 | if (e.status == 403 || e.status == 404 || e.status == 410) {
143 | // delete home set only if it was not accessible (40x)
144 | itHomeSets.remove();
145 | }
146 | } catch (DavException | IOException e) {
147 | e.printStackTrace();
148 | }
149 | }
150 |
151 | // check/refresh unconfirmed collections
152 | for (Iterator> iterator = collections.entrySet().iterator();
153 | iterator.hasNext(); ) {
154 | Map.Entry entry = iterator.next();
155 | HttpUrl url = entry.getKey();
156 | CollectionInfo info = entry.getValue();
157 | if (!info.confirmed) {
158 | try {
159 | DavResource dav = new DavResource(httpClient, url);
160 | dav.propfind(0, CollectionInfo.DAV_PROPERTIES);
161 | info = CollectionInfo.fromDavResource(dav);
162 | info.confirmed = true;
163 |
164 | // remove unusable collections
165 | if (info.type != CollectionInfo.Type.CALENDAR) {
166 | iterator.remove();
167 | }
168 | } catch (HttpException e) {
169 | if (e.status == 403 || e.status == 404 || e.status == 410) {
170 | // delete collection only if it was not accessible (40x)
171 | iterator.remove();
172 | }
173 | } catch (DavException | IOException e) {
174 | e.printStackTrace();
175 | }
176 | }
177 | }
178 |
179 | return collections;
180 | }
181 |
182 | /**
183 | * Get any data stored in online server for this collection
184 | *
185 | * @param httpClient
186 | * @param collectionUrl
187 | * @param supportsVEVENT
188 | * @param supportsVTODO
189 | * @param start
190 | * @param end
191 | * @return
192 | * @throws DavException
193 | * @throws IOException
194 | * @throws HttpException
195 | */
196 | public static Map getCollectionEvents(OkHttpClient httpClient,
197 | String collectionUrl,
198 | boolean supportsVEVENT,
199 | boolean supportsVTODO,
200 | Date start,
201 | Date end)
202 | throws DavException, IOException, HttpException {
203 | // TODO: 4/5/2017 Read last sync time from preferences
204 | /*Date limitStart = null;
205 | Integer pastDays = settings.getTimeRangePastDays();
206 | if (pastDays != null) {
207 | Calendar calendar = Calendar.getInstance();
208 | calendar.add(Calendar.DAY_OF_MONTH, -pastDays);
209 | limitStart = calendar.getTime();
210 | }*/
211 |
212 | DavCalendar davCalendar = new DavCalendar(httpClient, HttpUrl.parse(collectionUrl));
213 | Map remoteResource = new HashMap<>();
214 |
215 | if (supportsVEVENT) {
216 | remoteResource.putAll(getCalendarData(davCalendar, "VEVENT", start, end));
217 | }
218 |
219 | // TODO: 4/8/2017 Proceed TODOs
220 | /*if (supportsVTODO) {
221 | remoteResource.putAll(getCalendarData(davCalendar, "VTODO", start, end));
222 | }*/
223 |
224 | return remoteResource;
225 | }
226 |
227 | private static Map getCalendarData(DavCalendar davCalendar, String what, Date start,
228 | Date end)
229 | throws DavException, IOException, HttpException {
230 | davCalendar.calendarQuery(what, start, end);
231 | Map remoteResources = new HashMap<>(davCalendar.members.size());
232 |
233 | for (DavResource iCal : davCalendar.members) {
234 | String fileName = iCal.fileName();
235 | remoteResources.put(fileName, iCal);
236 | }
237 |
238 | return remoteResources;
239 | }
240 | }
241 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/DavUtils.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.util;
2 |
3 | import android.support.annotation.NonNull;
4 |
5 | import org.apache.commons.lang3.StringUtils;
6 |
7 | import java.util.Collections;
8 | import java.util.LinkedList;
9 | import java.util.List;
10 |
11 | import okhttp3.HttpUrl;
12 |
13 | /**
14 | * @author mehdok (mehdok@gmail.com) on 4/12/2017.
15 | */
16 |
17 | public class DavUtils {
18 | public static String lastSegmentOfUrl(@NonNull String url) {
19 | // the list returned by HttpUrl.pathSegments() is unmodifiable, so we have to create a copy
20 | List segments = new LinkedList<>(HttpUrl.parse(url).pathSegments());
21 | Collections.reverse(segments);
22 |
23 | for (String segment : segments)
24 | if (!StringUtils.isEmpty(segment)) {
25 | return segment;
26 | }
27 |
28 | return "/";
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/HttpClient.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the GNU Public License v3.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.gnu.org/licenses/gpl.html
7 | */
8 |
9 | package com.sixthsolution.lpisyncadapter.util;
10 |
11 | import android.accounts.Account;
12 | import android.content.Context;
13 | import android.os.Build;
14 | import android.support.annotation.NonNull;
15 | import android.support.annotation.Nullable;
16 |
17 | import com.sixthsolution.lpisyncadapter.App;
18 | import com.sixthsolution.lpisyncadapter.BuildConfig;
19 | import com.sixthsolution.lpisyncadapter.exceptions.InvalidAccountException;
20 |
21 | import java.io.IOException;
22 | import java.text.SimpleDateFormat;
23 | import java.util.Date;
24 | import java.util.Locale;
25 | import java.util.concurrent.TimeUnit;
26 |
27 | import at.bitfire.dav4android.BasicDigestAuthHandler;
28 | import at.bitfire.dav4android.UrlUtils;
29 | import okhttp3.Interceptor;
30 | import okhttp3.OkHttpClient;
31 | import okhttp3.Request;
32 | import okhttp3.Response;
33 | import okhttp3.logging.HttpLoggingInterceptor;
34 |
35 | public class HttpClient {
36 | private static final OkHttpClient client = new OkHttpClient();
37 | private static final UserAgentInterceptor userAgentInterceptor = new UserAgentInterceptor();
38 |
39 | private static final String userAgent;
40 |
41 | static {
42 | String date = new SimpleDateFormat("yyyy/MM/dd", Locale.US).format(new Date(BuildConfig.buildTime));
43 | userAgent = "sixthsolution/" +
44 | BuildConfig.VERSION_NAME +
45 | " (" +
46 | date +
47 | "; dav4android; okhttp3) Android/" +
48 | Build.VERSION.RELEASE;
49 | }
50 |
51 | private HttpClient() {
52 | }
53 |
54 | public static OkHttpClient create(@NonNull Context context, @NonNull Account account,
55 | @NonNull HttpLoggingInterceptor logging) throws InvalidAccountException {
56 | OkHttpClient.Builder builder = defaultBuilder(context);
57 |
58 | // use account settings for authentication
59 | AccountSettings settings = new AccountSettings(context, account);
60 | builder = addAuthentication(builder, null, settings.username(), settings.password());
61 |
62 | // Set logger
63 | builder.addInterceptor(logging);
64 |
65 | return builder.build();
66 | }
67 |
68 | private static OkHttpClient.Builder defaultBuilder(@Nullable Context context) {
69 | OkHttpClient.Builder builder = client.newBuilder();
70 |
71 | // use MemorizingTrustManager to manage self-signed certificates
72 | if (context != null) {
73 | App app = (App) context.getApplicationContext();
74 | if (App.sslSocketFactoryCompat != null && app.certManager != null) {
75 | builder.sslSocketFactory(App.sslSocketFactoryCompat, app.certManager);
76 | }
77 | if (App.hostnameVerifier != null) {
78 | builder.hostnameVerifier(App.hostnameVerifier);
79 | }
80 | }
81 |
82 | // set timeouts
83 | builder.connectTimeout(30, TimeUnit.SECONDS);
84 | builder.writeTimeout(30, TimeUnit.SECONDS);
85 | builder.readTimeout(120, TimeUnit.SECONDS);
86 |
87 | // don't allow redirects, because it would break PROPFIND handling
88 | builder.followRedirects(false);
89 |
90 | // add User-Agent to every request
91 | builder.addNetworkInterceptor(userAgentInterceptor);
92 |
93 | // add cookie store for non-persistent cookies (some services like Horde use cookies for session tracking)
94 | builder.cookieJar(new MemoryCookieStore());
95 |
96 | return builder;
97 | }
98 |
99 | private static OkHttpClient.Builder addAuthentication(@NonNull OkHttpClient.Builder builder,
100 | @Nullable String host, @NonNull String username,
101 | @NonNull String password) {
102 | BasicDigestAuthHandler authHandler =
103 | new BasicDigestAuthHandler(UrlUtils.hostToDomain(host), username, password);
104 | return builder
105 | .addNetworkInterceptor(authHandler)
106 | .authenticator(authHandler);
107 | }
108 |
109 | public static OkHttpClient addAuthentication(@NonNull OkHttpClient client, @NonNull String username,
110 | @NonNull String password) {
111 | OkHttpClient.Builder builder = client.newBuilder();
112 | addAuthentication(builder, null, username, password);
113 | return builder.build();
114 | }
115 |
116 | public static OkHttpClient addAuthentication(@NonNull OkHttpClient client, @NonNull String host,
117 | @NonNull String username, @NonNull String password) {
118 | OkHttpClient.Builder builder = client.newBuilder();
119 | addAuthentication(builder, host, username, password);
120 | return builder.build();
121 | }
122 |
123 |
124 | static class UserAgentInterceptor implements Interceptor {
125 | @Override
126 | public Response intercept(Chain chain) throws IOException {
127 | Locale locale = Locale.getDefault();
128 | Request request = chain.request().newBuilder()
129 | .header("User-Agent", userAgent)
130 | .header("Accept-Language", locale.getLanguage() +
131 | "-" +
132 | locale.getCountry() +
133 | ", " +
134 | locale.getLanguage() +
135 | ";q=0.7, *;q=0.5")
136 | .build();
137 | return chain.proceed(request);
138 | }
139 | }
140 |
141 | }
142 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/MemoryCookieStore.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.util;
2 |
3 | /**
4 | * @author mehdok (mehdok@gmail.com) on 4/4/2017.
5 | */
6 |
7 | import org.apache.commons.collections4.MapIterator;
8 | import org.apache.commons.collections4.keyvalue.MultiKey;
9 | import org.apache.commons.collections4.map.HashedMap;
10 | import org.apache.commons.collections4.map.MultiKeyMap;
11 |
12 | import java.util.LinkedList;
13 | import java.util.List;
14 |
15 | import okhttp3.Cookie;
16 | import okhttp3.CookieJar;
17 | import okhttp3.HttpUrl;
18 |
19 | /**
20 | * Primitive cookie store that stores cookies in a (volatile) hash map.
21 | * Will be sufficient for session cookies.
22 | */
23 | public class MemoryCookieStore implements CookieJar {
24 |
25 | /**
26 | * Stored cookies. The multi-key consists of three parts: name, domain, and path.
27 | * This ensures that cookies can be overwritten. [RFC 6265 5.3 Storage Model]
28 | * Not thread-safe!
29 | */
30 | protected final MultiKeyMap storage =
31 | MultiKeyMap.multiKeyMap(new HashedMap, Cookie>());
32 |
33 | @Override
34 | public void saveFromResponse(HttpUrl url, List cookies) {
35 | synchronized (storage) {
36 | for (Cookie cookie : cookies)
37 | storage.put(cookie.name(), cookie.domain(), cookie.path(), cookie);
38 | }
39 | }
40 |
41 | @Override
42 | public List loadForRequest(HttpUrl url) {
43 | List cookies = new LinkedList<>();
44 |
45 | synchronized (storage) {
46 | MapIterator, Cookie> iter = storage.mapIterator();
47 | while (iter.hasNext()) {
48 | iter.next();
49 | Cookie cookie = iter.getValue();
50 |
51 | // remove expired cookies
52 | if (cookie.expiresAt() <= System.currentTimeMillis()) {
53 | iter.remove();
54 | continue;
55 | }
56 |
57 | // add applicable cookies
58 | if (cookie.matches(url)) {
59 | cookies.add(cookie);
60 | }
61 | }
62 | }
63 |
64 | return cookies;
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/SSLSocketFactoryCompat.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the GNU Public License v3.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.gnu.org/licenses/gpl.html
7 | */
8 |
9 | package com.sixthsolution.lpisyncadapter.util;
10 |
11 | import android.os.Build;
12 | import android.support.annotation.NonNull;
13 | import android.util.Log;
14 |
15 | import java.io.IOException;
16 | import java.net.InetAddress;
17 | import java.net.Socket;
18 | import java.security.GeneralSecurityException;
19 | import java.util.Arrays;
20 | import java.util.HashSet;
21 | import java.util.LinkedList;
22 | import java.util.List;
23 | import java.util.Locale;
24 |
25 | import javax.net.ssl.SSLContext;
26 | import javax.net.ssl.SSLSocket;
27 | import javax.net.ssl.SSLSocketFactory;
28 | import javax.net.ssl.X509TrustManager;
29 |
30 | import lombok.Cleanup;
31 |
32 | public class SSLSocketFactoryCompat extends SSLSocketFactory {
33 |
34 | private SSLSocketFactory delegate;
35 |
36 | // Android 5.0+ (API level21) provides reasonable default settings
37 | // but it still allows SSLv3
38 | // https://developer.android.com/reference/javax/net/ssl/SSLSocket.html
39 | static String protocols[] = null, cipherSuites[] = null;
40 |
41 | static {
42 | try {
43 | @Cleanup SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
44 | if (socket != null) {
45 | /* set reasonable protocol versions */
46 | // - enable all supported protocols (enables TLSv1.1 and TLSv1.2 on Android <5.0)
47 | // - remove all SSL versions (especially SSLv3) because they're insecure now
48 | List protocols = new LinkedList<>();
49 | for (String protocol : socket.getSupportedProtocols())
50 | if (!protocol.toUpperCase(Locale.US).contains("SSL")) {
51 | protocols.add(protocol);
52 | }
53 | SSLSocketFactoryCompat.protocols = protocols.toArray(new String[protocols.size()]);
54 |
55 | /* set up reasonable cipher suites */
56 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
57 | // choose known secure cipher suites
58 | List allowedCiphers = Arrays.asList(
59 | // TLS 1.2
60 | "TLS_RSA_WITH_AES_256_GCM_SHA384",
61 | "TLS_RSA_WITH_AES_128_GCM_SHA256",
62 | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
63 | "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
64 | "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
65 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
66 | "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
67 | // maximum interoperability
68 | "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
69 | "TLS_RSA_WITH_AES_128_CBC_SHA",
70 | // additionally
71 | "TLS_RSA_WITH_AES_256_CBC_SHA",
72 | "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
73 | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
74 | "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
75 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");
76 | List availableCiphers = Arrays.asList(socket.getSupportedCipherSuites());
77 |
78 | // take all allowed ciphers that are available and put them into preferredCiphers
79 | HashSet preferredCiphers = new HashSet<>(allowedCiphers);
80 | preferredCiphers.retainAll(availableCiphers);
81 |
82 | /* For maximum security, preferredCiphers should *replace* enabled ciphers (thus disabling
83 | * ciphers which are enabled by default, but have become unsecure), but I guess for
84 | * the security level of DAVdroid and maximum compatibility, disabling of insecure
85 | * ciphers should be a server-side task */
86 |
87 | // add preferred ciphers to enabled ciphers
88 | HashSet enabledCiphers = preferredCiphers;
89 | enabledCiphers.addAll(new HashSet<>(Arrays.asList(socket.getEnabledCipherSuites())));
90 |
91 | SSLSocketFactoryCompat.cipherSuites =
92 | enabledCiphers.toArray(new String[enabledCiphers.size()]);
93 | }
94 | }
95 | } catch (IOException e) {
96 | Log.e("SSLSocketFactoryCompat", "Couldn't determine default TLS settings");
97 | e.printStackTrace();
98 | }
99 | }
100 |
101 | public SSLSocketFactoryCompat(@NonNull X509TrustManager trustManager) {
102 | try {
103 | SSLContext sslContext = SSLContext.getInstance("TLS");
104 | sslContext.init(null, new X509TrustManager[] {trustManager}, null);
105 | delegate = sslContext.getSocketFactory();
106 | } catch (GeneralSecurityException e) {
107 | throw new AssertionError(); // The system has no TLS. Just give up.
108 | }
109 | }
110 |
111 | private void upgradeTLS(SSLSocket ssl) {
112 | if (protocols != null) {
113 | ssl.setEnabledProtocols(protocols);
114 | }
115 |
116 | if (cipherSuites != null) {
117 | ssl.setEnabledCipherSuites(cipherSuites);
118 | }
119 | }
120 |
121 |
122 | @Override
123 | public String[] getDefaultCipherSuites() {
124 | return cipherSuites;
125 | }
126 |
127 | @Override
128 | public String[] getSupportedCipherSuites() {
129 | return cipherSuites;
130 | }
131 |
132 | @Override
133 | public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
134 | Socket ssl = delegate.createSocket(s, host, port, autoClose);
135 | if (ssl instanceof SSLSocket) {
136 | upgradeTLS((SSLSocket) ssl);
137 | }
138 | return ssl;
139 | }
140 |
141 | @Override
142 | public Socket createSocket(String host, int port) throws IOException {
143 | Socket ssl = delegate.createSocket(host, port);
144 | if (ssl instanceof SSLSocket) {
145 | upgradeTLS((SSLSocket) ssl);
146 | }
147 | return ssl;
148 | }
149 |
150 | @Override
151 | public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
152 | Socket ssl = delegate.createSocket(host, port, localHost, localPort);
153 | if (ssl instanceof SSLSocket) {
154 | upgradeTLS((SSLSocket) ssl);
155 | }
156 | return ssl;
157 | }
158 |
159 | @Override
160 | public Socket createSocket(InetAddress host, int port) throws IOException {
161 | Socket ssl = delegate.createSocket(host, port);
162 | if (ssl instanceof SSLSocket) {
163 | upgradeTLS((SSLSocket) ssl);
164 | }
165 | return ssl;
166 | }
167 |
168 | @Override
169 | public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
170 | throws IOException {
171 | Socket ssl = delegate.createSocket(address, port, localAddress, localPort);
172 | if (ssl instanceof SSLSocket) {
173 | upgradeTLS((SSLSocket) ssl);
174 | }
175 | return ssl;
176 | }
177 | }
178 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/java/com/sixthsolution/lpisyncadapter/util/Util.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter.util;
2 |
3 |
4 | import com.sixthsolution.lpisyncadapter.BuildConfig;
5 |
6 | import okhttp3.logging.HttpLoggingInterceptor;
7 |
8 | /**
9 | * @author mehdok (mehdok@gmail.com) on 4/5/2017.
10 | */
11 |
12 | public class Util {
13 | public static HttpLoggingInterceptor getHttpLogger() {
14 | HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
15 | if (BuildConfig.DEBUG) {
16 | logging.setLevel(HttpLoggingInterceptor.Level.BODY);
17 | } else {
18 | logging.setLevel(HttpLoggingInterceptor.Level.NONE);
19 | }
20 |
21 | return logging;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/lombok.config:
--------------------------------------------------------------------------------
1 | lombok.addGeneratedAnnotation = false
2 | lombok.anyConstructor.suppressConstructorProperties = true
3 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/res/drawable/ical_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/easy-apple-syncadapter/src/main/res/drawable/ical_icon.png
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/res/layout/activity_login.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
18 |
19 |
26 |
27 |
33 |
34 |
43 |
44 |
49 |
50 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | IcalAuthenticatorLib
3 | Sixthsolution CalDav
4 |
5 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/res/xml/authenticator.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/main/res/xml/sync_adapter.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
--------------------------------------------------------------------------------
/easy-apple-syncadapter/src/test/java/com/sixthsolution/lpisyncadapter/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sixthsolution.lpisyncadapter;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.assertEquals;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/6thsolution/EasyAppleSyncAdapter/f23cef6304880701962c27b726be04ad763c5aeb/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 19 08:27:41 IRDT 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':easy-apple-syncadapter'
2 |
--------------------------------------------------------------------------------