1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mms.model; 19 20 import com.android.mms.layout.LayoutManager; 21 import com.android.mms.layout.LayoutParameters; 22 23 import android.util.Config; 24 import android.util.Log; 25 26 import java.util.ArrayList; 27 28 public class LayoutModel extends Model { 29 private static final String TAG = "LayoutModel"; 30 private static final boolean DEBUG = false; 31 private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV; 32 33 public static final String IMAGE_REGION_ID = "Image"; 34 public static final String TEXT_REGION_ID = "Text"; 35 36 public static final int LAYOUT_BOTTOM_TEXT = 0; 37 public static final int LAYOUT_TOP_TEXT = 1; 38 public static final int DEFAULT_LAYOUT_TYPE = LAYOUT_BOTTOM_TEXT; 39 40 private int mLayoutType = DEFAULT_LAYOUT_TYPE; 41 private RegionModel mRootLayout; 42 private RegionModel mImageRegion; 43 private RegionModel mTextRegion; 44 private ArrayList<RegionModel> mNonStdRegions; 45 private LayoutParameters mLayoutParams; 46 LayoutModel()47 public LayoutModel() { 48 mLayoutParams = LayoutManager.getInstance().getLayoutParameters(); 49 // Create default root-layout and regions. 50 createDefaultRootLayout(); 51 createDefaultImageRegion(); 52 createDefaultTextRegion(); 53 } 54 LayoutModel(ArrayList<RegionModel> layouts)55 public LayoutModel(ArrayList<RegionModel> layouts) { 56 mLayoutParams = LayoutManager.getInstance().getLayoutParameters(); 57 // Root-Layout must be always provided as the first element. 58 mRootLayout = layouts.get(0); 59 mNonStdRegions = new ArrayList<RegionModel>(); 60 61 int size = layouts.size(); 62 if (size > 1) { 63 for (int i = 1; i < size; i++) { 64 RegionModel r = layouts.get(i); 65 String rId = r.getRegionId(); 66 if (rId.equals(IMAGE_REGION_ID)) { 67 mImageRegion = r; 68 } else if (rId.equals(TEXT_REGION_ID)) { 69 mTextRegion = r; 70 } else { 71 if (LOCAL_LOGV) { 72 Log.v(TAG, "Found non-standard region: " + rId); 73 } 74 mNonStdRegions.add(r); 75 } 76 } 77 } 78 79 validateLayouts(); 80 } 81 LayoutModel(RegionModel rootLayout, ArrayList<RegionModel> regions)82 public LayoutModel(RegionModel rootLayout, ArrayList<RegionModel> regions) { 83 mLayoutParams = LayoutManager.getInstance().getLayoutParameters(); 84 mRootLayout = rootLayout; 85 mNonStdRegions = new ArrayList<RegionModel>(); 86 87 for (RegionModel r : regions) { 88 String rId = r.getRegionId(); 89 if (rId.equals(IMAGE_REGION_ID)) { 90 mImageRegion = r; 91 } else if (rId.equals(TEXT_REGION_ID)) { 92 mTextRegion = r; 93 } else { 94 if (LOCAL_LOGV) { 95 Log.v(TAG, "Found non-standard region: " + rId); 96 } 97 mNonStdRegions.add(r); 98 } 99 } 100 101 validateLayouts(); 102 } 103 createDefaultRootLayout()104 private void createDefaultRootLayout() { 105 mRootLayout = new RegionModel(null, 0, 0, mLayoutParams.getWidth(), 106 mLayoutParams.getHeight()); 107 } 108 createDefaultImageRegion()109 private void createDefaultImageRegion() { 110 if (mRootLayout == null) { 111 throw new IllegalStateException("Root-Layout uninitialized."); 112 } 113 114 mImageRegion = new RegionModel(IMAGE_REGION_ID, 0, 0, 115 mRootLayout.getWidth(), mLayoutParams.getImageHeight()); 116 } 117 createDefaultTextRegion()118 private void createDefaultTextRegion() { 119 if (mRootLayout == null) { 120 throw new IllegalStateException("Root-Layout uninitialized."); 121 } 122 123 mTextRegion = new RegionModel( 124 TEXT_REGION_ID, 0, mLayoutParams.getImageHeight(), 125 mRootLayout.getWidth(), mLayoutParams.getTextHeight()); 126 } 127 validateLayouts()128 private void validateLayouts() { 129 if (mRootLayout == null) { 130 createDefaultRootLayout(); 131 } 132 133 if (mImageRegion == null) { 134 createDefaultImageRegion(); 135 } 136 137 if (mTextRegion == null) { 138 createDefaultTextRegion(); 139 } 140 } 141 getRootLayout()142 public RegionModel getRootLayout() { 143 return mRootLayout; 144 } 145 setRootLayout(RegionModel rootLayout)146 public void setRootLayout(RegionModel rootLayout) { 147 mRootLayout = rootLayout; 148 } 149 getImageRegion()150 public RegionModel getImageRegion() { 151 return mImageRegion; 152 } 153 setImageRegion(RegionModel imageRegion)154 public void setImageRegion(RegionModel imageRegion) { 155 mImageRegion = imageRegion; 156 } 157 getTextRegion()158 public RegionModel getTextRegion() { 159 return mTextRegion; 160 } 161 setTextRegion(RegionModel textRegion)162 public void setTextRegion(RegionModel textRegion) { 163 mTextRegion = textRegion; 164 } 165 166 /** 167 * Get all regions except root-layout. The result is READ-ONLY. 168 */ getRegions()169 public ArrayList<RegionModel> getRegions() { 170 ArrayList<RegionModel> regions = new ArrayList<RegionModel>(); 171 if (mImageRegion != null) { 172 regions.add(mImageRegion); 173 } 174 if (mTextRegion != null) { 175 regions.add(mTextRegion); 176 } 177 return regions; 178 } 179 findRegionById(String rId)180 public RegionModel findRegionById(String rId) { 181 if (IMAGE_REGION_ID.equals(rId)) { 182 return mImageRegion; 183 } else if (TEXT_REGION_ID.equals(rId)) { 184 return mTextRegion; 185 } else { 186 for (RegionModel r : mNonStdRegions) { 187 if (r.getRegionId().equals(rId)) { 188 return r; 189 } 190 } 191 192 if (LOCAL_LOGV) { 193 Log.v(TAG, "Region not found: " + rId); 194 } 195 return null; 196 } 197 } 198 getLayoutWidth()199 public int getLayoutWidth() { 200 return mRootLayout.getWidth(); 201 } 202 getLayoutHeight()203 public int getLayoutHeight() { 204 return mRootLayout.getHeight(); 205 } 206 getBackgroundColor()207 public String getBackgroundColor() { 208 return mRootLayout.getBackgroundColor(); 209 } 210 changeTo(int layout)211 public void changeTo(int layout) { 212 if (mRootLayout == null) { 213 throw new IllegalStateException("Root-Layout uninitialized."); 214 } 215 216 if (mLayoutParams == null) { 217 mLayoutParams = LayoutManager.getInstance().getLayoutParameters(); 218 } 219 220 if (mLayoutType != layout) { 221 switch (layout) { 222 case LAYOUT_BOTTOM_TEXT: { 223 mImageRegion.setTop(0); 224 mTextRegion.setTop(mLayoutParams.getImageHeight()); 225 mLayoutType = layout; 226 notifyModelChanged(true); 227 } 228 break; 229 case LAYOUT_TOP_TEXT: { 230 mImageRegion.setTop(mLayoutParams.getTextHeight()); 231 mTextRegion.setTop(0); 232 mLayoutType = layout; 233 notifyModelChanged(true); 234 } 235 break; 236 default: { 237 Log.w(TAG, "Unknown layout type: " + layout); 238 } 239 } 240 } else { 241 if (LOCAL_LOGV) { 242 Log.v(TAG, "Skip changing layout."); 243 } 244 } 245 } 246 getLayoutType()247 public int getLayoutType() { 248 return mLayoutType; 249 } 250 251 @Override registerModelChangedObserverInDescendants( IModelChangedObserver observer)252 protected void registerModelChangedObserverInDescendants( 253 IModelChangedObserver observer) { 254 if (mRootLayout != null) { 255 mRootLayout.registerModelChangedObserver(observer); 256 } 257 258 if (mImageRegion != null) { 259 mImageRegion.registerModelChangedObserver(observer); 260 } 261 262 if (mTextRegion != null) { 263 mTextRegion.registerModelChangedObserver(observer); 264 } 265 } 266 267 @Override unregisterModelChangedObserverInDescendants( IModelChangedObserver observer)268 protected void unregisterModelChangedObserverInDescendants( 269 IModelChangedObserver observer) { 270 if (mRootLayout != null) { 271 mRootLayout.unregisterModelChangedObserver(observer); 272 } 273 274 if (mImageRegion != null) { 275 mImageRegion.unregisterModelChangedObserver(observer); 276 } 277 278 if (mTextRegion != null) { 279 mTextRegion.unregisterModelChangedObserver(observer); 280 } 281 } 282 283 @Override unregisterAllModelChangedObserversInDescendants()284 protected void unregisterAllModelChangedObserversInDescendants() { 285 if (mRootLayout != null) { 286 mRootLayout.unregisterAllModelChangedObservers(); 287 } 288 289 if (mImageRegion != null) { 290 mImageRegion.unregisterAllModelChangedObservers(); 291 } 292 293 if (mTextRegion != null) { 294 mTextRegion.unregisterAllModelChangedObservers(); 295 } 296 } 297 hasNonStdRegions()298 public boolean hasNonStdRegions() { 299 return mNonStdRegions.size() > 0; 300 } 301 } 302