/*
 * 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.activity;

import java.util.List;

import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.btmura.android.reddit.Debug;
import com.btmura.android.reddit.R;
import com.btmura.android.reddit.content.AccountLoader;
import com.btmura.android.reddit.content.AccountLoader.AccountResult;
import com.btmura.android.reddit.fragment.AccountPreferenceFragment;
import com.btmura.android.reddit.provider.SubredditProvider;

public class SettingsActivity extends PreferenceActivity implements LoaderCallbacks<AccountResult> {

    public static final String TAG = "SettingsActivity";
    public static final boolean DEBUG = Debug.DEBUG;

    private static final String[] AUTHORITIES = {
            SubredditProvider.AUTHORITY,
    };

    private AccountResult result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLoaderManager().initLoader(0, null, this);
    }

    public Loader<AccountResult> onCreateLoader(int id, Bundle args) {
        return new AccountLoader(this, false);
    }

    public void onLoadFinished(Loader<AccountResult> loader, AccountResult result) {
        this.result = result;
        invalidateHeaders();
    }

    public void onLoaderReset(Loader<AccountResult> loader) {
        this.result = null;
        invalidateHeaders();
    }

    @Override
    public void onBuildHeaders(List<Header> target) {
        if (DEBUG) {
            Log.d(TAG, "onBuildHeaders");
        }
        loadHeadersFromResource(R.xml.settings_headers, target);
        if (result != null) {
            String[] accountNames = result.accountNames;
            int length = accountNames.length;
            for (int i = 0; i < length; i++) {
                Header header = new Header();
                header.title = accountNames[i];
                header.fragment = AccountPreferenceFragment.class.getName();

                Bundle args = new Bundle(1);
                args.putString(AccountPreferenceFragment.ARG_ACCOUNT_NAME, accountNames[i]);
                header.fragmentArguments = args;
                target.add(header);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.settings_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_account:
                handleAddAccount();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void handleAddAccount() {
        Intent intent = new Intent(Settings.ACTION_ADD_ACCOUNT);
        intent.putExtra(Settings.EXTRA_AUTHORITIES, AUTHORITIES);
        startActivity(intent);
    }
}

@@ -667,16 +667,46 @@
 .List;%0A%0A
+import android.app.ActionBar;%0A
 import a
@@ -1788,24 +1788,50 @@
 anceState);%0A
+        setupActionBar();%0A
         getL
@@ -1871,32 +1871,160 @@
 , this);%0A    %7D%0A%0A
+    private void setupActionBar() %7B%0A        ActionBar bar = getActionBar();%0A        bar.setDisplayHomeAsUpEnabled(true);%0A    %7D%0A%0A
     public Loade
@@ -3523,24 +3523,116 @@
 ItemId()) %7B%0A
+            case android.R.id.home:%0A                finish();%0A                return true;%0A%0A
             

Enable home up on Settings