1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.cooliris.app;
18
19import android.app.Activity;
20import android.content.Context;
21import android.os.Handler;
22import android.os.HandlerThread;
23import android.util.DisplayMetrics;
24import android.widget.Toast;
25
26import java.util.HashMap;
27import java.util.TimeZone;
28
29import com.cooliris.media.ReverseGeocoder;
30
31/*
32 *  TODO: consider adding HashMap<object, object> for globals globals
33 *  TODO: hook up other activity classes to App (besides Gallery and Search)
34 */
35public class App {
36	static private final HashMap<Context, App> mMap = new HashMap<Context, App>();
37
38	static public App get(Context context) {
39		return mMap.get(context);
40	}
41
42    public static final TimeZone CURRENT_TIME_ZONE = TimeZone.getDefault();
43    public static float PIXEL_DENSITY = 0.0f;
44
45	private final Context mContext;
46    private final HandlerThread mHandlerThread = new HandlerThread("AppHandlerThread");
47    private final Handler mHandler;
48    private ReverseGeocoder mReverseGeocoder = null;
49
50    private boolean mPaused = false;
51
52	public App(Context context) {
53		// register
54		mMap.put(context, this);
55
56		mContext = context;
57
58		if(PIXEL_DENSITY == 0.0f) {
59			DisplayMetrics metrics = new DisplayMetrics();
60			((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics);
61			PIXEL_DENSITY = metrics.density;
62		}
63
64        mHandlerThread.start();
65        mHandler = new Handler(mHandlerThread.getLooper());
66
67	    mReverseGeocoder = new ReverseGeocoder(mContext);
68	}
69
70	public void shutdown() {
71        mReverseGeocoder.shutdown();
72
73        // unregister
74        mMap.put(mContext, null);
75	}
76
77    public Context getContext() {
78        return mContext;
79    }
80
81    public Handler getHandler() {
82        while (mHandler == null) {
83            // Wait till the handler is created.
84            ;
85        }
86        return mHandler;
87    }
88
89    public ReverseGeocoder getReverseGeocoder() {
90        return mReverseGeocoder;
91    }
92
93    public boolean isPaused() {
94    	return mPaused;
95    }
96
97//    public void onCreate(Bundle savedInstanceState) {
98//    }
99//
100//    public void onStart() {
101//    }
102//
103//    public void onRestart() {
104//    }
105
106    public void onResume() {
107    	mPaused = false;
108    }
109
110    public void onPause() {
111    	mReverseGeocoder.flushCache();
112    	mPaused = true;
113    }
114
115//    public void onStop() {
116//
117//    }
118//
119//    public void onDestroy() {
120//    }
121
122    public void showToast(final String string, final int duration) {
123        mHandler.post(new Runnable() {
124            public void run() {
125                Toast.makeText(mContext, string, duration).show();
126            }
127        });
128    }
129}
130