Example #1
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);
  }
  @Override
  public void run() {
    // TODO: Implement this method
    super.run();
    start = 0;
    end = 0;

    LoadPageImage imp;
    HashMap<String, Object> map = new HashMap<String, Object>();
    try {

      start = html.indexOf("page-submission", 0);
      start = html.indexOf("small_url =", start);
      end = html.indexOf("var full_url", start);
      s = "http:" + html.substring(start + 13, end - 3);
      final String img = s;
      map.put("img", s);
      end = html.indexOf(">Download<", end);
      start = html.lastIndexOf("href=", end);
      s = "http:" + html.substring(start + 6, end - 1);
      map.put("download", s);
      int std = s.lastIndexOf("/");
      String name = PageActivity.HOME_PATH + s.substring(std + 1, s.length());
      File f = new File(name);
      if (f.exists()) {
        Bitmap b;
        b = BitmapFactory.decodeFile(f.getPath());
        map.put("bitmap", b);
      } else {
        name = PageActivity.HOME_PATH + "s-" + s.substring(std + 1, s.length());
        f = new File(name);
        if (f.exists()) {
          Bitmap b;
          b = BitmapFactory.decodeFile(f.getPath());
          map.put("bitmap", b);
        } else {
          imp = new LoadPageImage();
          imp.start(img, -1, now);
        }
      }
      end = html.indexOf("Main Gallery", end);
      start = html.lastIndexOf("href=", end);
      end = html.indexOf("class=", start);
      s = html.substring(start + 6, end - 2);
      map.put("gallery", s);
      start = html.indexOf("<tr>\n<td", end);
      start = html.indexOf("<b>", start);
      end = html.indexOf("</b>", start);
      s = html.substring(start + 3, end);
      map.put("title", s);
      start = html.indexOf("href=", start);
      end = html.indexOf("><img", start);
      s = "http://www.furaffinity.net" + html.substring(start + 6, end - 1);
      map.put("userlink", s);
      start = html.indexOf("alt=", end);
      end = html.indexOf("\" src", start);
      s = html.substring(start + 5, end);
      map.put("username", s);
      start = html.indexOf("src=", end);
      end = html.indexOf("></a>", start);
      s = "http:" + html.substring(start + 5, end - 1);
      imp = new LoadPageImage();
      imp.start(s, -2, now);
      map.put("usericon", s);
      start = html.indexOf("<br/><br/>", end);
      end = html.indexOf("</td>\n</tr>", start);
      int st = start;
      int ed = start - 2;
      String t = "";
      s = "";
      while (html.indexOf("<br/>", st + 1) < end - 6) {
        st = html.indexOf("<br/>", ed);
        ed = html.indexOf("<br/>", st + 1);
        t = html.substring(st + 5, ed);
        if (t.indexOf("href=") != -1) {
          int i1;
          int i2;
          i1 = t.indexOf("alt=\"", 0);
          i2 = t.indexOf("\"/>", i1);
          try {
            t = " @ " + t.substring(i1 + 5, i2) + " ";
          } catch (Exception e) {
            t = "";
          }
        }
        t = t.replace("</td>", "");
        t = t.replace("<td>", "");
        t = t.replace("</tr>", "");
        t = t.replace("<tr>", "");
        t = t.replace("</table>", "");
        t = t.replace("<table>", "");
        t = t.replace("<br>", "");
        t = t.replace("</br>", "");
        if (t.indexOf("bbcode") != -1) {
          t = "";
        }
        s += t;
      }

      map.put("info", s);
      if (now == PageActivity.now) {
        Message msg = Message.obtain();
        msg.arg2 = now;
        msg.obj = map;
        msg.what = 5;
        PageActivity.hander.sendMessage(msg);
      }
    } catch (Exception e) {
      File f = new File(PageActivity.HOME_PATH + "LOG/lastPage.html");
      if (f.exists()) {
        f.renameTo(new File(PageActivity.HOME_PATH + "LOG/lastErrorPage.html"));
      }
      Message msg = Message.obtain();
      msg.arg2 = now;
      msg.obj = map;
      msg.what = 998;
      PageActivity.hander.sendMessage(msg);
    }
  }