11package com .example .devicehive .android .client .sample ;
22
3+ import android .app .Activity ;
34import android .app .AlertDialog ;
5+ import android .content .ComponentName ;
46import android .content .DialogInterface ;
7+ import android .content .Intent ;
58import android .os .Bundle ;
69import android .view .MenuItem ;
710
11+ import com .actionbarsherlock .app .ActionBar ;
812import com .actionbarsherlock .app .SherlockFragmentActivity ;
913import com .dataart .android .devicehive .network .DeviceHiveResultReceiver ;
1014import com .dataart .android .devicehive .network .NetworkCommand ;
1115import com .dataart .android .devicehive .network .NetworkCommandConfig ;
1216
1317public class BaseActivity extends SherlockFragmentActivity {
1418
19+ private final static String NAMESPACE = BaseActivity .class .getName ();
20+
21+ private final static String EXTRA_PARENT_ACTIVITY = NAMESPACE
22+ .concat (".EXTRA_PARENT_ACTIVITY" );
23+
24+ private final static String EXTRA_PARENT_ACTIVITY_EXTRAS = NAMESPACE
25+ .concat (".EXTRA_PARENT_ACTIVITY_EXTRAS" );
26+
1527 private DeviceHiveResultReceiver resultReceiver = null ;
1628
1729 private final DeviceHiveResultReceiver .ResultListener resultListener = new DeviceHiveResultReceiver .ResultListener () {
@@ -21,6 +33,15 @@ public void onReceiveResult(int code, int tag, Bundle data) {
2133 }
2234 };
2335
36+ @ Override
37+ protected void onCreate (Bundle savedInstanceState ) {
38+ super .onCreate (savedInstanceState );
39+ final ActionBar actionBar = getSupportActionBar ();
40+ if (actionBar != null && showsHomeAsUpButton ()) {
41+ actionBar .setDisplayHomeAsUpEnabled (true );
42+ }
43+ }
44+
2445 @ Override
2546 protected void onStop () {
2647 super .onStop ();
@@ -30,8 +51,7 @@ protected void onStop() {
3051 }
3152 }
3253
33- protected final <T extends NetworkCommand > void startCommand (
34- final T command ) {
54+ protected final <T extends NetworkCommand > void startCommand (final T command ) {
3555 command .start (getApplicationContext (), getNetworkCommandConfig ());
3656 }
3757
@@ -73,7 +93,7 @@ protected static final int getTagId(final String tag) {
7393
7494 private static final int MENU_ID_SETTINGS = 0x01 ;
7595 private static final int MENU_ID_REFRESH = 0x02 ;
76-
96+
7797 private com .actionbarsherlock .view .Menu optionsMenu ;
7898
7999 @ Override
@@ -94,16 +114,63 @@ public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
94114 return super .onCreateOptionsMenu (menu );
95115 }
96116
117+ protected final static <T extends Activity > Intent setParentActivity (
118+ final Intent intent , final Class <T > parentActivityClass ) {
119+ return setParentActivity (intent , parentActivityClass , null );
120+ }
121+
122+ protected final static <T extends Activity > Intent setParentActivity (
123+ final Intent intent , final Class <T > parentActivityClass ,
124+ Bundle parentActivityExtras ) {
125+ intent .putExtra (EXTRA_PARENT_ACTIVITY , parentActivityClass .getName ());
126+ if (parentActivityExtras != null ) {
127+ intent .putExtra (EXTRA_PARENT_ACTIVITY_EXTRAS , parentActivityExtras );
128+ }
129+ return intent ;
130+ }
131+
132+ protected boolean showsHomeAsUpButton () {
133+ return true ;
134+ }
135+
136+ protected Intent getHomeUpNavigationTarget () {
137+ final String parentActivityClass = getIntent ().getStringExtra (
138+ EXTRA_PARENT_ACTIVITY );
139+ final Intent intent ;
140+ if (parentActivityClass == null ) {
141+ intent = new Intent (this , NetworksActivity .class );
142+ } else {
143+ intent = new Intent ();
144+ intent .setComponent (new ComponentName (this , parentActivityClass ));
145+ }
146+ final Bundle parentExtras = getIntent ().getBundleExtra (
147+ EXTRA_PARENT_ACTIVITY_EXTRAS );
148+ if (parentExtras != null ) {
149+ intent .putExtras (parentExtras );
150+ }
151+ return intent ;
152+ }
153+
97154 @ Override
98155 public boolean onOptionsItemSelected (
99156 com .actionbarsherlock .view .MenuItem item ) {
100- if (item .getItemId () == MENU_ID_SETTINGS ) {
157+ switch (item .getItemId ()) {
158+ case android .R .id .home : {
159+ final Intent intent = getHomeUpNavigationTarget ();
160+ intent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP );
161+ startActivity (intent );
162+ break ;
163+ }
164+ case MENU_ID_SETTINGS :
101165 onShowSettings ();
102- return true ;
103- } else if ( item . getItemId () == MENU_ID_REFRESH ) {
166+ break ;
167+ case MENU_ID_REFRESH :
104168 onRefresh ();
169+ break ;
170+ default :
171+ return super .onOptionsItemSelected (item );
105172 }
106- return super . onOptionsItemSelected ( item ) ;
173+ return true ;
107174 }
108175
109176 protected boolean showsSettingsActionItem () {
@@ -121,33 +188,33 @@ protected void onShowSettings() {
121188 protected void onRefresh () {
122189
123190 }
124-
191+
125192 protected boolean showsActionBarProgress () {
126- return false ;
127- }
128-
129- private int progressOperationsCount = 0 ;
130-
131- protected void incrementActionBarProgressOperationsCount (int count ) {
132- this .progressOperationsCount += count ;
133- setActionBarProgressVisibility (count > 0 );
134- }
135-
136- protected void decrementActionBarProgressOperationsCount () {
137- progressOperationsCount -= 1 ;
138- if (progressOperationsCount == 0 ) {
139- setActionBarProgressVisibility (false );
140- }
141- if (progressOperationsCount < 0 ) {
142- progressOperationsCount = 0 ;
143- }
144- }
145-
146- protected int getActionBarProgressOperationsCount () {
147- return progressOperationsCount ;
148- }
149-
150- protected void setActionBarProgressVisibility (boolean visible ) {
193+ return false ;
194+ }
195+
196+ private int progressOperationsCount = 0 ;
197+
198+ protected void incrementActionBarProgressOperationsCount (int count ) {
199+ this .progressOperationsCount += count ;
200+ setActionBarProgressVisibility (count > 0 );
201+ }
202+
203+ protected void decrementActionBarProgressOperationsCount () {
204+ progressOperationsCount -= 1 ;
205+ if (progressOperationsCount == 0 ) {
206+ setActionBarProgressVisibility (false );
207+ }
208+ if (progressOperationsCount < 0 ) {
209+ progressOperationsCount = 0 ;
210+ }
211+ }
212+
213+ protected int getActionBarProgressOperationsCount () {
214+ return progressOperationsCount ;
215+ }
216+
217+ protected void setActionBarProgressVisibility (boolean visible ) {
151218 if (optionsMenu != null ) {
152219 final com .actionbarsherlock .view .MenuItem item = optionsMenu
153220 .findItem (MENU_ID_REFRESH );
@@ -160,25 +227,21 @@ protected void setActionBarProgressVisibility(boolean visible) {
160227 }
161228 }
162229 }
163- }
164-
165- protected void showErrorDialog (String message ) {
230+ }
231+
232+ protected void showErrorDialog (String message ) {
166233 showDialog ("Error!" , message );
167234 }
168-
169- protected void showDialog (String title , String message ) {
235+
236+ protected void showDialog (String title , String message ) {
170237 AlertDialog .Builder builder = new AlertDialog .Builder (this );
171- final AlertDialog dialog = builder
172- .setTitle (title )
173- .setMessage (message )
174- .setPositiveButton ("OK" ,
175- new DialogInterface .OnClickListener () {
176- @ Override
177- public void onClick (DialogInterface dialog ,
178- int which ) {
179- dialog .dismiss ();
180- }
181- }).create ();
238+ final AlertDialog dialog = builder .setTitle (title ).setMessage (message )
239+ .setPositiveButton ("OK" , new DialogInterface .OnClickListener () {
240+ @ Override
241+ public void onClick (DialogInterface dialog , int which ) {
242+ dialog .dismiss ();
243+ }
244+ }).create ();
182245 dialog .show ();
183246 }
184247
0 commit comments