Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SystemServiceManager.java « holoeverywhere « org « src « library « HoloEverywhere « 3rd_party « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 993efb373e44f0a10a5ff5b057d8fdcf047ce47b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

package org.holoeverywhere;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.holoeverywhere.SystemServiceManager.SystemServiceCreator.SystemService;

import android.content.Context;

/**
 * Manager of system services
 *
 * @author prok (prototypegamez@gmail.com)
 */
public final class SystemServiceManager {
    public static interface SuperSystemService {
        public Object superGetSystemService(String name);
    }

    public static interface SystemServiceCreator<T> {
        @Target(ElementType.TYPE)
        @Retention(RetentionPolicy.RUNTIME)
        public static @interface SystemService {
            public String value();
        }

        public T createService(Context context);
    }

    private static final Map<Class<? extends SystemServiceCreator<?>>, SystemServiceCreator<?>> CREATORS_MAP = new HashMap<Class<? extends SystemServiceCreator<?>>, SystemServiceManager.SystemServiceCreator<?>>();
    private static final Map<String, Class<? extends SystemServiceCreator<?>>> MAP = new HashMap<String, Class<? extends SystemServiceCreator<?>>>();

    private static Object getSuperSystemService(Context context, String name) {
        if (context instanceof SuperSystemService) {
            return ((SuperSystemService) context).superGetSystemService(name);
        } else {
            return context.getSystemService(name);
        }
    }

    public static Object getSystemService(Context context, String name) {
        if (context == null || context.isRestricted()) {
            throw new RuntimeException("Invalid context");
        } else if (name == null || name.length() == 0) {
            return null;
        }
        Class<? extends SystemServiceCreator<?>> clazz = MAP.get(name);
        if (clazz == null) {
            return getSuperSystemService(context, name);
        }
        SystemServiceCreator<?> creator = CREATORS_MAP.get(clazz);
        if (creator == null) {
            try {
                creator = clazz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            CREATORS_MAP.put(clazz, creator);
        }
        if (creator != null) {
            Object o = creator.createService(context);
            if (o != null) {
                return o;
            }
        }
        return getSuperSystemService(context, name);
    }

    public static void register(Class<? extends SystemServiceCreator<?>> clazz) {
        if (!clazz.isAnnotationPresent(SystemService.class)) {
            throw new RuntimeException(
                    "SystemServiceCreator must be implement SystemService");
        }
        SystemService systemService = clazz.getAnnotation(SystemService.class);
        final String name = systemService.value();
        if (name == null || name.length() == 0) {
            throw new RuntimeException("SystemService has incorrect name");
        }
        MAP.put(name, clazz);
    }

    public static synchronized void unregister(
            Class<? extends SystemServiceCreator<?>> clazz) {
        if (MAP.containsValue(clazz)) {
            for (Entry<String, Class<? extends SystemServiceCreator<?>>> e : MAP
                    .entrySet()) {
                if (e.getValue() == clazz) {
                    MAP.remove(e.getKey());
                    break;
                }
            }
        }
        CREATORS_MAP.remove(clazz);
    }

    private SystemServiceManager() {
    }
}