Skip to content

Commit

Permalink
FlashAir STAモードに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
tateisu committed Mar 16, 2017
1 parent 32bce26 commit 188a6ba
Show file tree
Hide file tree
Showing 11 changed files with 674 additions and 296 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.text.SimpleDateFormat

apply plugin: 'com.android.application'

android {
Expand Down Expand Up @@ -31,14 +33,14 @@ android {

// Generate Signed APK のファイル名を変更
applicationVariants.all { variant ->
if (variant.buildType.name.equals("release")) {
if (variant.buildType.name == "release") {
variant.outputs.each { output ->
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
// Rename APK
def versionCode = defaultConfig.versionCode
def versionName = defaultConfig.versionName
def flavor = variant.flavorName
def date = new java.text.SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
def date = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
def newName = "FADownloader-${flavor}-${versionCode}-${versionName}-${date}.apk"
output.outputFile = new File((String) output.outputFile.parent, (String) newName)
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/jp/juggler/fadownloader/ActMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ void startDownloadService(){
boolean repeat = pref.getBoolean( Pref.UI_REPEAT, false );

// 設定から値を読んでバリデーション


int target_type = pref.getInt( Pref.UI_TARGET_TYPE, - 1 );
if( target_type < 0 || target_type > LocationTracker.LOCATION_HIGH_ACCURACY ){
showToast( true, getString( R.string.target_type_invalid ) );
return;
}

String flashair_url = pref.getString( Pref.UI_FLASHAIR_URL, "" ).trim();
if( TextUtils.isEmpty( flashair_url ) ){
showToast( true, getString( R.string.url_not_ok ) );
Expand Down Expand Up @@ -593,6 +601,8 @@ void startDownloadService(){
// 転送サービスを開始
Intent intent = new Intent( this, DownloadService.class );
intent.setAction( DownloadService.ACTION_START );

intent.putExtra( DownloadService.EXTRA_TARGET_TYPE, target_type );
intent.putExtra( DownloadService.EXTRA_REPEAT, repeat );
intent.putExtra( DownloadService.EXTRA_URI, flashair_url );
intent.putExtra( DownloadService.EXTRA_FOLDER_URI, folder_uri );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class DownloadService extends Service{
static final String EXTRA_LOCATION_MODE = "location_mode";
static final String EXTRA_FORCE_WIFI = "force_wifi";
static final String EXTRA_SSID = "ssid";
static final String EXTRA_TARGET_TYPE = "target_type" ;

static final int NOTIFICATION_ID_SERVICE = 1;

Expand Down Expand Up @@ -369,4 +370,6 @@ void setServiceNotification( String status ){
public static Location getLocation(){
return location;
}


}
108 changes: 62 additions & 46 deletions app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownloadWorker extends Thread implements CancelChecker{
public class DownloadWorker extends WorkerBase {

static final boolean RECORD_QUEUED_STATE = false;


public static final int TARGET_TYPE_FLASHAIR_AP = 0;
public static final int TARGET_TYPE_FLASHAIR_STA = 1;

public interface Callback{

void releaseWakeLock();
Expand All @@ -51,14 +55,15 @@ public interface Callback{
final Callback callback;

final boolean repeat;
final String flashair_url;
String flashair_url;
final String folder_uri;
final int interval;
final String file_type;
final LogWriter log;
final ArrayList<Pattern> file_type_list;
final boolean force_wifi;
final String ssid;
final int target_type;

public DownloadWorker( DownloadService service, Intent intent, Callback callback ){
this.service = service;
Expand All @@ -73,6 +78,7 @@ public DownloadWorker( DownloadService service, Intent intent, Callback callback
this.file_type = intent.getStringExtra( DownloadService.EXTRA_FILE_TYPE );
this.force_wifi = intent.getBooleanExtra( DownloadService.EXTRA_FORCE_WIFI, false );
this.ssid = intent.getStringExtra( DownloadService.EXTRA_SSID );
this.target_type =intent.getIntExtra( DownloadService.EXTRA_TARGET_TYPE, 0 );

LocationTracker.Setting location_setting = new LocationTracker.Setting();
location_setting.interval_desired = intent.getLongExtra( DownloadService.EXTRA_LOCATION_INTERVAL_DESIRED, LocationTracker.DEFAULT_INTERVAL_DESIRED );
Expand All @@ -81,6 +87,7 @@ public DownloadWorker( DownloadService service, Intent intent, Callback callback

Pref.pref( service ).edit()
.putBoolean( Pref.WORKER_REPEAT, repeat )
.putInt( Pref.WORKER_TARGET_TYPE, target_type )
.putString( Pref.WORKER_FLASHAIR_URL, flashair_url )
.putString( Pref.WORKER_FOLDER_URI, folder_uri )
.putInt( Pref.WORKER_INTERVAL, interval )
Expand All @@ -94,7 +101,7 @@ public DownloadWorker( DownloadService service, Intent intent, Callback callback

file_type_list = file_type_parse();

service.wifi_tracker.updateSetting( force_wifi, ssid );
service.wifi_tracker.updateSetting( force_wifi, ssid ,target_type,flashair_url);

service.location_tracker.updateSetting( location_setting );
}
Expand All @@ -114,6 +121,8 @@ public DownloadWorker( DownloadService service, String cause, Callback callback

this.force_wifi = pref.getBoolean( Pref.WORKER_FORCE_WIFI, false );
this.ssid = pref.getString( Pref.WORKER_SSID, null );
this.target_type = pref.getInt( Pref.WORKER_TARGET_TYPE, 0);


LocationTracker.Setting location_setting = new LocationTracker.Setting();
location_setting.interval_desired = pref.getLong( Pref.WORKER_LOCATION_INTERVAL_DESIRED, LocationTracker.DEFAULT_INTERVAL_DESIRED );
Expand All @@ -122,44 +131,22 @@ public DownloadWorker( DownloadService service, String cause, Callback callback

file_type_list = file_type_parse();

service.wifi_tracker.updateSetting( force_wifi, ssid );
service.wifi_tracker.updateSetting( force_wifi, ssid ,target_type ,flashair_url);

service.location_tracker.updateSetting( location_setting );
}

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

@Override public boolean isCancelled(){
return cancel_reason.get() != null;
}

public void cancel( String reason ){
public boolean cancel( String reason ){
boolean rv = super.cancel( reason );
if( rv ) log.i( R.string.thread_cancelled, reason );
try{
if( cancel_reason.compareAndSet( null, reason ) ){
log.i( R.string.thread_cancelled, reason );
}
synchronized( this ){
notify();
}
client.cancel( log );
}catch( Throwable ex ){
ex.printStackTrace();
}
}

void waitEx( long ms ){
try{
synchronized( this ){
wait( ms );
}
}catch( Throwable ex ){
ex.printStackTrace();
}
}

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

static final Pattern reJPEG = Pattern.compile( "\\.jp(g|eg?)\\z", Pattern.CASE_INSENSITIVE );
Expand Down Expand Up @@ -706,30 +693,59 @@ public String getStatus(){
}

callback.acquireWakeLock();
Object network = null;

// 通信状態の確認
setStatus( false, service.getString( R.string.wifi_check ) );
setStatus( false, service.getString( R.string.network_check ) );
long network_check_start = SystemClock.elapsedRealtime();
Object network = null;
while( ! isCancelled() ){
network = getWiFiNetwork();
if( network != null ) break;

// 一定時間待機してもダメならスレッドを停止する
// 通信状態変化でまた起こされる
long er_now = SystemClock.elapsedRealtime();
if( er_now - network_check_start >= 60 * 1000L ){
// Pref.pref( service ).edit().putLong( Pref.LAST_IDLE_START, System.currentTimeMillis() ).apply();
job_queue = null;
cancel( service.getString( R.string.wifi_not_good ) );
break;

if( target_type == TARGET_TYPE_FLASHAIR_STA ){
while( ! isCancelled() ){
boolean tracker_last_result = service.wifi_tracker.last_result.get();
String air_url = service.wifi_tracker.last_flash_air_url.get();
if( tracker_last_result && air_url != null ){
this.flashair_url = air_url;
break;
}

// 一定時間待機してもダメならスレッドを停止する
// 通信状態変化でまた起こされる
long er_now = SystemClock.elapsedRealtime();
if( er_now - network_check_start >= 60 * 1000L ){
// Pref.pref( service ).edit().putLong( Pref.LAST_IDLE_START, System.currentTimeMillis() ).apply();
job_queue = null;
cancel( service.getString( R.string.network_not_good ) );
break;
}

// 少し待って再確認
waitEx( 10000L );
}
}else{

// 少し待って再確認
waitEx( 10000L );
while( ! isCancelled() ){
network = getWiFiNetwork();
if( network != null ) break;

// 一定時間待機してもダメならスレッドを停止する
// 通信状態変化でまた起こされる
long er_now = SystemClock.elapsedRealtime();
if( er_now - network_check_start >= 60 * 1000L ){
// Pref.pref( service ).edit().putLong( Pref.LAST_IDLE_START, System.currentTimeMillis() ).apply();
job_queue = null;
cancel( service.getString( R.string.network_not_good ) );
break;
}

// 少し待って再確認
waitEx( 10000L );
}
}
if( isCancelled() ) break;




// ファイルスキャンの開始
if( job_queue == null ){
Pref.pref( service ).edit().putLong( Pref.LAST_IDLE_START, System.currentTimeMillis() ).apply();
Expand Down
45 changes: 43 additions & 2 deletions app/src/main/java/jp/juggler/fadownloader/PageSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public class PageSetting extends PagerAdapterBase.PageViewHolder implements View
EditText etSSID;
Switch swThumbnailAutoRotate;
Switch swCopyBeforeViewSend;
Spinner spTargetType;
View btnSSIDPicker;

public PageSetting( Activity activity, View ignored ){
super( activity, ignored );
}

@Override protected void onPageCreate( int page_idx, View root ) throws Throwable{

spTargetType =(Spinner) root.findViewById( R.id.spTargetType );
etURL = (EditText) root.findViewById( R.id.etURL );
tvFolder = (TextView) root.findViewById( R.id.tvFolder );
etInterval = (EditText) root.findViewById( R.id.etInterval );
Expand All @@ -48,6 +51,7 @@ public PageSetting( Activity activity, View ignored ){
etSSID = (EditText) root.findViewById( R.id.etSSID );
swThumbnailAutoRotate = (Switch) root.findViewById( R.id.swThumbnailAutoRotate );
swCopyBeforeViewSend = (Switch) root.findViewById( R.id.swCopyBeforeViewSend );
btnSSIDPicker = root.findViewById( R.id.btnSSIDPicker );

root.findViewById( R.id.btnFolderPicker ).setOnClickListener( this );
root.findViewById( R.id.btnFolderPickerHelp ).setOnClickListener( this );
Expand All @@ -62,6 +66,7 @@ public PageSetting( Activity activity, View ignored ){
root.findViewById( R.id.btnSSIDPicker ).setOnClickListener( this );
root.findViewById( R.id.btnThumbnailAutoRotateHelp ).setOnClickListener( this );
root.findViewById( R.id.btnCopyBeforeViewSendHelp ).setOnClickListener( this );
root.findViewById( R.id.btnTargetTypeHelp ).setOnClickListener( this );

ArrayAdapter<CharSequence> location_mode_adapter = new ArrayAdapter<>(
activity
Expand All @@ -86,6 +91,29 @@ public PageSetting( Activity activity, View ignored ){
updateFormEnabled();
}
} );


ArrayAdapter<CharSequence> target_type_adapter = new ArrayAdapter<>(
activity
, android.R.layout.simple_spinner_item
);
target_type_adapter.setDropDownViewResource( R.layout.spinner_dropdown );

target_type_adapter.addAll(
activity.getString( R.string.target_type_0 ),
activity.getString( R.string.target_type_1 )
);
spTargetType.setAdapter( target_type_adapter );
spTargetType.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener(){
@Override public void onItemSelected( AdapterView<?> parent, View view, int position, long id ){
updateFormEnabled();
}

@Override public void onNothingSelected( AdapterView<?> parent ){
updateFormEnabled();
}
} );

swForceWifi.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener(){
@Override public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ){
updateFormEnabled();
Expand Down Expand Up @@ -160,6 +188,11 @@ public PageSetting( Activity activity, View ignored ){
case R.id.btnCopyBeforeViewSendHelp:
( (ActMain) activity ).openHelp( activity.getString( R.string.help_copy_before_view_send ) );
break;
case R.id.btnTargetTypeHelp:
( (ActMain) activity ).openHelp( activity.getString( R.string.help_target_type ) );
break;


}
}

Expand All @@ -169,6 +202,9 @@ void ui_value_load(){
String sv;
int iv;
//
iv = pref.getInt( Pref.UI_TARGET_TYPE, - 1 );
if( iv >= 0 && iv < spTargetType.getCount() ) spTargetType.setSelection( iv );
//
sv = pref.getString( Pref.UI_FLASHAIR_URL, null );
if( sv != null ) etURL.setText( sv );
//
Expand Down Expand Up @@ -204,13 +240,18 @@ private void updateFormEnabled(){
etLocationIntervalDesired.setEnabled( location_enabled );
etLocationIntervalMin.setEnabled( location_enabled );

boolean force_wifi_enabled = swForceWifi.isChecked();
etSSID.setEnabled( force_wifi_enabled );
boolean force_wifi_enabled = spTargetType.getSelectedItemPosition() == 0;
swForceWifi.setEnabled( force_wifi_enabled );

boolean ssid_enabled = force_wifi_enabled && swForceWifi.isChecked();
etSSID.setEnabled( ssid_enabled );
btnSSIDPicker.setEnabled( ssid_enabled );
}

// UIフォームの値を設定ファイルに保存
void ui_value_save( SharedPreferences.Editor e ){
e
.putInt( Pref.UI_TARGET_TYPE, spTargetType.getSelectedItemPosition() )
.putString( Pref.UI_FLASHAIR_URL, etURL.getText().toString() )
.putString( Pref.UI_INTERVAL, etInterval.getText().toString() )
.putString( Pref.UI_FILE_TYPE, etFileType.getText().toString() )
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/jp/juggler/fadownloader/Pref.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static SharedPreferences pref( Context context ){
// UI画面に表示されている情報の永続化
public static final String UI_REPEAT = "ui_repeat";
public static final String UI_LAST_PAGE = "ui_last_page";
public static final String UI_TARGET_TYPE = "ui_target_type";
public static final String UI_FLASHAIR_URL = "ui_flashair_url";
public static final String UI_FOLDER_URI = "ui_folder_uri";
public static final String UI_INTERVAL = "ui_interval";
Expand Down Expand Up @@ -88,6 +89,7 @@ public static void initialize( Context context ){

// 最後にWorkerを手動開始した時の設定
public static final String WORKER_REPEAT = "worker_repeat";
public static final String WORKER_TARGET_TYPE = "worker_target_type";
public static final String WORKER_FLASHAIR_URL = "worker_flashair_url";
public static final String WORKER_FOLDER_URI = "worker_folder_uri";
public static final String WORKER_INTERVAL = "worker_interval";
Expand Down
Loading

0 comments on commit 188a6ba

Please sign in to comment.