VersionManager.java 7.25 KB
package com.sw.laryngoscope.manager;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.core.content.FileProvider;

import com.sw.laryngoscope.utils.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class VersionManager {	
	
	/**
    * @param 
    * @explain 获取App版本号
    */
	public static String getAppVersionName(Context context) {  
	    String versionName = "";  
	    try {
	        // ---get the package info---  
	        PackageManager pm = context.getPackageManager();  
	        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);  
	        versionName = pi.versionName;  	        
	        if (versionName == null || versionName.length() <= 0) {  
	            return "";  
	        }  
	    } catch (Exception e) {
	        Log.e("VersionInfo", "Exception", e);  
	    }  
	    return versionName;  
	}

	public static int getAppVersionCode(Context context) {
		int versionCode = 0;
		try {
			// ---get the package info---
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			versionCode = pi.versionCode;
		} catch (Exception e) {
			Log.e("versionCode", "Exception", e);
		}
		return versionCode;
	}

	public static int getAppVersionCode(Context context, String file) {
		int versionCode = 0;
		try {
			// ---get the package info---
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageArchiveInfo(file, PackageManager.GET_ACTIVITIES);
			versionCode = packageInfo.versionCode;
		} catch (Exception e) {
			Log.e("versionCode", "Exception", e);
		}
		return versionCode;
	}
	
	public static String getSpecAppVersionName(Context context, String name) {  
	    String versionName = "";  
	    try {  
	        // ---get the package info---  
	        PackageManager pm = context.getPackageManager();  
	        PackageInfo pi = pm.getPackageInfo(name, 0);  
	        versionName = pi.versionName;  	        
	        if (versionName == null || versionName.length() <= 0) {  
	            return "";  
	        }  
	    } catch (Exception e) {  
	        Log.e("VersionInfo", "Exception", e);  
	    }  
	    return versionName;  
	}

	/**
	 * @return 获取SDK版本号
	 */
	public static String getSdkVersion(){
		String versionOS = android.os.Build.VERSION.RELEASE;
		return versionOS;
	}
		
	/**
	 * @return 获取当前系统的版本号
	 */
	public static String getfirewareVersion(){
		String versionOS = android.os.Build.VERSION.INCREMENTAL + "";

		return versionOS;
	}
	
	/**
	 * @return 返回当前系统的android版本号
	 */
	public static String getSDKVersion() {
		int currentapiVersion = android.os.Build.VERSION.SDK_INT; 		
		return String.valueOf(currentapiVersion);
	}
	
	/** 
	* CORE-VER 
	* 内核版本 
	* return String 
	*/  	  
	public static String getLinuxCore_Ver() {  
		Process process = null;  
		String kernelVersion = "";  
		try {  
			process = Runtime.getRuntime().exec("cat /proc/version");  
		} catch (IOException e) {  
			// TODO Auto-generated catch block  
			e.printStackTrace();  
		} 
		  
		// get the output line  
		InputStream outs = process.getInputStream();  
		InputStreamReader isrout = new InputStreamReader(outs);  
		BufferedReader brout = new BufferedReader(isrout, 8 * 1024);  
		  		  
		String result = "";  
		String line;  
		// get the whole standard output string  
		try {  
			while ((line = brout.readLine()) != null) {  
				result += line;  
			}  
		} catch (IOException e) {  
			// TODO Auto-generated catch block  
			e.printStackTrace();  
		}  
		  		  
		try {  
			if (result != "") {  
				String Keyword = "version ";  
				int index = result.indexOf(Keyword);  
				line = result.substring(index + Keyword.length());  
				index = line.indexOf(" ");  
				kernelVersion = line.substring(0, index);  
			}  
		} catch (IndexOutOfBoundsException e) {  
			e.printStackTrace();  
		}  
		return kernelVersion; 
	}

	public static long getApkUpdateTime(Context context) {
		PackageManager pm = context.getPackageManager();
		ZipFile zf = null;
		try {
			PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
			zf = new ZipFile(packageInfo.applicationInfo.sourceDir);
			ZipEntry ze = zf.getEntry("classes.dex");
			return ze.getTime();
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (zf != null) {
				try {
					zf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return 0;
	}

	public static void installApk(Context mContext, File apkfile) {
		Intent apkIntent = new Intent();
		apkIntent.setAction(android.content.Intent.ACTION_VIEW);
		apkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		apkIntent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
		mContext.startActivity(apkIntent);
	}

	/**
	 * 安装APK
	 *
	 * @param context
	 * @param filePath
	 */
	public static void installApk(Context context, String filePath) {
		try {
			/**
			 * provider
			 * 处理android 7.0 及以上系统安装异常问题
			 */
			File file = new File(filePath);
			Intent install = new Intent();
			install.setAction(Intent.ACTION_VIEW);
			install.addCategory(Intent.CATEGORY_DEFAULT);
			install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
				Uri apkUri = FileProvider.getUriForFile(context,  context.getPackageName() + ".fileprovider", file);
				//Uri apkUri = FileProvider.getUriForFile(context,  "com.sw.laryngoscope.fileprovider", file);
				//在AndroidManifest中的android:authorities值
				install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
				//添加这一句表示对目标应用临时授权该Uri所代表的文件
				install.setDataAndType(apkUri, "application/vnd.android.package-archive");
				Logger.d(" txt_apk_upgrade ===");
			} else {
				install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
			}
			context.startActivity(install);
		} catch (Exception e) {
			e.printStackTrace();
			//TODO 文件解析失败
			Logger.d(" txt_apk_upgrade ===" + e.toString());
		}
	}

	/*public String getBuildTime(){
		String content = null;
		Resources resources=mContext.getResources();
		InputStream is=null;
		try{
			is=resources.openRawResource(R.raw.date);
			byte buffer[]=new byte[is.available()];
			is.read(buffer);
			content=new String(buffer);
			Log.i(TAG, "read:"+content);
		}
		catch(IOException e)
		{
			Log.e(TAG, "write file",e);
		}
		finally
		{
			if(is!=null)
			{
				try{
					is.close();
				}catch(IOException e)
				{
					Log.e(TAG, "close file",e);
				}
			}
		}
		return content;
	}*/

}