Skip to content

Commit 189cc2d

Browse files
committed
"Up navigation" implementation in the client sample.
1 parent 486cdb1 commit 189cc2d

4 files changed

Lines changed: 127 additions & 51 deletions

File tree

src/samples/client/src/com/example/devicehive/android/client/sample/BaseActivity.java

Lines changed: 112 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
package com.example.devicehive.android.client.sample;
22

3+
import android.app.Activity;
34
import android.app.AlertDialog;
5+
import android.content.ComponentName;
46
import android.content.DialogInterface;
7+
import android.content.Intent;
58
import android.os.Bundle;
69
import android.view.MenuItem;
710

11+
import com.actionbarsherlock.app.ActionBar;
812
import com.actionbarsherlock.app.SherlockFragmentActivity;
913
import com.dataart.android.devicehive.network.DeviceHiveResultReceiver;
1014
import com.dataart.android.devicehive.network.NetworkCommand;
1115
import com.dataart.android.devicehive.network.NetworkCommandConfig;
1216

1317
public 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

src/samples/client/src/com/example/devicehive/android/client/sample/DeviceActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ public class DeviceActivity extends BaseActivity implements
3636
+ ".EXTRA_DEVICE";
3737

3838
public static void start(Context context, DeviceData deviceData) {
39-
Intent intent = new Intent(context, DeviceActivity.class);
39+
final Intent intent = new Intent(context, DeviceActivity.class);
40+
final Bundle parentExtras = new Bundle(1);
41+
parentExtras.putParcelable(NetworkDevicesActivity.EXTRA_NETWORK,
42+
deviceData.getNetwork());
4043
intent.putExtra(EXTRA_DEVICE, deviceData);
44+
setParentActivity(intent, NetworkDevicesActivity.class, parentExtras);
45+
4146
context.startActivity(intent);
4247
}
4348

src/samples/client/src/com/example/devicehive/android/client/sample/NetworkDevicesActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424

2525
public class NetworkDevicesActivity extends BaseActivity {
2626

27-
private static final String EXTRA_NETWORK = NetworkDevicesActivity.class
27+
public static final String EXTRA_NETWORK = NetworkDevicesActivity.class
2828
.getName() + ".EXTRA_NETWORK";
2929

3030
public static void start(Context context, Network network) {
3131
Intent intent = new Intent(context, NetworkDevicesActivity.class);
3232
intent.putExtra(EXTRA_NETWORK, network);
33+
setParentActivity(intent, NetworksActivity.class);
3334
context.startActivity(intent);
3435
}
3536

src/samples/client/src/com/example/devicehive/android/client/sample/NetworksActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,20 @@ protected boolean showsRefreshActionItem() {
8989
return true;
9090
}
9191

92+
@Override
9293
protected void onShowSettings() {
9394
startSettingsActivity();
9495
}
9596

97+
@Override
9698
protected void onRefresh() {
9799
startNetworksRequest();
98100
}
101+
102+
@Override
103+
protected boolean showsHomeAsUpButton() {
104+
return false;
105+
}
99106

100107
private void startSettingsActivity() {
101108
startActivity(new Intent(this, SettingsActivity.class));

0 commit comments

Comments
 (0)