use of com.klinker.android.twitter.data.sq_lite.HomeDataSource in project Talon-for-Twitter by klinker24.
the class TimelineRefreshService method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
if (!MainActivity.canSwitch || CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning) {
return;
}
if (MainActivity.canSwitch) {
TimelineRefreshService.isRunning = true;
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
Context context = getApplicationContext();
int numberNew = 0;
AppSettings settings = AppSettings.getInstance(context);
// if they have mobile data on and don't want to sync over mobile data
if (intent.getBooleanExtra("on_start_refresh", false)) {
} else if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
return;
}
Twitter twitter = Utils.getTwitter(context, settings);
HomeDataSource dataSource = HomeDataSource.getInstance(context);
int currentAccount = sharedPrefs.getInt("current_account", 1);
List<twitter4j.Status> statuses = new ArrayList<twitter4j.Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
long[] lastId = null;
long id;
try {
lastId = dataSource.getLastIds(currentAccount);
id = lastId[1];
} catch (Exception e) {
try {
Thread.sleep(5000);
} catch (InterruptedException i) {
}
TimelineRefreshService.isRunning = false;
return;
}
if (id == 0) {
id = 1;
}
try {
paging.setSinceId(id);
} catch (Exception e) {
paging.setSinceId(1l);
}
for (int i = 0; i < settings.maxTweetsRefresh; i++) {
try {
if (!foundStatus) {
paging.setPage(i + 1);
List<Status> list = twitter.getHomeTimeline(paging);
statuses.addAll(list);
if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
Log.v("talon_inserting", "found status");
foundStatus = true;
} else {
Log.v("talon_inserting", "haven't found status");
foundStatus = false;
}
}
} catch (Exception e) {
// the page doesn't exist
foundStatus = true;
} catch (OutOfMemoryError o) {
// don't know why...
}
}
Log.v("talon_pull", "got statuses, new = " + statuses.size());
// hash set to check for duplicates I guess
HashSet hs = new HashSet();
hs.addAll(statuses);
statuses.clear();
statuses.addAll(hs);
Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
lastId = dataSource.getLastIds(currentAccount);
Long currentTime = Calendar.getInstance().getTimeInMillis();
if (currentTime - sharedPrefs.getLong("last_timeline_insert", 0l) < 10000) {
Log.v("talon_refresh", "don't insert the tweets on refresh");
sendBroadcast(new Intent("com.klinker.android.twitter.TIMELINE_REFRESHED").putExtra("number_new", 0));
TimelineRefreshService.isRunning = false;
context.getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
return;
} else {
sharedPrefs.edit().putLong("last_timeline_insert", currentTime).commit();
}
int inserted = HomeDataSource.getInstance(context).insertTweets(statuses, currentAccount, lastId);
if (inserted > 0 && statuses.size() > 0) {
sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
}
if (!intent.getBooleanExtra("on_start_refresh", false)) {
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
if (settings.notifications && (settings.timelineNot || settings.favoriteUserNotifications) && inserted > 0 && !intent.getBooleanExtra("from_launcher", false)) {
NotificationUtils.refreshNotification(context, !settings.timelineNot);
}
if (settings.preCacheImages) {
startService(new Intent(this, PreCacheService.class));
}
} else {
Log.v("talon_refresh", "sending broadcast to fragment");
sendBroadcast(new Intent("com.klinker.android.twitter.TIMELINE_REFRESHED").putExtra("number_new", inserted));
}
sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
TimelineRefreshService.isRunning = false;
}
}
use of com.klinker.android.twitter.data.sq_lite.HomeDataSource in project Talon-for-Twitter by klinker24.
the class WidgetRefreshService method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
// it is refreshing elsewhere, so don't start
if (WidgetRefreshService.isRunning || TimelineRefreshService.isRunning || CatchupPull.isRunning || !MainActivity.canSwitch) {
return;
}
WidgetRefreshService.isRunning = true;
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_icon).setTicker(getResources().getString(R.string.refreshing) + "...").setContentTitle(getResources().getString(R.string.app_name)).setContentText(getResources().getString(R.string.refreshing_widget) + "...").setProgress(100, 100, true).setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.drawer_sync_dark));
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(6, mBuilder.build());
Context context = getApplicationContext();
AppSettings settings = AppSettings.getInstance(context);
// if they have mobile data on and don't want to sync over mobile data
if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
return;
}
Twitter twitter = Utils.getTwitter(context, settings);
HomeDataSource dataSource = HomeDataSource.getInstance(context);
int currentAccount = sharedPrefs.getInt("current_account", 1);
List<twitter4j.Status> statuses = new ArrayList<twitter4j.Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
long[] lastId;
long id;
try {
lastId = dataSource.getLastIds(currentAccount);
id = lastId[0];
} catch (Exception e) {
WidgetRefreshService.isRunning = false;
return;
}
paging.setSinceId(id);
for (int i = 0; i < settings.maxTweetsRefresh; i++) {
try {
if (!foundStatus) {
paging.setPage(i + 1);
List<Status> list = twitter.getHomeTimeline(paging);
statuses.addAll(list);
if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
Log.v("talon_inserting", "found status");
foundStatus = true;
} else {
Log.v("talon_inserting", "haven't found status");
foundStatus = false;
}
}
} catch (Exception e) {
// the page doesn't exist
foundStatus = true;
} catch (OutOfMemoryError o) {
// don't know why...
}
}
Log.v("talon_pull", "got statuses, new = " + statuses.size());
// hash set to remove duplicates I guess
HashSet hs = new HashSet();
hs.addAll(statuses);
statuses.clear();
statuses.addAll(hs);
Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
lastId = dataSource.getLastIds(currentAccount);
int inserted = HomeDataSource.getInstance(context).insertTweets(statuses, currentAccount, lastId);
if (inserted > 0 && statuses.size() > 0) {
sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
}
if (settings.preCacheImages) {
startService(new Intent(this, PreCacheService.class));
}
context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
mNotificationManager.cancel(6);
WidgetRefreshService.isRunning = false;
}
use of com.klinker.android.twitter.data.sq_lite.HomeDataSource in project Talon-for-Twitter by klinker24.
the class CatchupPull method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
if (CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning || !MainActivity.canSwitch) {
return;
}
CatchupPull.isRunning = true;
Log.v("talon_pull", "catchup pull started");
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
final Context context = getApplicationContext();
int unreadNow = sharedPrefs.getInt("pull_unread", 0);
// stop it just in case
context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
AppSettings settings = AppSettings.getInstance(context);
if (settings.liveStreaming) {
Log.v("talon_pull", "into the try for catchup service");
Twitter twitter = Utils.getTwitter(context, settings);
HomeDataSource dataSource = HomeDataSource.getInstance(context);
int currentAccount = sharedPrefs.getInt("current_account", 1);
List<Status> statuses = new ArrayList<Status>();
boolean foundStatus = false;
Paging paging = new Paging(1, 200);
long[] lastId;
long id;
try {
lastId = dataSource.getLastIds(currentAccount);
id = lastId[0];
} catch (Exception e) {
context.startService(new Intent(context, TalonPullNotificationService.class));
CatchupPull.isRunning = false;
return;
}
try {
paging.setSinceId(id);
} catch (Exception e) {
paging.setSinceId(1l);
}
for (int i = 0; i < settings.maxTweetsRefresh; i++) {
try {
if (!foundStatus) {
paging.setPage(i + 1);
List<Status> list = twitter.getHomeTimeline(paging);
statuses.addAll(list);
if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
Log.v("talon_inserting", "found status");
foundStatus = true;
} else {
Log.v("talon_inserting", "haven't found status");
foundStatus = false;
}
}
} catch (Exception e) {
// the page doesn't exist
foundStatus = true;
e.printStackTrace();
} catch (OutOfMemoryError o) {
// don't know why...
o.printStackTrace();
}
}
Log.v("talon_pull", "got statuses, new = " + statuses.size());
// hash set to remove duplicates I guess
HashSet hs = new HashSet();
hs.addAll(statuses);
statuses.clear();
statuses.addAll(hs);
Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
lastId = dataSource.getLastIds(currentAccount);
int inserted = dataSource.insertTweets(statuses, currentAccount, lastId);
if (inserted > 0 && statuses.size() > 0) {
sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
unreadNow += statuses.size();
}
if (settings.preCacheImages) {
// delay it 15 seconds so that we can finish checking mentions first
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startService(new Intent(context, PreCacheService.class));
}
}, 15000);
}
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
}
try {
Twitter twitter = Utils.getTwitter(context, settings);
int currentAccount = sharedPrefs.getInt("current_account", 1);
User user = twitter.verifyCredentials();
MentionsDataSource dataSource = MentionsDataSource.getInstance(context);
long[] lastId = dataSource.getLastIds(currentAccount);
Paging paging;
paging = new Paging(1, 200);
if (lastId[0] > 0) {
paging.sinceId(lastId[0]);
}
List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);
int numNew = dataSource.insertTweets(statuses, currentAccount);
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
if (settings.notifications && settings.mentionsNot && numNew > 0) {
NotificationUtils.refreshNotification(context);
}
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
sharedPrefs.edit().putInt("pull_unread", unreadNow).commit();
context.startService(new Intent(context, TalonPullNotificationService.class));
context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
Log.v("talon_pull", "finished with the catchup service");
CatchupPull.isRunning = false;
}
use of com.klinker.android.twitter.data.sq_lite.HomeDataSource in project Talon-for-Twitter by klinker24.
the class WidgetViewsFactory method onDataSetChanged.
@Override
public void onDataSetChanged() {
Log.v("talon_widget", "on data set changed");
mWidgetItems = new ArrayList<Tweet>();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
HomeDataSource data = HomeDataSource.getInstance(mContext);
Cursor query = data.getWidgetCursor(sharedPrefs.getInt("current_account", 1));
try {
if (query.moveToFirst()) {
do {
mWidgetItems.add(new Tweet(query.getLong(query.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_TEXT)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_NAME)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_PRO_PIC)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_SCREEN_NAME)), query.getLong(query.getColumnIndex(HomeSQLiteHelper.COLUMN_TIME)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_RETWEETER)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_PIC_URL)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_URL)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_USERS)), query.getString(query.getColumnIndex(HomeSQLiteHelper.COLUMN_HASHTAGS))));
} while (query.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
try {
query.close();
} catch (Exception e) {
}
Log.v("talon_widget", "size of " + mWidgetItems.size());
mCount = mWidgetItems.size();
}
use of com.klinker.android.twitter.data.sq_lite.HomeDataSource in project Talon-for-Twitter by klinker24.
the class IOUtils method trimDatabase.
public static boolean trimDatabase(Context context, int account) {
try {
AppSettings settings = AppSettings.getInstance(context);
SharedPreferences sharedPrefs = context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
InteractionsDataSource interactions = InteractionsDataSource.getInstance(context);
Cursor inters = interactions.getCursor(account);
if (inters.getCount() > 50) {
if (inters.moveToPosition(inters.getCount() - 50)) {
do {
interactions.deleteInteraction(inters.getLong(inters.getColumnIndex(InteractionsSQLiteHelper.COLUMN_ID)));
} while (inters.moveToPrevious());
}
}
inters.close();
HomeDataSource home = HomeDataSource.getInstance(context);
home.deleteDups(settings.currentAccount);
Cursor timeline = home.getTrimmingCursor(account);
Log.v("trimming", "timeline size: " + timeline.getCount());
Log.v("trimming", "timeline settings size: " + settings.timelineSize);
if (timeline.getCount() > settings.timelineSize) {
if (timeline.moveToPosition(timeline.getCount() - settings.timelineSize)) {
Log.v("trimming", "in the trim section");
do {
home.deleteTweet(timeline.getLong(timeline.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID)));
} while (timeline.moveToPrevious());
}
}
timeline.close();
// trimming the lists
ListDataSource lists = ListDataSource.getInstance(context);
int account1List1 = sharedPrefs.getInt("account_" + account + "_list_1", 0);
int account1List2 = sharedPrefs.getInt("account_" + account + "_list_2", 0);
lists.deleteDups(account1List1);
lists.deleteDups(account1List2);
Cursor list1 = lists.getTrimmingCursor(account1List1);
Log.v("trimming", "lists size: " + list1.getCount());
Log.v("trimming", "lists settings size: " + 400);
if (list1.getCount() > 400) {
if (list1.moveToPosition(list1.getCount() - 400)) {
Log.v("trimming", "in the trim section");
do {
lists.deleteTweet(list1.getLong(list1.getColumnIndex(ListSQLiteHelper.COLUMN_TWEET_ID)));
} while (list1.moveToPrevious());
}
}
list1.close();
Cursor list2 = lists.getTrimmingCursor(account1List2);
Log.v("trimming", "lists size: " + list2.getCount());
Log.v("trimming", "lists settings size: " + 400);
if (list2.getCount() > 400) {
if (list2.moveToPosition(list2.getCount() - 400)) {
Log.v("trimming", "in the trim section");
do {
lists.deleteTweet(list2.getLong(list2.getColumnIndex(ListSQLiteHelper.COLUMN_TWEET_ID)));
} while (list2.moveToPrevious());
}
}
list2.close();
MentionsDataSource mentions = MentionsDataSource.getInstance(context);
mentions.deleteDups(settings.currentAccount);
timeline = mentions.getTrimmingCursor(account);
Log.v("trimming", "mentions size: " + timeline.getCount());
Log.v("trimming", "mentions settings size: " + settings.mentionsSize);
if (timeline.getCount() > settings.mentionsSize) {
if (timeline.moveToPosition(timeline.getCount() - settings.mentionsSize)) {
do {
mentions.deleteTweet(timeline.getLong(timeline.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID)));
} while (timeline.moveToPrevious());
}
}
timeline.close();
DMDataSource dm = DMDataSource.getInstance(context);
dm.deleteDups(settings.currentAccount);
timeline = dm.getCursor(account);
Log.v("trimming", "dm size: " + timeline.getCount());
Log.v("trimming", "dm settings size: " + settings.dmSize);
if (timeline.getCount() > settings.dmSize) {
if (timeline.moveToPosition(timeline.getCount() - settings.dmSize)) {
do {
dm.deleteTweet(timeline.getLong(timeline.getColumnIndex(HomeSQLiteHelper.COLUMN_TWEET_ID)));
} while (timeline.moveToPrevious());
}
}
timeline.close();
HashtagDataSource hashtag = HashtagDataSource.getInstance(context);
timeline = hashtag.getCursor("");
Log.v("trimming", "hashtag size: " + timeline.getCount());
if (timeline.getCount() > 300) {
if (timeline.moveToPosition(timeline.getCount() - 300)) {
do {
hashtag.deleteTag(timeline.getString(timeline.getColumnIndex(HashtagSQLiteHelper.COLUMN_TAG)));
} while (timeline.moveToPrevious());
}
}
timeline.close();
ActivityDataSource activity = ActivityDataSource.getInstance(context);
Cursor actCurs = activity.getCursor(account);
Log.v("trimming", "activity size: " + actCurs.getCount());
Log.v("trimming", "activity settings size: " + 200);
if (actCurs.getCount() > 200) {
int toDelete = actCurs.getCount() - 200;
if (actCurs.moveToFirst()) {
do {
activity.deleteItem(actCurs.getLong(actCurs.getColumnIndex(ActivitySQLiteHelper.COLUMN_ID)));
toDelete--;
} while (timeline.moveToNext() && toDelete > 0);
}
}
actCurs.close();
FavoriteTweetsDataSource favtweets = FavoriteTweetsDataSource.getInstance(context);
favtweets.deleteDups(settings.currentAccount);
timeline = favtweets.getCursor(account);
Log.v("trimming", "favtweets size: " + timeline.getCount());
Log.v("trimming", "favtweets settings size: " + 200);
if (timeline.getCount() > 200) {
if (timeline.moveToPosition(timeline.getCount() - 200)) {
do {
favtweets.deleteTweet(timeline.getLong(timeline.getColumnIndex(FavoriteTweetsSQLiteHelper.COLUMN_TWEET_ID)));
} while (timeline.moveToPrevious());
}
}
timeline.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Aggregations