summaryrefslogtreecommitdiffstats
path: root/sandbox/mobile-android/android-jdk-classes/src/org/apache/tuscany/sca/android/ContextRegistry.java
blob: 312c0059dbd531def02ada7a4a82e68fdb291e87 (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
package org.apache.tuscany.sca.android;

import java.util.HashSet;
import java.util.Hashtable;

import android.content.Context;

public class ContextRegistry {
	
	private static Hashtable<String, HashSet<Context>> contexts = new Hashtable<String, HashSet<Context>>();
	
	public static void registerContext(Context context) {
		String packageName = context.getPackageName();
		HashSet<Context> packContexts = contexts.get(packageName);
		
		if (packContexts == null) {
			packContexts = new HashSet<Context>();
			contexts.put(packageName, packContexts);
			
		}
		
		packContexts.add(context);
		
	}
	
	public static void unregisterContext(Context context) {
		String packageName = context.getPackageName();
		HashSet<Context> packContexts = contexts.get(packageName);
		
		if (packContexts != null) {
			packContexts.remove(context);
			
			if (packContexts.isEmpty()) {
				contexts.remove(packageName);
			}
			
		}
		
	}
	
	public static Context[] getContexts(String packageName) {
		HashSet<Context> packageContexts = contexts.get(packageName);
		
		if (packageContexts == null) {
			return new Context[0];
		}
		
		Context[] ret = new Context[packageContexts.size()];
		packageContexts.toArray(ret);
		
		return ret;
		
	}

}