Convert SDScanner MainActivity to fragment

This commit is contained in:
Xiao Bao Clark 2016-06-12 13:20:26 +10:00
parent 534f09e645
commit f9f8962064
5 changed files with 293 additions and 1 deletions

View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2013-2014 Jeremy Erickson
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-->
<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="match_parent"
android:paddingRight="16dp"
android:paddingLeft="16dp"
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/restrict_label" />
<Button
android:id="@+id/start_button"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_gravity="center"
android:text="@string/button_start">
<requestFocus />
</Button>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:progress="0" />
<TextView
android:id="@+id/progress_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10000"
android:text="@string/progress_unstarted_label" />
<TextView
android:id="@+id/debug_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="" />
</LinearLayout>

View File

@ -312,4 +312,5 @@ THE SOFTWARE.
<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

@ -45,6 +45,9 @@ THE SOFTWARE.
<header
android:fragment="ch.blinkenlights.android.vanilla.PreferencesTheme"
android:title="@string/theme" />
<header
android:fragment="ch.blinkenlights.android.vanilla.SDScannerFragment"
android:title="@string/sdscanner" />
<header
android:fragment="ch.blinkenlights.android.vanilla.PreferencesActivity$AboutFragment"
android:title="@string/about" />

View File

@ -0,0 +1,196 @@
/* SD Scanner - A manual implementation of the SD rescan process, compatible
* with Android 4.4
*
* Copyright (C) 2013-2014 Jeremy Erickson
* Copyright (C) 2016 Xiao Bao Clark
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
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;
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;
import com.gmail.jerickson314.sdscanner.ScanFragment;
import com.gmail.jerickson314.sdscanner.UIStringGenerator;
import java.io.File;
import java.io.IOException;
/**
* Fragment version of the MainActivity from the SD Scanner app
*/
public class SDScannerFragment extends Fragment
implements ScanFragment.ScanProgressCallbacks
{
ScanFragment mScanFragment;
@Override
public void updateProgressNum(int progressNum) {
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progress_bar);
progressBar.setProgress(progressNum);
}
@Override
public void updateProgressText(UIStringGenerator progressText) {
TextView progressLabel = (TextView)findViewById(R.id.progress_label);
progressLabel.setText(progressText.toString(getActivity()));
}
@Override
public void updateDebugMessages(UIStringGenerator debugMessages) {
TextView debugLabel = (TextView)findViewById(R.id.debug_label);
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) {
Button startButton = (Button)findViewById(R.id.start_button);
startButton.setEnabled(startButtonEnabled);
}
public void updateRestrictCheckboxChecked(boolean checked) {
CheckBox restrictCheckbox = (CheckBox) findViewById(R.id.restrict_checkbox);
restrictCheckbox.setChecked(checked);
}
@Override
public void signalFinished() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.sdscanner_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Setup with values from fragment.
updateProgressNum(mScanFragment.getProgressNum());
updateProgressText(mScanFragment.getProgressText());
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) {
try {
startButtonPressed(v);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
mScanFragment = (ScanFragment) fm.findFragmentByTag("scan");
if (mScanFragment == null) {
mScanFragment = new ScanFragment();
fm.beginTransaction().add(mScanFragment, "scan").commit();
}
mScanFragment.setScanProgressCallbacks(this);
}
private View findViewById(int viewId) {
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());
}
}

View File

@ -165,10 +165,16 @@ public class ScanFragment extends Fragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks = (ScanProgressCallbacks) activity;
if(activity instanceof ScanProgressCallbacks) {
mCallbacks = (ScanProgressCallbacks) activity;
}
mApplicationContext = activity.getApplicationContext();
}
public void setScanProgressCallbacks(ScanProgressCallbacks callbacks) {
mCallbacks = callbacks;
}
public ScanFragment() {
super();