From 42574940df6c1dd621369b4bc834064869bcc720 Mon Sep 17 00:00:00 2001 From: tateisu Date: Thu, 16 Mar 2017 04:22:04 +0900 Subject: [PATCH] =?UTF-8?q?Wi-Fi=E4=BB=A5=E5=A4=96=E3=81=AE=E6=8E=A5?= =?UTF-8?q?=E7=B6=9A(LTE=E7=AD=89)=E3=81=8CActive=E3=81=AA=E6=99=82,?= =?UTF-8?q?=E3=81=AB=E3=81=9D=E3=82=8C=E3=81=8C=E3=82=8F=E3=81=8B=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E7=8A=B6=E6=B3=81=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E3=82=92=E6=94=B9=E5=96=84=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../juggler/fadownloader/DownloadService.java | 20 +- .../juggler/fadownloader/LocationTracker.java | 39 +++- .../java/jp/juggler/fadownloader/Utils.java | 19 +- .../jp/juggler/fadownloader/WifiTracker.java | 186 ++++++++++++++---- app/src/main/res/layout/act_main.xml | 2 +- app/src/main/res/values-ja/strings.xml | 7 +- app/src/main/res/values/strings.xml | 7 +- 7 files changed, 213 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/jp/juggler/fadownloader/DownloadService.java b/app/src/main/java/jp/juggler/fadownloader/DownloadService.java index ef59a63..b9cfb15 100644 --- a/app/src/main/java/jp/juggler/fadownloader/DownloadService.java +++ b/app/src/main/java/jp/juggler/fadownloader/DownloadService.java @@ -81,7 +81,7 @@ public class DownloadService extends Service{ .build(); mGoogleApiClient.connect(); - location_tracker = new LocationTracker( log, mGoogleApiClient, new LocationTracker.Callback(){ + location_tracker = new LocationTracker( this,log, mGoogleApiClient, new LocationTracker.Callback(){ @Override public void onLocationChanged( Location location ){ DownloadService.location = location; } @@ -286,11 +286,19 @@ public static String getStatusForActivity( Context context ){ } StringBuilder sb = new StringBuilder(); - sb.append( context.getString( R.string.service_running_status - , service_instance.wake_lock.isHeld() ? "ON" : "OFF" - , service_instance.wifi_lock.isHeld() ? "ON" : "OFF" - - ) ); + sb.append( context.getString( R.string.service_running )); + sb.append( "WakeLock=" ) + .append( service_instance.wake_lock.isHeld() ? "ON" : "OFF" ) + .append( ", " ) + .append( "WiFiLock=" ) + .append( service_instance.wifi_lock.isHeld() ? "ON" : "OFF" ) + .append( ", " ) + .append( "Location=" ) + .append( service_instance.location_tracker.getStatus() ) + .append( ", " ) + .append( "Network=" ); + service_instance.wifi_tracker.getStatus(sb); + sb.append('\n'); if( service_instance.worker == null || ! service_instance.worker.isAlive() ){ sb.append( context.getString( R.string.thread_not_running_status ) ); diff --git a/app/src/main/java/jp/juggler/fadownloader/LocationTracker.java b/app/src/main/java/jp/juggler/fadownloader/LocationTracker.java index 253604b..926a7f8 100644 --- a/app/src/main/java/jp/juggler/fadownloader/LocationTracker.java +++ b/app/src/main/java/jp/juggler/fadownloader/LocationTracker.java @@ -1,5 +1,6 @@ package jp.juggler.fadownloader; +import android.content.Context; import android.location.Location; import android.support.annotation.NonNull; @@ -60,10 +61,12 @@ int getUpdatePriority(){ } } - interface Callback { - void onLocationChanged(Location location); + interface Callback{ + + void onLocationChanged( Location location ); } + final Context context; final Callback callback; final LogWriter log; final GoogleApiClient mGoogleApiClient; @@ -71,7 +74,8 @@ interface Callback { Setting location_setting; boolean is_disposed = false; - public LocationTracker( LogWriter log, GoogleApiClient client ,Callback callback){ + public LocationTracker( Context context, LogWriter log, GoogleApiClient client, Callback callback ){ + this.context = context; this.log = log; this.mGoogleApiClient = client; this.callback = callback; @@ -100,6 +104,25 @@ public synchronized void updateSetting( Setting setting ){ private boolean isTracked = false; + public String getStatus(){ + if( ! isTracked ) return "OFF"; + switch( location_setting.mode ){ + default: + return "?"; + case NO_LOCATION_UPDATE: + return context.getString( R.string.location_mode_0 ); + case LOCATION_NO_POWER: + return context.getString( R.string.location_mode_1 ); + case LOCATION_LOW_POWER: + return context.getString( R.string.location_mode_2 ); + case LOCATION_BALANCED: + return context.getString( R.string.location_mode_3 ); + case LOCATION_HIGH_ACCURACY: + return context.getString( R.string.location_mode_4 ); + + } + } + private void tracking_end(){ if( ! isTracked ) return; @@ -123,7 +146,7 @@ private void tracking_end(){ } }catch( Throwable ex ){ ex.printStackTrace(); - log.e( ex, "removeLocationUpdates() failed."); + log.e( ex, "removeLocationUpdates() failed." ); }finally{ isTracked = false; } @@ -131,7 +154,7 @@ private void tracking_end(){ private void tracking_start(){ - if( location_setting ==null || !location_setting.isUpdateRequired()){ + if( location_setting == null || ! location_setting.isUpdateRequired() ){ return; } @@ -139,7 +162,7 @@ private void tracking_start(){ log.d( "tracking_start: tracker is already disposed." ); return; } - + if( ! mGoogleApiClient.isConnected() ){ log.d( "tracking_start: api not connected." ); return; @@ -180,10 +203,10 @@ private void tracking_start(){ } } ); }catch( SecurityException ex ){ - log.e( ex,"requestLocationUpdates() failed." ); + log.e( ex, "requestLocationUpdates() failed." ); }catch( Throwable ex ){ ex.printStackTrace(); - log.e( ex,"requestLocationUpdates() failed." ); + log.e( ex, "requestLocationUpdates() failed." ); } } } diff --git a/app/src/main/java/jp/juggler/fadownloader/Utils.java b/app/src/main/java/jp/juggler/fadownloader/Utils.java index f7eaf6a..55b327c 100644 --- a/app/src/main/java/jp/juggler/fadownloader/Utils.java +++ b/app/src/main/java/jp/juggler/fadownloader/Utils.java @@ -489,7 +489,7 @@ static class FileInfo{ } } - static @NonNull Map getSecondaryStorageVolumesMap( Context context){ + static @NonNull Map getSecondaryStorageVolumesMap( Context context ){ Map result = new HashMap<>(); try{ @@ -506,7 +506,7 @@ static class FileInfo{ Method getVolumeList = sm.getClass().getMethod( "getVolumeList" ); Object[] volumes = (Object[]) getVolumeList.invoke( sm ); - // + // for( Object volume : volumes ){ Class volume_clazz = volume.getClass(); @@ -521,10 +521,21 @@ static class FileInfo{ if( ! TextUtils.isEmpty( uuid ) ) result.put( uuid, path ); } } - }catch(Throwable ex){ - ex.printStackTrace( ); + }catch( Throwable ex ){ + ex.printStackTrace(); } return result; } + + public static String toCamelCase( String src ){ + StringBuilder sb = new StringBuilder(); + for( String s : src.split( "_" ) ){ + if( TextUtils.isEmpty( s ) ) continue; + sb.append( Character.toUpperCase( s.charAt( 0 ) ) ); + sb.append( s.substring( 1, s.length() ).toLowerCase() ); + } + return sb.toString(); + } + } diff --git a/app/src/main/java/jp/juggler/fadownloader/WifiTracker.java b/app/src/main/java/jp/juggler/fadownloader/WifiTracker.java index a3a2f25..a0273b7 100644 --- a/app/src/main/java/jp/juggler/fadownloader/WifiTracker.java +++ b/app/src/main/java/jp/juggler/fadownloader/WifiTracker.java @@ -5,21 +5,29 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkInfo; import android.net.wifi.ScanResult; import android.net.wifi.SupplicantState; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; +import android.os.Build; import android.os.Handler; import android.os.SystemClock; +import android.text.TextUtils; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; +import java.util.List; public class WifiTracker{ interface Callback{ - void onConnectionEvent( boolean is_connected ,String cause); + void onConnectionEvent( boolean is_connected, String cause ); } final LogWriter log; @@ -28,6 +36,7 @@ interface Callback{ final Handler handler; final WifiManager wifiManager; + final ConnectivityManager cm; public WifiTracker( Context context, LogWriter log, Callback callback ){ this.log = log; @@ -35,6 +44,7 @@ public WifiTracker( Context context, LogWriter log, Callback callback ){ this.callback = callback; this.handler = new Handler(); this.wifiManager = (WifiManager) context.getApplicationContext().getSystemService( Context.WIFI_SERVICE ); + this.cm = (ConnectivityManager) context.getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE ); context.registerReceiver( receiver, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION ) ); context.registerReceiver( receiver, new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION ) ); @@ -54,13 +64,13 @@ public void dispose(){ public void updateSetting( boolean force_wifi, String ssid ){ this.force_wifi = force_wifi; this.target_ssid = ssid; - handler.post( proc_interval); + handler.post( proc_interval ); } static final int SCAN_INTERVAL = 100000; long last_scan_start; final LinkedList priority_list = new LinkedList<>(); - boolean last_status =false; + boolean last_status = false; final Runnable proc_interval = new Runnable(){ @Override public void run(){ handler.removeCallbacks( proc_interval ); @@ -68,8 +78,8 @@ public void updateSetting( boolean force_wifi, String ssid ){ long next = 3000L; try{ boolean b = keep_ap(); - if(b != last_status){ - callback.onConnectionEvent( true, "Wi-Fi tracker"); + if( b != last_status ){ + callback.onConnectionEvent( true, "Wi-Fi tracker" ); } last_status = b; next = b ? 30000L : 3000L; @@ -84,7 +94,7 @@ public void updateSetting( boolean force_wifi, String ssid ){ @Override public void onReceive( Context context, Intent intent ){ try{ boolean b = keep_ap(); - if(b != last_status){ + if( b != last_status ){ callback.onConnectionEvent( keep_ap(), intent.getAction() ); } last_status = b; @@ -94,6 +104,43 @@ public void updateSetting( boolean force_wifi, String ssid ){ } }; + static class NetworkStatus{ + + boolean is_active; + String type_name; + String sub_name; + String strWifiStatus; + + } + + private String buildCurrentStatus( ArrayList ns_list ){ + Collections.sort( ns_list, new Comparator(){ + @Override public int compare( NetworkStatus a, NetworkStatus b ){ + if( a.is_active && ! b.is_active ) return - 1; + if( ! a.is_active && b.is_active ) return 1; + return a.type_name.compareTo( b.type_name ); + } + } ); + StringBuilder sb = new StringBuilder(); + for( NetworkStatus ns : ns_list ){ + if( sb.length() > 0 ) sb.append( " / " ); + if( ns.is_active ) sb.append("(Active)"); + if( ns.strWifiStatus != null ){ + sb.append( "Wi-Fi(" ).append( ns.strWifiStatus ).append(')'); + }else{ + sb.append( ns.type_name ); + if( ! TextUtils.isEmpty( ns.sub_name ) ){ + sb.append( '(' ).append( ns.sub_name ).append( ')' ); + } + } + } + return sb.toString(); + } + + public void getStatus( StringBuilder sb ){ + sb.append( last_current_status ); + } + String last_current_status; String last_force_status; String last_error_status; @@ -102,16 +149,64 @@ public void updateSetting( boolean force_wifi, String ssid ){ @SuppressWarnings( "ConstantConditions" ) boolean keep_ap(){ if( is_dispose ) return false; - String current_status = null; + ArrayList ns_list = new ArrayList<>(); String force_status = null; String error_status = null; try{ + NetworkStatus wifi_status = null; + boolean other_active = false; + String active_name = null; + if( Build.VERSION.SDK_INT >= 23 ){ + Network n = cm.getActiveNetwork(); + if( n != null ){ + NetworkInfo ni = cm.getNetworkInfo( n ); + if( ni != null ){ + active_name = ni.getTypeName(); + } + } + }else{ + NetworkInfo ni = cm.getActiveNetworkInfo(); + if( ni != null ){ + active_name = ni.getTypeName(); + } + } + + NetworkInfo[] ni_list; + if( Build.VERSION.SDK_INT >= 21 ){ + Network[] src_list = cm.getAllNetworks(); + ni_list = new NetworkInfo[ src_list == null ? 0 : src_list.length ]; + for( int i = 0, ie = ni_list.length ; i < ie ; ++ i ){ + ni_list[ i ] = cm.getNetworkInfo( src_list[ i ] ); + } + }else{ + ni_list = cm.getAllNetworkInfo(); + } + for( NetworkInfo ni : ni_list ){ + boolean is_wifi = ( ni.getType() == ConnectivityManager.TYPE_WIFI ); + if( ! is_wifi && ! ni.isConnected() ) continue; + NetworkStatus ns = new NetworkStatus(); + ns_list.add( ns ); + if( is_wifi ) wifi_status = ns; + ns.type_name = ni.getTypeName(); + ns.sub_name = ni.getSubtypeName(); + + if( active_name != null && active_name.equals( ns.type_name ) ){ + ns.is_active = true; + if( !is_wifi) other_active = true; + } + } + + if( wifi_status == null ){ + wifi_status = new NetworkStatus(); + ns_list.add( wifi_status ); + wifi_status.type_name = "WIFI"; + } // Wi-Fiが無効なら有効にする try{ - current_status = context.getString( R.string.wifi_status_unknown ); + wifi_status.strWifiStatus = "?"; if( ! wifiManager.isWifiEnabled() ){ - current_status = context.getString( R.string.wifi_not_enabled ); + wifi_status.strWifiStatus = context.getString( R.string.not_enabled ); if( force_wifi ) wifiManager.setWifiEnabled( true ); return false; } @@ -143,42 +238,47 @@ public void updateSetting( boolean force_wifi, String ssid ){ WifiConfiguration target_config = null; int priority_max = 0; try{ - current_status = context.getString( R.string.wifi_no_ap_associated ); + wifi_status.strWifiStatus = context.getString( R.string.no_ap_associated ); - for( WifiConfiguration wc : wifiManager.getConfiguredNetworks() ){ - String ssid = wc.SSID.replace( "\"", "" ); + List wc_list = wifiManager.getConfiguredNetworks(); + if( wc_list == null){ + // getConfiguredNetworks() はたまにnullを返す + return false; + }else{ + for( WifiConfiguration wc : wc_list ){ + String ssid = wc.SSID.replace( "\"", "" ); - if( wc.priority > priority_max ){ - priority_max = wc.priority; - } + if( wc.priority > priority_max ){ + priority_max = wc.priority; + } - // 目的のAPを覚えておく - if( target_ssid != null && target_ssid.equals( ssid ) ){ - target_config = wc; - } + // 目的のAPを覚えておく + if( target_ssid != null && target_ssid.equals( ssid ) ){ + target_config = wc; + } - // 接続中のAPの情報 - if( ssid.equals( current_ssid ) ){ - current_network_id = wc.networkId; - current_status = context.getString( R.string.wifi_current_connection - , ssid - , Integer.toString( wc.networkId ) - , Integer.toString( wc.priority ) - , Utils.getSupplicantStateString( current_supp_state ) - ); - if( ! force_wifi ){ - // AP強制ではないなら、何かアクティブな接続が生きていればOK - return current_supp_state == SupplicantState.COMPLETED; + // 接続中のAPの情報 + if( ssid.equals( current_ssid ) ){ + current_network_id = wc.networkId; + // + String strState = ( current_supp_state == null ? "?" : current_supp_state.toString() ); + strState = Utils.toCamelCase( strState ); + if( "Completed".equals( strState ) ) strState = "Connected"; + wifi_status.strWifiStatus = ssid + "," + strState; + if( ! force_wifi ){ + // AP強制ではないなら、何かアクティブな接続が生きていればOK + return current_supp_state == SupplicantState.COMPLETED; + } } } - } - if( ! force_wifi ){ - // AP強制ではない場合、接続中のAPがなければNGを返す - return false; - }else if( target_config == null ){ - force_status = context.getString( R.string.wifi_target_ssid_not_found, target_ssid ); - return false; + if( ! force_wifi ){ + // AP強制ではない場合、接続中のAPがなければNGを返す + return false; + }else if( target_config == null ){ + force_status = context.getString( R.string.wifi_target_ssid_not_found, target_ssid ); + return false; + } } }catch( Throwable ex ){ @@ -212,8 +312,8 @@ public void updateSetting( boolean force_wifi, String ssid ){ if( current_ssid != null && current_network_id == target_config.networkId ){ switch( current_supp_state ){ case COMPLETED: - // その接続の認証が終わっていたらOK - return true; + // その接続の認証が終わっていて、他の種類の接続がActiveでなければOK + return ! other_active; case ASSOCIATING: case ASSOCIATED: case AUTHENTICATING: @@ -292,9 +392,10 @@ public void updateSetting( boolean force_wifi, String ssid ){ return false; }finally{ - + String current_status = buildCurrentStatus( ns_list ); if( current_status != null && ! current_status.equals( last_current_status ) ){ - log.d( last_current_status = current_status ); + last_current_status = current_status; + log.d( context.getString( R.string.network_status, current_status ) ); } if( error_status != null && ! error_status.equals( last_error_status ) ){ @@ -306,4 +407,5 @@ public void updateSetting( boolean force_wifi, String ssid ){ } } } + } diff --git a/app/src/main/res/layout/act_main.xml b/app/src/main/res/layout/act_main.xml index 9e4a0fa..00c26f9 100644 --- a/app/src/main/res/layout/act_main.xml +++ b/app/src/main/res/layout/act_main.xml @@ -73,7 +73,7 @@ android:paddingTop="4dp" android:paddingBottom="4dp" android:textColor="#FFF" - android:minLines="3" + android:minLines="4" /> 未対応のインテント受信. action=%1$s スレッド実行中 サービス停止中。REPEATまたはONCEボタンを押すと開始します - サービス起動中。WakeLock=%1$s, WiFiLock=%2$s\n + サービス起動中。 スレッド停止中。WiFi通信状態の変化や時間経過で開始するかも スレッド作成:パラメータ指定あり スレッド再作成. event=%1$s @@ -53,7 +53,7 @@ 短い待機 あと%1$s アラーム待機 あと%1$s Wi-Fi確認中 - Wi-Fi APが選択されていません + AP未選択 FlashAir更新チェック 接続先ホスト名を解決できません。もしかして:端末のWi-FiからFlashAirのAPに接続していない FlashAirホストが見つからない @@ -100,7 +100,7 @@ SSIDを指定してください Wi-Fi APのSSID WI-Fi AP:SSID=%1$s,id=%2$s,priority=%3$s,state=%4$s - 現在 Wi-Fiが有効ではありません + 現在 Wi-Fiが有効ではありません Wi-Fiスキャンを開始しました Wi-Fiの状態が不明です Wi-Fi AP %1$s への接続設定がありません @@ -139,4 +139,5 @@ ダウンロード履歴に表示されるサムネイルを、Exifの回転情報を使って自動回転します。デフォルトはONです。 サムネイル サムネイルの自動回転 + 通信状態: %1$s diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 75cfc50..66a1a20 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,7 +43,7 @@ unsupported intent received. action=%1$s thread running service not running. press REPEAT or ONCE to start. - service running. WakeLock=%1$s, WiFiLock=%2$s + service running. thread not running. maybe start when Wi-Fi state changed, or alarm resumed. thread created with parameter. thread restart. event=%1$s @@ -56,7 +56,7 @@ wait short time. remain=%1$s wait for alarm. remain=%1$s Wi-Fi check - Wi-Fi AP not associated. + AP not associated. FlashAir update check could not resolve remote host. perhaps:device\'s Wi-Fi is not connected to FlashAir AP missing FlashAir host. @@ -106,7 +106,7 @@ Wi-Fi強制機能をONにしてFlashAir APのSSIDを入力すると、Wi-Fi接続がより安定します。\n・Wi-FiがOFFになっていたらONにします\n・指定したSSIDの優先度を上げます\n・指定したSSIDと現在の接続先が異なる場合、Wi-Fi APのスキャンを定期的に行います\n・指定したSSIDがスキャン結果に含まれる場合、そのWi-Fi AP に接続先を切り替えます\n SSIDにはFlashAir Wi-Fi APのSSIDを指定してください\n SSID is empty. - 現在 Wi-Fiが有効ではありません + 無効 wifi status unknown WI-Fi AP:SSID=%1$s,id=%2$s,priority=%3$s,state=%4$s Wi-Fi AP %1$s is not configured. @@ -146,5 +146,6 @@ copy before view/send ダウンロード履歴に表示されるサムネイルを、Exifの回転情報を使って自動回転します。デフォルトはONです。 ダウンロード履歴から「見る/送る」で外部アプリを起動する際、Android OS バージョンによっては SDカードなどの non-primari storage へのアクセスが制限されるため、外部アプリがそのファイルを扱えないことがあります。\nこの設定をONにすると、外部アプリを開く際に、ファイルを端末のダウンロードフォルダにコピーして、コピー後のファイルURIを外部アプリに渡すようにします。\nデフォルトはOFFです。 + network status: %1$s