Skip to content

Commit

Permalink
1.3: Wi-FiAP強制機能を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
tateisu committed Mar 6, 2017
1 parent 848f0bb commit 8302068
Show file tree
Hide file tree
Showing 23 changed files with 486 additions and 256 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "jp.juggler.fadownloader"
minSdkVersion 21
targetSdkVersion 25
versionCode 4
versionName "1.2-beta"
versionCode 6
versionName "1.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

<application
android:name="jp.juggler.fadownloader.App1"
Expand Down
71 changes: 70 additions & 1 deletion app/src/main/java/jp/juggler/fadownloader/ActMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,20 @@ void startDownloadService(){
}
}

boolean force_wifi = pref.getBoolean( Pref.UI_FORCE_WIFI, false );

String ssid;
if( !force_wifi){
ssid = "";
}else{
sv = pref.getString( Pref.UI_SSID, "" );
ssid = sv.trim();
if( TextUtils.isEmpty( ssid ) ){
Toast.makeText( this, getString( R.string.ssid_empty ), Toast.LENGTH_SHORT ).show();
return;
}
}

// 最後に押したボタンを覚えておく
pref.edit()
.putInt( Pref.LAST_MODE, repeat ? Pref.LAST_MODE_REPEAT : Pref.LAST_MODE_ONCE )
Expand All @@ -478,6 +492,8 @@ void startDownloadService(){
intent.putExtra( DownloadService.EXTRA_LOCATION_INTERVAL_DESIRED, location_update_interval_desired );
intent.putExtra( DownloadService.EXTRA_LOCATION_INTERVAL_MIN, location_update_interval_min );
intent.putExtra( DownloadService.EXTRA_LOCATION_MODE, location_mode );
intent.putExtra( DownloadService.EXTRA_FORCE_WIFI, force_wifi );
intent.putExtra( DownloadService.EXTRA_SSID, ssid );

startService( intent );
}
Expand Down Expand Up @@ -505,13 +521,66 @@ void openHelp( int layout_id ){
}
} );
}
void openHelp( String text ){
View v = getLayoutInflater().inflate( R.layout.help_single_text, null, false );
((TextView)v.findViewById( R.id.text )).setText(text);

final Dialog d = new Dialog( this );
d.requestWindowFeature( Window.FEATURE_NO_TITLE );
d.setContentView( v );
d.getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
d.show();
v.findViewById( R.id.btnClose ).setOnClickListener( new View.OnClickListener(){
@Override public void onClick( View view ){
d.dismiss();
}
} );
}
GoogleApiClient mGoogleApiClient;

final GoogleApiClient.OnConnectionFailedListener connection_fail_callback = new GoogleApiClient.OnConnectionFailedListener(){
@Override public void onConnectionFailed( @NonNull ConnectionResult connectionResult ){
String msg = getString( R.string.play_service_connection_failed, connectionResult.getErrorCode(), connectionResult.getErrorMessage() );
int code = connectionResult.getErrorCode();
String msg = connectionResult.getErrorMessage();
if( TextUtils.isEmpty( msg )){
switch(code){
case ConnectionResult.SUCCESS: msg="SUCCESS";break;
case ConnectionResult.SERVICE_MISSING: msg="SERVICE_MISSING";break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: msg="SERVICE_VERSION_UPDATE_REQUIRED";break;
case ConnectionResult.SERVICE_DISABLED: msg="SERVICE_DISABLED";break;
case ConnectionResult.SIGN_IN_REQUIRED: msg="SIGN_IN_REQUIRED";break;
case ConnectionResult.INVALID_ACCOUNT: msg="INVALID_ACCOUNT";break;
case ConnectionResult.RESOLUTION_REQUIRED: msg="RESOLUTION_REQUIRED";break;
case ConnectionResult.NETWORK_ERROR: msg="NETWORK_ERROR";break;
case ConnectionResult.INTERNAL_ERROR: msg="INTERNAL_ERROR";break;
case ConnectionResult.SERVICE_INVALID: msg="SERVICE_INVALID";break;
case ConnectionResult.DEVELOPER_ERROR: msg="DEVELOPER_ERROR";break;
case ConnectionResult.LICENSE_CHECK_FAILED: msg="LICENSE_CHECK_FAILED";break;
case ConnectionResult.CANCELED: msg="CANCELED";break;
case ConnectionResult.TIMEOUT: msg="TIMEOUT";break;
case ConnectionResult.INTERRUPTED: msg="INTERRUPTED";break;
case ConnectionResult.API_UNAVAILABLE: msg="API_UNAVAILABLE";break;
case ConnectionResult.SIGN_IN_FAILED: msg="SIGN_IN_FAILED";break;
case ConnectionResult.SERVICE_UPDATING: msg="SERVICE_UPDATING";break;
case ConnectionResult.SERVICE_MISSING_PERMISSION: msg="SERVICE_MISSING_PERMISSION";break;
case ConnectionResult.RESTRICTED_PROFILE: msg="RESTRICTED_PROFILE";break;

}
}

msg = getString( R.string.play_service_connection_failed,code, msg);
Toast.makeText( ActMain.this, msg, Toast.LENGTH_SHORT ).show();

if( code == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ){
try{
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setData( Uri.parse( "market://details?id=com.google.android.gms" ) );
startActivity( intent );
}catch(Throwable ex){
ex.printStackTrace( );
}

}
}
};
final GoogleApiClient.ConnectionCallbacks connection_callback = new GoogleApiClient.ConnectionCallbacks(){
Expand Down
45 changes: 17 additions & 28 deletions app/src/main/java/jp/juggler/fadownloader/DownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -39,32 +35,13 @@ public class DownloadService extends Service{
static final String EXTRA_LOCATION_INTERVAL_DESIRED = "location_interval_desired";
static final String EXTRA_LOCATION_INTERVAL_MIN = "location_interval_min";
static final String EXTRA_LOCATION_MODE = "location_mode";
static final String EXTRA_FORCE_WIFI = "force_wifi";
static final String EXTRA_SSID = "ssid";

static final int NOTIFICATION_ID_SERVICE = 1;

LogWriter log;

final BroadcastReceiver receiver = new BroadcastReceiver(){
@Override public void onReceive( Context context, Intent intent ){
try{
String action = intent.getAction();
if( ConnectivityManager.CONNECTIVITY_ACTION.equals( action ) ){
Network n = Utils.getWiFiNetwork( context );
if( n != null ){
log.v( getString(R.string.wifi_event_connected) );
int last_mode = Pref.pref( context ).getInt( Pref.LAST_MODE, Pref.LAST_MODE_STOP );
if( last_mode != Pref.LAST_MODE_STOP ){
worker_wakeup();
}
}else{
log.v( getString(R.string.wifi_event_disconnected) );
}
}
}catch( Throwable ex ){
ex.printStackTrace();
}
}
};

boolean is_alive;
boolean allow_cancel_alarm;
Expand All @@ -74,6 +51,7 @@ public class DownloadService extends Service{
Handler handler;

LocationTracker location_tracker;
WifiTracker wifi_tracker;

@Override public void onCreate(){
super.onCreate();
Expand All @@ -95,7 +73,6 @@ public class DownloadService extends Service{
wifi_lock = wm.createWifiLock( WifiManager.WIFI_MODE_FULL, getPackageName() );
wifi_lock.setReferenceCounted( false );

registerReceiver( receiver, new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION ) );

setServiceNotification(getString(R.string.service_idle));

Expand All @@ -108,13 +85,25 @@ public class DownloadService extends Service{
mGoogleApiClient.connect();

location_tracker = new LocationTracker(log,mGoogleApiClient);

wifi_tracker = new WifiTracker( this, log, new WifiTracker.Callback(){
@Override public void onConnectionEvent(boolean is_connected){
if( is_connected ){
int last_mode = Pref.pref( DownloadService.this ).getInt( Pref.LAST_MODE, Pref.LAST_MODE_STOP );
if( last_mode != Pref.LAST_MODE_STOP ){
worker_wakeup();
}
}
}
} );
}

@Override public void onDestroy(){

is_alive = false;

location_tracker.dispose();
wifi_tracker.dispose();

if( mGoogleApiClient.isConnected() ){
mGoogleApiClient.disconnect();
Expand All @@ -140,7 +129,7 @@ public class DownloadService extends Service{
wifi_lock.release();
wifi_lock = null;

unregisterReceiver( receiver );


stopForeground( true );

Expand Down Expand Up @@ -262,7 +251,7 @@ void worker_wakeup(){
}
}

@Override public void onThreadStart(final LocationTracker.Setting location_setting){
@Override public void onThreadStart(){
setServiceNotification(getString(R.string.thread_running ));
}

Expand Down
28 changes: 22 additions & 6 deletions app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface Callback{

void acquireWakeLock();

void onThreadStart(LocationTracker.Setting location_setting);
void onThreadStart();

void onThreadEnd( boolean allow_stop_service );

Expand All @@ -49,7 +49,6 @@ public interface Callback{
final String file_type;
final LogWriter log;
final ArrayList<Pattern> file_type_list;
final LocationTracker.Setting location_setting;

public DownloadWorker( DownloadService service, Intent intent, Callback callback ){
this.service = service;
Expand All @@ -62,8 +61,10 @@ public DownloadWorker( DownloadService service, Intent intent, Callback callback
this.folder_uri = intent.getStringExtra( DownloadService.EXTRA_FOLDER_URI );
this.interval = intent.getIntExtra( DownloadService.EXTRA_INTERVAL, 86400 );
this.file_type = intent.getStringExtra( DownloadService.EXTRA_FILE_TYPE );
boolean force_wifi = intent.getBooleanExtra( DownloadService.EXTRA_FORCE_WIFI ,false);
String ssid =intent.getStringExtra( DownloadService.EXTRA_SSID );

location_setting = new LocationTracker.Setting();
LocationTracker.Setting location_setting = new LocationTracker.Setting();
location_setting.interval_desired = intent.getLongExtra( DownloadService.EXTRA_LOCATION_INTERVAL_DESIRED ,LocationTracker.DEFAULT_INTERVAL_DESIRED);
location_setting.interval_min = intent.getLongExtra( DownloadService.EXTRA_LOCATION_INTERVAL_MIN ,LocationTracker.DEFAULT_INTERVAL_MIN);
location_setting.mode = intent.getIntExtra( DownloadService.EXTRA_LOCATION_MODE ,LocationTracker.DEFAULT_MODE);
Expand All @@ -77,10 +78,14 @@ public DownloadWorker( DownloadService service, Intent intent, Callback callback
.putLong( Pref.WORKER_LOCATION_INTERVAL_DESIRED, location_setting.interval_desired )
.putLong( Pref.WORKER_LOCATION_INTERVAL_MIN, location_setting.interval_min )
.putInt( Pref.WORKER_LOCATION_MODE, location_setting.mode )
.putBoolean( Pref.WORKER_FORCE_WIFI, force_wifi )
.putString( Pref.WORKER_SSID, ssid )
.apply();

file_type_list = file_type_parse();

service.wifi_tracker.updateSetting(force_wifi,ssid);

service.location_tracker.updateSetting(location_setting);
}

Expand All @@ -97,15 +102,19 @@ public DownloadWorker( DownloadService service, Callback callback ){
this.interval = pref.getInt( Pref.WORKER_INTERVAL, 86400 );
this.file_type = pref.getString( Pref.WORKER_FILE_TYPE, null );

location_setting = new LocationTracker.Setting();
boolean force_wifi = pref.getBoolean( Pref.WORKER_FORCE_WIFI, false );
String ssid =pref.getString( Pref.WORKER_SSID, null );

LocationTracker.Setting location_setting = new LocationTracker.Setting();
location_setting.interval_desired = pref.getLong(Pref.WORKER_LOCATION_INTERVAL_DESIRED ,LocationTracker.DEFAULT_INTERVAL_DESIRED);
location_setting.interval_min = pref.getLong(Pref.WORKER_LOCATION_INTERVAL_MIN,LocationTracker.DEFAULT_INTERVAL_MIN);
location_setting.mode = pref.getInt(Pref.WORKER_LOCATION_MODE ,LocationTracker.DEFAULT_MODE);

file_type_list = file_type_parse();

service.location_tracker.updateSetting(location_setting);
service.wifi_tracker.updateSetting(force_wifi,ssid);

service.location_tracker.updateSetting(location_setting);
}

final AtomicReference<String> status = new AtomicReference<>( "?" );
Expand Down Expand Up @@ -279,7 +288,7 @@ private static DocumentFile bsearch( ArrayList<DocumentFile> local_files, String
boolean allow_stop_service = false;


callback.onThreadStart(location_setting);
callback.onThreadStart();

while( ! isCancelled() ){
status.set( service.getString( R.string.initializing ) );
Expand Down Expand Up @@ -546,6 +555,13 @@ public byte[] onHTTPClientStream( LogWriter log, CancelChecker cancel_checker, I
// no log.
}else if( data == null ){
log.e( "FILE %s :HTTP error %s", fname, client.last_error );

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();
}

has_error = true;
}else{
log.i( "FILE %s :download complete. %dms", fname, SystemClock.elapsedRealtime() - time_start );
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/jp/juggler/fadownloader/LogWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,17 @@ public void f(int string_id,Object... args){
LogData.insert( cr, cv, System.currentTimeMillis(), LogData.LEVEL_FLOOD, fmt );
}
}

@SuppressWarnings( "unused" )
public void e(Throwable ex,String fmt,Object... args){
if( args.length > 0) fmt = String.format( fmt,args);
synchronized(cv){
LogData.insert( cr, cv, System.currentTimeMillis(), LogData.LEVEL_ERROR, fmt+String.format(":%s %s",ex.getClass().getSimpleName(),ex.getMessage()) );
}
}
@SuppressWarnings( "unused" )
public String formatError(Throwable ex,String fmt,Object... args){
if( args.length > 0) fmt = String.format( fmt,args);
return fmt+String.format(":%s %s",ex.getClass().getSimpleName(),ex.getMessage());
}
}
Loading

0 comments on commit 8302068

Please sign in to comment.