Home | History | Annotate | Download | only in ui
      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.ui;
     19 
     20 import java.lang.reflect.Constructor;
     21 import java.lang.reflect.InvocationTargetException;
     22 
     23 import android.content.Context;
     24 import android.util.Log;
     25 
     26 import com.android.mms.LogTag;
     27 import com.android.mms.model.Model;
     28 
     29 /**
     30  * The factory of concrete presenters.
     31  */
     32 public class PresenterFactory {
     33     private static final String TAG = LogTag.TAG;
     34     private static final String PRESENTER_PACKAGE = "com.android.mms.ui.";
     35 
     36     public static Presenter getPresenter(String className, Context context,
     37             ViewInterface view, Model model) {
     38         try {
     39             if (className.indexOf(".") == -1) {
     40                 className = PRESENTER_PACKAGE + className;
     41             }
     42 
     43             Class c = Class.forName(className);
     44             Constructor constructor = c.getConstructor(
     45                     Context.class, ViewInterface.class, Model.class);
     46             return (Presenter) constructor.newInstance(context, view, model);
     47         } catch (ClassNotFoundException e) {
     48             Log.e(TAG, "Type not found: " + className, e);
     49         } catch (NoSuchMethodException e) {
     50             // Impossible to reach here.
     51             Log.e(TAG, "No such constructor.", e);
     52         } catch (InvocationTargetException e) {
     53             Log.e(TAG, "Unexpected InvocationTargetException", e);
     54         } catch (IllegalAccessException e) {
     55             Log.e(TAG, "Unexpected IllegalAccessException", e);
     56         } catch (InstantiationException e) {
     57             Log.e(TAG, "Unexpected InstantiationException", e);
     58         }
     59 
     60         return null;
     61     }
     62 }
     63