Code example for Parcel

Methods: readBundle, readByte, readDouble, readFloat, readInt, readLong, readString

0
		   mValue = value;
		   mMode = mode;
	   } 
	    
	   public SetLayoutSize(Parcel parcel) {
		   mViewId = parcel.readInt();
		   mMode = parcel.readInt();
		   mValue = parcel.readInt();
	   } 
	    
	   public void writeToParcel(Parcel dest, int flags) {
		   dest.writeInt(TAG);
		   dest.writeInt(mViewId);
		   dest.writeInt(mMode);
		   dest.writeInt(mValue);
	   } 
	    
	    
	   @Override 
	   public void apply(View root) throws ActionException {
		   View target = root.findViewById(mViewId);
		   if (target != null) {
			   ViewGroup.LayoutParams lps = target.getLayoutParams();
			   if (mMode == WIDTH)
				   lps.width = mValue;
			   else 
				   lps.height = mValue;
			   target.setLayoutParams(lps);
		   } 
	   } 
   } 
    
    
   /** 
    * Equivalent to calling a combination of {@link Drawable#setAlpha(int)}, 
    * {@link Drawable#setColorFilter(int, android.graphics.PorterDuff.Mode)}, 
    * and/or {@link Drawable#setLevel(int)} on the {@link Drawable} of a given view. 
    * <p> 
    * These operations will be performed on the {@link Drawable} returned by the 
    * target {@link View#getBackground()} by default.  If targetBackground is false, 
    * we assume the target is an {@link ImageView} and try applying the operations 
    * to {@link ImageView#getDrawable()}. 
    * <p> 
    * You can omit specific calls by marking their values with null or -1. 
    */ 
   private class SetDrawableParameters extends Action { 
       public SetDrawableParameters(int id, boolean targetBackground, int alpha,
               int colorFilter, PorterDuff.Mode mode, int level) {
           this.viewId = id;
           this.targetBackground = targetBackground;
           this.alpha = alpha;
           this.colorFilter = colorFilter;
           this.filterMode = mode;
           this.level = level;
       } 
        
       public SetDrawableParameters(Parcel parcel) {
           viewId = parcel.readInt();
           targetBackground = parcel.readInt() != 0;
           alpha = parcel.readInt();
           colorFilter = parcel.readInt();
           boolean hasMode = parcel.readInt() != 0;
           if (hasMode) {
               filterMode = PorterDuff.Mode.valueOf(parcel.readString());
           } else { 
               filterMode = null;
           } 
           level = parcel.readInt();
       } 
        
       public void writeToParcel(Parcel dest, int flags) {
           dest.writeInt(TAG);
           dest.writeInt(viewId);
           dest.writeInt(targetBackground ? 1 : 0);
           dest.writeInt(alpha);
           dest.writeInt(colorFilter);
           if (filterMode != null) {
               dest.writeInt(1);
               dest.writeString(filterMode.toString());
           } else { 
               dest.writeInt(0);
           } 
           dest.writeInt(level);
       } 
        
       @Override 
       public void apply(View root) {
           final View target = root.findViewById(viewId);
           if (target == null) {
               return; 
           } 
            
           // Pick the correct drawable to modify for this view 
           Drawable targetDrawable = null;
           if (targetBackground) {
               targetDrawable = target.getBackground();
           } else if (target instanceof ImageView) {
               ImageView imageView = (ImageView) target;
               targetDrawable = imageView.getDrawable();
           } 
            
           if (targetDrawable != null) {
               // Perform modifications only if values are set correctly 
               if (alpha != -1) {
                   targetDrawable.setAlpha(alpha);
               } 
               if (colorFilter != -1 && filterMode != null) {
                   targetDrawable.setColorFilter(colorFilter, filterMode);
               } 
               if (level != -1) {
                   targetDrawable.setLevel(level);
               } 
           } 
       } 
        
       int viewId;
       boolean targetBackground;
       int alpha;
       int colorFilter;
       PorterDuff.Mode filterMode;
       int level;
 
       public final static int TAG = 3;
   } 
    
   /** 
    * Equivalent to calling 
    * {@link android.view.View#setOnClickListener(android.view.View.OnClickListener)} 
    * to launch the provided {@link PendingIntent}. 
    */ 
   protected class SetOnClickPendingIntent extends Action { 
       public SetOnClickPendingIntent(int id, PendingIntent pendingIntent) {
           this.viewId = id;
           this.pendingIntent = pendingIntent;
       } 
        
       public SetOnClickPendingIntent(Parcel parcel) {
           viewId = parcel.readInt();
           pendingIntent = PendingIntent.readPendingIntentOrNullFromParcel(parcel);
       } 
        
        
       public void writeToParcel(Parcel dest, int flags) {
           dest.writeInt(TAG);
           dest.writeInt(viewId);
           pendingIntent.writeToParcel(dest, 0 /* no flags */);
       } 
        
       @Override 
       public void apply(View root) {
           final View target = root.findViewById(viewId);           
           if (target != null && pendingIntent != null) {
               OnClickListener listener = new OnClickListener() {
                   public void onClick(View v) {
                       // Find target view location in screen coordinates and 
                       // fill into PendingIntent before sending. 
                	   final int[] location = new int[2];
                       v.getLocationOnScreen(location);
                       Rect srcRect = new Rect();
                       srcRect.left = location[0];
                       srcRect.top = location[1];
                       srcRect.right = srcRect.left + v.getWidth();
                       srcRect.bottom = srcRect.top + v.getHeight();
 
                       final Intent intent = new Intent();
                       intent.setSourceBounds(srcRect);
                       try { 
                       	pendingIntent.send(v.getContext(), 0, intent, null, null);
                       } catch (PendingIntent.CanceledException e) {
                           android.util.Log.e("SetOnClickPendingIntent", "Cannot send pending intent: ", e);
                       } 
                   } 
               }; 
               target.setOnClickListener(listener);
           } 
       } 
               
       int viewId;
       PendingIntent pendingIntent;
 
       public final static int TAG = 1;
   } 
    
   /** 
    * Base class for the reflection actions. 
    */ 
   protected class ReflectionAction extends Action { 
       static final int TAG = 2;
 
       static final int BOOLEAN = 1;
       static final int BYTE = 2;
       static final int SHORT = 3;
       static final int INT = 4;
       static final int LONG = 5;
       static final int FLOAT = 6;
       static final int DOUBLE = 7;
       static final int CHAR = 8;
       static final int STRING = 9;
       static final int CHAR_SEQUENCE = 10;
       static final int URI = 11;
       static final int BITMAP = 12;
       static final int BUNDLE = 13;
 
       int viewId;
       String methodName;
       int type;
       Object value;
 
       ReflectionAction(int viewId, String methodName, int type, Object value) {
    	   this(viewId, methodName, type);
    	   this.value = value;
       } 
        
       protected ReflectionAction(int viewId, String methodName, int type) {
           this.viewId = viewId;
           this.methodName = methodName;
           this.type = type;        
       } 
 
       ReflectionAction(Parcel in) {
           this.viewId = in.readInt();
           this.methodName = in.readString();
           this.type = in.readInt();
           readValue(in);
       } 
        
       protected void readValue(Parcel in) {
           switch (this.type) {
               case BOOLEAN:
                   this.value = in.readInt() != 0;
                   break; 
               case BYTE:
                   this.value = in.readByte();
                   break; 
               case SHORT:
                   this.value = (short)in.readInt();
                   break; 
               case INT:
                   this.value = in.readInt();
                   break; 
               case LONG:
                   this.value = in.readLong();
                   break; 
               case FLOAT:
                   this.value = in.readFloat();
                   break; 
               case DOUBLE:
                   this.value = in.readDouble();
                   break; 
               case CHAR:
                   this.value = (char)in.readInt();
                   break; 
               case STRING:
                   this.value = in.readString();
                   break; 
               case CHAR_SEQUENCE:
                   this.value = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
                   break; 
               case URI:
                   this.value = Uri.CREATOR.createFromParcel(in);
                   break; 
               case BITMAP:
                   this.value = Bitmap.CREATOR.createFromParcel(in);
                   break; 
               case BUNDLE:
                   this.value = in.readBundle();
                   break; 
               default: 
                   break; 
           } 
       }