use of com.newsrob.util.Timing in project newsrob by marianokamp.
the class EntryManager method deleteArticles.
private int deleteArticles(final SyncJob job, final Cursor idsOfEntriesToDeleteCursor) {
Timing t = new Timing("DeleteArticles's assets", ctx);
final List<String> articleIdsToDeleteInDatabase = new ArrayList<String>(idsOfEntriesToDeleteCursor.getCount());
int noOfEntriesDeleted = 0;
try {
// TODO too
job.target = idsOfEntriesToDeleteCursor.getCount();
if (!fileContextAdapter.canWrite()) {
Log.w(TAG, "EntryManager.deleteArticles even though fileContext.canWrite() returns false. Skipping.");
} else {
while (idsOfEntriesToDeleteCursor.moveToNext()) {
if (job.isCancelled())
return noOfEntriesDeleted;
if (!fileContextAdapter.canWrite())
return noOfEntriesDeleted;
// PERFORMANCE ooh. performance
// problem. skip
// this step. don't load an entry first
// to delete it LATER
String mostRecentArticleAtomId = getMostRecentArticleAtomId();
String atomId = idsOfEntriesToDeleteCursor.getString(1);
// don't delete the article currently viewed by the user
if (mostRecentArticleAtomId == null || !atomId.endsWith(mostRecentArticleAtomId)) {
WebPageDownloadDirector.removeAssetsForId(atomId, fileContextAdapter, ctx);
articleIdsToDeleteInDatabase.add(idsOfEntriesToDeleteCursor.getString(0));
noOfEntriesDeleted++;
}
job.actual = noOfEntriesDeleted;
if (job.actual % 5 == 0)
fireStatusUpdated();
}
fireModelUpdated();
Log.d(TAG, "EntryManager.deleteArticles() cleaned up " + noOfEntriesDeleted + " articles' assets.");
}
// Debug.stopMethodTracing();
} finally {
idsOfEntriesToDeleteCursor.close();
t.stop();
}
deleteArticlesFromDb(job, articleIdsToDeleteInDatabase);
return noOfEntriesDeleted;
}
use of com.newsrob.util.Timing in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method removeDeletedNotes.
public void removeDeletedNotes() throws IOException, SAXException, ParserConfigurationException, GRTokenExpiredException {
NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
Timing t = new Timing("RemoveDeletedNotes", context);
final int noOfNotesToKeep = getEntryManager().getNoOfNotesToKeep();
long[] noteIds = getStreamIDsFromGR(httpClient, GOOGLE_STATE_CREATED, null, noOfNotesToKeep);
PL.log("EntriesRetriever.getStreamIds(Notes) done.", context);
entryManager.populateTempTable(noteIds);
entryManager.removeDeletedNotes();
t.stop();
} catch (GRAnsweredBadRequestException e) {
throw new IOException("GR: Bad Request.");
} finally {
httpClient.close();
}
}
use of com.newsrob.util.Timing in project newsrob by marianokamp.
the class SyncChangedArticlesStatusJob method doSync.
protected void doSync(final boolean uploadOnly, final boolean manualSync) {
final EntryManager entryManager = getEntryManager();
final EntriesRetriever grf = entryManager.getEntriesRetriever();
final IStorageAdapter fileContextAdapter = entryManager.getStorageAdapter();
if (entryManager.isModelCurrentlyUpdated())
return;
entryManager.lockModel("SSer.doSync");
PL.log("SynchronizationService. Used settings: " + SettingsRenderer.renderSettings(entryManager, new StringBuilder("\n")), SynchronizationService.this);
// entryManager.runningThread = new Thread(new Runnable() {
Throwable caughtThrowable = null;
// public void run() {
final Context ctx = getApplicationContext();
PL.log("SynchronizationService - start", SynchronizationService.this);
PL.log("Last successful login: " + entryManager.getLastSuccessfulLogin(), SynchronizationService.this);
// Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST);
Timing t = new Timing("Synchronization Runnable", this);
long started = System.currentTimeMillis();
Log.i(TAG, "Synchronization started at " + new Date().toString() + ". started=" + started);
final SyncJobStatus syncJobStatus = new SyncJobStatus();
// last used
long lastUsed = entryManager.getLastUsed();
ModelUpdateResult result = null;
try {
PL.log("Run Mark - in Try", SynchronizationService.this);
if (!uploadOnly) {
try {
if (!Feed.restoreFeedsIfNeccesary(this))
Feed.saveFeedSettings(this);
} catch (Exception e) {
e.printStackTrace();
}
}
entryManager.fireModelUpdateStarted("Synchronization", uploadOnly, manualSync);
List<Job> jobList = new LinkedList<Job>();
Job deleteReadArticlesJob = new DeleteArticlesJob(SynchronizationService.this, entryManager, syncJobStatus);
Job reduceToCapacityJob = new ReduceToCapacityJob(SynchronizationService.this, entryManager, syncJobStatus);
if (!uploadOnly) {
if (entryManager.shouldReadItemsBeDeleted())
jobList.add(deleteReadArticlesJob);
jobList.add(reduceToCapacityJob);
}
jobList.add(new Job("Submitting annotated articles", entryManager) {
@Override
public void run() throws Exception {
if (entryManager.syncCurrentlyEnabled(manualSync))
entryManager.getEntriesRetriever().submitNotes(this);
}
});
jobList.add(new SyncChangedArticlesStatusJob(SynchronizationService.this, entryManager, syncJobStatus, manualSync));
if (!uploadOnly && entryManager.shouldReadItemsBeDeleted())
jobList.add(deleteReadArticlesJob);
Job removeDeletedNotes = new Job("Removing submitted notes", entryManager) {
@Override
public void run() throws Exception {
if (entryManager.syncCurrentlyEnabled(manualSync))
entryManager.getEntriesRetriever().removeDeletedNotes();
}
};
if (!uploadOnly) {
jobList.add(new Job("Unsubscribing from feeds", entryManager) {
@Override
public void run() throws Exception {
if (!entryManager.syncCurrentlyEnabled(manualSync))
return;
Cursor c = entryManager.getFeeds2UnsubscribeCursor();
try {
while (c.moveToNext()) {
String feedAtomId = c.getString(1);
PL.log("Unsubscribing: " + feedAtomId, SynchronizationService.this);
entryManager.getEntriesRetriever().unsubscribeFeed(feedAtomId);
}
} finally {
c.close();
}
}
});
}
if (!uploadOnly)
jobList.add(removeDeletedNotes);
if (!uploadOnly)
jobList.add(new FetchUnreadArticlesJob(SynchronizationService.this, entryManager, syncJobStatus, manualSync));
if (!uploadOnly)
jobList.add(new Job("Daily update of subscriptions (feed titles)", entryManager) {
@Override
public void run() throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException {
if (entryManager.syncCurrentlyEnabled(manualSync)) {
grf.updateSubscriptionList(entryManager, this);
entryManager.fireModelUpdated();
}
}
});
if (!uploadOnly)
jobList.add(reduceToCapacityJob);
if (!uploadOnly && entryManager.shouldReadItemsBeDeleted())
jobList.add(deleteReadArticlesJob);
if (!uploadOnly) {
// make sure that a manual sync moves the automatic sync
// forward,
// i.e. when pushing "Refresh" in the middle of a 24h sync,
// reset timer to 0, so that it will be another 24h from this
// point on
entryManager.getScheduler().resetBackgroundSchedule();
final float screenSizeFactor = getScreenSizeFactor(ctx);
final float previewScaleFactor = ctx.getResources().getDisplayMetrics().density;
jobList.add(new Job("Downloading articles", entryManager) {
private int currentArticle = 0;
private Collection<Long> entries2Download;
@Override
public void run() {
if (entryManager.getSharedPreferences().getString(EntryManager.SETTINGS_STORAGE_ASSET_DOWNLOAD, EntryManager.DOWNLOAD_YES).equals(EntryManager.DOWNLOAD_NO)) {
Log.d(TAG, "Downloading of assets is disabled in the settings. Therefore skipping downloading webpages.");
return;
}
Timing tSql = new Timing("SQL Query findAllToDownload", SynchronizationService.this);
entries2Download = entryManager.findAllArticleIds2Download();
tSql.stop();
Timing tOutter = new Timing("Downloading all " + entries2Download.size() + " pages or well, the ones that were downloaded", SynchronizationService.this);
for (Long articleId : entries2Download) {
// get the current data
// LATER use a real cursor and somehow find out when
// data became stale
Entry entry = entryManager.findArticleById(articleId);
if (entry == null)
continue;
if (!entryManager.downloadContentCurrentlyEnabled(manualSync))
return;
if (!fileContextAdapter.canWrite()) {
Log.d(TAG, "File context adapter (" + fileContextAdapter.getClass().getName() + ") cannot be written to at the moment. Mounted? Read Only? Not downloading web pages.");
return;
}
if (isCancelled())
break;
// System.out.println("----------------- " +
// entry.getTitle());
currentArticle++;
entryManager.fireStatusUpdated();
// ones
if (entry.getReadState() == ReadState.READ && !entry.isStarred() && !entry.isNote())
continue;
int resolvedDownloadPref = entry.getResolvedDownloadPref(entryManager);
if (resolvedDownloadPref == Feed.DOWNLOAD_HEADERS_ONLY) {
continue;
}
// check against the db, because in the
// meantime
// the
// read status might have changed
Timing tInner = new Timing("Downloading page " + entry.getAlternateHRef(), SynchronizationService.this);
try {
// check free space
float freeSpaceLeft = fileContextAdapter.megaBytesFree();
Log.d(TAG, String.format("Free space remaining for downloads: %.2f MB.", freeSpaceLeft));
if (freeSpaceLeft < 0) {
PL.log(TAG + ": Oh no, free space left is a negative value ;-( Ignoring it.", ctx);
} else if (freeSpaceLeft < fileContextAdapter.megaBytesThreshold()) {
PL.log(TAG + ": Not enough space left to download page.", ctx);
entryManager.getNewsRobNotificationManager().createSyncSpaceExceededProblemNotification(fileContextAdapter.megaBytesThreshold());
break;
}
boolean downloadTheWholePage = (resolvedDownloadPref == Feed.DOWNLOAD_PREF_FEED_AND_MOBILE_WEBPAGE || resolvedDownloadPref == Feed.DOWNLOAD_PREF_FEED_AND_WEBPAGE);
String summary = entry.getContent() != null ? entry.getContent() : UIHelper.linkize(entry.getAlternateHRef(), entry.getTitle());
WebPageDownloadDirector.downloadWebPage(entry.getShortAtomId(), new URL(entry.getBaseUrl(entryManager)), fileContextAdapter, this, summary, downloadTheWholePage, entryManager, manualSync);
if (true) {
File assetsDir = entry.getAssetsDir(entryManager);
System.out.println("generatePreview=" + new PreviewGenerator(ctx, assetsDir, (int) (100 * previewScaleFactor * screenSizeFactor), (int) (100 * previewScaleFactor * screenSizeFactor), (int) (6 * previewScaleFactor)).generatePreview());
}
// TODO
// only
// one
// instance
// TODO use display metrics?
// TODO use orientation? Larger thumbs for
// larger screens?
entry.setDownloaded(downloadTheWholePage ? Entry.STATE_DOWNLOADED_FULL_PAGE : Entry.STATE_DOWNLOADED_FEED_CONTENT);
entry.setError(null);
entryManager.fireModelUpdated();
} catch (Exception e) {
Log.e(TAG, "Problem dowloading page " + entry.getAlternateHRef() + ".", e);
Throwable cause = null;
if (e instanceof DownloadException) {
cause = ((DownloadException) e).getCause();
Log.d(TAG, "DownloadException cause=" + cause);
} else
Log.d(TAG, "Exception=" + e);
boolean downloadError = false;
if (e instanceof DownloadCancelledException || cause != null && (cause instanceof FileNotFoundException || cause instanceof SocketTimeoutException || cause instanceof SocketException || cause instanceof NoHttpResponseException || cause instanceof UnknownHostException || cause instanceof DownloadCancelledException || cause instanceof DownloadTimedOutException)) {
Log.d(TAG, "Caught a FNFE");
} else {
Log.d(TAG, "Marked download as error.");
downloadError = true;
}
StringBuilder renderedStackTrace = new StringBuilder();
U.renderStackTrace(e, renderedStackTrace);
entry.setError(cause != null ? "Cause: " + cause.getClass().getSimpleName() + ": " + cause.getMessage() : e.getClass().getSimpleName() + ": " + e.getMessage() + "\nStacktrace: " + renderedStackTrace);
entry.setDownloaded(downloadError ? Entry.STATE_DOWNLOAD_ERROR : Entry.STATE_NOT_DOWNLOADED);
}
entryManager.updatedDownloaded(entry);
tInner.stop();
}
tOutter.stop();
}
@Override
public boolean isProgressMeassurable() {
return true;
}
@Override
public int[] getProgress() {
return new int[] { currentArticle, entries2Download != null ? entries2Download.size() : 0 };
}
});
}
PL.log("Run Mark - Jobs added", this);
entryManager.runJobs(jobList);
Log.d(TAG, "NoOfEntriesUpdated=" + syncJobStatus.noOfEntriesUpdated);
Log.d(TAG, "NoOfEntriesFetched=" + syncJobStatus.noOfEntriesFetched);
PL.log("Run Mark - Mission accomplished. -> complete ", this);
result = new SynchronizeModelSucceeded(syncJobStatus.noOfEntriesUpdated);
} catch (Throwable throwable) {
result = new SynchronizeModelFailed(throwable);
Log.d(TAG, "Problem during synchronization.", throwable);
} finally {
PL.log("Run Mark - In Finally", this);
entryManager.unlockModel("SSer.doSync");
entryManager.clearCancelState();
entryManager.fireModelUpdateFinished(result);
entryManager.fireStatusUpdated();
Log.i(TAG, "Synchronization finished at " + new Date().toString() + ". started=" + started);
t.stop();
if (!uploadOnly)
entryManager.setLastSync(caughtThrowable == null);
int noOfNewArticles = entryManager.getNoOfNewArticlesSinceLastUsed(lastUsed);
entryManager.getNewsRobNotificationManager().notifyNewArticles(entryManager, lastUsed, noOfNewArticles);
PL.log("Run Mark - End of Finally", this);
resetLastStarted();
releaseWakeLock();
}
}
use of com.newsrob.util.Timing in project newsrob by marianokamp.
the class DB method clearTempTable.
public void clearTempTable() {
Timing t = new Timing("DB.clearTempTable", context);
SQLiteDatabase dbase = getDb();
dbase.beginTransaction();
dbase.execSQL(CLEAR_TEMP_TABLE_SQL);
dbase.setTransactionSuccessful();
dbase.endTransaction();
t.stop();
}
use of com.newsrob.util.Timing in project newsrob by marianokamp.
the class DB method updateStatesFromTempTable.
public void updateStatesFromTempTable(ArticleDbState state) {
Timing t = new Timing("DB.updateStatesFromTempTable state=" + state, context);
String stateColumn = null;
String statePendingColumn = null;
int targetValueOn = 1;
int targetValueOff = 0;
if (state == ArticleDbState.READ) {
stateColumn = Entries.READ_STATE;
statePendingColumn = Entries.READ_STATE_PENDING;
targetValueOn = 0;
targetValueOff = 1;
} else if (state == ArticleDbState.STARRED) {
stateColumn = Entries.STARRED_STATE;
statePendingColumn = Entries.STARRED_STATE_PENDING;
} else if (state == ArticleDbState.SHARED) {
stateColumn = Entries.SHARED_STATE;
statePendingColumn = Entries.SHARED_STATE_PENDING;
} else if (state == ArticleDbState.LIKED) {
stateColumn = Entries.LIKED_STATE;
statePendingColumn = Entries.LIKED_STATE_PENDING;
}
if (stateColumn == null)
throw new IllegalStateException("stateColumn must not be null here.");
SQLiteDatabase dbase = getDb();
dbase.beginTransaction();
// Mark all articles as read where the read_state is not pending
// and they were not read before
Timing t3 = new Timing("DB.updateStatesFromTempTable - mark existing read", context);
final String markExistingSQL = "UPDATE " + Entries.TABLE_NAME + " SET " + stateColumn + " = " + targetValueOff + " WHERE " + stateColumn + " = " + targetValueOn + " AND " + statePendingColumn + " = 0;";
dbase.execSQL(markExistingSQL);
t3.stop();
// Mark all articles unread that exists in the temp table and are not
// read state pending
Timing t4 = new Timing("DB.updateStatesFromTempTable - mark as x", context);
String sql = context.getString(R.string.sql_mark_as_x);
sql = sql.replaceAll("-STATE-", stateColumn);
sql = sql.replaceAll("-STATE_PENDING-", statePendingColumn);
sql = sql.replaceAll("-SET-", targetValueOn + "");
sql = sql.replaceAll("-CLEAR-", targetValueOff + "");
dbase.execSQL(sql);
t4.stop();
if (state == ArticleDbState.READ) {
Timing t5 = new Timing("DB.updateReadStates - mark as read even when pinned", context);
dbase.execSQL(context.getString(R.string.sql_mark_as_read_even_when_pinned));
t5.stop();
}
dbase.setTransactionSuccessful();
dbase.endTransaction();
t.stop();
}
Aggregations