1 /*
2  * Copyright 2013 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.common.accounts;
18 
19 import android.accounts.AbstractAccountAuthenticator;
20 import android.accounts.Account;
21 import android.accounts.AccountAuthenticatorResponse;
22 import android.accounts.NetworkErrorException;
23 import android.app.Service;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.os.IBinder;
28 import android.util.Log;
29 
30 public class GenericAccountService extends Service {
31     private static final String TAG = "GenericAccountService";
32     public static final String ACCOUNT_NAME = "Account";
33     private Authenticator mAuthenticator;
34 
35     /**
36      * Obtain a handle to the {@link android.accounts.Account} used for sync in this application.
37      *
38      * <p>It is important that the accountType specified here matches the value in your sync adapter
39      * configuration XML file for android.accounts.AccountAuthenticator (often saved in
40      * res/xml/syncadapter.xml). If this is not set correctly, you'll receive an error indicating
41      * that "caller uid XXXXX is different than the authenticator's uid".
42      *
43      * @param accountType AccountType defined in the configuration XML file for
44      *                    android.accounts.AccountAuthenticator (e.g. res/xml/syncadapter.xml).
45      * @return Handle to application's account (not guaranteed to resolve unless CreateSyncAccount()
46      *         has been called)
47      */
GetAccount(String accountType)48     public static Account GetAccount(String accountType) {
49         // Note: Normally the account name is set to the user's identity (username or email
50         // address). However, since we aren't actually using any user accounts, it makes more sense
51         // to use a generic string in this case.
52         //
53         // This string should *not* be localized. If the user switches locale, we would not be
54         // able to locate the old account, and may erroneously register multiple accounts.
55         final String accountName = ACCOUNT_NAME;
56         return new Account(accountName, accountType);
57     }
58 
59     @Override
onCreate()60     public void onCreate() {
61         Log.i(TAG, "Service created");
62         mAuthenticator = new Authenticator(this);
63     }
64 
65     @Override
onDestroy()66     public void onDestroy() {
67         Log.i(TAG, "Service destroyed");
68     }
69 
70     @Override
onBind(Intent intent)71     public IBinder onBind(Intent intent) {
72         return mAuthenticator.getIBinder();
73     }
74 
75     public class Authenticator extends AbstractAccountAuthenticator {
Authenticator(Context context)76         public Authenticator(Context context) {
77             super(context);
78         }
79 
80         @Override
editProperties(AccountAuthenticatorResponse accountAuthenticatorResponse, String s)81         public Bundle editProperties(AccountAuthenticatorResponse accountAuthenticatorResponse,
82                                      String s) {
83             throw new UnsupportedOperationException();
84         }
85 
86         @Override
addAccount(AccountAuthenticatorResponse accountAuthenticatorResponse, String s, String s2, String[] strings, Bundle bundle)87         public Bundle addAccount(AccountAuthenticatorResponse accountAuthenticatorResponse,
88                                  String s, String s2, String[] strings, Bundle bundle)
89                 throws NetworkErrorException {
90             return null;
91         }
92 
93         @Override
confirmCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, Bundle bundle)94         public Bundle confirmCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse,
95                                          Account account, Bundle bundle)
96                 throws NetworkErrorException {
97             return null;
98         }
99 
100         @Override
getAuthToken(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle)101         public Bundle getAuthToken(AccountAuthenticatorResponse accountAuthenticatorResponse,
102                                    Account account, String s, Bundle bundle)
103                 throws NetworkErrorException {
104             throw new UnsupportedOperationException();
105         }
106 
107         @Override
getAuthTokenLabel(String s)108         public String getAuthTokenLabel(String s) {
109             throw new UnsupportedOperationException();
110         }
111 
112         @Override
updateCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle)113         public Bundle updateCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse,
114                                         Account account, String s, Bundle bundle)
115                 throws NetworkErrorException {
116             throw new UnsupportedOperationException();
117         }
118 
119         @Override
hasFeatures(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String[] strings)120         public Bundle hasFeatures(AccountAuthenticatorResponse accountAuthenticatorResponse,
121                                   Account account, String[] strings)
122                 throws NetworkErrorException {
123             throw new UnsupportedOperationException();
124         }
125     }
126 
127 }
128 
129