Skip to content

Commit

Permalink
リファクタ
Browse files Browse the repository at this point in the history
  • Loading branch information
tateisu committed Mar 8, 2017
1 parent 5928a3a commit f3273e9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ public class DownloadService extends Service{
boolean will_restart;

void worker_wakeup( String cause ){
if( worker != null && worker.isAlive() ) return;
if( worker != null && worker.isAlive() ){
worker.notifyEx();
return;
}

try{
worker = new DownloadWorker( this, cause, worker_callback );
Expand Down
81 changes: 38 additions & 43 deletions app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,6 @@ public DownloadWorker( DownloadService service, String cause, Callback callback
service.location_tracker.updateSetting( location_setting );
}

final AtomicReference<String> status = new AtomicReference<>( "?" );

public String getStatus(){
return status.get();
}

static final Pattern reJPEG = Pattern.compile( "\\.jp(g|eg?)\\z", Pattern.CASE_INSENSITIVE );

static final Pattern reFileType = Pattern.compile( "(\\S+)" );

private ArrayList<Pattern> file_type_parse(){
ArrayList<Pattern> list = new ArrayList<>();
Matcher m = reFileType.matcher( file_type );
while( m.find() ){
try{
String spec = m.group( 1 ).replaceAll( "(\\W)", "\\\\$1" ).replaceAll( "\\\\\\*", ".*?" );
list.add( Pattern.compile( spec + "\\z", Pattern.CASE_INSENSITIVE ) );
}catch( Throwable ex ){
ex.printStackTrace();
log.e( R.string.file_type_parse_error, m.group( 1 ), ex.getClass().getSimpleName(), ex.getMessage() );
}
}
return list;
}

final HTTPClient client = new HTTPClient( 30000, 4, "HTTP Client", this );
final AtomicReference<String> cancel_reason = new AtomicReference<>( null );

Expand All @@ -155,8 +130,8 @@ public void cancel( String reason ){
}
synchronized( this ){
notify();
client.cancel( log );
}
client.cancel( log );
}catch( Throwable ex ){
ex.printStackTrace();
}
Expand All @@ -172,6 +147,34 @@ void waitEx( long ms ){
}
}

public synchronized void notifyEx(){
notify();
}

final AtomicReference<String> status = new AtomicReference<>( "?" );

public String getStatus(){
return status.get();
}

static final Pattern reJPEG = Pattern.compile( "\\.jp(g|eg?)\\z", Pattern.CASE_INSENSITIVE );
static final Pattern reFileType = Pattern.compile( "(\\S+)" );

private ArrayList<Pattern> file_type_parse(){
ArrayList<Pattern> list = new ArrayList<>();
Matcher m = reFileType.matcher( file_type );
while( m.find() ){
try{
String spec = m.group( 1 ).replaceAll( "(\\W)", "\\\\$1" ).replaceAll( "\\\\\\*", ".*?" );
list.add( Pattern.compile( spec + "\\z", Pattern.CASE_INSENSITIVE ) );
}catch( Throwable ex ){
ex.printStackTrace();
log.e( R.string.file_type_parse_error, m.group( 1 ), ex.getClass().getSimpleName(), ex.getMessage() );
}
}
return list;
}

static class Item{

final String air_path;
Expand Down Expand Up @@ -243,7 +246,6 @@ static class Item{
ex.printStackTrace();
log.e( "待機の設定に失敗 %s %s", ex.getClass().getSimpleName(), ex.getMessage() );
}
callback.releaseWakeLock();
cancel( service.getString( R.string.wait_alarm, Utils.formatTimeDuration( remain ) ) );
break;
}
Expand All @@ -264,18 +266,15 @@ static class Item{
if( network != null ) break;
//
long er_now = SystemClock.elapsedRealtime();
if( network_check_start == 0L ) network_check_start = er_now;
if( er_now - network_check_start >= 60 * 1000L ) break;
if( er_now - network_check_start >= 60 * 1000L ){
cancel( service.getString( R.string.wifi_not_good ) );
break;
}
//
waitEx( 333L );
waitEx( 1000L );
}
if( isCancelled() ) break;

if( network == null ){
cancel( service.getString( R.string.wifi_not_good ) );
break;
}

// 成功しても失敗しても、次回待機の計算はここから
Pref.pref( service ).edit().putLong( Pref.LAST_START, System.currentTimeMillis() ).apply();

Expand All @@ -285,15 +284,14 @@ static class Item{
status.set( service.getString( R.string.flashair_update_status_check ) );
String cgi_url = flashair_url + "command.cgi?op=121";
byte[] data = client.getHTTP( log, network, cgi_url );
if( isCancelled() ) break;
if( data == null ){
if( client.last_error.contains( "UnknownHostException" ) ){
client.last_error = service.getString( R.string.flashair_host_error );
cancel( service.getString( R.string.flashair_host_error_short ) );
callback.releaseWakeLock();
}else if( client.last_error.contains( "ENETUNREACH" ) ){
client.last_error = service.getString( R.string.network_unreachable );
cancel( service.getString( R.string.network_unreachable ) );
callback.releaseWakeLock();
}
log.e( R.string.flashair_update_check_failed, cgi_url, client.last_error );
continue;
Expand All @@ -303,7 +301,6 @@ static class Item{
}catch( Throwable ex ){
log.e( R.string.flashair_update_status_error );
cancel( service.getString( R.string.flashair_update_status_error ) );
callback.releaseWakeLock();
continue;
}
long flashair_update_status_old = Pref.pref( service ).getLong( Pref.FLASHAIR_UPDATE_STATUS_OLD, - 1L );
Expand Down Expand Up @@ -335,7 +332,6 @@ static class Item{
if( ! repeat ){
Pref.pref( service ).edit().putInt( Pref.LAST_MODE, Pref.LAST_MODE_STOP ).apply();
cancel( service.getString( R.string.repeat_off ) );
callback.releaseWakeLock();
allow_stop_service = true;
}
}
Expand All @@ -351,15 +347,15 @@ static class Item{
// フォルダを読む
String cgi_url = flashair_url + "command.cgi?op=100&DIR=" + Uri.encode( item.air_path );
byte[] data = client.getHTTP( log, network, cgi_url );
if( isCancelled() ) break;

if( data == null ){
if( client.last_error.contains( "UnknownHostException" ) ){
client.last_error = service.getString( R.string.flashair_host_error );
cancel( service.getString( R.string.flashair_host_error_short ) );
callback.releaseWakeLock();
}else if( client.last_error.contains( "ENETUNREACH" ) ){
client.last_error = service.getString( R.string.network_unreachable );
cancel( service.getString( R.string.network_unreachable ) );
callback.releaseWakeLock();
}
log.e( R.string.folder_list_failed, item.air_path, cgi_url, client.last_error );
has_error = true;
Expand Down Expand Up @@ -469,11 +465,9 @@ public byte[] onHTTPClientStream( LogWriter log, CancelChecker cancel_checker, I
if( client.last_error.contains( "UnknownHostException" ) ){
client.last_error = service.getString( R.string.flashair_host_error );
cancel( service.getString( R.string.flashair_host_error_short ) );
callback.releaseWakeLock();
}else if( client.last_error.contains( "ENETUNREACH" ) ){
client.last_error = service.getString( R.string.network_unreachable );
cancel( service.getString( R.string.network_unreachable ) );
callback.releaseWakeLock();
}

has_error = true;
Expand All @@ -499,6 +493,7 @@ public byte[] onHTTPClientStream( LogWriter log, CancelChecker cancel_checker, I
}
}
}
callback.releaseWakeLock();
status.set( service.getString( R.string.thread_end ) );
callback.onThreadEnd( allow_stop_service );
}
Expand Down

0 comments on commit f3273e9

Please sign in to comment.