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.Parcel; 21 import android.os.Parcelable; 22 23 /** 24 * A single undoable operation. You must subclass this to implement the state 25 * and behavior for your operation. Instances of this class are placed and 26 * managed in an {@link UndoManager}. 27 * 28 * @hide 29 */ 30 public abstract class UndoOperation<DATA> implements Parcelable { 31 UndoOwner mOwner; 32 33 /** 34 * Create a new instance of the operation. 35 * @param owner Who owns the data being modified by this undo state; must be 36 * returned by {@link UndoManager#getOwner(String, Object) UndoManager.getOwner}. 37 */ 38 @UnsupportedAppUsage UndoOperation(UndoOwner owner)39 public UndoOperation(UndoOwner owner) { 40 mOwner = owner; 41 } 42 43 /** 44 * Construct from a Parcel. 45 */ 46 @UnsupportedAppUsage UndoOperation(Parcel src, ClassLoader loader)47 protected UndoOperation(Parcel src, ClassLoader loader) { 48 } 49 50 /** 51 * Owning object as given to {@link #UndoOperation(UndoOwner)}. 52 */ getOwner()53 public UndoOwner getOwner() { 54 return mOwner; 55 } 56 57 /** 58 * Synonym for {@link #getOwner()}.{@link android.content.UndoOwner#getData()}. 59 */ getOwnerData()60 public DATA getOwnerData() { 61 return (DATA)mOwner.getData(); 62 } 63 64 /** 65 * Return true if this undo operation is a member of the given owner. 66 * The default implementation is <code>owner == getOwner()</code>. You 67 * can override this to provide more sophisticated dependencies between 68 * owners. 69 */ matchOwner(UndoOwner owner)70 public boolean matchOwner(UndoOwner owner) { 71 return owner == getOwner(); 72 } 73 74 /** 75 * Return true if this operation actually contains modification data. The 76 * default implementation always returns true. If you return false, the 77 * operation will be dropped when the final undo state is being built. 78 */ hasData()79 public boolean hasData() { 80 return true; 81 } 82 83 /** 84 * Return true if this operation can be merged with a later operation. 85 * The default implementation always returns true. 86 */ allowMerge()87 public boolean allowMerge() { 88 return true; 89 } 90 91 /** 92 * Called when this undo state is being committed to the undo stack. 93 * The implementation should perform the initial edits and save any state that 94 * may be needed to undo them. 95 */ commit()96 public abstract void commit(); 97 98 /** 99 * Called when this undo state is being popped off the undo stack (in to 100 * the temporary redo stack). The implementation should remove the original 101 * edits and thus restore the target object to its prior value. 102 */ undo()103 public abstract void undo(); 104 105 /** 106 * Called when this undo state is being pushed back from the transient 107 * redo stack to the main undo stack. The implementation should re-apply 108 * the edits that were previously removed by {@link #undo}. 109 */ redo()110 public abstract void redo(); 111 describeContents()112 public int describeContents() { 113 return 0; 114 } 115 } 116