Home | History | Annotate | Download | only in layout
      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.layout;
     19 
     20 import android.content.Context;
     21 import android.content.res.Configuration;
     22 import android.util.Log;
     23 
     24 /**
     25  * MMS presentation layout management.
     26  */
     27 public class LayoutManager {
     28     private static final String TAG = "LayoutManager";
     29     private static final boolean DEBUG = false;
     30     private static final boolean LOCAL_LOGV = false;
     31 
     32     private final Context mContext;
     33     private LayoutParameters mLayoutParams;
     34 
     35     private static LayoutManager sInstance;
     36 
     37     private LayoutManager(Context context) {
     38         mContext = context;
     39         initLayoutParameters(context.getResources().getConfiguration());
     40     }
     41 
     42     private void initLayoutParameters(Configuration configuration) {
     43         mLayoutParams = getLayoutParameters(
     44                 configuration.orientation == Configuration.ORIENTATION_PORTRAIT
     45                 ? LayoutParameters.HVGA_PORTRAIT
     46                 : LayoutParameters.HVGA_LANDSCAPE);
     47 
     48         if (LOCAL_LOGV) {
     49             Log.v(TAG, "LayoutParameters: " + mLayoutParams.getTypeDescription()
     50                     + ": " + mLayoutParams.getWidth() + "x" + mLayoutParams.getHeight());
     51         }
     52     }
     53 
     54     private LayoutParameters getLayoutParameters(int displayType) {
     55         switch (displayType) {
     56             case LayoutParameters.HVGA_LANDSCAPE:
     57                 return new HVGALayoutParameters(mContext, LayoutParameters.HVGA_LANDSCAPE);
     58             case LayoutParameters.HVGA_PORTRAIT:
     59                 return new HVGALayoutParameters(mContext, LayoutParameters.HVGA_PORTRAIT);
     60         }
     61 
     62         throw new IllegalArgumentException(
     63                 "Unsupported display type: " + displayType);
     64     }
     65 
     66     public static void init(Context context) {
     67         if (LOCAL_LOGV) {
     68             Log.v(TAG, "DefaultLayoutManager.init()");
     69         }
     70 
     71         if (sInstance != null) {
     72             Log.w(TAG, "Already initialized.");
     73         }
     74         sInstance = new LayoutManager(context);
     75     }
     76 
     77     public static LayoutManager getInstance() {
     78         if (sInstance == null) {
     79             throw new IllegalStateException("Uninitialized.");
     80         }
     81         return sInstance;
     82     }
     83 
     84     public void onConfigurationChanged(Configuration newConfig) {
     85         if (LOCAL_LOGV) {
     86             Log.v(TAG, "-> LayoutManager.onConfigurationChanged().");
     87         }
     88         initLayoutParameters(newConfig);
     89     }
     90 
     91     public int getLayoutType() {
     92         return mLayoutParams.getType();
     93     }
     94 
     95     public int getLayoutWidth() {
     96         return mLayoutParams.getWidth();
     97     }
     98 
     99     public int getLayoutHeight() {
    100         return mLayoutParams.getHeight();
    101     }
    102 
    103     public LayoutParameters getLayoutParameters() {
    104         return mLayoutParams;
    105     }
    106 }
    107