simplify sd scanner code

This commit is contained in:
Adrian Ulrich 2016-07-03 15:59:03 +02:00
parent ca6d5d1743
commit 0e1b9a327b
4 changed files with 37 additions and 107 deletions

View File

@ -24,39 +24,12 @@ Copied from SD Scanner's layout/main.xml with minor changes
android:paddingBottom="16dp"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:text="@string/path_label" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/path_widget"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<ImageButton
android:id="@+id/path_button"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_revert"
style="?android:attr/borderlessButtonStyle" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="center_vertical"
android:text="@string/db_label" />
<CheckBox
android:id="@+id/restrict_checkbox"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/restrict_label" />
android:gravity="center_vertical"
android:text="@string/sdscan_help" />
<Button
android:id="@+id/start_button"
android:layout_width="match_parent"

View File

@ -296,7 +296,7 @@ THE SOFTWARE.
<string name="reverse_sort">Reverse sort</string>
<!-- SD Scanner -->
<string name="sdscan_help">Starting a rescan causes Vanilla Music to trigger a full rebuild of the media database.</string>
<string name="button_start">Start Rescan</string>
<string name="database_proc">Examined</string>
<string name="delete_proc">Removed reference to</string>
@ -305,13 +305,11 @@ THE SOFTWARE.
<string name="db_error_recovered">Encountered error reading media database, but recovered.</string>
<string name="db_error_retrying">Encountered error reading media database. Retrying in 1 second...</string>
<string name="final_proc">Processed</string>
<string name="path_label">Path to check for new files:</string>
<string name="progress_completed_label">Completed, ready to start another scan.</string>
<string name="progress_error_bad_path_label">Scan failed: bad path specified for new file search.</string>
<string name="progress_filelist_label">Preparing initial list of files...</string>
<string name="progress_database_label">Querying database...</string>
<string name="progress_unstarted_label">Not yet started.</string>
<string name="restrict_label">Ignore updated and deleted files outside of the specified path.</string>
<string name="skipping_folder_label">Encountered an error and skipping</string>
<string name="sdscanner">SD Scanner</string>
</resources>

View File

@ -19,8 +19,6 @@ package ch.blinkenlights.android.vanilla;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
@ -28,8 +26,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
@ -47,6 +43,12 @@ public class SDScannerFragment extends Fragment
{
ScanFragment mScanFragment;
/**
* List of common directories with media files
*/
private File[] mScanTargetStages = { Environment.getExternalStorageDirectory(), new File("/storage/sdcard1") };
@Override
public void updateProgressNum(int progressNum) {
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progress_bar);
@ -65,11 +67,6 @@ public class SDScannerFragment extends Fragment
debugLabel.setText(debugMessages.toString(getActivity()));
}
@Override
public void updatePath(String path) {
EditText pathText = (EditText) findViewById(R.id.path_widget);
pathText.setText(path);
}
@Override
public void updateStartButtonEnabled(boolean startButtonEnabled) {
@ -77,14 +74,12 @@ public class SDScannerFragment extends Fragment
startButton.setEnabled(startButtonEnabled);
}
public void updateRestrictCheckboxChecked(boolean checked) {
CheckBox restrictCheckbox = (CheckBox) findViewById(R.id.restrict_checkbox);
restrictCheckbox.setChecked(checked);
@Override
public void updatePath(String path) {
}
@Override
public void signalFinished() {
}
@Override
@ -101,35 +96,10 @@ public class SDScannerFragment extends Fragment
updateDebugMessages(mScanFragment.getDebugMessages());
updateStartButtonEnabled(mScanFragment.getStartButtonEnabled());
// Update path from preferences
SharedPreferences preferences = getActivity().getPreferences(Context.MODE_PRIVATE);
try {
updatePath(preferences.getString("path",
Environment.getExternalStorageDirectory().getCanonicalPath()));
updateRestrictCheckboxChecked(preferences.getBoolean(
"restrict_db_scan", false));
}
catch (IOException Ex) {
// Should never happen, but getCanonicalPath() declares the throw.
updatePath("");
updateRestrictCheckboxChecked(false);
}
// Make debug output scrollable.
TextView debugLabel = (TextView)findViewById(R.id.debug_label);
debugLabel.setMovementMethod(new ScrollingMovementMethod());
view.findViewById(R.id.path_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
defaultButtonPressed(v);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
view.findViewById(R.id.start_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -168,35 +138,8 @@ public class SDScannerFragment extends Fragment
return getView().findViewById(viewId);
}
@Override
public void onStop() {
super.onStop();
// Write setting to preferences
EditText pathText = (EditText) findViewById(R.id.path_widget);
CheckBox restrictCheckbox = (CheckBox) findViewById(R.id.restrict_checkbox);
SharedPreferences preferences = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("path", pathText.getText().toString());
editor.putBoolean("restrict_db_scan", restrictCheckbox.isChecked());
editor.commit();
}
public void defaultButtonPressed(View view) throws IOException {
updatePath(Environment.getExternalStorageDirectory().getCanonicalPath());
}
public void startButtonPressed(View view) throws IOException {
startScan();
}
public void startScan() throws IOException {
EditText pathText = (EditText) findViewById(R.id.path_widget);
File path = new File(pathText.getText().toString());
CheckBox restrictCheckbox = (CheckBox) findViewById(R.id.restrict_checkbox);
mScanFragment.startScan(path.getCanonicalFile(), restrictCheckbox.isChecked());
mScanFragment.startScan(mScanTargetStages);
}
}

View File

@ -72,6 +72,8 @@ public class ScanFragment extends Fragment {
boolean mStartButtonEnabled;
boolean mHasStarted = false;
ArrayList<File> mDirectoryScanList;
/**
* Callback interface used by the fragment to update the Activity.
*/
@ -204,17 +206,22 @@ public class ScanFragment extends Fragment {
addDebugMessage(listString.toString());
}
public void scannerEnded() {
updateProgressNum(0);
updateProgressText(R.string.progress_completed_label);
updateStartButtonEnabled(true);
signalFinished();
public void advanceScanner() {
if (mDirectoryScanList != null && mDirectoryScanList.isEmpty() == false) {
File nextDir = mDirectoryScanList.remove(0);
startScan(nextDir, false);
} else {
updateProgressNum(0);
updateProgressText(R.string.progress_completed_label);
updateStartButtonEnabled(true);
signalFinished();
}
}
public void startMediaScanner(){
//listPathNamesOnDebug();
if (mPathNames.size() == 0) {
scannerEnded();
advanceScanner();
}
else {
MediaScannerConnection.scanFile(
@ -229,6 +236,15 @@ public class ScanFragment extends Fragment {
}
}
public void startScan(File[] pathList) {
mDirectoryScanList = new ArrayList<File>();
for (File f : pathList) {
if (f.exists() && f.isDirectory())
mDirectoryScanList.add(f);
}
advanceScanner();
}
public void startScan(File path, boolean restrictDbUpdate) {
mHasStarted = true;
updateStartButtonEnabled(false);
@ -528,7 +544,7 @@ public class ScanFragment extends Fragment {
int progress = (100 * (mLastGoodProcessedIndex + 1))
/ mPathNames.size();
if (progress == 100) {
scannerEnded();
advanceScanner();
}
else {
updateProgressNum(progress);