Home | History | Annotate | Download | only in syncadapter
      1 /*
      2  * Copyright 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.sampletvinput.syncadapter;
     18 
     19 import android.accounts.Account;
     20 import android.accounts.AccountManager;
     21 import android.content.ContentResolver;
     22 import android.content.Context;
     23 import android.media.tv.TvContract;
     24 import android.os.Bundle;
     25 import android.util.Log;
     26 
     27 /**
     28  * Static helper methods for working with the SyncAdapter framework.
     29  */
     30 public class SyncUtils {
     31     private static final String TAG = "SyncUtils";
     32     private static final String CONTENT_AUTHORITY = TvContract.AUTHORITY;
     33     public static final String ACCOUNT_TYPE = "com.example.android.sampletvinput.account";
     34 
     35     public static void setUpPeriodicSync(Context context, String inputId) {
     36         Account account = DummyAccountService.getAccount(ACCOUNT_TYPE);
     37         AccountManager accountManager =
     38                 (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
     39         if (!accountManager.addAccountExplicitly(account, null, null)) {
     40             Log.e(TAG, "Account already exists.");
     41         }
     42         ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
     43         ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
     44         Bundle bundle = new Bundle();
     45         bundle.putString(SyncAdapter.BUNDLE_KEY_INPUT_ID, inputId);
     46         ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, bundle,
     47                 SyncAdapter.SYNC_FREQUENCY_SEC);
     48     }
     49 
     50     public static void requestSync(String inputId) {
     51         Bundle bundle = new Bundle();
     52         bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
     53         bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
     54         bundle.putString(SyncAdapter.BUNDLE_KEY_INPUT_ID, inputId);
     55         ContentResolver.requestSync(DummyAccountService.getAccount(ACCOUNT_TYPE), CONTENT_AUTHORITY,
     56                 bundle);
     57     }
     58 }
     59