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

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.telephony.TelephonyManager;
import android.util.Log;

public class AppUtils {

	private AppUtils() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");

	}

	/**
	 * 获取应用程序名称
	 */
	public static String getAppName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			int labelRes = packageInfo.applicationInfo.labelRes;
			return context.getResources().getString(labelRes);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * [获取应用程序版本名称信息] 获取版本号
	 * 
	 * @param mContext
	 * @return 当前应用的版本名称
	 */
	public static String getVersionName(Context mContext) {
		try {
			PackageManager packageManager = mContext.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					mContext.getPackageName(), 0);
			return packageInfo.versionName;

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return "1.0";
	}

	/**
	 * 自动安装新apk
	 * 
	 * @author zwj<br/>
	 *         2015-4-26 下午6:15:56<br/>
	 * @param context
	 * @param filePath
	 *            文件位置
	 * @param name
	 *            文件名称
	 */

	public static void installApk(Context context, String filePath, String name) {
//		Intent intent = new Intent();
//		intent.setAction(Intent.ACTION_VIEW);
//		File file = new File(filePath, name);
//		intent.setDataAndType(Uri.fromFile(file),
//				"application/vnd.android.package-archive");
//		context.startActivity(intent);
		  File file = new File(filePath,name);
	        Log.d("showfile",file.toString());
	        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {//判读版本是否在7.0以上
	            Uri apkUri = FileProvider.getUriForFile(context, "cn.net.caoyao.cpzshandset.pro.fileprovider", file);
	            Intent install = new Intent(Intent.ACTION_VIEW);
	            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
	                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
	            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	            install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
	            install.setDataAndType(apkUri, "application/vnd.android.package-archive");
	            context.startActivity(install);
	        }else {
	            Intent intent = new Intent(Intent.ACTION_VIEW);
	            intent.setDataAndType(Uri.parse("file://"+file), "application/vnd.android.package-archive");
	            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	            context.startActivity(intent);
	        }
		
	}

	/**
	 * 获取sd路径
	 * 
	 * @author zwj<br/>
	 *         2015-4-26 下午6:34:29<br/>
	 * @return
	 */
	public static String getDownLoadPath(String fileName) {
		fileName = fileName != null ? ("/" + fileName + "/") : "";
		return Environment.getExternalStorageDirectory().toString()+fileName;
	}
	/**
	 * 获取imei号
	 * <br/>2015-5-27下午4:45:43
	 * @param context
	 * @return
	 */
	public static String getIMEI(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String sInspectorId = telephonyManager.getDeviceId();// 用户唯一标识码， 稽查用户Id
		if (sInspectorId == null || "".equals(sInspectorId)) {
			sInspectorId = "0";
		}
		return sInspectorId;
	}
	//跳转
	public static void nextActivity( Context con ,Class <?> clazz){
		Intent intent = new Intent(con ,clazz);
		con.startActivity(intent);
	}
}
