1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.content; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.os.Build; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 /** 25 * A single undoable operation. You must subclass this to implement the state 26 * and behavior for your operation. Instances of this class are placed and 27 * managed in an {@link UndoManager}. 28 * 29 * @hide 30 */ 31 public abstract class UndoOperation<DATA> implements Parcelable { 32 UndoOwner mOwner; 33 34 /** 35 * Create a new instance of the operation. 36 * @param owner Who owns the data being modified by this undo state; must be 37 * returned by {@link UndoManager#getOwner(String, Object) UndoManager.getOwner}. 38 */ 39 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) UndoOperation(UndoOwner owner)40 public UndoOperation(UndoOwner owner) { 41 mOwner = owner; 42 } 43 44 /** 45 * Construct from a Parcel. 46 */ 47 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) UndoOperation(Parcel src, ClassLoader loader)48 protected UndoOperation(Parcel src, ClassLoader loader) { 49 } 50 51 /** 52 * Owning object as given to {@link #UndoOperation(UndoOwner)}. 53 */ getOwner()54 public UndoOwner getOwner() { 55 return mOwner; 56 } 57 58 /** 59 * Synonym for {@link #getOwner()}.{@link android.content.UndoOwner#getData()}. 60 */ getOwnerData()61 public DATA getOwnerData() { 62 return (DATA)mOwner.getData(); 63 } 64 65 /** 66 * Return true if this undo operation is a member of the given owner. 67 * The default implementation is <code>owner == getOwner()</code>. You 68 * can override this to provide more sophisticated dependencies between 69 * owners. 70 */ matchOwner(UndoOwner owner)71 public boolean matchOwner(UndoOwner owner) { 72 return owner == getOwner(); 73 } 74 75 /** 76 * Return true if this operation actually contains modification data. The 77 * default implementation always returns true. If you return false, the 78 * operation will be dropped when the final undo state is being built. 79 */ hasData()80 public boolean hasData() { 81 return true; 82 } 83 84 /** 85 * Return true if this operation can be merged with a later operation. 86 * The default implementation always returns true. 87 */ allowMerge()88 public boolean allowMerge() { 89 return true; 90 } 91 92 /** 93 * Called when this undo state is being committed to the undo stack. 94 * The implementation should perform the initial edits and save any state that 95 * may be needed to undo them. 96 */ commit()97 public abstract void commit(); 98 99 /** 100 * Called when this undo state is being popped off the undo stack (in to 101 * the temporary redo stack). The implementation should remove the original 102 * edits and thus restore the target object to its prior value. 103 */ undo()104 public abstract void undo(); 105 106 /** 107 * Called when this undo state is being pushed back from the transient 108 * redo stack to the main undo stack. The implementation should re-apply 109 * the edits that were previously removed by {@link #undo}. 110 */ redo()111 public abstract void redo(); 112 describeContents()113 public int describeContents() { 114 return 0; 115 } 116 } 117