package com.wangbb.naruto.app; import android.app.Activity; import android.app.ActivityManager; import android.app.Application; import android.content.Context; import android.graphics.Bitmap; import android.os.Handler; import android.support.v4.util.LruCache; import android.util.DisplayMetrics; import android.view.Display; import com.android.volley.RequestQueue; import com.android.volley.toolbox.Volley; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * @author Kituri */ public class NarutoApplication extends Application { // ����list��������ÿһ��activity�ǹؼ� private List<Activity> mList = new LinkedList<Activity>(); private HashMap<String, Boolean> mapActivity = new HashMap<String, Boolean>();// ���ڴ洢activity��Ӧ�ļ���״̬ private static Context appContext; // Ϊ��ʵ��ÿ��ʹ�ø���ʱ�������µĶ���������ľ�̬���� private static NarutoApplication application; private DisplayMetrics displayMetrics = null; //image memory cache private LruCache<String, Bitmap> appBitmapCache = null; public static RequestQueue mVolleyQueue; private Handler handler = new Handler(); // ���췽�� public synchronized static NarutoApplication getInstance() { return application; } @Override public void onCreate() { super.onCreate(); if (appContext == null) { appContext = getApplicationContext(); } application = this; mVolleyQueue = Volley.newRequestQueue(this); } @Override public void onTerminate() { super.onTerminate(); } public static Context getApplication() { return appContext; } // add Activity public void addActivity(Activity activity) { mList.add(activity); } // �ر�ÿһ��list�ڵ�activity public void exitApp() { try { for (Activity activity : mList) { if (activity != null) activity.finish(); } } catch (Exception e) { e.printStackTrace(); } finally { System.exit(0); } } // �ر�ÿһ��list�ڵ�activity public void closeActivity() { try { for (Activity activity : mList) { if (activity != null) { activity.finish(); } } } catch (Exception e) { e.printStackTrace(); } } /** * ����ָ����Activity */ public void finishActivity(Activity activity) { if (activity != null) { activity.finish(); activity = null; } } // ɱ���� @Override public void onLowMemory() { super.onLowMemory(); System.gc(); } /** * ��Activity��״̬�ŵ�List�й��� * * @param activity */ public void addActivityStatus(Activity activity, boolean isAlive) { if (mapActivity.containsKey(activity.getClass().getName())) { mapActivity.remove(activity.getClass().getName()); mapActivity.put(activity.getClass().getName(), isAlive); } else { mapActivity.put(activity.getClass().getName(), isAlive); } } /** * �ж��Ƿ���Activity�ǻ״̬ * * @return */ public boolean isAllActivityAlive() { boolean res = false; Iterator<?> iter = mapActivity.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, Boolean> entry = (Map.Entry<String, Boolean>) iter .next(); // Object key = entry.getKey(); boolean value = entry.getValue(); if (value) { return true; } } return res; } public synchronized LruCache<String, Bitmap> getBitmapCache() { if (appBitmapCache == null) { buildCache(); } return appBitmapCache; } private void buildCache() { int memClass = ((ActivityManager) getSystemService( Context.ACTIVITY_SERVICE)).getMemoryClass(); int cacheSize = Math.max(1024 * 1024 * 8, 1024 * 1024 * memClass / 6); appBitmapCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } }; } //image size private Activity mActivity = null; public Activity getActivity() { return mActivity; } public void setActivity(Activity activity) { this.mActivity = activity; } public Handler getUIHandler() { return handler; } public DisplayMetrics getDisplayMetrics() { if (displayMetrics != null) { return displayMetrics; } else { Activity a = getActivity(); if (a != null) { Display display = getActivity().getWindowManager().getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); this.displayMetrics = metrics; return metrics; } else { //default screen is 800x480 DisplayMetrics metrics = new DisplayMetrics(); metrics.widthPixels = 480; metrics.heightPixels = 800; return metrics; } } } }