-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
・AccountsDBのスキーマ変更 ・ホームタイムライン取得の場合は60秒間隔以上 ・リストをTLとして使用する場合は1秒間隔以上 ・設定からユーザーが秒数を指定する必要あり ・UserStreamの完全廃止は08/24 1:00なのでそれまではコードをそのまま残しておく
- Loading branch information
1 parent
182c172
commit 8974dda
Showing
12 changed files
with
256 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package sugtao4423.lod; | ||
|
||
import java.util.Collections; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.Notification; | ||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.app.Service; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Build; | ||
import android.os.IBinder; | ||
import twitter4j.Paging; | ||
import twitter4j.ResponseList; | ||
import twitter4j.Status; | ||
import twitter4j.TwitterException; | ||
|
||
public class AutoLoadTLService extends Service{ | ||
|
||
public interface AutoLoadTLListener{ | ||
void onStatus(ResponseList<Status> statuses); | ||
} | ||
|
||
private Timer autoLoadTimer; | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId){ | ||
App app = (App)getApplicationContext(); | ||
AutoLoadTLListener listener = app.getAutoLoadTLListener(); | ||
int interval = app.getCurrentAccount().getAutoLoadTLInterval(); | ||
long listAsTL = app.getCurrentAccount().getListAsTL(); | ||
|
||
AutoLoadTLTask task = new AutoLoadTLTask(app, listener, listAsTL); | ||
autoLoadTimer = new Timer(true); | ||
autoLoadTimer.schedule(task, interval * 1000, interval * 1000); | ||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ | ||
startNotification(intent); | ||
} | ||
return super.onStartCommand(intent, flags, startId); | ||
} | ||
|
||
@TargetApi(26) | ||
public void startNotification(Intent intent){ | ||
Intent appIntent = new Intent(this, MainActivity.class); | ||
appIntent.setAction(Intent.ACTION_MAIN); | ||
appIntent.addCategory(Intent.CATEGORY_LAUNCHER); | ||
|
||
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
String channelId = "default"; | ||
String title = "Running AutoLoadTL Service"; | ||
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); | ||
NotificationChannel channel = new NotificationChannel(channelId, title, NotificationManager.IMPORTANCE_DEFAULT); | ||
if(notificationManager != null){ | ||
notificationManager.createNotificationChannel(channel); | ||
Notification notification = new Notification.Builder(getApplicationContext(), channelId) | ||
.setContentTitle(title) | ||
.setSmallIcon(R.drawable.notification_icon) | ||
.setAutoCancel(true) | ||
.setContentIntent(pendingIntent) | ||
.setWhen(System.currentTimeMillis()) | ||
.build(); | ||
startForeground(1, notification); | ||
} | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent){ | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onDestroy(){ | ||
autoLoadTimer.cancel(); | ||
autoLoadTimer.purge(); | ||
super.onDestroy(); | ||
} | ||
|
||
class AutoLoadTLTask extends TimerTask{ | ||
|
||
private App app; | ||
private AutoLoadTLListener listener; | ||
private long listAsTL; | ||
|
||
public AutoLoadTLTask(App app, AutoLoadTLListener listener, long listAsTL){ | ||
this.app = app; | ||
this.listener = listener; | ||
this.listAsTL = listAsTL; | ||
} | ||
|
||
@Override | ||
public void run(){ | ||
try{ | ||
ResponseList<Status> statuses; | ||
if(listAsTL > 0){ | ||
statuses = app.getTwitter().getUserListStatuses(listAsTL, new Paging(1, 50).sinceId(app.getLatestTweetId())); | ||
}else{ | ||
statuses = app.getTwitter().getHomeTimeline(new Paging(1, 50).sinceId(app.getLatestTweetId())); | ||
} | ||
Collections.reverse(statuses); | ||
listener.onStatus(statuses); | ||
}catch(TwitterException e){ | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
8974dda
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
定期的に更新というより、まだ取得していない新しいツイートを取得するだけなので通信量を気にする必要はさほどない(はず)