package cn.net.caoyao.cpzshandset.common.util;

import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.SDCardConfig;

public class CleanCacheService {

	public static void cleanCache(Context context) {
		new CleanTask(context).execute(SDCardConfig.getCachePath(context));
	}

	private static class CleanTask extends AsyncTask<String, Integer, String> {

		private Context mContext;
		private Dialog dialog;
		
		public CleanTask(Context context) {
			// TODO Auto-generated constructor stub
			this.mContext=context;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog=DialogUtil.getLoadingDialog(mContext, mContext.getResources().getString(R.string.clean_cache_tips));
			dialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String size= FileSizeUtil.getAutoFileOrFilesSize(params[0], mContext);
			delWebViewCache(mContext);
			
			FileUtils.delDir(params[0]);
			
			return size;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			Toast.makeText(mContext, mContext.getResources().getString(R.string.clean_cache_tips_result)+result, Toast.LENGTH_SHORT).show();
		}

	}
	
	

	public static void delWebViewCache(Context context) {
		
		context.deleteDatabase("webview.db");
		context.deleteDatabase("webview.db-shm");
		context.deleteDatabase("webview.db-wal");
		context.deleteDatabase("webviewCache.db");
		context.deleteDatabase("webviewCache.db-shm");
		context.deleteDatabase("webviewCache.db-wal");
		
	};

}
