Skip to content

Commit

Permalink
ダウンロード履歴に名前があるファイルはスキップする設定を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
tateisu committed Sep 7, 2018
1 parent 26ae7af commit f2b9abc
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 48 deletions.
10 changes: 6 additions & 4 deletions app/src/main/java/jp/juggler/fadownloader/ActMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );
Expand Down Expand Up @@ -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()
Expand All @@ -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 );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/jp/juggler/fadownloader/DownloadRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ){
Expand All @@ -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( );
}
}
}
};

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
91 changes: 68 additions & 23 deletions app/src/main/java/jp/juggler/fadownloader/DownloadWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 );
Expand All @@ -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();
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -353,14 +358,48 @@ 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
, long lap_time
, 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 );
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
38 changes: 24 additions & 14 deletions app/src/main/java/jp/juggler/fadownloader/PageSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );
Expand All @@ -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<CharSequence> location_mode_adapter = new ArrayAdapter<>(
activity
Expand Down Expand Up @@ -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;

}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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() は呼び出し側で行う
}
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 @@ -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;

Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
Loading

0 comments on commit f2b9abc

Please sign in to comment.