cmparts: Fix navigation issues
* Make sure we're always showing the right stuff in the ActionBar
* Inherit from SettingsDrawerActivity so we get the hamburger menu
Change-Id: Ia421f00f09b94876d4e51a9881bdcd4a986240e4
diff --git a/src/org/cyanogenmod/cmparts/PartsActivity.java b/src/org/cyanogenmod/cmparts/PartsActivity.java
index 6f1eb0d..37443ea 100644
--- a/src/org/cyanogenmod/cmparts/PartsActivity.java
+++ b/src/org/cyanogenmod/cmparts/PartsActivity.java
@@ -28,15 +28,22 @@
import android.nfc.Tag;
import android.os.Bundle;
import android.os.IBinder;
+import android.support.v14.preference.PreferenceFragment;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.widget.Button;
+import com.android.settingslib.drawer.SettingsDrawerActivity;
+
import org.cyanogenmod.cmparts.profiles.NFCProfileTagCallback;
import org.cyanogenmod.internal.cmparts.IPartsCatalog;
import org.cyanogenmod.internal.cmparts.PartInfo;
-public class PartsActivity extends Activity {
+public class PartsActivity extends SettingsDrawerActivity implements
+ PreferenceFragment.OnPreferenceStartFragmentCallback,
+ PreferenceFragment.OnPreferenceStartScreenCallback {
private static final String TAG = "PartsActivity";
@@ -56,10 +63,7 @@
private NFCProfileTagCallback mNfcProfileCallback;
- private ActionBar mActionBar;
- private SwitchBar mSwitchBar;
private CharSequence mInitialTitle;
- private int mInitialTitleResId;
@Override
public void onCreate(Bundle bundle) {
@@ -74,6 +78,7 @@
String fragmentClass = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
String component = getIntent().getComponent().getClassName();
Bundle initialArgs = getIntent().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
+ boolean homeAsUp = true;
Log.d(TAG, "Launched with: " + getIntent().toString() + " action: " +
getIntent().getAction() + " component: " + component +
@@ -91,6 +96,7 @@
// Alias mode
info = PartsCatalog.getPartInfoForClass(getResources(),
getIntent().getComponent().getClassName());
+ homeAsUp = false;
}
if (info == null) {
throw new UnsupportedOperationException("Unable to get part info: " + getIntent().toString());
@@ -104,7 +110,9 @@
setTitleFromIntent(getIntent(), info);
- switchToFragment(fragmentClass, initialArgs, mInitialTitleResId, mInitialTitle);
+ switchToFragment(fragmentClass, initialArgs, -1, mInitialTitle);
+
+ showHomeAsUp(homeAsUp);
}
@Override
@@ -113,6 +121,25 @@
disconnectCatalog();
}
+ @Override
+ public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
+ startPreferencePanel(pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
+ null, 0);
+ return true;
+ }
+
+ @Override
+ public boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) {
+ startPreferencePanel(pref.getFragment(), pref.getExtras(), -1, pref.getTitle(),
+ null, 0);
+ return true;
+ }
+
+ private void showHomeAsUp(boolean on) {
+ getActionBar().setDisplayHomeAsUpEnabled(on);
+ getActionBar().setHomeButtonEnabled(on);
+ }
+
private void connectCatalog() {
Intent i = new Intent(this, PartsCatalog.class);
bindService(i, mConnection, Context.BIND_AUTO_CREATE);
@@ -150,8 +177,13 @@
super.onNewIntent(intent);
}
- public SwitchBar getSwitchBar() {
- return mSwitchBar;
+ @Override
+ public void onBackPressed() {
+ setTitle(mInitialTitle);
+
+ if (!getFragmentManager().popBackStackImmediate()) {
+ super.onBackPressed();
+ }
}
public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
@@ -175,7 +207,7 @@
if (resultTo == null) {
startActivity(intent);
} else {
- startActivityForResult(intent, resultRequestCode);
+ resultTo.startActivityForResult(intent, resultRequestCode);
}
}
@@ -184,29 +216,32 @@
finish();
}
- public void switchToFragment(String fragmentClass, Bundle args, int titleRes, CharSequence titleText) {
- Log.d(TAG, "Launching fragment: " + fragmentClass);
-
+ public boolean switchToFragment(String fragmentClass, Bundle args, int titleRes,
+ CharSequence titleText) {
Fragment fragment = Fragment.instantiate(this, fragmentClass);
if (fragment == null) {
Log.e(TAG, "Invalid fragment! " + fragmentClass);
- return;
+ return false;
}
+ return switchToFragment(fragment, args, titleRes, titleText);
+ }
+
+ private boolean switchToFragment(Fragment fragment, Bundle args, int titleRes,
+ CharSequence titleText) {
+ Log.d(TAG, "Launching fragment: " + fragment.getClass().getName());
+
fragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_content, fragment);
- transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
if (titleRes > 0) {
transaction.setBreadCrumbTitle(titleRes);
} else if (titleText != null) {
transaction.setBreadCrumbTitle(titleText);
}
-
- transaction.commitAllowingStateLoss();
- getFragmentManager().executePendingTransactions();
-
- refreshBars();
+ transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
+ transaction.commit();
+ return true;
}
@@ -222,39 +257,23 @@
findViewById(R.id.button_bar).setVisibility(show ? View.VISIBLE : View.GONE);
}
- private void refreshBars() {
- mSwitchBar = (SwitchBar) findViewById(R.id.switch_bar);
- mActionBar = getActionBar();
- if (mActionBar != null) {
- mActionBar.setDisplayHomeAsUpEnabled(true);
- mActionBar.setHomeButtonEnabled(true);
- }
- }
-
- private void setTitleFromPart(PartInfo part) {
- mInitialTitleResId = 0;
- mInitialTitle = part.getTitle();
- setTitle(mInitialTitle);
+ public SwitchBar getSwitchBar() {
+ return (SwitchBar) findViewById(R.id.switch_bar);
}
private void setTitleFromIntent(Intent intent, PartInfo part) {
if (part != null) {
- mInitialTitleResId = -1;
mInitialTitle = part.getTitle();
- setTitle(mInitialTitle);
} else {
final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
if (initialTitleResId > 0) {
- mInitialTitle = null;
- mInitialTitleResId = initialTitleResId;
- setTitle(mInitialTitleResId);
+ mInitialTitle = getResources().getString(initialTitleResId);
} else {
- mInitialTitleResId = -1;
final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
- setTitle(mInitialTitle);
}
}
+ setTitle(mInitialTitle);
}
}