Merge "Fix toasts." into honeycomb
This commit is contained in:
@@ -33,8 +33,6 @@ import android.view.WindowManagerImpl;
|
|||||||
import android.view.accessibility.AccessibilityEvent;
|
import android.view.accessibility.AccessibilityEvent;
|
||||||
import android.view.accessibility.AccessibilityManager;
|
import android.view.accessibility.AccessibilityManager;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A toast is a view containing a quick little message for the user. The toast class
|
* A toast is a view containing a quick little message for the user. The toast class
|
||||||
* helps you create and show those.
|
* helps you create and show those.
|
||||||
@@ -72,11 +70,6 @@ public class Toast {
|
|||||||
final Context mContext;
|
final Context mContext;
|
||||||
final TN mTN;
|
final TN mTN;
|
||||||
int mDuration;
|
int mDuration;
|
||||||
int mGravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
|
|
||||||
int mX, mY;
|
|
||||||
float mHorizontalMargin;
|
|
||||||
float mVerticalMargin;
|
|
||||||
View mView;
|
|
||||||
View mNextView;
|
View mNextView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,8 +81,8 @@ public class Toast {
|
|||||||
*/
|
*/
|
||||||
public Toast(Context context) {
|
public Toast(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mTN = new TN(this);
|
mTN = new TN();
|
||||||
mY = context.getResources().getDimensionPixelSize(
|
mTN.mY = context.getResources().getDimensionPixelSize(
|
||||||
com.android.internal.R.dimen.toast_y_offset);
|
com.android.internal.R.dimen.toast_y_offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,9 +96,11 @@ public class Toast {
|
|||||||
|
|
||||||
INotificationManager service = getService();
|
INotificationManager service = getService();
|
||||||
String pkg = mContext.getPackageName();
|
String pkg = mContext.getPackageName();
|
||||||
|
TN tn = mTN;
|
||||||
|
tn.mNextView = mNextView;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
service.enqueueToast(pkg, mTN, mDuration);
|
service.enqueueToast(pkg, tn, mDuration);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
// Empty
|
// Empty
|
||||||
}
|
}
|
||||||
@@ -165,22 +160,22 @@ public class Toast {
|
|||||||
* notification
|
* notification
|
||||||
*/
|
*/
|
||||||
public void setMargin(float horizontalMargin, float verticalMargin) {
|
public void setMargin(float horizontalMargin, float verticalMargin) {
|
||||||
mHorizontalMargin = horizontalMargin;
|
mTN.mHorizontalMargin = horizontalMargin;
|
||||||
mVerticalMargin = verticalMargin;
|
mTN.mVerticalMargin = verticalMargin;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the horizontal margin.
|
* Return the horizontal margin.
|
||||||
*/
|
*/
|
||||||
public float getHorizontalMargin() {
|
public float getHorizontalMargin() {
|
||||||
return mHorizontalMargin;
|
return mTN.mHorizontalMargin;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the vertical margin.
|
* Return the vertical margin.
|
||||||
*/
|
*/
|
||||||
public float getVerticalMargin() {
|
public float getVerticalMargin() {
|
||||||
return mVerticalMargin;
|
return mTN.mVerticalMargin;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -189,9 +184,9 @@ public class Toast {
|
|||||||
* @see #getGravity
|
* @see #getGravity
|
||||||
*/
|
*/
|
||||||
public void setGravity(int gravity, int xOffset, int yOffset) {
|
public void setGravity(int gravity, int xOffset, int yOffset) {
|
||||||
mGravity = gravity;
|
mTN.mGravity = gravity;
|
||||||
mX = xOffset;
|
mTN.mX = xOffset;
|
||||||
mY = yOffset;
|
mTN.mY = yOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -200,21 +195,21 @@ public class Toast {
|
|||||||
* @see #getGravity
|
* @see #getGravity
|
||||||
*/
|
*/
|
||||||
public int getGravity() {
|
public int getGravity() {
|
||||||
return mGravity;
|
return mTN.mGravity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the X offset in pixels to apply to the gravity's location.
|
* Return the X offset in pixels to apply to the gravity's location.
|
||||||
*/
|
*/
|
||||||
public int getXOffset() {
|
public int getXOffset() {
|
||||||
return mX;
|
return mTN.mX;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the Y offset in pixels to apply to the gravity's location.
|
* Return the Y offset in pixels to apply to the gravity's location.
|
||||||
*/
|
*/
|
||||||
public int getYOffset() {
|
public int getYOffset() {
|
||||||
return mY;
|
return mTN.mY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -281,21 +276,6 @@ public class Toast {
|
|||||||
tv.setText(s);
|
tv.setText(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void trySendAccessibilityEvent() {
|
|
||||||
AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(mContext);
|
|
||||||
if (!accessibilityManager.isEnabled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// treat toasts as notifications since they are used to
|
|
||||||
// announce a transient piece of information to the user
|
|
||||||
AccessibilityEvent event = AccessibilityEvent.obtain(
|
|
||||||
AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
|
|
||||||
event.setClassName(getClass().getName());
|
|
||||||
event.setPackageName(mContext.getPackageName());
|
|
||||||
mView.dispatchPopulateAccessibilityEvent(event);
|
|
||||||
accessibilityManager.sendAccessibilityEvent(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
// =======================================================================================
|
// =======================================================================================
|
||||||
// All the gunk below is the interaction with the Notification Service, which handles
|
// All the gunk below is the interaction with the Notification Service, which handles
|
||||||
// the proper ordering of these system-wide.
|
// the proper ordering of these system-wide.
|
||||||
@@ -312,8 +292,6 @@ public class Toast {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static class TN extends ITransientNotification.Stub {
|
private static class TN extends ITransientNotification.Stub {
|
||||||
final Handler mHandler = new Handler();
|
|
||||||
|
|
||||||
final Runnable mShow = new Runnable() {
|
final Runnable mShow = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
handleShow();
|
handleShow();
|
||||||
@@ -327,12 +305,20 @@ public class Toast {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
|
private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
|
||||||
private final WeakReference<Toast> mToast;
|
final Handler mHandler = new Handler();
|
||||||
|
|
||||||
|
int mGravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
|
||||||
|
int mX, mY;
|
||||||
|
float mHorizontalMargin;
|
||||||
|
float mVerticalMargin;
|
||||||
|
|
||||||
|
|
||||||
|
View mView;
|
||||||
|
View mNextView;
|
||||||
|
|
||||||
WindowManagerImpl mWM;
|
WindowManagerImpl mWM;
|
||||||
|
|
||||||
TN(Toast toast) {
|
TN() {
|
||||||
mToast = new WeakReference<Toast>(toast);
|
|
||||||
// XXX This should be changed to use a Dialog, with a Theme.Toast
|
// XXX This should be changed to use a Dialog, with a Theme.Toast
|
||||||
// defined that sets up the layout params appropriately.
|
// defined that sets up the layout params appropriately.
|
||||||
final WindowManager.LayoutParams params = mParams;
|
final WindowManager.LayoutParams params = mParams;
|
||||||
@@ -364,53 +350,64 @@ public class Toast {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void handleShow() {
|
public void handleShow() {
|
||||||
final Toast toast = mToast.get();
|
if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
|
||||||
if (toast != null) {
|
+ " mNextView=" + mNextView);
|
||||||
if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + toast.mView
|
if (mView != mNextView) {
|
||||||
+ " mNextView=" + toast.mNextView);
|
// remove the old view if necessary
|
||||||
if (toast.mView != toast.mNextView) {
|
handleHide();
|
||||||
// remove the old view if necessary
|
mView = mNextView;
|
||||||
handleHide();
|
mWM = WindowManagerImpl.getDefault();
|
||||||
toast.mView = toast.mNextView;
|
final int gravity = mGravity;
|
||||||
mWM = WindowManagerImpl.getDefault();
|
mParams.gravity = gravity;
|
||||||
final int gravity = toast.mGravity;
|
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
|
||||||
mParams.gravity = gravity;
|
mParams.horizontalWeight = 1.0f;
|
||||||
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
|
|
||||||
mParams.horizontalWeight = 1.0f;
|
|
||||||
}
|
|
||||||
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
|
|
||||||
mParams.verticalWeight = 1.0f;
|
|
||||||
}
|
|
||||||
mParams.x = toast.mX;
|
|
||||||
mParams.y = toast.mY;
|
|
||||||
mParams.verticalMargin = toast.mVerticalMargin;
|
|
||||||
mParams.horizontalMargin = toast.mHorizontalMargin;
|
|
||||||
if (toast.mView.getParent() != null) {
|
|
||||||
if (localLOGV) Log.v(TAG, "REMOVE! " + toast.mView + " in " + this);
|
|
||||||
mWM.removeView(toast.mView);
|
|
||||||
}
|
|
||||||
if (localLOGV) Log.v(TAG, "ADD! " + toast.mView + " in " + this);
|
|
||||||
mWM.addView(toast.mView, mParams);
|
|
||||||
toast.trySendAccessibilityEvent();
|
|
||||||
}
|
}
|
||||||
|
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
|
||||||
|
mParams.verticalWeight = 1.0f;
|
||||||
|
}
|
||||||
|
mParams.x = mX;
|
||||||
|
mParams.y = mY;
|
||||||
|
mParams.verticalMargin = mVerticalMargin;
|
||||||
|
mParams.horizontalMargin = mHorizontalMargin;
|
||||||
|
if (mView.getParent() != null) {
|
||||||
|
if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
|
||||||
|
mWM.removeView(mView);
|
||||||
|
}
|
||||||
|
if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this);
|
||||||
|
mWM.addView(mView, mParams);
|
||||||
|
trySendAccessibilityEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleHide() {
|
private void trySendAccessibilityEvent() {
|
||||||
final Toast toast = mToast.get();
|
AccessibilityManager accessibilityManager =
|
||||||
if (toast != null) {
|
AccessibilityManager.getInstance(mView.getContext());
|
||||||
if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + toast.mView);
|
if (!accessibilityManager.isEnabled()) {
|
||||||
if (toast.mView != null) {
|
return;
|
||||||
// note: checking parent() just to make sure the view has
|
}
|
||||||
// been added... i have seen cases where we get here when
|
// treat toasts as notifications since they are used to
|
||||||
// the view isn't yet added, so let's try not to crash.
|
// announce a transient piece of information to the user
|
||||||
if (toast.mView.getParent() != null) {
|
AccessibilityEvent event = AccessibilityEvent.obtain(
|
||||||
if (localLOGV) Log.v(TAG, "REMOVE! " + toast.mView + " in " + this);
|
AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
|
||||||
mWM.removeView(toast.mView);
|
event.setClassName(getClass().getName());
|
||||||
}
|
event.setPackageName(mView.getContext().getPackageName());
|
||||||
|
mView.dispatchPopulateAccessibilityEvent(event);
|
||||||
|
accessibilityManager.sendAccessibilityEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
toast.mView = null;
|
public void handleHide() {
|
||||||
|
if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + mView);
|
||||||
|
if (mView != null) {
|
||||||
|
// note: checking parent() just to make sure the view has
|
||||||
|
// been added... i have seen cases where we get here when
|
||||||
|
// the view isn't yet added, so let's try not to crash.
|
||||||
|
if (mView.getParent() != null) {
|
||||||
|
if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
|
||||||
|
mWM.removeView(mView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mView = null;
|
||||||
|
mNextView = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user