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