リアルタイム通知まわりの動作改善
This commit is contained in:
parent
e4c71a2cb2
commit
321d865792
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -37,7 +37,7 @@
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
@ -9,8 +9,8 @@ android {
|
||||
applicationId "jp.juggler.subwaytooter"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 26
|
||||
versionCode 141
|
||||
versionName "1.4.1"
|
||||
versionCode 142
|
||||
versionName "1.4.2"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package jp.juggler.subwaytooter;
|
||||
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.google.firebase.messaging.FirebaseMessagingService;
|
||||
import com.google.firebase.messaging.RemoteMessage;
|
||||
|
||||
@ -8,9 +10,18 @@ import java.util.Map;
|
||||
import jp.juggler.subwaytooter.util.LogCategory;
|
||||
|
||||
public class MyFirebaseMessagingService extends FirebaseMessagingService {
|
||||
static final LogCategory log = new LogCategory("MyFirebaseMessagingService");
|
||||
static final LogCategory log = new LogCategory( "MyFirebaseMessagingService" );
|
||||
|
||||
@Override public void onCreate(){
|
||||
super.onCreate();
|
||||
|
||||
// メインスレッド上でPollingWorkerを初期化しておく
|
||||
PollingWorker.getInstance( getApplicationContext() );
|
||||
}
|
||||
|
||||
@Override public void onMessageReceived( RemoteMessage remoteMessage ){
|
||||
long time_start = SystemClock.elapsedRealtime();
|
||||
|
||||
super.onMessageReceived( remoteMessage );
|
||||
|
||||
String tag = null;
|
||||
@ -25,7 +36,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
|
||||
}
|
||||
}
|
||||
|
||||
PollingWorker.handleFCMMessage( getApplicationContext() ,tag);
|
||||
PollingWorker.handleFCMMessage( getApplicationContext(), time_start, tag );
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,37 +2,24 @@ package jp.juggler.subwaytooter;
|
||||
|
||||
import android.app.job.JobParameters;
|
||||
import android.app.job.JobService;
|
||||
import jp.juggler.subwaytooter.util.LogCategory;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class PollingService extends JobService {
|
||||
|
||||
static final LogCategory log = new LogCategory( "PollingService" );
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// ワーカースレッドの管理
|
||||
|
||||
PollingWorker polling_worker;
|
||||
|
||||
@Override public void onCreate(){
|
||||
log.d( "onCreate" );
|
||||
super.onCreate();
|
||||
|
||||
// クラッシュレポートによると App1.onCreate より前にここを通る場合がある
|
||||
// データベースへアクセスできるようにする
|
||||
App1.prepare( getApplicationContext() );
|
||||
|
||||
polling_worker = PollingWorker.getInstance( getApplicationContext() );
|
||||
}
|
||||
|
||||
@Override public void onDestroy(){
|
||||
log.d( "onDestroy" );
|
||||
super.onDestroy();
|
||||
polling_worker.cancelAllJob();
|
||||
polling_worker.onJobServiceDestroy();
|
||||
}
|
||||
|
||||
@Override public boolean onStartJob( JobParameters params ){
|
||||
return polling_worker.onStartJob( this,params );
|
||||
return polling_worker.onStartJob( this, params );
|
||||
}
|
||||
|
||||
@Override public boolean onStopJob( JobParameters params ){
|
||||
|
@ -133,9 +133,9 @@ public class PollingWorker {
|
||||
final WifiManager.WifiLock wifi_lock;
|
||||
|
||||
private PollingWorker( Context c ){
|
||||
log.d( "ctor" );
|
||||
|
||||
this.context = c.getApplicationContext();
|
||||
this.handler = new Handler( c.getMainLooper() );
|
||||
this.pref = Pref.pref( context );
|
||||
this.notification_manager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE );
|
||||
this.scheduler = (JobScheduler) context.getSystemService( Context.JOB_SCHEDULER_SERVICE );
|
||||
|
||||
@ -148,6 +148,13 @@ public class PollingWorker {
|
||||
wifi_lock = wifi_manager.createWifiLock( PollingWorker.class.getName() );
|
||||
wifi_lock.setReferenceCounted( false );
|
||||
|
||||
// クラッシュレポートによると App1.onCreate より前にここを通る場合がある
|
||||
// データベースへアクセスできるようにする
|
||||
App1.prepare( context );
|
||||
|
||||
this.pref = App1.pref;
|
||||
this.handler = new Handler( context.getMainLooper() );
|
||||
|
||||
//
|
||||
worker = new Worker();
|
||||
worker.start();
|
||||
@ -238,7 +245,9 @@ public class PollingWorker {
|
||||
// ジョブの管理
|
||||
|
||||
// JobService#onDestroy から呼ばれる
|
||||
public void cancelAllJob(){
|
||||
public void onJobServiceDestroy(){
|
||||
log.d( "onJobServiceDestroy" );
|
||||
|
||||
synchronized( job_list ){
|
||||
Iterator< JobItem > it = job_list.iterator();
|
||||
while( it.hasNext() ){
|
||||
@ -487,6 +496,8 @@ public class PollingWorker {
|
||||
this.job = job;
|
||||
this.taskId = taskId;
|
||||
|
||||
long process_db_id = -1L;
|
||||
|
||||
if( taskId == TASK_APP_DATA_IMPORT_BEFORE ){
|
||||
scheduler.cancelAll();
|
||||
for( SavedAccount a : SavedAccount.loadAccountList( context, log ) ){
|
||||
@ -519,6 +530,7 @@ public class PollingWorker {
|
||||
if( tag != null ){
|
||||
for( SavedAccount sa : SavedAccount.loadByTag( context, log, tag ) ){
|
||||
NotificationTracking.resetLastLoad( sa.db_id );
|
||||
process_db_id = sa.db_id;
|
||||
bDone = true;
|
||||
}
|
||||
}
|
||||
@ -567,6 +579,7 @@ public class PollingWorker {
|
||||
LinkedList< AccountThread > thread_list = new LinkedList<>();
|
||||
for( SavedAccount _a : SavedAccount.loadAccountList( context, log ) ){
|
||||
if( _a.isPseudo() ) continue;
|
||||
if( process_db_id != -1L && _a.db_id != process_db_id ) continue;
|
||||
AccountThread t = new AccountThread( _a );
|
||||
thread_list.add( t );
|
||||
t.start();
|
||||
@ -728,6 +741,8 @@ public class PollingWorker {
|
||||
|
||||
}catch( Throwable ex ){
|
||||
log.trace( ex );
|
||||
}finally{
|
||||
job.notifyWorkerThread();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1341,7 +1356,10 @@ public class PollingWorker {
|
||||
// FCMメッセージの処理
|
||||
//
|
||||
|
||||
public static void handleFCMMessage( @NonNull Context context, @Nullable String tag ){
|
||||
public static void handleFCMMessage( @NonNull Context context, long time_start, @Nullable String tag ){
|
||||
// FirebaseMessagingService#onMessageReceived はバックグラウンドスレッドから実行されるので、少しなら待機してもよい
|
||||
// https://firebase.google.com/docs/cloud-messaging/android/receive
|
||||
// 10秒を超えるとプロセスごと殺されるかもしれない
|
||||
|
||||
// タスクを追加
|
||||
JSONObject data = new JSONObject();
|
||||
@ -1357,16 +1375,14 @@ public class PollingWorker {
|
||||
PollingWorker pw = getInstance( context );
|
||||
pw.addJob( JOB_FCM );
|
||||
|
||||
// FirebaseMessagingService#onMessageReceived はバックグラウンドスレッドから実行されるので、少しなら待機してもよい
|
||||
long start = SystemClock.elapsedRealtime();
|
||||
for( ; ; ){
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
if( ! pw.hasJob( JOB_FCM ) ){
|
||||
log.d( "handleFCMMessage: JOB_FCM completed." );
|
||||
log.d( "handleFCMMessage: JOB_FCM completed. time=%.2f",(now-time_start)/1000f );
|
||||
break;
|
||||
}
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
if( now - start >= ( 1000L * 60 ) ){
|
||||
log.d( "handleFCMMessage: JOB_FCM is not completed. exit onMessageReceived..." );
|
||||
if( now - time_start >= ( 1000L * 300 ) ){
|
||||
log.d( "handleFCMMessage: JOB_FCM timeout. exit onMessageReceived..." );
|
||||
break;
|
||||
}
|
||||
try{
|
||||
|
Loading…
x
Reference in New Issue
Block a user