/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2012 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any
 * later version. Please see the file LICENSE-GPL for details.
 *
 * Web Page: http://mielke.cc/brltty/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

package org.a11y.brltty.android;

import java.util.List;

import android.util.Log;

import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.os.Bundle;

import android.content.Intent;
import android.view.MenuItem;

public class SettingsActivity extends PreferenceActivity {
  private final String LOG_TAG = this.getClass().getName();

  @Override
  protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getActionBar().setDisplayHomeAsUpEnabled(true);
  }

  @Override
  public void onBuildHeaders (List<Header> target) {
    loadHeadersFromResource(R.xml.settings_headers, target);
  }

  @Override
  public boolean onOptionsItemSelected (MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home: {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
      }

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

  public static class GeneralSettings extends PreferenceFragment {
    @Override
    public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      addPreferencesFromResource(R.xml.settings_general);
    }
  }

  public static class DeviceSettings extends PreferenceFragment {
    @Override
    public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      addPreferencesFromResource(R.xml.settings_device);
    }
  }

  public static class AdvancedSettings extends PreferenceFragment {
    @Override
    public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      addPreferencesFromResource(R.xml.settings_advanced);
    }
  }
}

@@ -833,16 +833,92 @@
 .Bundle;
+%0Aimport android.app.FragmentManager;%0Aimport android.app.FragmentTransaction;
 %0A%0Aimport
@@ -1519,24 +1519,171 @@
 .id.home: %7B%0A
+        FragmentManager fm = getFragmentManager();%0A%0A        if (fm.getBackStackEntryCount() %3E 0) %7B%0A          fm.popBackStack();%0A        %7D else %7B%0A  
         Inte
@@ -1732,16 +1732,18 @@
 class);%0A
+  
         
@@ -1831,16 +1831,18 @@
         
+  
 startAct
@@ -1856,16 +1856,27 @@
 ntent);%0A
+        %7D%0A%0A
         
@@ -2003,48 +2003,251 @@
 tic 
-class GeneralSettings extends Preference
+abstract class SettingsFragment extends PreferenceFragment %7B%0A    @Override%0A    public void onCreate (Bundle savedInstanceState) %7B%0A      super.onCreate(savedInstanceState);%0A    %7D%0A  %7D%0A%0A  public static final class GeneralSettings extends Settings
 Frag
@@ -2446,24 +2446,30 @@
 blic static 
+final 
 class Device
@@ -2481,34 +2481,32 @@
 ngs extends 
-Preference
+Settings
 Fragment %7B%0A 
@@ -2700,16 +2700,22 @@
  static 
+final 
 class Ad
@@ -2733,34 +2733,32 @@
 ngs extends 
-Preference
+Settings
 Fragment %7B%0A 

Add an intermediate class which is common to all Android settings fragment classes. (dm)