
/*
 * Copyright 2014 Uwe Trottmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.battlelancer.seriesguide.sync;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import com.battlelancer.seriesguide.BuildConfig;
import com.battlelancer.seriesguide.SeriesGuideApplication;
import timber.log.Timber;

public class AccountUtils {

    public static final int SYNC_FREQUENCY = 24 * 60 * 60; // 1 day (in seconds)

    private static final String ACCOUNT_NAME = "SeriesGuide Sync";

    private static final String ACCOUNT_TYPE = BuildConfig.APPLICATION_ID;

    public static void createAccount(Context context) {
        Timber.d("Setting up account...");

        // remove any existing accounts
        removeAccount(context);

        // try to create a new account
        AccountManager manager = AccountManager.get(context);
        Account account = new Account(ACCOUNT_NAME, ACCOUNT_TYPE);

        boolean isNewAccountAdded;
        try {
            isNewAccountAdded = manager != null
                    && manager.addAccountExplicitly(account, null, null);
        } catch (SecurityException e) {
            Timber.e(e, "Setting up account...FAILED Account could not be added");
            return;
        }
        if (isNewAccountAdded) {
            // Inform the system that this account supports sync
            ContentResolver.setIsSyncable(account, SeriesGuideApplication.CONTENT_AUTHORITY, 1);
            // Inform the system that this account is eligible for auto sync
            // when the network is up
            ContentResolver.setSyncAutomatically(account, SeriesGuideApplication.CONTENT_AUTHORITY,
                    true);
            // Recommend a schedule for automatic synchronization. The system
            // may modify this based
            // on other scheduled syncs and network utilization.
            ContentResolver.addPeriodicSync(account, SeriesGuideApplication.CONTENT_AUTHORITY,
                    new Bundle(), SYNC_FREQUENCY);
        }

        Timber.d("Setting up account...DONE");
    }

    private static void removeAccount(Context context) {
        Timber.d("Removing existing accounts...");

        AccountManager manager = AccountManager.get(context);
        Account[] accounts = manager.getAccountsByType(ACCOUNT_TYPE);
        for (Account account : accounts) {
            manager.removeAccount(account, null, null);
        }

        Timber.d("Removing existing accounts...DONE");
    }

    public static boolean isAccountExists(Context context) {
        AccountManager manager = AccountManager.get(context);
        Account[] accounts = manager.getAccountsByType(ACCOUNT_TYPE);
        return accounts.length > 0;
    }

    public static Account getAccount(Context context) {
        AccountManager manager = AccountManager.get(context);
        Account[] accounts = manager.getAccountsByType(ACCOUNT_TYPE);

        // return first available account
        if (accounts.length > 0) {
            return accounts[0];
        }

        return null;
    }
}

@@ -706,24 +706,61 @@
 untManager;%0A
+import android.annotation.TargetApi;%0A
 import andro
@@ -783,24 +783,24 @@
 ntResolver;%0A
-
 import andro
@@ -819,16 +819,41 @@
 ontext;%0A
+import android.os.Build;%0A
 import a
@@ -919,16 +919,16 @@
 Config;%0A
-
 import c
@@ -979,16 +979,67 @@
 cation;%0A
+import com.uwetrottmann.androidutils.AndroidUtils;%0A
 import t
@@ -2802,24 +2802,73 @@
 E%22);%0A    %7D%0A%0A
+    @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)%0A
     private 
@@ -2904,32 +2904,32 @@
 text context) %7B%0A
-
         Timber.d
@@ -3135,24 +3135,214 @@
 accounts) %7B%0A
+            if (AndroidUtils.isLollipopMR1OrHigher()) %7B%0A                manager.removeAccount(account, null, null, null);%0A            %7D else %7B%0A                //noinspection deprecation%0A    
             
@@ -3377,32 +3377,46 @@
 t, null, null);%0A
+            %7D%0A
         %7D%0A%0A     

Use new method for account removal (L_MR1 though).