Make Presentation demo retain image and color.
Store which Displays are presenting, their images and their background colors. That way the don't change between onPause and onResume. Change-Id: I01328583ef309bf12e7f5750aa4abea50f847a99
This commit is contained in:
committed by
Jeff Brown
parent
9dac35adda
commit
51f883381d
@@ -28,7 +28,7 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentLeft="true"
|
android:layout_alignParentLeft="true"
|
||||||
android:layout_centerVertical="true"
|
android:layout_centerVertical="true"
|
||||||
android:layout_marginRight="10dip"/>
|
android:layout_marginRight="16dip"/>
|
||||||
|
|
||||||
<TextView android:id="@+id/display_id"
|
<TextView android:id="@+id/display_id"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
|
|||||||
@@ -74,7 +74,11 @@ public class PresentationActivity extends Activity
|
|||||||
private ListView mListView;
|
private ListView mListView;
|
||||||
private int mNextChannelNumber;
|
private int mNextChannelNumber;
|
||||||
|
|
||||||
// All active presentations indexed by display id.
|
// Indexed by displayId. Persists across pause/resume.
|
||||||
|
private final SparseArray<PresentationContents> mSavedPresentationContents =
|
||||||
|
new SparseArray<PresentationContents>();
|
||||||
|
|
||||||
|
// Indexed by displayId. Contains all currently displayed Presentations.
|
||||||
private final SparseArray<DemoPresentation> mActivePresentations =
|
private final SparseArray<DemoPresentation> mActivePresentations =
|
||||||
new SparseArray<DemoPresentation>();
|
new SparseArray<DemoPresentation>();
|
||||||
|
|
||||||
@@ -109,6 +113,17 @@ public class PresentationActivity extends Activity
|
|||||||
// Update our list of displays on resume.
|
// Update our list of displays on resume.
|
||||||
mDisplayListAdapter.updateContents();
|
mDisplayListAdapter.updateContents();
|
||||||
|
|
||||||
|
final int numDisplays = mDisplayListAdapter.getCount();
|
||||||
|
for (int i = 0; i < numDisplays; i++) {
|
||||||
|
final Display display = mDisplayListAdapter.getItem(i);
|
||||||
|
final PresentationContents contents =
|
||||||
|
mSavedPresentationContents.get(display.getDisplayId());
|
||||||
|
if (contents != null) {
|
||||||
|
showPresentation(display, contents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mSavedPresentationContents.clear();
|
||||||
|
|
||||||
// Register to receive events from the display manager.
|
// Register to receive events from the display manager.
|
||||||
mDisplayManager.registerDisplayListener(mDisplayListener, null);
|
mDisplayManager.registerDisplayListener(mDisplayListener, null);
|
||||||
}
|
}
|
||||||
@@ -122,9 +137,11 @@ public class PresentationActivity extends Activity
|
|||||||
mDisplayManager.unregisterDisplayListener(mDisplayListener);
|
mDisplayManager.unregisterDisplayListener(mDisplayListener);
|
||||||
|
|
||||||
// Dismiss all of our presentations.
|
// Dismiss all of our presentations.
|
||||||
Log.d(TAG, "Activity is being paused. Dimissing all active presentation.");
|
Log.d(TAG, "Activity is being paused. Dismissing all active presentation.");
|
||||||
for (int i = 0; i < mActivePresentations.size(); i++) {
|
for (int i = 0; i < mActivePresentations.size(); i++) {
|
||||||
Presentation presentation = mActivePresentations.valueAt(i);
|
DemoPresentation presentation = mActivePresentations.valueAt(i);
|
||||||
|
int displayId = mActivePresentations.keyAt(i);
|
||||||
|
mSavedPresentationContents.put(displayId, presentation.mContents);
|
||||||
presentation.dismiss();
|
presentation.dismiss();
|
||||||
}
|
}
|
||||||
mActivePresentations.clear();
|
mActivePresentations.clear();
|
||||||
@@ -133,21 +150,19 @@ public class PresentationActivity extends Activity
|
|||||||
/**
|
/**
|
||||||
* Shows a {@link Presentation} on the specified display.
|
* Shows a {@link Presentation} on the specified display.
|
||||||
*/
|
*/
|
||||||
private void showPresentation(Display display) {
|
private void showPresentation(Display display, PresentationContents contents) {
|
||||||
// Do nothing if there is already a presentation showing on the display.
|
final int displayId = display.getDisplayId();
|
||||||
if (mActivePresentations.get(display.getDisplayId()) != null) {
|
if (mActivePresentations.get(displayId) != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d(TAG, "Showing presentation on display " + display.getDisplayId() + ".");
|
Log.d(TAG, "Showing presentation channel " + contents.channel
|
||||||
|
+ " on display " + displayId + ".");
|
||||||
|
|
||||||
int channelNumber = mNextChannelNumber;
|
DemoPresentation presentation = new DemoPresentation(this, display, contents);
|
||||||
mNextChannelNumber = (mNextChannelNumber + 1) % CHANNELS.length;
|
|
||||||
|
|
||||||
final DemoPresentation presentation = new DemoPresentation(this, display, channelNumber);
|
|
||||||
mActivePresentations.put(display.getDisplayId(), presentation);
|
|
||||||
presentation.show();
|
presentation.show();
|
||||||
presentation.setOnDismissListener(mOnDismissListener);
|
presentation.setOnDismissListener(mOnDismissListener);
|
||||||
|
mActivePresentations.put(displayId, presentation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -156,12 +171,19 @@ public class PresentationActivity extends Activity
|
|||||||
private void hidePresentation(Display display) {
|
private void hidePresentation(Display display) {
|
||||||
final int displayId = display.getDisplayId();
|
final int displayId = display.getDisplayId();
|
||||||
DemoPresentation presentation = mActivePresentations.get(displayId);
|
DemoPresentation presentation = mActivePresentations.get(displayId);
|
||||||
if (presentation != null) {
|
if (presentation == null) {
|
||||||
Log.d(TAG, "Dimissing presentation on display " + displayId + ".");
|
|
||||||
presentation.dismiss();
|
|
||||||
// presentation will be removed from mActivePresentations in onDismiss().
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "Dismissing presentation on display " + displayId + ".");
|
||||||
|
|
||||||
|
presentation.dismiss();
|
||||||
|
mActivePresentations.delete(displayId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getNextChannel() {
|
||||||
|
mNextChannelNumber = (mNextChannelNumber + 1) % CHANNELS.length;
|
||||||
|
return mNextChannelNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -169,7 +191,8 @@ public class PresentationActivity extends Activity
|
|||||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||||
final Display display = (Display)buttonView.getTag();
|
final Display display = (Display)buttonView.getTag();
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
showPresentation(display);
|
PresentationContents contents = new PresentationContents(getNextChannel());
|
||||||
|
showPresentation(display, contents);
|
||||||
} else {
|
} else {
|
||||||
hidePresentation(display);
|
hidePresentation(display);
|
||||||
}
|
}
|
||||||
@@ -212,11 +235,6 @@ public class PresentationActivity extends Activity
|
|||||||
public void onDisplayAdded(int displayId) {
|
public void onDisplayAdded(int displayId) {
|
||||||
Log.d(TAG, "Display " + displayId + " added.");
|
Log.d(TAG, "Display " + displayId + " added.");
|
||||||
mDisplayListAdapter.updateContents();
|
mDisplayListAdapter.updateContents();
|
||||||
|
|
||||||
Display display = mDisplayManager.getDisplay(displayId);
|
|
||||||
if (display != null) {
|
|
||||||
showPresentation(display);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -242,19 +260,8 @@ public class PresentationActivity extends Activity
|
|||||||
DemoPresentation presentation = (DemoPresentation)dialog;
|
DemoPresentation presentation = (DemoPresentation)dialog;
|
||||||
int displayId = presentation.getDisplay().getDisplayId();
|
int displayId = presentation.getDisplay().getDisplayId();
|
||||||
Log.d(TAG, "Presentation on display " + displayId + " was dismissed.");
|
Log.d(TAG, "Presentation on display " + displayId + " was dismissed.");
|
||||||
mActivePresentations.remove(displayId);
|
mActivePresentations.delete(displayId);
|
||||||
|
mDisplayListAdapter.notifyDataSetChanged();
|
||||||
// Uncheck the checkbox once removed.
|
|
||||||
final int numChildren = mListView.getChildCount();
|
|
||||||
for (int i = 0; i < numChildren; i++) {
|
|
||||||
CheckBox cb =
|
|
||||||
(CheckBox)mListView.getChildAt(i).findViewById(R.id.checkbox_presentation);
|
|
||||||
Display cbDisplay = (Display)cb.getTag();
|
|
||||||
if (cbDisplay.getDisplayId() == displayId) {
|
|
||||||
cb.setChecked(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -281,15 +288,17 @@ public class PresentationActivity extends Activity
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Display display = getItem(position);
|
final Display display = getItem(position);
|
||||||
|
final int displayId = display.getDisplayId();
|
||||||
|
|
||||||
CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox_presentation);
|
CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox_presentation);
|
||||||
cb.setTag(display);
|
cb.setTag(display);
|
||||||
cb.setOnCheckedChangeListener(PresentationActivity.this);
|
cb.setOnCheckedChangeListener(PresentationActivity.this);
|
||||||
onCheckedChanged(cb, cb.isChecked());
|
cb.setChecked(mActivePresentations.indexOfKey(displayId) >= 0
|
||||||
|
|| mSavedPresentationContents.indexOfKey(displayId) >= 0);
|
||||||
|
|
||||||
TextView tv = (TextView)v.findViewById(R.id.display_id);
|
TextView tv = (TextView)v.findViewById(R.id.display_id);
|
||||||
tv.setText(v.getContext().getResources().getString(
|
tv.setText(v.getContext().getResources().getString(
|
||||||
R.string.presentation_display_id_text, display.getDisplayId()));
|
R.string.presentation_display_id_text, displayId));
|
||||||
|
|
||||||
Button b = (Button)v.findViewById(R.id.info);
|
Button b = (Button)v.findViewById(R.id.info);
|
||||||
b.setTag(display);
|
b.setTag(display);
|
||||||
@@ -323,12 +332,12 @@ public class PresentationActivity extends Activity
|
|||||||
* own {@link Context} whenever we load resources.
|
* own {@link Context} whenever we load resources.
|
||||||
*/
|
*/
|
||||||
private final class DemoPresentation extends Presentation {
|
private final class DemoPresentation extends Presentation {
|
||||||
// Specifies the content that we want to show in this presentation.
|
|
||||||
private final int mChannelNumber;
|
|
||||||
|
|
||||||
public DemoPresentation(Context context, Display display, int channelNumber) {
|
final PresentationContents mContents;
|
||||||
|
|
||||||
|
public DemoPresentation(Context context, Display display, PresentationContents contents) {
|
||||||
super(context, display);
|
super(context, display);
|
||||||
mChannelNumber = channelNumber;
|
mContents = contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -343,14 +352,17 @@ public class PresentationActivity extends Activity
|
|||||||
// Inflate the layout.
|
// Inflate the layout.
|
||||||
setContentView(R.layout.presentation_content);
|
setContentView(R.layout.presentation_content);
|
||||||
|
|
||||||
|
final Display display = getDisplay();
|
||||||
|
final int displayId = display.getDisplayId();
|
||||||
|
final int channel = mContents.channel;
|
||||||
|
|
||||||
// Show a text message to describe what's going on.
|
// Show a text message to describe what's going on.
|
||||||
TextView text = (TextView)findViewById(R.id.text);
|
TextView text = (TextView)findViewById(R.id.text);
|
||||||
text.setText(r.getString(R.string.presentation_channel_text, mChannelNumber + 1,
|
text.setText(r.getString(R.string.presentation_channel_text, channel, displayId));
|
||||||
getDisplay().getDisplayId()));
|
|
||||||
|
|
||||||
// Show a n image for visual interest.
|
// Show a n image for visual interest.
|
||||||
ImageView image = (ImageView)findViewById(R.id.image);
|
ImageView image = (ImageView)findViewById(R.id.image);
|
||||||
image.setImageDrawable(r.getDrawable(CHANNELS[mChannelNumber]));
|
image.setImageDrawable(r.getDrawable(CHANNELS[channel]));
|
||||||
|
|
||||||
GradientDrawable drawable = new GradientDrawable();
|
GradientDrawable drawable = new GradientDrawable();
|
||||||
drawable.setShape(GradientDrawable.RECTANGLE);
|
drawable.setShape(GradientDrawable.RECTANGLE);
|
||||||
@@ -360,11 +372,20 @@ public class PresentationActivity extends Activity
|
|||||||
Point p = new Point();
|
Point p = new Point();
|
||||||
getDisplay().getSize(p);
|
getDisplay().getSize(p);
|
||||||
drawable.setGradientRadius(Math.max(p.x, p.y) / 2);
|
drawable.setGradientRadius(Math.max(p.x, p.y) / 2);
|
||||||
drawable.setColors(new int[] {
|
drawable.setColors(mContents.colors);
|
||||||
((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000,
|
|
||||||
((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000
|
|
||||||
});
|
|
||||||
findViewById(android.R.id.content).setBackground(drawable);
|
findViewById(android.R.id.content).setBackground(drawable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final static class PresentationContents {
|
||||||
|
final int channel;
|
||||||
|
final int[] colors;
|
||||||
|
|
||||||
|
public PresentationContents(int channel) {
|
||||||
|
this.channel = channel;
|
||||||
|
colors = new int[] {
|
||||||
|
((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000,
|
||||||
|
((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000 };
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user