Giter VIP home page Giter VIP logo

easyapplesyncadapter's Introduction

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
allprojects {
    repositories {
        jcenter()
        maven { url "http://dl.bintray.com/6thsolution/public-maven" }
    }
}

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

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

Authenticator config : Add Authenticator service to your manifest:

<service
    android:name="com.sixthsolution.lpisyncadapter.authenticator.ICalAuthenticatorService"
    android:exported="false"
    >
    <intent-filter>
        <action android:name="android.accounts.AccountAuthenticator"/>
    </intent-filter>

    <meta-data
        android:name="android.accounts.AccountAuthenticator"
        android:resource="@xml/authenticator"
        />
    <meta-data
        android:name="login_activity_class"
        android:value="com.sixthsolution.applecalendar.CustomLoginActivity"
        />
    <meta-data
        android:name="unique_authentication_type"
        android:value="com.sixthsolution.lpisyncadapter.ical_access"
        />
</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:
<account-authenticator 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="com.sixthsolution.lpisyncadapter.ical_access"
    android:icon="@drawable/ical_icon"
    android:smallIcon="@drawable/ical_icon"
    android:label="@string/caldav_authenticator">
</account-authenticator>
  1. 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.

  2. 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:

<service
    android:name="com.sixthsolution.lpisyncadapter.syncadapter.ICalSyncService"
    android:exported="true"
    >
    <intent-filter>
        <action android:name="android.content.SyncAdapter"/>
    </intent-filter>
    <meta-data
        android:name="android.content.SyncAdapter"
        android:resource="@xml/sync_adapter"
    />
</service>
  1. sync_adapter file is something like this:
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.android.calendar"
    android:accountType="com.sixthsolution.lpisyncadapter.ical_access"
    android:userVisible="true"
    android:allowParallelSyncs="false"
    android:isAlwaysSyncable="true"
    android:supportsUploading="true">
</sync-adapter>
  1. android:contentAuthority="com.android.calendar" always must be it. (do NOT change it except when you need to).
  2. 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.
accountManager.addAccount(AUTHTOKEN_TYPE_FULL_ACCESS, AUTHTOKEN_TYPE_FULL_ACCESS, null, null, this,
                                          new AccountManagerCallback<Bundle>() {
                                              @Override
                                              public void run(AccountManagerFuture<Bundle> future) {
                                                  try {
                                                      Bundle bnd = future.getResult();
                                                  } catch (Exception e) {
                                                      e.printStackTrace();
                                                  }
                                              }
                                          }, null);
  • Get list of available accounts:
AccountManager accountManager = AccountManager.get(this);
Account availableAccounts[] = accountManager.getAccountsByType(AUTHTOKEN_TYPE_FULL_ACCESS);
  • Requesting manual sync:
Bundle params = new Bundle();
params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
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 :
ContentObserver contentObserver = new ContentObserver(new Handler()) {
    @Override
    public void onChange(boolean selfChange, Uri uri) {
    super.onChange(selfChange, uri);
    showMessage("Sync Finished");
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getContentResolver().registerContentObserver(GlobalConstant.CONTENT_URI, true, contentObserver);
}

do NOT forget to unregister it:

@Override
protected void onDestroy() {
    super.onDestroy();
    getContentResolver().unregisterContentObserver(contentObserver);
}
  • Get list of Calendars associated with account:
LocalCalendar[] localCalendars = (LocalCalendar[]) LocalCalendar.find(account,
                                                         // get contentProviderClient for your authority
                                                         getContentResolver().acquireContentProviderClient(GlobalConstant.AUTHORITY),
                                                         LocalCalendar.Factory.INSTANCE,
                                                         null,
                                                         null);
  • Get list of events associated with LocalCalendar: You can get list of all resource by:
LocalResource[] localResources = localCalendar.getAll();

Then cast if to LocalEvent:

(LocalEvent) localResources[i]

License

Copyright 2016-2017 6thSolution Technologies Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

easyapplesyncadapter's People

Contributors

mehdok avatar

Watchers

James Cloos avatar ezTask.io avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.