/*
 * Copyright (C) 2012 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.accounts;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.fragment.AddAccountFragment;
import com.btmura.android.reddit.fragment.AddAccountFragment.OnAccountAddedListener;

public class AccountAuthenticatorActivity extends android.accounts.AccountAuthenticatorActivity
        implements OnAccountAddedListener {

    public static final String TAG = "AccountAuthenticatorActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_authenticator);

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.single_container, AddAccountFragment.newInstance());
        ft.commit();
    }

    public void onAccountAdded(String login, String cookie, String modhash) {
        Log.d(TAG, "onAccountAdded");

        Account account = new Account(login, getString(R.string.account_type));
        AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
        manager.addAccountExplicitly(account, cookie, null);

        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, login);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.account_type));
        setAccountAuthenticatorResult(result);
        setResult(RESULT_OK);
        finish();
    }
}

@@ -1203,16 +1203,143 @@
 vity%22;%0A%0A
+    private static final String AUTH_TOKEN_COOKIE = %22cookie%22;%0A    private static final String AUTH_TOKEN_MODHASH = %22modhash%22;%0A%0A
     @Ove
@@ -1794,24 +1794,87 @@
 ntAdded%22);%0A%0A
+        String accountType = getString(R.string.account_type);%0A
         Acco
@@ -1910,42 +1910,22 @@
 in, 
-getString(R.string.
 account
-_t
+T
 ype)
-)
 ;
+%0A
 %0A   
@@ -1954,17 +1954,16 @@
 nager = 
-(
 AccountM
@@ -1972,42 +1972,17 @@
 ager
-) getSystemService(ACCOUNT_SERVICE
+.get(this
 );%0A 
@@ -2030,20 +2030,152 @@
 nt, 
-cookie, null
+null, null);%0A        manager.setAuthToken(account, AUTH_TOKEN_COOKIE, cookie);%0A        manager.setAuthToken(account, AUTH_TOKEN_MODHASH, modhash
 );%0A%0A
@@ -2340,41 +2340,20 @@
 PE, 
-getString(R.string.
 account
-_t
+T
 ype)
-)
 ;%0A  
@@ -2397,16 +2397,17 @@
 esult);%0A
+%0A
         

Save cookie and modhash as separate auth tokens