package com.mn.keeplive.account;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;

public class AccountHelper {
    private static final  String ACCOUNT_TYPE = "com.mn.keeplive.account";

    //添加账户到系统account中去
    public static void addAccount(Context context){
        AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
        Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
        if(accounts.length>0){
            //已存在，就不需要添加
            return;
        }
        //这里只是添加了账户，没有同步账户
        Account account=new Account("mn",ACCOUNT_TYPE);
        accountManager.addAccountExplicitly(account,"mn",new Bundle());
    }

    //真正去同步账户列表
    public static void autoSync(){
        //进程间通信
        Account account = new Account("mn",ACCOUNT_TYPE);
        //告诉账户去同步
        ContentResolver.setIsSyncable(account,"com.mn.keeplive.account.SyncProvider",1);
        //告诉系统账户进程是否自动同步
        ContentResolver.setSyncAutomatically(account,"com.mn.keeplive.account.SyncProvider",true);
        //去排队，增加到同步队列
        ContentResolver.addPeriodicSync(account,"com.mn.keeplive.account.SyncProvider",new Bundle(),1);

    }
}
