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 17 package com.android.messaging.datamodel.binding; 18 19 /** 20 * Base class for data objects that will be bound to a piece of the UI 21 */ 22 public abstract class BindableData { 23 /** 24 * Called by Binding during unbind to allow data to proactively unregister callbacks 25 * Data instance should release all listeners that may call back to the host UI 26 */ unregisterListeners()27 protected abstract void unregisterListeners(); 28 29 /** 30 * Key used to identify the piece of UI that the data is currently bound to 31 */ 32 private String mBindingId; 33 34 /** 35 * Bind this data to the ui host - checks data is currently unbound 36 */ bind(final String bindingId)37 public void bind(final String bindingId) { 38 if (isBound() || bindingId == null) { 39 throw new IllegalStateException(); 40 } 41 mBindingId = bindingId; 42 } 43 44 /** 45 * Unbind this data from the ui host - checks that the data is currently bound to specified id 46 */ unbind(final String bindingId)47 public void unbind(final String bindingId) { 48 if (!isBound(bindingId)) { 49 throw new IllegalStateException(); 50 } 51 unregisterListeners(); 52 mBindingId = null; 53 } 54 55 /** 56 * Check to see if the data is bound to anything 57 * 58 * TODO: This should be package private because it's supposed to only be used by Binding, 59 * however, several classes call this directly. We want the classes to track what they are 60 * bound to. 61 */ isBound()62 protected boolean isBound() { 63 return (mBindingId != null); 64 } 65 66 /** 67 * Check to see if data is still bound with specified bindingId before calling over to ui 68 */ isBound(final String bindingId)69 public boolean isBound(final String bindingId) { 70 return (bindingId.equals(mBindingId)); 71 } 72 } 73