From f2b9abc50c5c4f2e9d93c742d4c76f8b2af844bb Mon Sep 17 00:00:00 2001 From: tateisu Date: Sat, 8 Sep 2018 05:47:51 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=80=E3=82=A6=E3=83=B3=E3=83=AD=E3=83=BC?= =?UTF-8?q?=E3=83=89=E5=B1=A5=E6=AD=B4=E3=81=AB=E5=90=8D=E5=89=8D=E3=81=8C?= =?UTF-8?q?=E3=81=82=E3=82=8B=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AF?= =?UTF-8?q?=E3=82=B9=E3=82=AD=E3=83=83=E3=83=97=E3=81=99=E3=82=8B=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/jp/juggler/fadownloader/ActMain.java | 10 +- .../jp/juggler/fadownloader/DataProvider.java | 2 +- .../juggler/fadownloader/DownloadRecord.java | 14 +++ .../juggler/fadownloader/DownloadService.java | 1 + .../juggler/fadownloader/DownloadWorker.java | 91 ++++++++++++++----- .../jp/juggler/fadownloader/PageSetting.java | 38 +++++--- .../java/jp/juggler/fadownloader/Pref.java | 2 + .../fadownloader/targets/FlashAir.java | 3 +- .../fadownloader/targets/PentaxKP.java | 7 +- .../fadownloader/targets/PqiAirCard.java | 2 +- app/src/main/res/layout/page_setting.xml | 21 +++++ app/src/main/res/values-ja/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 13 files changed, 147 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/jp/juggler/fadownloader/ActMain.java b/app/src/main/java/jp/juggler/fadownloader/ActMain.java index 3cf2ec8..82d38c1 100644 --- a/app/src/main/java/jp/juggler/fadownloader/ActMain.java +++ b/app/src/main/java/jp/juggler/fadownloader/ActMain.java @@ -300,7 +300,7 @@ protected void onCreate( Bundle savedInstanceState ){ setupIabHelper(); - mAdView = (AdView) findViewById( R.id.adView ); + mAdView = findViewById( R.id.adView ); if( BuildVariant.AD_FREE ){ ( (ViewGroup) mAdView.getParent() ).removeView( mAdView ); mAdView = null; @@ -317,9 +317,9 @@ protected void onCreate( Bundle savedInstanceState ){ findViewById( R.id.btnRepeat ).setOnClickListener( this ); findViewById( R.id.btnModeHelp ).setOnClickListener( this ); - tvStatus = (TextView) findViewById( R.id.tvStatus ); + tvStatus = findViewById( R.id.tvStatus ); - pager = (ViewPager) findViewById( R.id.pager ); + pager = findViewById( R.id.pager ); pager_adapter = new PagerAdapterBase( this ); page_idx_setting = pager_adapter.addPage( getString( R.string.setting ), R.layout.page_setting, PageSetting.class ); @@ -638,6 +638,7 @@ void startDownloadService(){ } boolean protected_only = pref.getBoolean( Pref.UI_PROTECTED_ONLY, false ); + boolean skip_already_download = pref.getBoolean( Pref.UI_SKIP_ALREADY_DOWNLOAD, false ); // 最後に押したボタンを覚えておく pref.edit() @@ -662,7 +663,8 @@ void startDownloadService(){ intent.putExtra( DownloadService.EXTRA_FORCE_WIFI, force_wifi ); intent.putExtra( DownloadService.EXTRA_SSID, ssid ); intent.putExtra( DownloadService.EXTRA_PROTECTED_ONLY, protected_only ); - + intent.putExtra( DownloadService.EXTRA_SKIP_ALREADY_DOWNLOAD, skip_already_download ); + startService( intent ); } diff --git a/app/src/main/java/jp/juggler/fadownloader/DataProvider.java b/app/src/main/java/jp/juggler/fadownloader/DataProvider.java index ce2d24b..b41b6ae 100644 --- a/app/src/main/java/jp/juggler/fadownloader/DataProvider.java +++ b/app/src/main/java/jp/juggler/fadownloader/DataProvider.java @@ -17,7 +17,7 @@ public class DataProvider extends ContentProvider{ static final String AUTHORITY = BuildVariant.DATA_PROVIDER_AUTHORITY; static final String DB_NAME = "data"; - static final int DB_SCHEMA_VERSION = 3; + static final int DB_SCHEMA_VERSION = 4; static class DBHelper1 extends SQLiteOpenHelper{ diff --git a/app/src/main/java/jp/juggler/fadownloader/DownloadRecord.java b/app/src/main/java/jp/juggler/fadownloader/DownloadRecord.java index 4f44ea6..351aea1 100644 --- a/app/src/main/java/jp/juggler/fadownloader/DownloadRecord.java +++ b/app/src/main/java/jp/juggler/fadownloader/DownloadRecord.java @@ -39,6 +39,9 @@ public class DownloadRecord{ db.execSQL( "create unique index if not exists " + table + "_air_path on " + table + "(ap)" ); + db.execSQL( + "create unique index if not exists " + table + "_name on " + table + "(n)" + ); } @Override public void onDBUpgrade( SQLiteDatabase db, int v_old, int v_new ){ @@ -53,6 +56,15 @@ public class DownloadRecord{ ex.printStackTrace( ); } } + if( v_old < 4 && v_new >= 4){ + try{ + db.execSQL( + "create unique index if not exists " + table + "_name on " + table + "(n)" + ); + }catch(Throwable ex){ + ex.printStackTrace( ); + } + } } }; @@ -110,6 +122,8 @@ public void loadFrom( Cursor cursor, ColIdx colIdx ){ lap_time = cursor.getLong( colIdx.idx_lap_time ); size = cursor.getLong( colIdx.idx_size ); } + + public static Uri insert( ContentResolver cr diff --git a/app/src/main/java/jp/juggler/fadownloader/DownloadService.java b/app/src/main/java/jp/juggler/fadownloader/DownloadService.java index 9c5505c..0495d80 100644 --- a/app/src/main/java/jp/juggler/fadownloader/DownloadService.java +++ b/app/src/main/java/jp/juggler/fadownloader/DownloadService.java @@ -42,6 +42,7 @@ public class DownloadService extends Service{ static final int NOTIFICATION_ID_SERVICE = 1; public static final String EXTRA_PROTECTED_ONLY = "protected_only"; + public static final String EXTRA_SKIP_ALREADY_DOWNLOAD = "skip_already_download"; public LogWriter log; diff --git a/app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java b/app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java index 9fa0ff4..86f5479 100644 --- a/app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java +++ b/app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.database.Cursor; import android.location.Location; import android.net.ConnectivityManager; import android.net.Network; @@ -19,13 +20,6 @@ import org.apache.commons.io.IOUtils; -import it.sephiroth.android.library.exif2.ExifInterface; -import jp.juggler.fadownloader.targets.FlashAir; -import jp.juggler.fadownloader.targets.PentaxKP; -import jp.juggler.fadownloader.targets.PqiAirCard; - -import java.io.File; -import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; @@ -34,6 +28,11 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import it.sephiroth.android.library.exif2.ExifInterface; +import jp.juggler.fadownloader.targets.FlashAir; +import jp.juggler.fadownloader.targets.PentaxKP; +import jp.juggler.fadownloader.targets.PqiAirCard; + public class DownloadWorker extends WorkerBase{ public static final boolean RECORD_QUEUED_STATE = false; @@ -71,6 +70,7 @@ public interface Callback{ public final int target_type; public final LocationTracker.Setting location_setting; public final boolean protected_only; + public final boolean skip_already_download; public DownloadWorker( DownloadService service, Intent intent, Callback callback ){ this.service = service; @@ -87,6 +87,7 @@ public DownloadWorker( DownloadService service, Intent intent, Callback callback this.ssid = intent.getStringExtra( DownloadService.EXTRA_SSID ); this.target_type = intent.getIntExtra( DownloadService.EXTRA_TARGET_TYPE, 0 ); this.protected_only = intent.getBooleanExtra( DownloadService.EXTRA_PROTECTED_ONLY, false ); + this.skip_already_download = intent.getBooleanExtra( DownloadService.EXTRA_SKIP_ALREADY_DOWNLOAD, false ); this.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 ); @@ -105,6 +106,7 @@ public DownloadWorker( DownloadService service, Intent intent, Callback callback .putBoolean( Pref.WORKER_FORCE_WIFI, force_wifi ) .putString( Pref.WORKER_SSID, ssid ) .putBoolean( Pref.WORKER_PROTECTED_ONLY, protected_only ) + .putBoolean( Pref.WORKER_SKIP_ALREADY_DOWNLOAD, skip_already_download ) .apply(); this.file_type_list = file_type_parse(); @@ -130,6 +132,7 @@ public DownloadWorker( DownloadService service, String cause, Callback callback this.ssid = pref.getString( Pref.WORKER_SSID, null ); this.target_type = pref.getInt( Pref.WORKER_TARGET_TYPE, 0 ); this.protected_only = pref.getBoolean( Pref.WORKER_PROTECTED_ONLY, false ); + this.skip_already_download = pref.getBoolean( Pref.WORKER_SKIP_ALREADY_DOWNLOAD, false ); this.location_setting = new LocationTracker.Setting(); location_setting.interval_desired = pref.getLong( Pref.WORKER_LOCATION_INTERVAL_DESIRED, LocationTracker.DEFAULT_INTERVAL_DESIRED ); @@ -207,7 +210,9 @@ public void setAlarm( long now,long remain ){ // レシーバーは受け取れるが端末のIDLE状態は解除されない。アプリが動けるのは10秒。IDLEからの復帰は15分に1度だけ許される }else */ - if( Build.VERSION.SDK_INT >= 21 ){ + if(am == null){ + throw new IllegalStateException( "missing AlarmManager" ); + }else if( Build.VERSION.SDK_INT >= 21 ){ am.setAlarmClock( new AlarmManager.AlarmClockInfo( now + remain, pi ), pi ); }else if( Build.VERSION.SDK_INT >= 19 ){ am.setExact( AlarmManager.RTC_WAKEUP, now + remain, pi ); @@ -353,6 +358,40 @@ public static class ErrorAndMessage{ } } } + + // ダウンロードをスキップするなら真を返す + public boolean checkSkip( LocalFile local_file, LogWriter log, long size ){ + + // ローカルにあるファイルのサイズが指定以上ならスキップする + if( local_file.length( log ) >= size ) return true; + + if( skip_already_download ){ + String name = local_file.getName(); + if( !TextUtils.isEmpty( name )){ + Cursor cursor = service.getContentResolver().query( + DownloadRecord.meta.content_uri, + null, + DownloadRecord.COL_NAME+"=?", + new String[]{ name}, + null + ); + if( cursor != null){ + try{ + if( cursor.moveToFirst()){ + // ダウンロード履歴に同じ名前のファイルがあるのでスキップする + log.i( "skip %s : already found in download record.",name ); + return true; + } + }finally{ + cursor.close(); + } + } + } + } + + return false; + } + public void record( ScanItem item @@ -360,7 +399,7 @@ public void record( , int state , String state_message ){ - if( ! DownloadWorker.RECORD_QUEUED_STATE ){ + if( ! RECORD_QUEUED_STATE ){ if( state == DownloadRecord.STATE_QUEUED ) return; } String local_uri = item.local_file.getFileUri( log ); @@ -477,25 +516,31 @@ public void onFileScanComplete(long file_count){ boolean isForcedSSID(){ if( ! force_wifi ) return true; WifiManager wm = (WifiManager) service.getApplicationContext().getSystemService( Context.WIFI_SERVICE ); - WifiInfo wi = wm.getConnectionInfo(); - String current_ssid = wi.getSSID().replace( "\"", "" ); - return ! TextUtils.isEmpty( current_ssid ) && current_ssid.equals( this.ssid ); + if(wm==null){ + return false; + }else{ + WifiInfo wi = wm.getConnectionInfo(); + String current_ssid = wi.getSSID().replace( "\"", "" ); + return ! TextUtils.isEmpty( current_ssid ) && current_ssid.equals( this.ssid ); + } } @SuppressWarnings( "deprecation" ) public Object getWiFiNetwork(){ ConnectivityManager cm = (ConnectivityManager) service.getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE ); - if( Build.VERSION.SDK_INT >= 21 ){ - for( Network n : cm.getAllNetworks() ){ - NetworkInfo info = cm.getNetworkInfo( n ); - if( info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI ){ - if( isForcedSSID() ) return n; + if( cm != null ){ + if( Build.VERSION.SDK_INT >= 21 ){ + for( Network n : cm.getAllNetworks() ){ + NetworkInfo info = cm.getNetworkInfo( n ); + if( info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI ){ + if( isForcedSSID() ) return n; + } } - } - }else{ - for( NetworkInfo info : cm.getAllNetworkInfo() ){ - if( info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI ){ - if( isForcedSSID() ) return info; - return info; + }else{ + for( NetworkInfo info : cm.getAllNetworkInfo() ){ + if( info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI ){ + if( isForcedSSID() ) return info; + return info; + } } } } diff --git a/app/src/main/java/jp/juggler/fadownloader/PageSetting.java b/app/src/main/java/jp/juggler/fadownloader/PageSetting.java index b60aae6..6e6d49c 100644 --- a/app/src/main/java/jp/juggler/fadownloader/PageSetting.java +++ b/app/src/main/java/jp/juggler/fadownloader/PageSetting.java @@ -32,6 +32,8 @@ public class PageSetting extends PagerAdapterBase.PageViewHolder implements View Switch swThumbnailAutoRotate; Switch swCopyBeforeViewSend; Switch swProtectedOnly; + Switch swSkipAlreadyDownload; + View btnSSIDPicker; boolean bLoading; int last_target_type; @@ -44,21 +46,22 @@ public PageSetting( Activity activity, View ignored ){ bLoading = true; last_target_type = - 1; - spTargetType = (Spinner) root.findViewById( R.id.spTargetType ); - etTargetUrl = (EditText) root.findViewById( R.id.etTargetUrl ); - tvLocalFolder = (TextView) root.findViewById( R.id.tvFolder ); - etInterval = (EditText) root.findViewById( R.id.etRepeatInterval ); - etFileType = (EditText) root.findViewById( R.id.etFileType ); - spLocationMode = (Spinner) root.findViewById( R.id.spLocationMode ); - etLocationIntervalDesired = (EditText) root.findViewById( R.id.etLocationIntervalDesired ); - etLocationIntervalMin = (EditText) root.findViewById( R.id.etLocationIntervalMin ); - swForceWifi = (Switch) root.findViewById( R.id.swForceWifi ); - etSSID = (EditText) root.findViewById( R.id.etSSID ); - swThumbnailAutoRotate = (Switch) root.findViewById( R.id.swThumbnailAutoRotate ); - swCopyBeforeViewSend = (Switch) root.findViewById( R.id.swCopyBeforeViewSend ); + spTargetType = root.findViewById( R.id.spTargetType ); + etTargetUrl = root.findViewById( R.id.etTargetUrl ); + tvLocalFolder = root.findViewById( R.id.tvFolder ); + etInterval = root.findViewById( R.id.etRepeatInterval ); + etFileType = root.findViewById( R.id.etFileType ); + spLocationMode = root.findViewById( R.id.spLocationMode ); + etLocationIntervalDesired = root.findViewById( R.id.etLocationIntervalDesired ); + etLocationIntervalMin = root.findViewById( R.id.etLocationIntervalMin ); + swForceWifi = root.findViewById( R.id.swForceWifi ); + etSSID = root.findViewById( R.id.etSSID ); + swThumbnailAutoRotate = root.findViewById( R.id.swThumbnailAutoRotate ); + swCopyBeforeViewSend = root.findViewById( R.id.swCopyBeforeViewSend ); btnSSIDPicker = root.findViewById( R.id.btnSSIDPicker ); - swProtectedOnly = (Switch) root.findViewById( R.id.swProtectedOnly ); - + swProtectedOnly = root.findViewById( R.id.swProtectedOnly ); + swSkipAlreadyDownload = root.findViewById( R.id.swSkipAlreadyDownload ); + root.findViewById( R.id.btnFolderPicker ).setOnClickListener( this ); root.findViewById( R.id.btnHelpFolderPicker ).setOnClickListener( this ); root.findViewById( R.id.btnHelpTargetUrl ).setOnClickListener( this ); @@ -74,6 +77,7 @@ public PageSetting( Activity activity, View ignored ){ root.findViewById( R.id.btnCopyBeforeViewSendHelp ).setOnClickListener( this ); root.findViewById( R.id.btnTargetTypeHelp ).setOnClickListener( this ); root.findViewById( R.id.btnHelpProtectedOnly ).setOnClickListener( this ); + root.findViewById( R.id.btnHelpSkipAlreadyDownload ).setOnClickListener( this ); ArrayAdapter location_mode_adapter = new ArrayAdapter<>( activity @@ -223,6 +227,9 @@ public PageSetting( Activity activity, View ignored ){ case R.id.btnHelpProtectedOnly: ( (ActMain) activity ).openHelp( activity.getString( R.string.protected_only_help ) ); break; + case R.id.btnHelpSkipAlreadyDownload: + ( (ActMain) activity ).openHelp( activity.getString( R.string.skip_already_downloaded_help ) ); + break; } } @@ -275,6 +282,8 @@ void ui_value_load(){ // swProtectedOnly.setChecked( pref.getBoolean( Pref.UI_PROTECTED_ONLY,false ) ); // + swSkipAlreadyDownload.setChecked( pref.getBoolean( Pref.UI_SKIP_ALREADY_DOWNLOAD,false ) ); + // updateFormEnabled(); bLoading = false; } @@ -299,6 +308,7 @@ void ui_value_save( SharedPreferences.Editor e ){ .putBoolean( Pref.UI_FORCE_WIFI, swForceWifi.isChecked() ) .putString( Pref.UI_SSID, etSSID.getText().toString() ) .putBoolean( Pref.UI_PROTECTED_ONLY, swProtectedOnly.isChecked() ) + .putBoolean( Pref.UI_SKIP_ALREADY_DOWNLOAD, swSkipAlreadyDownload.isChecked() ) ; // .apply() は呼び出し側で行う } diff --git a/app/src/main/java/jp/juggler/fadownloader/Pref.java b/app/src/main/java/jp/juggler/fadownloader/Pref.java index ab70b6a..fbf9560 100644 --- a/app/src/main/java/jp/juggler/fadownloader/Pref.java +++ b/app/src/main/java/jp/juggler/fadownloader/Pref.java @@ -79,6 +79,7 @@ public static void saveTargetUrl( SharedPreferences.Editor edit, int target_type public static final String UI_THUMBNAIL_AUTO_ROTATE = "ui_thumbnail_auto_rotate"; public static final String UI_COPY_BEFORE_VIEW_SEND = "ui_copy_before_view_send"; public static final String UI_PROTECTED_ONLY = "ui_protected_only"; + public static final String UI_SKIP_ALREADY_DOWNLOAD = "ui_skip_already_download"; public static final boolean DEFAULT_THUMBNAIL_AUTO_ROTATE = true; @@ -147,6 +148,7 @@ public static void initialize( Context context ){ public static final String WORKER_FORCE_WIFI = "worker_force_wifi"; public static final String WORKER_SSID = "worker_ssid"; public static final String WORKER_PROTECTED_ONLY = "worker_protected_only"; + public static final String WORKER_SKIP_ALREADY_DOWNLOAD = "worker_skip_already_download"; // ファイルスキャンが完了した時刻 public static final String LAST_IDLE_START = "last_idle_start"; diff --git a/app/src/main/java/jp/juggler/fadownloader/targets/FlashAir.java b/app/src/main/java/jp/juggler/fadownloader/targets/FlashAir.java index c8cc5f9..897fa4e 100644 --- a/app/src/main/java/jp/juggler/fadownloader/targets/FlashAir.java +++ b/app/src/main/java/jp/juggler/fadownloader/targets/FlashAir.java @@ -146,7 +146,8 @@ void loadFolder( final Object network, final ScanItem item ){ // マッチした // ローカルのファイルサイズを調べて既読スキップ - if( local_file.length( log ) >= size ) continue; + if( thread.checkSkip(local_file,log,size ))continue; + String mime_type = Utils.getMimeType( log, file_name ); diff --git a/app/src/main/java/jp/juggler/fadownloader/targets/PentaxKP.java b/app/src/main/java/jp/juggler/fadownloader/targets/PentaxKP.java index 042c04a..9fd94af 100644 --- a/app/src/main/java/jp/juggler/fadownloader/targets/PentaxKP.java +++ b/app/src/main/java/jp/juggler/fadownloader/targets/PentaxKP.java @@ -110,10 +110,9 @@ boolean loadFolder( Object network ){ String remote_path = "/" + sub_dir_name + "/" + file_name; LocalFile local_file = new LocalFile( sub_dir_local, file_name ); - - // ローカルにあるファイルのサイズが1以上ならスキップする - final long local_size = local_file.length( log ); - if( local_size >= 1L ) continue; + + // ローカルのファイルサイズを調べて既読スキップ + if( thread.checkSkip(local_file,log,1L ))continue; // 進捗表示用のファイルサイズは超適当 long size = 1000000L; diff --git a/app/src/main/java/jp/juggler/fadownloader/targets/PqiAirCard.java b/app/src/main/java/jp/juggler/fadownloader/targets/PqiAirCard.java index dfda4f4..db43088 100644 --- a/app/src/main/java/jp/juggler/fadownloader/targets/PqiAirCard.java +++ b/app/src/main/java/jp/juggler/fadownloader/targets/PqiAirCard.java @@ -148,7 +148,7 @@ void loadFolder( final Object network, final ScanItem item ){ // マッチした // ローカルのファイルサイズを調べて既読スキップ - if( local_file.length( log ) >= size ) continue; + if( thread.checkSkip( local_file,log,size )) continue; String mime_type = Utils.getMimeType( log, file_name ); diff --git a/app/src/main/res/layout/page_setting.xml b/app/src/main/res/layout/page_setting.xml index 25459ca..b1b41e5 100644 --- a/app/src/main/res/layout/page_setting.xml +++ b/app/src/main/res/layout/page_setting.xml @@ -141,6 +141,27 @@ + + + + + + +