diff --git a/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/BuiltInSensorsFragment.java b/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/BuiltInSensorsFragment.java index b3cd142c5d385343166b4970017d975641628fee..9c9ed7ab0333f3152f74f4aa2872e3b37cf91e6f 100644 --- a/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/BuiltInSensorsFragment.java +++ b/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/BuiltInSensorsFragment.java @@ -136,7 +136,6 @@ public class BuiltInSensorsFragment extends ListFragment implements SensorEventL break; case Sensor.TYPE_MAGNETIC_FIELD: sens.setLastValue(values, 3); - sens.setID("and11"); break; case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED: diff --git a/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/MyDocumentsActivity.java b/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/MyDocumentsActivity.java index 30d474c0d958e1245722e11db6fa7905a6d0ab42..1bd352fc55db72e016d3394119b18ada367a615a 100644 --- a/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/MyDocumentsActivity.java +++ b/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/MyDocumentsActivity.java @@ -3,31 +3,51 @@ package fieldscience.cs.earlham.edu.fieldday; import android.app.Activity; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; +import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; +import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.ListView; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.List; public class MyDocumentsActivity extends Activity { public static final String directory = Environment.getExternalStorageDirectory().toString() + - File.separator + "FieldDay"; + File.separator + "FieldDay" + File.separator + "myDocuments"; Button downloadFiles; private static final String TAG = "MyDocumentsActivity"; - public static List<String> fileList; + public static List<String> downloadList, dirList; + public static ArrayList<File> localDirList; + private Boolean firstPage = true, insideDirectory = false; + private String baseURL = "", currentPath = "", currentDir = "", initialDir = ""; + private ListView filesListView; + private MyDocumentsAdapter myDocumentsAdapter; + SharedPreferences settings; + SharedPreferences.Editor editor; + Context context; + @Override protected void onCreate(Bundle savedInstanceState){ @@ -43,11 +63,19 @@ public class MyDocumentsActivity extends Activity { } }); - fileList = new ArrayList<String>(); + downloadList = new ArrayList<String>(); + dirList = new ArrayList<String>(); + localDirList = new ArrayList<File>(); + firstPage = true; + + context = this; - // Create the directory to store the files that are downloaded/to be downloaded. - createDirectory(); + filesListView = (ListView) findViewById(R.id.listFileResults); + myDocumentsAdapter = new MyDocumentsAdapter(context, localDirList); + filesListView.setAdapter(myDocumentsAdapter); + settings= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + editor = settings.edit(); } @@ -56,7 +84,9 @@ public class MyDocumentsActivity extends Activity { LayoutInflater layoutInflater = this.getLayoutInflater(); final View dialog = layoutInflater.inflate(R.layout.alert_dialog_sigin, null); final EditText host = (EditText) dialog.findViewById(R.id.fileHost); + host.setText(settings.getString("docURL", "")); final EditText path = (EditText) dialog.findViewById(R.id.directoryPath); + path.setText(settings.getString("docPath", "")); builder.setView(dialog); builder.setNegativeButton("Cancel", null); builder.setTitle("File Host Details"); @@ -65,33 +95,39 @@ public class MyDocumentsActivity extends Activity { @Override public void onClick(DialogInterface dialog, int which) { String[] info = {host.getText().toString(), path.getText().toString()}; + editor.putString("docURL", host.getText().toString()); + editor.putString("docPath", path.getText().toString()); + editor.apply(); new GetListOfFiles().execute(info); } }); builder.create().show(); } - public void createDirectory() { + public File createDirectory(String dirName) { try { File sd = Environment.getExternalStorageDirectory(); if (sd.canWrite()) { File baseDir = new File(directory); if (!baseDir.exists()){ baseDir.mkdir(); + Log.d( TAG, "Created directory: " + directory); } - File documentsDir = new File(directory + "/myDocuments"); + File documentsDir = new File(directory + File.separator + dirName); if (!documentsDir.exists()){ documentsDir.mkdir(); + Log.d(TAG, "Created directory: " + dirName); } + return documentsDir; } } catch (Exception e) { e.printStackTrace(); Log.e("My Documents Activity", "Could not created directories in external storage"); } + return null; } - - private class GetListOfFiles extends AsyncTask<String, Void, List<String>> { + private class GetListOfFiles extends AsyncTask<String, Void, Boolean> { @Override protected void onPreExecute() { @@ -99,85 +135,182 @@ public class MyDocumentsActivity extends Activity { } @Override - protected List<String> doInBackground(String... url){ + protected Boolean doInBackground(String... url){ try { // We use JSoup to do the HTML parsing of the files available - Document doc = Jsoup.connect(url[0] + '/' + url[1]).get(); - List<String> files = new ArrayList<String>(); + currentDir = url[1]; + if (firstPage){ + baseURL = url[0]; + currentPath = '/' + currentDir; + initialDir = currentDir; + } else { + currentPath = '/' + initialDir + currentDir; + } + Document doc = Jsoup.connect(baseURL + currentPath).get(); for (Element file : doc.select("tr td a")) { - - // This checks if it's a directory. + // Makes sure the element selected is not the parent directory. We don't need to list that. if (!file.text().equals("Parent Directory")) { - files.add(file.attr("href")); + dirList.add(file.attr("href")); } } - return files; + // Now we display a list of files that we found to the user. The user will select which + // files they want to download. + MyDocumentsActivity.this.runOnUiThread(new Runnable() { + @Override + public void run() { + AlertDialog.Builder builder = new AlertDialog.Builder(MyDocumentsActivity.this, AlertDialog.THEME_HOLO_LIGHT); + final String[] fileArray = dirList.toArray(new String[dirList.size()]); + builder.setTitle("Select files to download"); + + // Check if this time is the first page or if the user selected a directory + // before and now we are showing a new directory listing. If it's not the + // first page, then we have to change the negative button to back where we + // will show the directory before that. + if (firstPage) { + builder.setNegativeButton("Cancel", null); + builder.setPositiveButton("Download", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + new downloadListOfFiles().execute(); + dirList.clear(); + } + }); + } else { + builder.setNegativeButton("Back", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which){ + dialog.dismiss(); + currentPath = currentPath.substring(0, currentPath.length() - currentDir.length()); + Log.d("Current Path", currentPath); + } + }); + } + final boolean[] checked = new boolean[fileArray.length]; + for (int i=0; i<fileArray.length; i++){ + if (downloadList.contains(fileArray[i])){ + checked[i] = true; + } else { + checked[i] = false; + } + } + builder.setMultiChoiceItems(fileArray, checked, new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(DialogInterface dialog, int which, boolean isChecked) { + String file = fileArray[which]; + if (isChecked) { + // This checks if it's a directory. If it is, then we have to + // open a new dialog that has asks for which files in that + // directory to download. + if (file.matches("^(\\w+)(/)$")){ + firstPage = false; + dirList.clear(); + File dir = createDirectory(fileArray[which]); + if (!localDirList.contains(dir)){ + localDirList.add(dir); + } + new GetListOfFiles().execute(new String[] {currentPath, file}); + } else { + Log.d("On click", currentPath); + if (currentPath.endsWith(currentDir)) { + downloadList.add(currentDir + fileArray[which]); + } else { + downloadList.add(fileArray[which]); + } + } + } else if (downloadList.contains(file)) { + // The user has decided they don't want to download that + // item anymore. + + // Check if the item clicked is a directory. + if (file.matches("^(\\w+)(/)$")){ + // Delete any files in that subdirectory from the list. + for (String item : fileArray){ + if (item.contains(file)){ + downloadList.remove(downloadList.indexOf(item)); + } + } + + } + File dir = createDirectory(fileArray[which]); + localDirList.remove(localDirList.indexOf(dir)); + downloadList.remove(fileArray[which]); + } + } + }); + builder.create().show(); + } + }); + return true; } catch (Exception e) { e.printStackTrace(); + return false; } - return null; } @Override - protected void onPostExecute(List<String> listOfFiles) { - AlertDialog.Builder builder = new AlertDialog.Builder(MyDocumentsActivity.this, AlertDialog.THEME_HOLO_LIGHT); - String[] fileArray = listOfFiles.toArray(new String[listOfFiles.size()]); - builder.setTitle("Select files to download"); - builder.setNegativeButton("Cancel", null); - builder.setPositiveButton("Download", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - - } - }); - builder.setMultiChoiceItems(fileArray, null, new DialogInterface.OnMultiChoiceClickListener() { - @Override - public void onClick(DialogInterface dialog, int which, boolean isChecked) { - - } - }); - builder.create().show(); + protected void onPostExecute(Boolean result) { } } - private class downloadListOfFiles extends AsyncTask<List<String>, Void, Void> { + private class downloadListOfFiles extends AsyncTask<Void, Void, Boolean> { @Override - protected Void doInBackground(List<String>... list){ - // String path = url[1]; - // URL fileHost = new URL(url[0] + '/' + path); - // HttpURLConnection conn = (HttpURLConnection) fileHost.openConnection(); - // conn.setRequestMethod("GET"); - // conn.connect(); - // Log.d(TAG, fileHost.toString()); - - //InputStream in = conn.getInputStream(); - /* if (in != null) { - BufferedReader br = new BufferedReader(new InputStreamReader(in)); - StringBuilder sb = new StringBuilder(); - String inputString; - - while ((inputString = br.readLine()) != null) { - sb.append(inputString); - sb.append("\n"); + protected Boolean doInBackground(Void... params){ + URL fileHost = null; + for (String file : downloadList) { + if (file.matches("^(\\w+)(/)$")) { + File dir = createDirectory(file); + Log.i(TAG, "Creating directory:" + file); + localDirList.add(dir); + } else { + String fileName = ""; + File writeFile = null; + if (file.contains("/")) { + String[] tokens = file.split("/"); + fileName = tokens[tokens.length-1]; + String path = file.substring(0, file.length() - fileName.length()); + createDirectory(path); + Log.d("FileName", fileName); + Log.d("Path", path); + writeFile = new File(directory + '/' + path, fileName); + } else { + writeFile = new File(directory, file); + localDirList.add(writeFile); + } + try { + fileHost = new URL(baseURL + '/' + initialDir + file); + Log.d(TAG, "Made URL for: " + baseURL + '/' + initialDir + file); + HttpURLConnection conn = (HttpURLConnection) fileHost.openConnection(); + conn.setRequestMethod("GET"); + conn.connect(); + InputStream is = conn.getInputStream(); + if (is != null) { + BufferedInputStream inputStream = new BufferedInputStream(is); + FileOutputStream fileOutputStream = new FileOutputStream(writeFile); + int totalSize = conn.getContentLength(); + byte[] buffer = new byte[1024 * 1024]; + int bufferLength = 0; + while ((bufferLength = inputStream.read(buffer)) > 0) { + fileOutputStream.write(buffer, 0, bufferLength); + } + fileOutputStream.close(); + } + } catch(MalformedURLException e) { + e.printStackTrace(); + return false; + } catch (IOException e) { + e.printStackTrace(); + return false; } - br.close(); - response = sb.toString(); - Log.d(TAG, Html.fromHtml(response).toString()); - in.close(); - }*/ - - /* if (!file.attr("href").matches("^(\\w+)(/)$")){ - fileList.add(file.attr("href")); - Log.d(TAG, file.text()); + } } + return true; + } - // Checks if it's their home directory - if (file.attr("href").matches("^(/~?)(\\w?/+)")) { - Log.d(TAG, "home dir"); - }*/ - return null; + @Override + protected void onPostExecute(Boolean result){ + myDocumentsAdapter.notifyDataSetChanged(); } } } diff --git a/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/MyDocumentsAdapter.java b/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/MyDocumentsAdapter.java new file mode 100644 index 0000000000000000000000000000000000000000..8098782ef2bb386d8db49871562d7d3d1cbb6a15 --- /dev/null +++ b/app/src/main/java/fieldscience/cs/earlham/edu/fieldday/MyDocumentsAdapter.java @@ -0,0 +1,48 @@ +package fieldscience.cs.earlham.edu.fieldday; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import java.io.File; +import java.util.ArrayList; + +public class MyDocumentsAdapter extends ArrayAdapter<File> { + + private Context context; + + public MyDocumentsAdapter(Context c, ArrayList<File> files) { + super(c, R.layout.file_list, files); + context = c; + } + + public View getView(int position, View convertView, ViewGroup parent){ + File file = getItem(position); + + if (convertView == null){ + LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = layoutInflater.inflate(R.layout.file_list, parent, false); + } + + TextView fName = (TextView) convertView.findViewById(R.id.objectName); + ImageView fType = (ImageView) convertView.findViewById(R.id.objectType); + + + fName.setText(file.getName()); + if (file.isDirectory()){ + fType.setImageDrawable(context.getResources().getDrawable(R.drawable.folder_black_icon, null)); + } else { + fType.setImageDrawable(context.getResources().getDrawable(R.drawable.file_white_icon, null)); + } + + return convertView; + } + + + + +}