Program: 165355
Project: 165355
Buggy source:
protected void onHandleIntent(Intent intent){
  receiver=intent.getParcelableExtra("String_Node_Str");
  long startTime=System.currentTimeMillis();
  String errmsg="String_Node_Str";
  try {
    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    if (isScheduledRun()) {
      long lastUpdate=prefs.getLong("String_Node_Str",0);
      String sint=prefs.getString("String_Node_Str","String_Node_Str");
      int interval=Integer.parseInt(sint);
      if (interval == 0) {
        Log.d("String_Node_Str","String_Node_Str");
        return;
      }
      long elapsed=System.currentTimeMillis() - lastUpdate;
      if (elapsed < interval * 60 * 60* 1000) {
        Log.d("String_Node_Str","String_Node_Str" + elapsed + "String_Node_Str"+ interval+ "String_Node_Str");
        return;
      }
    }
 else {
      Log.d("String_Node_Str","String_Node_Str");
    }
    boolean notify=prefs.getBoolean("String_Node_Str",false);
    int prevUpdates=0;
    int newUpdates=0;
    List<DB.Repo> repos;
    try {
      DB db=DB.getDB();
      repos=db.getRepos();
    }
  finally {
      DB.releaseDB();
    }
    List<DB.App> apps=new ArrayList<DB.App>();
    List<Integer> keeprepos=new ArrayList<Integer>();
    boolean success=true;
    boolean changes=false;
    for (    DB.Repo repo : repos) {
      if (repo.inuse) {
        sendStatus(STATUS_INFO,getString(R.string.status_connecting_to_repo,repo.address));
        StringBuilder newetag=new StringBuilder();
        String err=RepoXMLHandler.doUpdate(getBaseContext(),repo,apps,newetag,keeprepos,this);
        if (err == null) {
          String nt=newetag.toString();
          if (!nt.equals(repo.lastetag)) {
            repo.lastetag=newetag.toString();
            changes=true;
          }
        }
 else {
          success=false;
          err="String_Node_Str" + repo.address + "String_Node_Str"+ err;
          Log.d("String_Node_Str",err);
          if (errmsg.length() == 0)           errmsg=err;
 else           errmsg+="String_Node_Str" + err;
        }
      }
    }
    if (changes && success) {
      sendStatus(STATUS_INFO,getString(R.string.status_checking_compatibility));
      List<DB.App> acceptedapps=new ArrayList<DB.App>();
      List<DB.App> prevapps=((FDroidApp)getApplication()).getApps();
      DB db=DB.getDB();
      try {
        for (        int keep : keeprepos) {
          for (          DB.App app : prevapps) {
            boolean keepapp=false;
            for (            DB.Apk apk : app.apks) {
              if (apk.repo == keep) {
                keepapp=true;
                break;
              }
            }
            if (keepapp) {
              DB.App app_k=null;
              for (              DB.App app2 : apps) {
                if (app2.id.equals(app.id)) {
                  app_k=app2;
                  break;
                }
              }
              if (app_k == null) {
                apps.add(app);
                app_k=app;
              }
              app_k.updated=true;
              if (!app_k.detail_Populated) {
                db.populateDetails(app_k,keep);
              }
              for (              DB.Apk apk : app.apks)               if (apk.repo == keep)               apk.updated=true;
            }
          }
        }
        prevUpdates=db.beginUpdate(prevapps);
        for (        DB.App app : apps) {
          if (db.updateApplication(app))           acceptedapps.add(app);
        }
        db.endUpdate();
        if (notify)         newUpdates=db.getNumUpdates();
        for (        DB.Repo repo : repos)         db.writeLastEtag(repo);
      }
 catch (      Exception ex) {
        db.cancelUpdate();
        Log.e("String_Node_Str","String_Node_Str" + Log.getStackTraceString(ex));
        errmsg="String_Node_Str" + ex.getMessage();
        success=false;
      }
 finally {
        DB.releaseDB();
      }
      if (success) {
        for (        DB.App app : acceptedapps)         getIcon(app,repos);
        ((FDroidApp)getApplication()).invalidateApps();
      }
    }
    if (success && changes && notify) {
      Log.d("String_Node_Str","String_Node_Str" + prevUpdates + "String_Node_Str"+ newUpdates);
      if (newUpdates > prevUpdates) {
        NotificationManager n=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.icon,"String_Node_Str",System.currentTimeMillis());
        Context context=getApplicationContext();
        CharSequence contentTitle="String_Node_Str";
        CharSequence contentText="String_Node_Str";
        Intent notificationIntent=new Intent(UpdateService.this,FDroid.class);
        notificationIntent.putExtra(FDroid.EXTRA_TAB_UPDATE,true);
        PendingIntent contentIntent=PendingIntent.getActivity(UpdateService.this,0,notificationIntent,0);
        notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
        notification.flags|=Notification.FLAG_AUTO_CANCEL;
        n.notify(1,notification);
      }
    }
    if (!success) {
      if (errmsg.length() == 0)       errmsg="String_Node_Str";
      sendStatus(STATUS_ERROR,errmsg);
    }
 else {
      sendStatus(STATUS_COMPLETE);
    }
    if (success) {
      Editor e=prefs.edit();
      e.putLong("String_Node_Str",System.currentTimeMillis());
      e.commit();
    }
  }
 catch (  Exception e) {
    Log.e("String_Node_Str","String_Node_Str" + Log.getStackTraceString(e));
    if (errmsg.length() == 0)     errmsg="String_Node_Str";
    sendStatus(STATUS_ERROR,errmsg);
  }
 finally {
    Log.d("String_Node_Str","String_Node_Str" + ((System.currentTimeMillis() - startTime) / 1000) + "String_Node_Str");
    receiver=null;
  }
}

Fixed version (ground truth):
protected void onHandleIntent(Intent intent){
  receiver=intent.getParcelableExtra("String_Node_Str");
  long startTime=System.currentTimeMillis();
  String errmsg="String_Node_Str";
  try {
    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    if (isScheduledRun()) {
      long lastUpdate=prefs.getLong("String_Node_Str",0);
      String sint=prefs.getString("String_Node_Str","String_Node_Str");
      int interval=Integer.parseInt(sint);
      if (interval == 0) {
        Log.d("String_Node_Str","String_Node_Str");
        return;
      }
      long elapsed=System.currentTimeMillis() - lastUpdate;
      if (elapsed < interval * 60 * 60* 1000) {
        Log.d("String_Node_Str","String_Node_Str" + elapsed + "String_Node_Str"+ interval+ "String_Node_Str");
        return;
      }
    }
 else {
      Log.d("String_Node_Str","String_Node_Str");
    }
    boolean notify=prefs.getBoolean("String_Node_Str",false);
    int prevUpdates=0;
    int newUpdates=0;
    List<DB.Repo> repos;
    try {
      DB db=DB.getDB();
      repos=db.getRepos();
    }
  finally {
      DB.releaseDB();
    }
    List<DB.App> apps=new ArrayList<DB.App>();
    List<Integer> keeprepos=new ArrayList<Integer>();
    boolean success=true;
    boolean changes=false;
    for (    DB.Repo repo : repos) {
      if (repo.inuse) {
        sendStatus(STATUS_INFO,getString(R.string.status_connecting_to_repo,repo.address));
        StringBuilder newetag=new StringBuilder();
        String err=RepoXMLHandler.doUpdate(getBaseContext(),repo,apps,newetag,keeprepos,this);
        if (err == null) {
          String nt=newetag.toString();
          if (!nt.equals(repo.lastetag)) {
            repo.lastetag=newetag.toString();
            changes=true;
          }
        }
 else {
          success=false;
          err="String_Node_Str" + repo.address + "String_Node_Str"+ err;
          Log.d("String_Node_Str",err);
          if (errmsg.length() == 0)           errmsg=err;
 else           errmsg+="String_Node_Str" + err;
        }
      }
    }
    if (changes && success) {
      sendStatus(STATUS_INFO,getString(R.string.status_checking_compatibility));
      List<DB.App> acceptedapps=new ArrayList<DB.App>();
      List<DB.App> prevapps=((FDroidApp)getApplication()).getApps();
      boolean anyRepoToUpdate=false;
      for (      DB.Repo repo : repos) {
        boolean keepThisRepo=false;
        for (        int repoId : keeprepos) {
          if (repo.id == repoId) {
            keepThisRepo=true;
            break;
          }
        }
        if (!keepThisRepo) {
          anyRepoToUpdate=true;
          break;
        }
      }
      if (!anyRepoToUpdate) {
        Log.d("String_Node_Str","String_Node_Str");
      }
 else {
        sendStatus(STATUS_INFO,getString(R.string.status_checking_compatibility));
        List<DB.App> acceptedapps=new ArrayList<DB.App>();
        List<DB.App> prevapps=((FDroidApp)getApplication()).getApps();
        DB db=DB.getDB();
        try {
          Log.d("String_Node_Str","String_Node_Str" + keeprepos.size() + "String_Node_Str");
          for (          int keep : keeprepos) {
            Log.d("String_Node_Str","String_Node_Str" + keep + "String_Node_Str");
            for (            DB.App app : prevapps) {
              boolean keepapp=false;
              for (              DB.Apk apk : app.apks) {
                if (apk.repo == keep) {
                  Log.d("String_Node_Str","String_Node_Str" + app.id + "String_Node_Str"+ apk.id+ "String_Node_Str"+ keep);
                  keepapp=true;
                  break;
                }
              }
              if (keepapp) {
                DB.App app_k=null;
                Log.d("String_Node_Str","String_Node_Str" + app.id + "String_Node_Str");
                for (                DB.App app2 : apps) {
                  if (app2.id.equals(app.id)) {
                    Log.d("String_Node_Str","String_Node_Str" + app2.id + "String_Node_Str");
                    app_k=app2;
                    break;
                  }
                }
                if (app_k == null) {
                  Log.d("String_Node_Str","String_Node_Str" + apps.size() + "String_Node_Str");
                  apps.add(app);
                  app_k=app;
                }
                app_k.updated=true;
                Log.d("String_Node_Str","String_Node_Str" + app_k.id + "String_Node_Str");
                db.populateDetails(app_k,keep);
                for (                DB.Apk apk : app.apks)                 if (apk.repo == keep)                 apk.updated=true;
              }
            }
          }
          prevUpdates=db.beginUpdate(prevapps);
          for (          DB.App app : apps) {
            if (db.updateApplication(app))             acceptedapps.add(app);
          }
          db.endUpdate();
          if (notify)           newUpdates=db.getNumUpdates();
          for (          DB.Repo repo : repos)           db.writeLastEtag(repo);
        }
 catch (        Exception ex) {
          db.cancelUpdate();
          Log.e("String_Node_Str","String_Node_Str" + Log.getStackTraceString(ex));
          errmsg="String_Node_Str" + ex.getMessage();
          success=false;
        }
 finally {
          DB.releaseDB();
        }
        if (success) {
          for (          DB.App app : acceptedapps)           getIcon(app,repos);
          ((FDroidApp)getApplication()).invalidateApps();
        }
      }
    }
    if (success && changes && notify) {
      Log.d("String_Node_Str","String_Node_Str" + prevUpdates + "String_Node_Str"+ newUpdates);
      if (newUpdates > prevUpdates) {
        NotificationManager n=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.icon,"String_Node_Str",System.currentTimeMillis());
        Context context=getApplicationContext();
        CharSequence contentTitle="String_Node_Str";
        CharSequence contentText="String_Node_Str";
        Intent notificationIntent=new Intent(UpdateService.this,FDroid.class);
        notificationIntent.putExtra(FDroid.EXTRA_TAB_UPDATE,true);
        PendingIntent contentIntent=PendingIntent.getActivity(UpdateService.this,0,notificationIntent,0);
        notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
        notification.flags|=Notification.FLAG_AUTO_CANCEL;
        n.notify(1,notification);
      }
    }
    if (!success) {
      if (errmsg.length() == 0)       errmsg="String_Node_Str";
      sendStatus(STATUS_ERROR,errmsg);
    }
 else {
      sendStatus(STATUS_COMPLETE);
    }
    if (success) {
      Editor e=prefs.edit();
      e.putLong("String_Node_Str",System.currentTimeMillis());
      e.commit();
    }
  }
 catch (  Exception e) {
    Log.e("String_Node_Str","String_Node_Str" + Log.getStackTraceString(e));
    if (errmsg.length() == 0)     errmsg="String_Node_Str";
    sendStatus(STATUS_ERROR,errmsg);
  }
 finally {
    Log.d("String_Node_Str","String_Node_Str" + ((System.currentTimeMillis() - startTime) / 1000) + "String_Node_Str");
    receiver=null;
  }
}