Пример #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);
  }
Пример #2
0
  private List<Map<String, Object>> getData() {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    String linuxVer = cmd.executeCat("/proc/version");
    int pos = linuxVer.indexOf(" (");
    if (pos >= 0) {
      linuxVer = linuxVer.substring(0, pos);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("title", "Linux version");
    map.put("info", linuxVer);
    // map.put("no", 1);
    list.add(map);

    String cpuInfo = cmd.executeCat("/proc/cpuinfo");
    pos = cpuInfo.indexOf(": ");
    int pos2 = cpuInfo.indexOf("\n");
    cpuInfo = cpuInfo.substring(pos + 2, pos2);
    map = new HashMap<String, Object>();
    map.put("title", "Cpu info");
    map.put("info", cpuInfo);
    // map.put("no", 2);
    list.add(map);

    ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memInfo);
    String memStr = cmd.executeCat("/proc/meminfo");
    Pattern pattern = Pattern.compile("(\\d+)");
    Matcher match = pattern.matcher(memStr);
    if (match.find()) {
      int allMem = Integer.parseInt(match.group(1));
      memStr = "" + allMem / 1024;
    }
    map = new HashMap<String, Object>();
    map.put("title", "Memory");
    map.put(
        "info", "Total: " + memStr + " MB  Available: " + memInfo.availMem / 1024 / 1024 + " MB");
    // map.put("no", 3);
    list.add(map);

    String netInfo = getIp();
    if (netInfo == null) {
      netInfo = cmd.execute(new String[] {"/system/bin/netcfg"});
      if (netInfo.length() > 0) {
        netInfo = netInfo.substring(0, netInfo.length() - 1);
      }
    }
    map = new HashMap<String, Object>();
    map.put("title", "Network");
    map.put("info", netInfo);
    // map.put("no", 4);
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("title", "Screen");
    map.put("info", getDisplayMetrics());
    // map.put("no", 5);
    list.add(map);

    cpuInfo = cmd.executeCat("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
    //		Log.v("Hardware Activity", "cpu freq=" + cpuInfo);
    long cpuFreq = -1;
    try {
      cpuInfo = cpuInfo.substring(0, cpuInfo.length() - 1);
      cpuFreq = Long.valueOf(cpuInfo);
    } catch (Exception e) {
    }
    map = new HashMap<String, Object>();
    map.put("title", "CPU Freq");
    map.put("info", cpuFreq > 0 ? (cpuFreq / 1000 + " MHz") : "unknow");
    // map.put("no", 6);
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("title", "Battery");
    map.put("info", mLastBattery);
    // map.put("no", 7);
    list.add(map);

    long[] romInfo = getRomMemroy();
    map = new HashMap<String, Object>();
    map.put("title", "Rom info");
    map.put("info", "All: " + formatSize(romInfo[0]) + "  Available: " + formatSize(romInfo[1]));
    // map.put("no", 8);
    list.add(map);

    long[] cardInfo = getSDCardMemory();
    map = new HashMap<String, Object>();
    map.put("title", "SDCard info");
    map.put("info", "All: " + formatSize(cardInfo[0]) + "  Available: " + formatSize(cardInfo[1]));
    // map.put("no", 9);
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("title", "Mac Address");
    map.put("info", getMacAddress());
    // map.put("no", 10);
    list.add(map);

    map = new HashMap<String, Object>();
    map.put("title", "Startup ElapsedTime");
    map.put("info", getStartupElapsedTimes());
    // map.put("no", 11);
    list.add(map);

    Runtime runtime = Runtime.getRuntime();
    int maxMemory = ((int) runtime.maxMemory()) / 1024 / 1024;
    // 应用程序已获得内存
    long totalMemory = ((int) runtime.totalMemory()) / 1024 / 1024;
    // 应用程序已获得内存中未使用内存
    long freeMemory = ((int) runtime.freeMemory()) / 1024 / 1024;

    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    int heapMemory = am.getMemoryClass();
    int largeHeapMemory = am.getLargeMemoryClass();
    //        /system/build.prop  dalvik.vm.heapstartsize dalvik.vm.heapgrowthlimit
    // dalvik.vm.heapsize=512m
    map = new HashMap<String, Object>();
    map.put("title", "App Memory");
    map.put(
        "info",
        String.format(
            Locale.getDefault(),
            "maxMemory=%dM, totalMemory=%dM, freeMemory=%dM, heapMemory=%dM, largeHeapMemory=%dM(可能还是heap, 待验证)",
            maxMemory,
            totalMemory,
            freeMemory,
            heapMemory,
            largeHeapMemory));
    // map.put("no", 12);
    list.add(map);

    return list;
  }