package wei.mark.example;

import wei.mark.standout.StandOutWindow;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @author Mark Wei <markwei@gmail.com>
 * 
 */
public class StandOutMultiWindow extends StandOutWindow {

	@Override
	protected View createAndAttachView(int id, ViewGroup root) {
		switch (id) {
			default:
				LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
				View view = inflater.inflate(R.layout.body, root, true);

				TextView idText = (TextView) view.findViewById(R.id.id);
				idText.setText(String.valueOf(id));

				return view;
		}
	}

	private int nextId = DEFAULT_ID;

	@Override
	protected LayoutParams getParams(int id, View view) {
		LayoutParams params = (LayoutParams) view.getLayoutParams();
		if (params == null) {
			return new LayoutParams(200, 200);
		} else {
			return params;
		}
	}

	// we want the system window decorations
	@Override
	protected int getFlags(int id) {
		return FLAG_DECORATION_SYSTEM | FLAG_BODY_MOVE_ENABLE;
	}

	@Override
	protected Notification getPersistentNotification(int id) {
		int icon = android.R.drawable.ic_menu_add;
		long when = System.currentTimeMillis();
		Context c = getApplicationContext();
		String contentTitle = "StandOutWindow Example";
		String contentText = "Click to add a new StandOutWindow.";
		String tickerText = String.format("%s: %s", contentTitle, contentText);

		Intent notificationIntent = StandOutWindow.getShowIntent(this,
				StandOutMultiWindow.class, id + 1);

		PendingIntent contentIntent = PendingIntent.getService(this, 0,
				notificationIntent,
				// flag updates existing notification
				PendingIntent.FLAG_UPDATE_CURRENT);

		Notification notification = new Notification(icon, tickerText, when);
		notification.setLatestEventInfo(c, contentTitle, contentText,
				contentIntent);
		return notification;
	}

	@Override
	protected Notification getHiddenNotification(int id) {
		int icon = android.R.drawable.ic_menu_info_details;
		long when = System.currentTimeMillis();
		Context c = getApplicationContext();
		String contentTitle = "Hidden StandOutWindow Example";
		String contentText = "Click to restore #" + id + ".";
		String tickerText = String.format("%s: %s", contentTitle, contentText);

		Intent notificationIntent = StandOutWindow.getShowIntent(this,
				StandOutMultiWindow.class, id);

		PendingIntent contentIntent = PendingIntent.getService(this, 0,
				notificationIntent,
				// flag updates existing notification
				PendingIntent.FLAG_UPDATE_CURRENT);

		Notification notification = new Notification(icon, tickerText, when);
		notification.setLatestEventInfo(c, contentTitle, contentText,
				contentIntent);
		return notification;
	}

	@Override
	protected boolean onShow(int id, View view) {
		super.onShow(id, view);

		this.nextId = Math.max(this.nextId, id) + 1;

		return false;
	}
}

@@ -428,16 +428,65 @@
 ndow %7B%0A%0A
+%09// we need to create a view for the window body%0A
 %09@Overri
@@ -578,16 +578,57 @@
 efault:%0A
+%09%09%09%09// create a new layout from body.xml%0A
 %09%09%09%09Layo
@@ -903,41 +903,44 @@
 %7D%0A%0A%09
-private int nextId = DEFAULT_ID;%0A
+// every window is initially 200x200
 %0A%09@O
@@ -1006,96 +1006,8 @@
 ) %7B%0A
-%09%09LayoutParams params = (LayoutParams) view.getLayoutParams();%0A%09%09if (params == null) %7B%0A%09
 %09%09re
@@ -1043,41 +1043,8 @@
 0);%0A
-%09%09%7D else %7B%0A%09%09%09return params;%0A%09%09%7D%0A
 %09%7D%0A%0A
@@ -1084,16 +1084,46 @@
 orations
+, and we want to drag the body
 %0A%09@Overr
@@ -1217,24 +1217,92 @@
 ENABLE;%0A%09%7D%0A%0A
+%09// our persistent notification creates a new window on every click%0A
 %09@Override%0A%09
@@ -1352,32 +1352,141 @@
 ation(int id) %7B%0A
+%09%09// basic notification stuff%0A%09%09// http://developer.android.com/guide/topics/ui/notifiers/notifications.html%0A
 %09%09int icon = and
@@ -1773,32 +1773,177 @@
  contentText);%0A%0A
+%09%09// getPersistentNotification() is called for every new window%0A%09%09// so we replace the old notification with a new one that has%0A%09%09// a bigger id%0A
 %09%09Intent notific
@@ -2143,32 +2143,43 @@
 pdates existing 
+persistent 
 notification%0A%09%09%09
@@ -2391,32 +2391,92 @@
 tification;%0A%09%7D%0A%0A
+%09// each hidden notification will restore the hidden window%0A
 %09@Override%0A%09prot
@@ -2526,16 +2526,64 @@
 t id) %7B%0A
+%09%09// same basics as getPersistentNotification()%0A
 %09%09int ic
@@ -2897,16 +2897,73 @@
 Text);%0A%0A
+%09%09// the difference here is we are providing the same id%0A
 %09%09Intent
@@ -3149,88 +3149,10 @@
 ent,
-%0A%09%09%09%09// flag updates existing notification%0A%09%09%09%09PendingIntent.FLAG_UPDATE_CURRENT
+ 0
 );%0A%0A
@@ -3336,160 +3336,7 @@
 ;%0A%09%7D
-%0A%0A%09@Override%0A%09protected boolean onShow(int id, View view) %7B%0A%09%09super.onShow(id, view);%0A%0A%09%09this.nextId = Math.max(this.nextId, id) + 1;%0A%0A%09%09return false;%0A%09%7D
 %0A%7D%0A

comment example