Example #1
0
  /** Upgrading device firmware over the air (OTA). */
  public void upgradeFirmware(boolean isStatusCheck) {
    Log.i(TAG, "An upgrade has been requested");
    Context context = this.getApplicationContext();
    Preference.putBoolean(
        context,
        context.getResources().getString(R.string.firmware_status_check_in_progress),
        isStatusCheck);
    Preference.putString(
        context,
        context.getResources().getString(R.string.firmware_download_progress),
        String.valueOf(DEFAULT_STATE_INFO_CODE));
    Preference.putInt(
        context, context.getResources().getString(R.string.operation_id), operationId);

    String schedule = null;
    String server;
    if (command != null && !command.trim().isEmpty()) {
      try {
        JSONObject upgradeData = new JSONObject(command);
        if (!upgradeData.isNull(context.getResources().getString(R.string.alarm_schedule))) {
          schedule =
              (String) upgradeData.get(context.getResources().getString(R.string.alarm_schedule));
        }

        if (!upgradeData.isNull(context.getResources().getString(R.string.firmware_server))) {
          server =
              (String) upgradeData.get(context.getResources().getString(R.string.firmware_server));
          if (URLUtil.isValidUrl(server)) {
            Preference.putString(
                context, context.getResources().getString(R.string.firmware_server), server);
          }
        }
      } catch (JSONException e) {
        Log.e(TAG, "Firmware upgrade payload parsing failed." + e);
      }
    }
    if (schedule != null && !schedule.trim().isEmpty()) {
      Log.i(TAG, "Upgrade has been scheduled to " + schedule);
      Preference.putString(
          context, context.getResources().getString(R.string.alarm_schedule), schedule);
      try {
        AlarmUtils.setOneTimeAlarm(context, schedule, Constants.Operation.UPGRADE_FIRMWARE, null);
      } catch (ParseException e) {
        Log.e(TAG, "One time alarm time string parsing failed." + e);
      }
    } else {
      if (isStatusCheck) {
        Log.i(TAG, "Firmware status check is initiated by admin.");
      } else {
        Log.i(TAG, "Upgrade request initiated by admin.");
      }
      // Prepare for upgrade
      OTADownload otaDownload = new OTADownload(context);
      otaDownload.startOTA();
    }
  }
Example #2
0
 /** Silently installs the app resides in the provided URI. */
 private void silentInstallApp(Context context, String packageUri, String schedule) {
   if (schedule != null && !schedule.trim().isEmpty() && !schedule.equals("undefined")) {
     Log.i(TAG, "Silent install has been scheduled to " + schedule);
     Preference.putString(
         context, context.getResources().getString(R.string.alarm_schedule), schedule);
     Preference.putString(context, context.getResources().getString(R.string.app_uri), packageUri);
     try {
       AlarmUtils.setOneTimeAlarm(
           context, schedule, Constants.Operation.SILENT_INSTALL_APPLICATION, packageUri);
     } catch (ParseException e) {
       Log.e(TAG, "One time alarm time string parsing failed." + e);
     }
   } else {
     AppUtils.silentInstallApp(context, Uri.parse(packageUri));
   }
 }