Sync Adapter 프레임워크에서 인증기의 역할 이해하기
Android 앱 개발에서 백그라운드 데이터 동기화를 구현할 때 Sync Adapter 프레임워크는 강력한 도구입니다. 이 프레임워크는 각 Sync Adapter가 반드시 인증기(Authenticator) 컴포넌트와 연결되어야 하며, 앱에서 실제로 사용자 인증이 필요하지 않더라도 요구됩니다.
Stub 인증기가 왜 필요한가
Sync Adapter 프레임워크는 모든 동기화 작업에 계정 인증이 필요하다고 가정하므로 개발자에게 인증기 컴포넌트 제공을 강제합니다. 다음과 같은 상황에서 Stub 인증기가 유용합니다:
- 사용자 로그인이 불필요한 앱
- 계정 시스템이 없는 앱
- 단순한 백그라운드 데이터 동기화만 필요한 앱
Stub(스텁) 인증기는 필수 메서드를 구현하지만 실제 기능을 수행하지 않는 가짜 구현체입니다.
Stub 인증기 클래스 구현하기
AbstractAccountAuthenticator를 상속받는 클래스를 만들고 모든 추상 메서드를 재정의합니다:
public class StubAuthenticator extends AbstractAccountAuthenticator {
public StubAuthenticator(Context context) {
super(context);
}
@Override
public Bundle editProperties(AccountAuthenticatorResponse response,
String accountType) {
throw new UnsupportedOperationException("인증서 속성 편집은 지원되지 않습니다");
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response,
String accountType,
String authTokenType,
String[] requiredFeatures,
Bundle options) {
return null; // 계정 추가 기능 비활성화
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response,
Account account,
Bundle options) {
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response,
Account account,
String authTokenType,
Bundle options) {
throw new UnsupportedOperationException("인증 토큰 발급은 지원되지 않습니다");
}
@Override
public String getAuthTokenLabel(String authTokenType) {
return null;
}
@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response,
Account account,
String[] features) {
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response,
Account account,
String authTokenType,
Bundle options) {
return null;
}
@Override
public Bundle isCredentialsUpdateSuggested(AccountAuthenticatorResponse response,
Account account,
String authTokenType) {
return null;
}
}
핵심 포인트:
- 모든 메서드는 실제 동작 없이 구현됨
addAccount()가null을 반환하면 계정 추가 UI가 표시되지 않음- 일부 메서드는
UnsupportedOperationException을 던져 해당 기능이 없음을 명시
바인딩 서비스 생성하기
인증기는 시스템에 서비스로 노출되어야 합니다:
public class AuthBridgeService extends Service {
private StubAuthenticator authenticatorInstance;
@Override
public void onCreate() {
super.onCreate();
authenticatorInstance = new StubAuthenticator(this);
}
@Override
public IBinder onBind(Intent intent) {
return authenticatorInstance.getIBinder();
}
}
서비스의 역할:
onCreate()에서 인증기 인스턴스를 초기화onBind()를 통해 인증기의IBinder를 반환- 시스템과 인증기 사이의 통신 채널 역할 수행
인증기 메타데이터 설정하기
res/xml/authenticator.xml에 인증기 메타데이터를 정의합니다:
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.example.myapp.sync"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher_small"
android:label="@string/app_name"/>
속성 설명:
accountType: 고유 식별자, 일반적으로 패키지명 사용icon및smallIcon: 시스템 설정 화면에서 표시될 아이콘label: 시스템 설정 화면에서 표시될 이름
Manifest 파일에 서비스 등록하기
AndroidManifest.xml에서 서비스를 선언합니다:
<service
android:name=".AuthBridgeService"
android:exported="true">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator"/>
</service>
중요 사항:
android.accounts.AccountAuthenticator액션에 대한 인텐트 필터 필수meta-data를 통해 인증기 설정 파일 연결android:exported="true"로 설정하여 시스템이 서비스에 접근 가능