Skip to content

Commit

Permalink
TLを自動で定期的に更新する機能を実装!
Browse files Browse the repository at this point in the history
・AccountsDBのスキーマ変更
・ホームタイムライン取得の場合は60秒間隔以上
・リストをTLとして使用する場合は1秒間隔以上
・設定からユーザーが秒数を指定する必要あり
・UserStreamの完全廃止は08/24 1:00なのでそれまではコードをそのまま残しておく
  • Loading branch information
sugtao4423 committed Aug 22, 2018
1 parent 182c172 commit 8974dda
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 14 deletions.
2 changes: 2 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@

<service android:name="UserStreamService" >
</service>
<service android:name="AutoLoadTLService" >
</service>
</application>

</manifest>
2 changes: 2 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<string name="error_getOriginalImage">オリジナル画像の取得に失敗しました</string>
<string name="error_getUserDetail">ユーザー情報を取得できませんでした</string>

<string name="error_autoLoadTLInterval">TLを自動で取得するには60秒間隔以上で設定する必要があります\nこれを回避するにはリストをTLとして使用してください</string>

<string name="icon_bomb">&#xf1e2;</string>
<string name="icon_search">&#xf002;</string>
<string name="icon_refresh">&#xf021;</string>
Expand Down
12 changes: 9 additions & 3 deletions res/xml/preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@
android:positiveButtonText="OK"
android:title="フォーマット" />
</PreferenceCategory>
<PreferenceCategory android:title="TL自動取得" >
<CheckBoxPreference
android:key="listAsTL"
android:title="リストをTLとして読み込む" />

<Preference
android:key="autoLoadTLInterval"
android:title="TLを自動で取得する秒間隔 (要再起動)" />
</PreferenceCategory>
<PreferenceCategory android:title="その他" >
<CheckBoxPreference
android:key="isWebm"
android:summaryOff="mp4"
android:summaryOn="webm"
android:title="動画をwebmで取得する" />
<CheckBoxPreference
android:key="listAsTL"
android:title="リストをTLとして読み込む" />

<Preference
android:key="listSetting"
Expand Down
5 changes: 3 additions & 2 deletions src/sugtao4423/lod/AccountDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public AccountDB(Context context){
public void onCreate(SQLiteDatabase db){
// create table in "Accounts"
db.execSQL(String.format(
"CREATE TABLE accounts(%s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER, %s TEXT, %s TEXT, %s TEXT)",
"CREATE TABLE accounts(%s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT, %s TEXT)",
Keys.SCREEN_NAME, Keys.CK, Keys.CS, Keys.ACCESS_TOKEN, Keys.ACCESS_TOKEN_SECRET,
Keys.LIST_AS_TIMELINE, Keys.SELECT_LIST_IDS, Keys.SELECT_LIST_NAMES, Keys.APP_START_LOAD_LISTS));
Keys.LIST_AS_TIMELINE, Keys.AUTO_LOAD_TL_INTERVAL,
Keys.SELECT_LIST_IDS, Keys.SELECT_LIST_NAMES, Keys.APP_START_LOAD_LISTS));
}

@Override
Expand Down
21 changes: 21 additions & 0 deletions src/sugtao4423/lod/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.widget.Toast;
import sugtao4423.lod.AutoLoadTLService.AutoLoadTLListener;
import sugtao4423.lod.dataclass.Account;
import sugtao4423.lod.dataclass.Music;
import sugtao4423.lod.dataclass.TwitterList;
Expand All @@ -38,6 +39,8 @@ public class App extends Application{
private TwitterStream twitterStream;
private Pattern mentionPattern;
private UserStreamAdapter userStreamAdapter;
private AutoLoadTLListener autoLoadTLListener;
private long latestTweetId;
private ConnectionLifeCycleListener clcl;
private TwitterList[] lists;
private Options options;
Expand Down Expand Up @@ -161,6 +164,24 @@ public UserStreamAdapter getUserStreamAdapter(){
return userStreamAdapter;
}

// AutoLoadTLListener
public void setAutoLoadTLListener(AutoLoadTLListener autoLoadTLListener){
this.autoLoadTLListener = autoLoadTLListener;
}

public AutoLoadTLListener getAutoLoadTLListener(){
return autoLoadTLListener;
}

// LatestTweetId
public void setLatestTweetId(long latestTweetId){
this.latestTweetId = latestTweetId;
}

public long getLatestTweetId(){
return latestTweetId;
}

// ConnectionLifeCycleListener
public void setConnectionLifeCycleListener(ConnectionLifeCycleListener clcl){
this.clcl = clcl;
Expand Down
111 changes: 111 additions & 0 deletions src/sugtao4423/lod/AutoLoadTLService.java
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){
}
}

}

}
1 change: 1 addition & 0 deletions src/sugtao4423/lod/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public class Keys{
public static final String APP_START_LOAD_LISTS = "appStartLoadLists";
public static final String EXPERIENCE = "experience";
public static final String LIST_AS_TIMELINE = "listAsTL";
public static final String AUTO_LOAD_TL_INTERVAL = "autoLoadTLInterval";

}
28 changes: 28 additions & 0 deletions src/sugtao4423/lod/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.view.View;
import sugtao4423.icondialog.IconDialog;
import sugtao4423.icondialog.IconItem;
import sugtao4423.lod.AutoLoadTLService.AutoLoadTLListener;
import sugtao4423.lod.dataclass.TwitterList;
import sugtao4423.lod.main_fragment.Fragment_home;
import sugtao4423.lod.main_fragment.Fragment_mention;
Expand Down Expand Up @@ -73,6 +74,7 @@ public void logIn(){
twitter = app.getTwitter();
getList();
connectStreaming();
autoLoadTL();
}
}

Expand Down Expand Up @@ -147,6 +149,31 @@ public void run(){
}
}

public void autoLoadTL(){
if(app.getCurrentAccount().getAutoLoadTLInterval() == 0){
return;
}
AutoLoadTLListener listener = new AutoLoadTLListener(){

@Override
public void onStatus(ResponseList<Status> statuses){
for(Status s : statuses){
fragmentHome.insert(s);
if(app.getMentionPattern().matcher(s.getText()).find() && !s.isRetweet()){
fragmentMention.insert(s);
}
}
}
};
app.setAutoLoadTLListener(listener);
Intent intent = new Intent(this, AutoLoadTLService.class);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
startForegroundService(intent);
}else{
startService(intent);
}
}

public void setMusicReceiver(){
musicReceiver = new MusicReceiver();
IntentFilter filter = new IntentFilter();
Expand Down Expand Up @@ -201,6 +228,7 @@ public void onDestroy(){
super.onDestroy();
unregisterReceiver(musicReceiver);
stopService(new Intent(this, UserStreamService.class));
stopService(new Intent(this, AutoLoadTLService.class));
app.resetAccount();
if(resetFlag){
resetFlag = false;
Expand Down
Loading

1 comment on commit 8974dda

@sugtao4423
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定期的に更新というより、まだ取得していない新しいツイートを取得するだけなので通信量を気にする必要はさほどない(はず)

Please sign in to comment.