<commit_before>package by.istin.android.xcore.source.sync.helper;

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

import by.istin.android.xcore.XCoreHelper;
import by.istin.android.xcore.utils.AppUtils;


public class SyncHelper implements XCoreHelper.IAppServiceKey {
	
	public static final String ACCOUNT_TYPE = ".account";
    public static final String APP_SERVICE_KEY = "xcore:synchelper";

    private String mAccountName;

    private long mPeriod;

    private Context mContext;

    private String mModelContentProviderAuthority;

    public static SyncHelper get(Context context) {
        return (SyncHelper) AppUtils.get(context, APP_SERVICE_KEY);
    }

    public SyncHelper(Context context, String accountName, long period, String modelContentProviderAuthority) {
        mAccountName = accountName;
        mPeriod = period;
        mContext = context;
        mModelContentProviderAuthority = modelContentProviderAuthority;
    }

	public void addSyncAccount(){
		AccountManager am = AccountManager
				.get(mContext);
		Account account = new Account(mAccountName, mContext.getPackageName()+ACCOUNT_TYPE);
		Bundle params = new Bundle();
		params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
		ContentResolver.setSyncAutomatically(account,
                mModelContentProviderAuthority, true);
		ContentResolver.addPeriodicSync(account, mModelContentProviderAuthority,
				params, mPeriod);
		ContentResolver.setIsSyncable(account, mModelContentProviderAuthority, 1);
		am.addAccountExplicitly(account, null, null);
	}
	
	public void removeSyncAccount(){
		AccountManager am = AccountManager
				.get(mContext);
		Account account = new Account(mAccountName,
                mContext.getPackageName()+ACCOUNT_TYPE);
		Bundle params = new Bundle();
		params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
		ContentResolver.removePeriodicSync(account, mModelContentProviderAuthority,
				params);
		ContentResolver.setIsSyncable(account, mModelContentProviderAuthority, 0);
		am.addAccountExplicitly(account, null, null);

	}
	
	public void removeAccount(){
		AccountManager am = AccountManager
				.get(mContext);
		Account account = new Account(mAccountName,<commit_msg>SecurityException fixed on rooted devices<commit_after>package by.istin.android.xcore.source.sync.helper;

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

import by.istin.android.xcore.XCoreHelper;
import by.istin.android.xcore.utils.AppUtils;


public class SyncHelper implements XCoreHelper.IAppServiceKey {
	
	public static final String ACCOUNT_TYPE = ".account";
    public static final String APP_SERVICE_KEY = "xcore:synchelper";

    private String mAccountName;

    private long mPeriod;

    private Context mContext;

    private String mModelContentProviderAuthority;

    public static SyncHelper get(Context context) {
        return (SyncHelper) AppUtils.get(context, APP_SERVICE_KEY);
    }

    public SyncHelper(Context context, String accountName, long period, String modelContentProviderAuthority) {
        mAccountName = accountName;
        mPeriod = period;
        mContext = context;
        mModelContentProviderAuthority = modelContentProviderAuthority;
    }

	public void addSyncAccount(){
        Context applicationContext = mContext.getApplicationContext();
        AccountManager am = AccountManager
				.get(applicationContext);
		Account account = new Account(mAccountName, applicationContext.getPackageName()+ACCOUNT_TYPE);
		Bundle params = new Bundle();
		params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
		ContentResolver.setSyncAutomatically(account,
                mModelContentProviderAuthority, true);
		ContentResolver.addPeriodicSync(account, mModelContentProviderAuthority,
				params, mPeriod);
		ContentResolver.setIsSyncable(account, mModelContentProviderAuthority, 1);
        try {
		    am.addAccountExplicitly(account, null, null);
        } catch (SecurityException ex) {
            //TODO handle
        }
	}
	
	public void removeSyncAccount(){
        Context applicationContext = mContext.getApplicationContext();
        AccountManager am = AccountManager
				.get(applicationContext);
		Account account = new Account(mAccountName,
                applicationContext.getPackageName()+ACCOUNT_TYPE);
		Bundle params = new Bundle();
		params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
		params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
		ContentResolver.removePeriodicSync(account, mModelContentProviderAuthority,
				params);
		ContentResolver.setIsSyncable(account, mModelContentProviderAuthority, 0);
        try {
		    am.addAccountExplicitly(account, null, null);
        } catch (SecurityException ex) {
            //TODO handle
        }

	}
	
	public void removeAccount(){
		AccountManager am = AccountManager
				.get(mContext);
		Account account = new Account(mAccountName,
{'fraction_non_alphanumeric': 0.0800711743772242, 'fraction_numerical': 0.0007117437722419929, 'mean_word_length': 4.765128205128205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6744283', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1551, 'n_words': 317}