OtaUpgradeUtils.java 4.55 KB
package com.sw.laryngoscope.manager;

import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import android.os.RecoverySystem;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class OtaUpgradeUtils {
	
	private static final String TAG = "OtaUpgradeUtils" ;

    public static final String CHCHE_PARTITION = "/cache/";

    public static final String DEFAULT_PACKAGE_NAME = "update.zip";

    public static final String DEFAULT_PATH = "/data/update.zip";
    //public static final String DEFAULT_PATH = "/data/media/0/update.zip";

    private static File RECOVERY_DIR = new File("/cache/recovery");
    private static File COMMAND_FILE = new File(RECOVERY_DIR, "command");
    private static File LOG_FILE = new File(RECOVERY_DIR, "log");
    private Context mContext;


    public OtaUpgradeUtils(Context context) {
        mContext = context;
    }

    public interface ProgressListener extends RecoverySystem.ProgressListener {
        @Override
        public void onProgress(int progress);

        public void onVerifyFailed(int errorCode, Object object);

        public void onCopyProgress(int progress);

        public void onCopyFailed(int errorCode, Object object);
    }

    public boolean upgredeFromOta(File packageFile, ProgressListener progressListener) {
		Log.d(TAG, " CHCHE_PARTITION ");
        installPackage(mContext, new File(CHCHE_PARTITION + DEFAULT_PACKAGE_NAME));
        return false;
    }

    public boolean upgradeFromOta(String packagePath, ProgressListener progressListener) {
        return upgredeFromOta(new File(packagePath), progressListener);
    }

    public boolean upgradeFromOta(String packagePath) {
        return upgredeFromOta(new File(packagePath), null);
    }

    public static boolean copyFile(File src, File dst, ProgressListener listener) {
        long inSize = src.length();
        long outSize = 0;
        int progress = 0;
        //listener.onCopyProgress(progress);
        try {
            if(dst.exists()){
                dst.delete();
            }
            dst.createNewFile();
            FileInputStream in = new FileInputStream(src);
            FileOutputStream out = new FileOutputStream(dst);
            int length = -1;
            byte[] buf = new byte[1024 * 8];
            while ((length = in.read(buf)) != -1) {
                out.write(buf, 0, length);
                outSize += length;
                int temp = (int) (((float) outSize) / inSize * 100);
                if (temp != progress) {
                    progress = temp;
                    //listener.onCopyProgress(progress);
                }
            }
            out.flush();
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean copyFile(String src, String dst, ProgressListener listener) {
        return copyFile(new File(src), new File(dst), listener);
    }

    public static void installPackage(Context context, File packageFile) {
        try {
            RecoverySystem.installPackage(context, packageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean checkVersion(long newVersion, String product) {
        return (Build.TIME <= newVersion * 1000 && (Build.DEVICE.equals(product) || Build.PRODUCT
                .equals(product)));
    }

    public static boolean checkIncVersion(String fingerprinter, String product) {
        return (Build.FINGERPRINT.equals(fingerprinter) && (Build.DEVICE.equals(product) || Build.PRODUCT
                .equals(product)));
    }
    
    private static void bootCommand(Context context, String arg) throws IOException {
        RECOVERY_DIR.mkdirs();  // In case we need it
        COMMAND_FILE.delete();  // In case it's not writable
        LOG_FILE.delete();

        FileWriter command = new FileWriter(COMMAND_FILE);
        try {
            command.write(arg);
            command.write("\n");
        } finally {
            command.close();
        }

        // Having written the command file, go ahead and reboot
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        pm.reboot("recovery");

        throw new IOException("Reboot failed (no permissions?)");
    }
}