项目作者: 6thsolution

项目描述 :
Easy Apple Sync Adapter, an Android Library for syncing with apple calendar service.
高级语言: Java
项目地址: git://github.com/6thsolution/EasyAppleSyncAdapter.git
创建时间: 2017-04-19T05:07:40Z
项目社区:https://github.com/6thsolution/EasyAppleSyncAdapter

开源协议:Apache License 2.0

下载


Easy Apple Sync Adapter is an Android Library for syncing with apple calendar service.

Performing authentication and full duplex sync with apple caldav server.

This library is based on DavDroid, and borrows many code from them.
we just simplify the process of reusing the library

Features

  • Easy to use.
  • Powerful encryption for passwords.

Installation

1) Configure your top-level build.gradle to include our repository

  1. allprojects {
  2. repositories {
  3. jcenter()
  4. maven { url "http://dl.bintray.com/6thsolution/public-maven" }
  5. }
  6. }

Then config your app-level build.gradle to include the library as dependency:

  1. compile 'com.sixthsolution:easyapplesyncadapter:1.0.0-beta1'

2) config:

Authenticator config : Add Authenticator service to your manifest:

  1. <service
  2. android:name="com.sixthsolution.lpisyncadapter.authenticator.ICalAuthenticatorService"
  3. android:exported="false"
  4. >
  5. <intent-filter>
  6. <action android:name="android.accounts.AccountAuthenticator"></action>
  7. </intent-filter>
  8. <meta-data
  9. android:name="android.accounts.AccountAuthenticator"
  10. android:resource="@xml/authenticator"
  11. />
  12. <meta-data
  13. android:name="login_activity_class"
  14. android:value="com.sixthsolution.applecalendar.CustomLoginActivity"
  15. />
  16. <meta-data
  17. android:name="unique_authentication_type"
  18. android:value="com.sixthsolution.lpisyncadapter.ical_access"
  19. />
  20. </service>

There is a few metadata that you can pass your parameters to the service via them:

1) android.accounts.AccountAuthenticator
The authenticator config file, something like this:

  1. <account-authenticator
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:accountType="com.sixthsolution.lpisyncadapter.ical_access"
  4. android:icon="@drawable/ical_icon"
  5. android:smallIcon="@drawable/ical_icon"
  6. android:label="@string/caldav_authenticator">
  7. </account-authenticator>

2) android:accountType parameter must be the same value you passed via unique_authentication_type metadata.
this must be unique, otherwise your app may not work. so use something package specific.

3) login_activity_class
You can pass a custom login activity with custom ui for login process.
The value must be complete path with package name like: com.sixthsolution.applecalendar.CustomLoginActivity.
Your activity must extend BaseLoginActivity and your layout must have some view that is necessary for login process.
two EditText with exact id as user_name and password . and one Button with id signin_button and
After calling setContentView() in onCreate() you must call init().

SyncAdapter config :
You also need to add sync adapter service to your manifest:

  1. <service
  2. android:name="com.sixthsolution.lpisyncadapter.syncadapter.ICalSyncService"
  3. android:exported="true"
  4. >
  5. <intent-filter>
  6. <action android:name="android.content.SyncAdapter"></action>
  7. </intent-filter>
  8. <meta-data
  9. android:name="android.content.SyncAdapter"
  10. android:resource="@xml/sync_adapter"
  11. />
  12. </service>

1) sync_adapter file is something like this:

  1. <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:contentAuthority="com.android.calendar"
  3. android:accountType="com.sixthsolution.lpisyncadapter.ical_access"
  4. android:userVisible="true"
  5. android:allowParallelSyncs="false"
  6. android:isAlwaysSyncable="true"
  7. android:supportsUploading="true">
  8. </sync-adapter>

2) android:contentAuthority="com.android.calendar" always must be it. (do NOT change it except when you need to).
3) android:accountType must be the same value you set for authenticator config.

Usage

  • Add new account:
    You must call AccountManager#addAccount for adding new account. it automatically you the config and open
    LoginActivity and handle add progress by itself.
    1. accountManager.addAccount(AUTHTOKEN_TYPE_FULL_ACCESS, AUTHTOKEN_TYPE_FULL_ACCESS, null, null, this,
    2. new AccountManagerCallback<Bundle>() {
    3. @Override
    4. public void run(AccountManagerFuture<Bundle> future) {
    5. try {
    6. Bundle bnd = future.getResult();
    7. } catch (Exception e) {
    8. e.printStackTrace();
    9. }
    10. }
    11. }, null);
  • Get list of available accounts:
    1. AccountManager accountManager = AccountManager.get(this);
    2. Account availableAccounts[] = accountManager.getAccountsByType(AUTHTOKEN_TYPE_FULL_ACCESS);
  • Requesting manual sync:
    1. Bundle params = new Bundle();
    2. params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    3. params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    4. ContentResolver.requestSync(account, GlobalConstant.AUTHORITY, params);
  • Listening for sync finish:
    if you wana know when the sync finish or show progress during progress use a ContentObserver :
    1. ContentObserver contentObserver = new ContentObserver(new Handler()) {
    2. @Override
    3. public void onChange(boolean selfChange, Uri uri) {
    4. super.onChange(selfChange, uri);
    5. showMessage("Sync Finished");
    6. }
    7. };
    1. @Override
    2. protected void onCreate(Bundle savedInstanceState) {
    3. super.onCreate(savedInstanceState);
    4. setContentView(R.layout.activity_main);
    5. getContentResolver().registerContentObserver(GlobalConstant.CONTENT_URI, true, contentObserver);
    6. }
    do NOT forget to unregister it:
    1. @Override
    2. protected void onDestroy() {
    3. super.onDestroy();
    4. getContentResolver().unregisterContentObserver(contentObserver);
    5. }
  • Get list of Calendars associated with account:
    1. LocalCalendar[] localCalendars = (LocalCalendar[]) LocalCalendar.find(account,
    2. // get contentProviderClient for your authority
    3. getContentResolver().acquireContentProviderClient(GlobalConstant.AUTHORITY),
    4. LocalCalendar.Factory.INSTANCE,
    5. null,
    6. null);
  • Get list of events associated with LocalCalendar:
    You can get list of all resource by:
    1. LocalResource[] localResources = localCalendar.getAll();
    Then cast if to LocalEvent:
    1. (LocalEvent) localResources[i]

License

  1. Copyright 2016-2017 6thSolution Technologies Inc.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.