New and improved Presentation demo.

Less clutter, bigger letters!

Change-Id: Ie0d5626c6a83a0e96bbda9220aea1f945c1bd825
This commit is contained in:
Craig Mautner
2012-09-10 09:41:18 -07:00
parent 39e3b7fe23
commit 9dac35adda
4 changed files with 171 additions and 37 deletions

View File

@@ -19,8 +19,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="match_parent">
<!-- A picture for visual interest. -->
<ImageView android:id="@+id/image"
@@ -32,6 +31,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"/>
android:textAppearance="?android:attr/textAppearanceLarge"/>
</FrameLayout>

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2012 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The content that we show on secondary displays.
See corresponding Java code PresentationActivity.java. -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox_presentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"/>
<TextView android:id="@+id/display_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/checkbox_presentation"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/presentation_info_text"/>
</RelativeLayout>

View File

@@ -113,8 +113,14 @@
<string name="apply">Apply</string>
<string name="activity_presentation">App/Activity/Presentation</string>
<string name="presentation_activity_text">This activity demonstrates how to use the Presentation activity to show content on other Displays. Try connecting a secondary display and watch what happens.</string>
<string name="presentation_activity_text">This activity demonstrates how to use the Presentation activity to show content on other Displays.\n
Try connecting a secondary display and watch what happens.\n
Selecting a Display will open a Presentation on it.</string>
<string name="presentation_channel_text">You\'re watching channel %1$d on display %2$d.</string>
<string name="presentation_info_text">Info</string>
<string name="presentation_display_id_text">Display #%1$d</string>
<string name="presentation_alert_info_text">Display %1$d Info</string>
<string name="presentation_alert_dismiss_text">OK</string>
<string name="fragment_alert_dialog">App/Fragment/Alert Dialog</string>

View File

@@ -21,19 +21,26 @@ package com.example.android.apis.app;
import com.example.android.apis.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Presentation;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.drawable.GradientDrawable;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
@@ -51,18 +58,20 @@ import android.widget.TextView;
* a few simulated secondary displays.
* </p>
*/
public class PresentationActivity extends Activity {
public class PresentationActivity extends Activity
implements OnCheckedChangeListener, OnClickListener {
private final String TAG = "PresentationActivity";
// The content that we want to show on the presentation.
private static final int[] CHANNELS = new int[] {
R.drawable.sample_4, R.drawable.frantic, R.drawable.beach,
R.drawable.photo1, R.drawable.photo2, R.drawable.photo3,
R.drawable.photo4, R.drawable.photo5, R.drawable.photo6,
};
private DisplayManager mDisplayManager;
private ListView mDisplayList;
private DisplayListAdapter mDisplayListAdapter;
private ListView mListView;
private int mNextChannelNumber;
// All active presentations indexed by display id.
@@ -88,10 +97,8 @@ public class PresentationActivity extends Activity {
setContentView(R.layout.presentation_activity);
mDisplayListAdapter = new DisplayListAdapter(this);
mDisplayList = (ListView)findViewById(R.id.display_list);
mDisplayList.setAdapter(mDisplayListAdapter);
mDisplayList.setOnItemClickListener(mOnItemClickListener);
mListView = (ListView)findViewById(R.id.display_list);
mListView.setAdapter(mDisplayListAdapter);
}
@Override
@@ -144,20 +151,50 @@ public class PresentationActivity extends Activity {
}
/**
* Shows a {@link Presentation} on the specified display, or dismisses it if one
* already showing there.
* Hides a {@link Presentation} on the specified display.
*/
private void showOrDismissPresentation(Display display) {
// Dismiss if already showing.
DemoPresentation presentation = mActivePresentations.get(display.getDisplayId());
private void hidePresentation(Display display) {
final int displayId = display.getDisplayId();
DemoPresentation presentation = mActivePresentations.get(displayId);
if (presentation != null) {
Log.d(TAG, "Dimissing presentation on display " + display.getDisplayId() + ".");
Log.d(TAG, "Dimissing presentation on display " + displayId + ".");
presentation.dismiss();
// presentation will be removed from mActivePresentations in onDismiss().
return;
}
}
// Otherwise show the presentation.
showPresentation(display);
@Override
/** Presentation CheckBox */
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final Display display = (Display)buttonView.getTag();
if (isChecked) {
showPresentation(display);
} else {
hidePresentation(display);
}
}
@Override
/** Info Button */
public void onClick(View v) {
Context context = v.getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final Display display = (Display)v.getTag();
Resources r = context.getResources();
AlertDialog alert = builder
.setTitle(r.getString(
R.string.presentation_alert_info_text, display.getDisplayId()))
.setMessage(display.toString())
.setNeutralButton(R.string.presentation_alert_dismiss_text,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
alert.show();
}
/**
@@ -203,9 +240,21 @@ public class PresentationActivity extends Activity {
@Override
public void onDismiss(DialogInterface dialog) {
DemoPresentation presentation = (DemoPresentation)dialog;
Display display = presentation.getDisplay();
Log.d(TAG, "Presentation on display " + display.getDisplayId() + " was dismissed.");
mActivePresentations.remove(display.getDisplayId());
int displayId = presentation.getDisplay().getDisplayId();
Log.d(TAG, "Presentation on display " + displayId + " was dismissed.");
mActivePresentations.remove(displayId);
// 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;
}
}
}
};
@@ -214,8 +263,39 @@ public class PresentationActivity extends Activity {
* Shows information about all displays.
*/
private final class DisplayListAdapter extends ArrayAdapter<Display> {
final Context mContext;
public DisplayListAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1);
super(context, R.layout.presentation_list_item);
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View v;
if (convertView == null) {
v = ((Activity) mContext).getLayoutInflater().inflate(
R.layout.presentation_list_item, null);
} else {
v = convertView;
}
final Display display = getItem(position);
CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox_presentation);
cb.setTag(display);
cb.setOnCheckedChangeListener(PresentationActivity.this);
onCheckedChanged(cb, cb.isChecked());
TextView tv = (TextView)v.findViewById(R.id.display_id);
tv.setText(v.getContext().getResources().getString(
R.string.presentation_display_id_text, display.getDisplayId()));
Button b = (Button)v.findViewById(R.id.info);
b.setTag(display);
b.setOnClickListener(PresentationActivity.this);
return v;
}
/**
@@ -235,19 +315,6 @@ public class PresentationActivity extends Activity {
}
}
/**
* Called when an item in the display list is clicked.
*
* Causes a presentation to be shown or dismissed on that display if already showing.
*/
private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Display display = (Display)parent.getItemAtPosition(position);
showOrDismissPresentation(display);
}
};
/**
* The presentation to show on the secondary display.
*
@@ -284,6 +351,20 @@ public class PresentationActivity extends Activity {
// Show a n image for visual interest.
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(r.getDrawable(CHANNELS[mChannelNumber]));
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
// Set the background to a random gradient.
Point p = new Point();
getDisplay().getSize(p);
drawable.setGradientRadius(Math.max(p.x, p.y) / 2);
drawable.setColors(new int[] {
((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000,
((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000
});
findViewById(android.R.id.content).setBackground(drawable);
}
}
}