• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Log;
24 
25 import java.util.ArrayList;
26 
27 public class LayoutModel extends Model {
28     private static final String TAG = SlideModel.TAG;
29     private static final boolean DEBUG = false;
30     private static final boolean LOCAL_LOGV = false;
31 
32     public static final String IMAGE_REGION_ID = "Image";
33     public static final String TEXT_REGION_ID  = "Text";
34 
35     public static final int LAYOUT_BOTTOM_TEXT = 0;
36     public static final int LAYOUT_TOP_TEXT    = 1;
37     public static final int DEFAULT_LAYOUT_TYPE = LAYOUT_BOTTOM_TEXT;
38 
39     private int mLayoutType = DEFAULT_LAYOUT_TYPE;
40     private RegionModel mRootLayout;
41     private RegionModel mImageRegion;
42     private RegionModel mTextRegion;
43     private ArrayList<RegionModel> mNonStdRegions;
44     private LayoutParameters mLayoutParams;
45 
LayoutModel()46     public LayoutModel() {
47         mLayoutParams = LayoutManager.getInstance().getLayoutParameters();
48         // Create default root-layout and regions.
49         createDefaultRootLayout();
50         createDefaultImageRegion();
51         createDefaultTextRegion();
52     }
53 
LayoutModel(RegionModel rootLayout, ArrayList<RegionModel> regions)54     public LayoutModel(RegionModel rootLayout, ArrayList<RegionModel> regions) {
55         mLayoutParams = LayoutManager.getInstance().getLayoutParameters();
56         mRootLayout = rootLayout;
57         mNonStdRegions = new ArrayList<RegionModel>();
58 
59         for (RegionModel r : regions) {
60             String rId = r.getRegionId();
61             if (rId.equals(IMAGE_REGION_ID)) {
62                 mImageRegion = r;
63             } else if (rId.equals(TEXT_REGION_ID)) {
64                 mTextRegion = r;
65             } else {
66                 if (LOCAL_LOGV) {
67                     Log.v(TAG, "Found non-standard region: " + rId);
68                 }
69                 mNonStdRegions.add(r);
70             }
71         }
72 
73         validateLayouts();
74     }
75 
createDefaultRootLayout()76     private void createDefaultRootLayout() {
77         mRootLayout = new RegionModel(null, 0, 0, mLayoutParams.getWidth(),
78                                                   mLayoutParams.getHeight());
79     }
80 
createDefaultImageRegion()81     private void createDefaultImageRegion() {
82         if (mRootLayout == null) {
83             throw new IllegalStateException("Root-Layout uninitialized.");
84         }
85 
86         mImageRegion = new RegionModel(IMAGE_REGION_ID, 0, 0,
87                 mRootLayout.getWidth(), mLayoutParams.getImageHeight());
88     }
89 
createDefaultTextRegion()90     private void createDefaultTextRegion() {
91         if (mRootLayout == null) {
92             throw new IllegalStateException("Root-Layout uninitialized.");
93         }
94 
95         mTextRegion = new RegionModel(
96                 TEXT_REGION_ID, 0, mLayoutParams.getImageHeight(),
97                 mRootLayout.getWidth(), mLayoutParams.getTextHeight());
98     }
99 
validateLayouts()100     private void validateLayouts() {
101         if (mRootLayout == null) {
102             createDefaultRootLayout();
103         }
104 
105         if (mImageRegion == null) {
106             createDefaultImageRegion();
107         }
108 
109         if (mTextRegion == null) {
110             createDefaultTextRegion();
111         }
112     }
113 
getRootLayout()114     public RegionModel getRootLayout() {
115         return mRootLayout;
116     }
117 
setRootLayout(RegionModel rootLayout)118     public void setRootLayout(RegionModel rootLayout) {
119         mRootLayout = rootLayout;
120     }
121 
getImageRegion()122     public RegionModel getImageRegion() {
123         return mImageRegion;
124     }
125 
setImageRegion(RegionModel imageRegion)126     public void setImageRegion(RegionModel imageRegion) {
127         mImageRegion = imageRegion;
128     }
129 
getTextRegion()130     public RegionModel getTextRegion() {
131         return mTextRegion;
132     }
133 
setTextRegion(RegionModel textRegion)134     public void setTextRegion(RegionModel textRegion) {
135         mTextRegion = textRegion;
136     }
137 
138     /**
139      * Get all regions except root-layout. The result is READ-ONLY.
140      */
getRegions()141     public ArrayList<RegionModel> getRegions() {
142         ArrayList<RegionModel> regions = new ArrayList<RegionModel>();
143         if (mImageRegion != null) {
144             regions.add(mImageRegion);
145         }
146         if (mTextRegion != null) {
147             regions.add(mTextRegion);
148         }
149         return regions;
150     }
151 
findRegionById(String rId)152     public RegionModel findRegionById(String rId) {
153         if (IMAGE_REGION_ID.equals(rId)) {
154             return mImageRegion;
155         } else if (TEXT_REGION_ID.equals(rId)) {
156             return mTextRegion;
157         } else {
158             for (RegionModel r : mNonStdRegions) {
159                 if (r.getRegionId().equals(rId)) {
160                     return r;
161                 }
162             }
163 
164             if (LOCAL_LOGV) {
165                 Log.v(TAG, "Region not found: " + rId);
166             }
167             return null;
168         }
169     }
170 
getLayoutWidth()171     public int getLayoutWidth() {
172         return mRootLayout.getWidth();
173     }
174 
getLayoutHeight()175     public int getLayoutHeight() {
176         return mRootLayout.getHeight();
177     }
178 
getBackgroundColor()179     public String getBackgroundColor() {
180         return mRootLayout.getBackgroundColor();
181     }
182 
changeTo(int layout)183     public void changeTo(int layout) {
184         if (mRootLayout == null) {
185             throw new IllegalStateException("Root-Layout uninitialized.");
186         }
187 
188         if (mLayoutParams == null) {
189             mLayoutParams = LayoutManager.getInstance().getLayoutParameters();
190         }
191 
192         if (mLayoutType != layout) {
193             switch (layout) {
194                 case LAYOUT_BOTTOM_TEXT: {
195                     mImageRegion.setTop(0);
196                     mTextRegion.setTop(mLayoutParams.getImageHeight());
197                     mLayoutType = layout;
198                     notifyModelChanged(true);
199                 }
200                 break;
201                 case LAYOUT_TOP_TEXT: {
202                     mImageRegion.setTop(mLayoutParams.getTextHeight());
203                     mTextRegion.setTop(0);
204                     mLayoutType = layout;
205                     notifyModelChanged(true);
206                 }
207                 break;
208                 default: {
209                     Log.w(TAG, "Unknown layout type: " + layout);
210                 }
211             }
212         } else {
213             if (LOCAL_LOGV) {
214                 Log.v(TAG, "Skip changing layout.");
215             }
216         }
217     }
218 
getLayoutType()219     public int getLayoutType() {
220         return mLayoutType;
221     }
222 
223     @Override
registerModelChangedObserverInDescendants( IModelChangedObserver observer)224     protected void registerModelChangedObserverInDescendants(
225             IModelChangedObserver observer) {
226         if (mRootLayout != null) {
227             mRootLayout.registerModelChangedObserver(observer);
228         }
229 
230         if (mImageRegion != null) {
231             mImageRegion.registerModelChangedObserver(observer);
232         }
233 
234         if (mTextRegion != null) {
235             mTextRegion.registerModelChangedObserver(observer);
236         }
237     }
238 
239     @Override
unregisterModelChangedObserverInDescendants( IModelChangedObserver observer)240     protected void unregisterModelChangedObserverInDescendants(
241             IModelChangedObserver observer) {
242         if (mRootLayout != null) {
243             mRootLayout.unregisterModelChangedObserver(observer);
244         }
245 
246         if (mImageRegion != null) {
247             mImageRegion.unregisterModelChangedObserver(observer);
248         }
249 
250         if (mTextRegion != null) {
251             mTextRegion.unregisterModelChangedObserver(observer);
252         }
253     }
254 
255     @Override
unregisterAllModelChangedObserversInDescendants()256     protected void unregisterAllModelChangedObserversInDescendants() {
257         if (mRootLayout != null) {
258             mRootLayout.unregisterAllModelChangedObservers();
259         }
260 
261         if (mImageRegion != null) {
262             mImageRegion.unregisterAllModelChangedObservers();
263         }
264 
265         if (mTextRegion != null) {
266             mTextRegion.unregisterAllModelChangedObservers();
267         }
268     }
269 }
270