-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
位置情報を追加する際に一時ファイルがMesiaScannerに使われないようにした
- Loading branch information
Showing
5 changed files
with
182 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/jp/juggler/fadownloader/MediaScannerTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package jp.juggler.fadownloader; | ||
|
||
import android.content.Context; | ||
import android.media.MediaScannerConnection; | ||
import android.net.Uri; | ||
import android.os.Handler; | ||
import android.os.SystemClock; | ||
import android.text.TextUtils; | ||
|
||
import java.io.File; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
public class MediaScannerTracker implements MediaScannerConnection.MediaScannerConnectionClient{ | ||
|
||
@Override public void onMediaScannerConnected(){ | ||
handler.post( queue_reader ); | ||
} | ||
|
||
@Override public void onScanCompleted( String path, Uri uri ){ | ||
} | ||
|
||
final Context context; | ||
final LogWriter log; | ||
final MediaScannerConnection conn; | ||
final Handler handler; | ||
long last_connect_start; | ||
|
||
volatile boolean is_dispose = false; | ||
|
||
void dispose(){ | ||
is_dispose = true; | ||
} | ||
|
||
public MediaScannerTracker( Context context, LogWriter log ){ | ||
this.context = context; | ||
this.log = log; | ||
this.conn = new MediaScannerConnection( context, this ); | ||
this.handler = new Handler(); | ||
prepareConnection(); | ||
} | ||
|
||
static class Item{ | ||
|
||
String path; | ||
String mime_type; | ||
} | ||
|
||
final ConcurrentLinkedQueue<Item> queue = new ConcurrentLinkedQueue<>(); | ||
|
||
public void addFile( File file, String mime_type ){ | ||
if( file == null || ! file.isFile() ) return; | ||
if( TextUtils.isEmpty( mime_type ) ) return; | ||
Item item = new Item(); | ||
item.path = file.getAbsolutePath(); | ||
item.mime_type = mime_type; | ||
queue.add( item ); | ||
handler.post( queue_reader ); | ||
} | ||
|
||
private boolean prepareConnection(){ | ||
if( conn.isConnected() ) return true; | ||
|
||
long now = SystemClock.elapsedRealtime(); | ||
if( now - last_connect_start >= 5000L ){ | ||
last_connect_start = now; | ||
conn.connect(); | ||
} | ||
return false; | ||
} | ||
|
||
final Runnable queue_reader = new Runnable(){ | ||
@Override public void run(){ | ||
handler.removeCallbacks( queue_reader ); | ||
for( ; ; ){ | ||
|
||
Item item = queue.peek(); | ||
|
||
if( item == null ){ | ||
if( is_dispose ){ | ||
conn.disconnect(); | ||
} | ||
break; | ||
} | ||
|
||
if( ! prepareConnection() ){ | ||
handler.postDelayed( queue_reader, 1000L ); | ||
break; | ||
} | ||
|
||
conn.scanFile( item.path, item.mime_type ); | ||
|
||
queue.poll(); | ||
} | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters