Skip to content

Commit

Permalink
Protect UI code from exception because of some possible raise during …
Browse files Browse the repository at this point in the history
…screen rotate
  • Loading branch information
trishika committed Feb 2, 2014
1 parent 9b3c290 commit e380455
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 68 deletions.
58 changes: 39 additions & 19 deletions src/main/java/org/droidupnp/view/ContentDirectoryFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public void updateSearchVisibility()
a.runOnUiThread(new Runnable() {
@Override
public void run() {
Main.setSearchVisibility(contentDirectoryCommand!=null && contentDirectoryCommand.isSearchAvailable());
try {
Main.setSearchVisibility(contentDirectoryCommand!=null && contentDirectoryCommand.isSearchAvailable());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand Down Expand Up @@ -310,8 +314,12 @@ public Void call() throws java.lang.Exception {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
setListShown(true);
mPullToRefreshLayout.setRefreshComplete();
try {
setListShown(true);
mPullToRefreshLayout.setRefreshComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand Down Expand Up @@ -341,10 +349,14 @@ public Void call() throws java.lang.Exception
a.runOnUiThread(new Runnable() {
@Override
public void run() {
// Empty the list
contentList.clear();
// Fill the list
contentList.addAll(content);
try {
// Empty the list
contentList.clear();
// Fill the list
contentList.addAll(content);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand All @@ -364,9 +376,13 @@ public synchronized void refresh()
a.runOnUiThread(new Runnable() {
@Override
public void run() {
setListShown(false);
mPullToRefreshLayout.setRefreshComplete();
mPullToRefreshLayout.setRefreshing(true);
try {
setListShown(false);
mPullToRefreshLayout.setRefreshComplete();
mPullToRefreshLayout.setRefreshing(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand Down Expand Up @@ -488,15 +504,19 @@ private void launchURI(final IDIDLItem uri)
a.runOnUiThread(new Runnable(){
@Override
public void run() {
RendererDialog rendererDialog = new RendererDialog();
rendererDialog.setCallback(new Callable<Void>() {
@Override
public Void call() throws Exception {
launchURIRenderer(uri);
return null;
}
});
rendererDialog.show(getActivity().getFragmentManager(), "RendererDialog");
try {
RendererDialog rendererDialog = new RendererDialog();
rendererDialog.setCallback(new Callable<Void>() {
@Override
public Void call() throws Exception {
launchURIRenderer(uri);
return null;
}
});
rendererDialog.show(getActivity().getFragmentManager(), "RendererDialog");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand Down
75 changes: 45 additions & 30 deletions src/main/java/org/droidupnp/view/RendererFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,21 @@ public RendererFragment()

public void hide()
{
getActivity().findViewById(R.id.progressBar).setVisibility(View.INVISIBLE);
getActivity().findViewById(R.id.separator).setVisibility(View.INVISIBLE);
Activity a = getActivity();
if (a==null)
return;
a.findViewById(R.id.progressBar).setVisibility(View.INVISIBLE);
a.findViewById(R.id.separator).setVisibility(View.INVISIBLE);
getFragmentManager().beginTransaction().hide(this).commit();
}

public void show()
{
getActivity().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
getActivity().findViewById(R.id.separator).setVisibility(View.VISIBLE);
Activity a = getActivity();
if (a==null)
return;
a.findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
a.findViewById(R.id.separator).setVisibility(View.VISIBLE);
getFragmentManager().beginTransaction().show(this).commit();
}

Expand Down Expand Up @@ -164,7 +170,11 @@ public void startControlPoint()
@Override
public void run()
{
hide();
try {
hide();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand Down Expand Up @@ -209,40 +219,45 @@ public void updateRenderer()
@Override
public void run()
{
show();
try {
show();

TextView title = (TextView) a.findViewById(R.id.title);
TextView artist = (TextView) a.findViewById(R.id.artist);
SeekBar seek = (SeekBar) a.findViewById(R.id.progressBar);
SeekBar volume = (SeekBar) a.findViewById(R.id.volume);
TextView durationElapse = (TextView) a.findViewById(R.id.trackDurationElapse);
TextView title = (TextView) a.findViewById(R.id.title);
TextView artist = (TextView) a.findViewById(R.id.artist);
SeekBar seek = (SeekBar) a.findViewById(R.id.progressBar);
SeekBar volume = (SeekBar) a.findViewById(R.id.volume);
TextView durationElapse = (TextView) a.findViewById(R.id.trackDurationElapse);

if (title == null || artist == null || seek == null || duration == null || durationElapse == null)
return;
if (title == null || artist == null || seek == null || duration == null || durationElapse == null)
return;

if (durationRemaining)
duration.setText(rendererState.getRemainingDuration());
else
duration.setText(rendererState.getDuration());
if (durationRemaining)
duration.setText(rendererState.getRemainingDuration());
else
duration.setText(rendererState.getDuration());

durationElapse.setText(rendererState.getPosition());
durationElapse.setText(rendererState.getPosition());

seek.setProgress(rendererState.getElapsedPercent());
seek.setProgress(rendererState.getElapsedPercent());

title.setText(rendererState.getTitle());
artist.setText(rendererState.getArtist());
title.setText(rendererState.getTitle());
artist.setText(rendererState.getArtist());

if (rendererState.getState() == RendererState.State.PLAY)
play_pauseButton.setImageResource(R.drawable.pause);
else
play_pauseButton.setImageResource(R.drawable.play);
if (rendererState.getState() == RendererState.State.PLAY)
play_pauseButton.setImageResource(R.drawable.pause);
else
play_pauseButton.setImageResource(R.drawable.play);

if (rendererState.isMute())
volumeButton.setImageResource(R.drawable.volume_mute);
else
volumeButton.setImageResource(R.drawable.volume);
if (rendererState.isMute())
volumeButton.setImageResource(R.drawable.volume_mute);
else
volumeButton.setImageResource(R.drawable.volume);

volume.setProgress(rendererState.getVolume());
volume.setProgress(rendererState.getVolume());

} catch (Exception e) {
e.printStackTrace();
}
}
});

Expand Down
46 changes: 27 additions & 19 deletions src/main/java/org/droidupnp/view/UpnpDeviceListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,27 @@ public void addedDevice(IUpnpDevice device)
@Override
public void run()
{
int position = list.getPosition(d);
if (position >= 0)
{
// Device already in the list, re-set new value at same position
list.remove(d);
list.insert(d, position);
}
else
{
list.add(d);
}
if (isSelected(d.getDevice()))
{
position = list.getPosition(d);
getListView().setItemChecked(position, true);

Log.i(TAG, d.toString() + " is selected at position " + position);
try {
int position = list.getPosition(d);
if (position >= 0)
{
// Device already in the list, re-set new value at same position
list.remove(d);
list.insert(d, position);
}
else
{
list.add(d);
}
if (isSelected(d.getDevice()))
{
position = list.getPosition(d);
getListView().setItemChecked(position, true);

Log.i(TAG, d.toString() + " is selected at position " + position);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
Expand All @@ -158,8 +162,12 @@ public void removedDevice(IUpnpDevice device)
@Override
public void run()
{
// Remove device from list
list.remove(d);
try {
// Remove device from list
list.remove(d);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Expand Down

0 comments on commit e380455

Please sign in to comment.