1 /* 2 * Copyright (C) 2015 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 package com.android.messaging.datamodel.binding; 17 18 import com.android.messaging.util.Assert; 19 20 /** 21 * An extension on {@link Binding} that allows for temporary data detachment from the UI component. 22 * This is used when, instead of destruction or data rebinding, the owning UI undergoes a 23 * "detached from window" -> "re-attached to window" transition, in which case we want to 24 * temporarily unbind the data and remember it so that it can be rebound when the UI is re-attached 25 * to window later. 26 */ 27 public class DetachableBinding<T extends BindableData> extends Binding<T> { 28 private T mDetachedData; 29 30 DetachableBinding(Object owner) { 31 super(owner); 32 } 33 34 @Override 35 public void bind(T data) { 36 super.bind(data); 37 // Rebinding before re-attaching. Pre-emptively throw away the detached data because 38 // it's now stale. 39 mDetachedData = null; 40 } 41 42 public void detach() { 43 Assert.isNull(mDetachedData); 44 Assert.isTrue(isBound()); 45 mDetachedData = getData(); 46 unbind(); 47 } 48 49 public void reAttachIfPossible() { 50 if (mDetachedData != null) { 51 Assert.isFalse(isBound()); 52 bind(mDetachedData); 53 mDetachedData = null; 54 } 55 } 56 } 57