Ejemplo n.º 1
0
  private boolean unpackFile(ZipFile zip, byte[] buf, ZipEntry fileEntry, String name)
      throws IOException, FileNotFoundException {
    if (fileEntry == null) fileEntry = zip.getEntry(name);
    if (fileEntry == null)
      throw new FileNotFoundException("Can't find " + name + " in " + zip.getName());

    File outFile = new File(sGREDir, name);
    if (outFile.lastModified() == fileEntry.getTime() && outFile.length() == fileEntry.getSize())
      return false;

    File dir = outFile.getParentFile();
    if (!dir.exists()) dir.mkdirs();

    InputStream fileStream;
    fileStream = zip.getInputStream(fileEntry);

    OutputStream outStream = new FileOutputStream(outFile);

    while (fileStream.available() > 0) {
      int read = fileStream.read(buf, 0, buf.length);
      outStream.write(buf, 0, read);
    }

    fileStream.close();
    outStream.close();
    outFile.setLastModified(fileEntry.getTime());
    return true;
  }
Ejemplo n.º 2
0
  private void checkAndLaunchUpdate() {
    Log.i(LOG_FILE_NAME, "Checking for an update");

    int statusCode = 8; // UNEXPECTED_ERROR
    File baseUpdateDir = null;
    if (Build.VERSION.SDK_INT >= 8)
      baseUpdateDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    else baseUpdateDir = new File(Environment.getExternalStorageDirectory().getPath(), "download");

    File updateDir = new File(new File(baseUpdateDir, "updates"), "0");

    File updateFile = new File(updateDir, "update.apk");
    File statusFile = new File(updateDir, "update.status");

    if (!statusFile.exists() || !readUpdateStatus(statusFile).equals("pending")) return;

    if (!updateFile.exists()) return;

    Log.i(LOG_FILE_NAME, "Update is available!");

    // Launch APK
    File updateFileToRun = new File(updateDir, getPackageName() + "-update.apk");
    try {
      if (updateFile.renameTo(updateFileToRun)) {
        String amCmd =
            "/system/bin/am start -a android.intent.action.VIEW "
                + "-n com.android.packageinstaller/.PackageInstallerActivity -d file://"
                + updateFileToRun.getPath();
        Log.i(LOG_FILE_NAME, amCmd);
        Runtime.getRuntime().exec(amCmd);
        statusCode = 0; // OK
      } else {
        Log.i(LOG_FILE_NAME, "Cannot rename the update file!");
        statusCode = 7; // WRITE_ERROR
      }
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error launching installer to update", e);
    }

    // Update the status file
    String status = statusCode == 0 ? "succeeded\n" : "failed: " + statusCode + "\n";

    OutputStream outStream;
    try {
      byte[] buf = status.getBytes("UTF-8");
      outStream = new FileOutputStream(statusFile);
      outStream.write(buf, 0, buf.length);
      outStream.close();
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error writing status file", e);
    }

    if (statusCode == 0) System.exit(0);
  }
Ejemplo n.º 3
0
  /**
   * Tests that the data updated in a Clob is always reflected in the InputStream got. Here the
   * updates into the Clob are done using both an OutputStream obtained from this Clob as well as
   * using Clob.setString.
   *
   * @throws Exception
   */
  public void testGetAsciiStreamClobUpdates() throws Exception {
    // The String that will be used
    // to do the inserts into the
    // Clob.
    String str1 = "Hi I am the insert string";

    // Stores the byte array representation of
    // the insert string.
    byte[] str1_bytes = str1.getBytes();

    // The String that will be used in the
    // second series of updates
    String str2 = "Hi I am the update string";

    // create the empty Clob.
    Clob clob = getConnection().createClob();

    // Get the InputStream from this
    // Clob before any writes happen.
    InputStream is_BeforeWrite = clob.getAsciiStream();

    // Get an OutputStream from this Clob
    // into which the data can be written
    OutputStream os = clob.setAsciiStream(1);
    os.write(str1_bytes);

    // Doing a setString now on the Clob
    // should reflect the same extension
    // in the InputStream also.
    clob.setString((str1_bytes.length) + 1, str2);

    // Get the input stream from the
    // Clob after the update
    InputStream is_AfterWrite = clob.getAsciiStream();

    // Now check if the two InputStreams
    // match
    assertEquals(is_BeforeWrite, is_AfterWrite);
  }