Commit 66e79553 authored by Kristin Muterspaw's avatar Kristin Muterspaw
Browse files

In My Documents activity, when an item is pressed, Field Day checks whether...

In My Documents activity, when an item is pressed, Field Day checks whether it's a directory or file. If it's a directory, then the list of files is replaced with the file in the selected directory and the parent directory of that one so the user can go back if they want. If it's a file, it opens My Document Viewer and displays the file. PDFs are rendered as bitmaps in Android, so those are handled separately than text files. My Document Viewer currently only works for PDFs. PDF is rendered!
parent 23316907
......@@ -45,8 +45,14 @@
android:label="Connect to Google Drive"
android:theme="@style/MainTheme">
</activity>
<activity android:name=".MyDocumentsActivity"
android:label="Interact with documents"
<activity
android:name=".MyDocumentsActivity"
android:label="My Documents"
android:theme="@style/MainTheme">
</activity>
<activity
android:name=".MyDocumentViewer"
android:label="My Documents Viewer"
android:theme="@style/MainTheme">
</activity>
<service
......
package fieldscience.cs.earlham.edu.fieldday;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyDocumentViewer extends Activity {
File fileToOpen;
private static int index = 0;
private ImageView pdfImageView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mydocviewier);
pdfImageView = (ImageView) findViewById(R.id.pdfView);
fileToOpen = (File) getIntent().getExtras().get("fileToOpen");
if (fileToOpen.getName().endsWith(".pdf")){
openPDF();
Log.d("Open PDF", fileToOpen.getName());
}
}
public void openPDF(){
try {
ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(fileToOpen, ParcelFileDescriptor.MODE_READ_ONLY);
PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
PdfRenderer.Page page = pdfRenderer.openPage(index);
Bitmap pdf = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_8888);
page.render(pdf, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
pdfImageView.setImageBitmap(pdf);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
......@@ -5,6 +5,7 @@ import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
......@@ -69,10 +70,8 @@ public class MyDocumentsActivity extends Activity {
dirList = new ArrayList<String>();
localDirList = new ArrayList<File>();
File rootDir = new File(directory);
for (File f : rootDir.listFiles()){
localDirList.add(f);
}
final File rootDir = new File(directory);
localDirList = getFilesInDir(rootDir);
firstPage = true;
context = this;
......@@ -83,7 +82,20 @@ public class MyDocumentsActivity extends Activity {
filesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (myDocumentsAdapter.getItem(position).isDirectory()){
File folder = myDocumentsAdapter.getItem(position);
localDirList = getFilesInDir(folder);
if (!folder.equals(rootDir)) {
localDirList.add(folder.getParentFile());
}
myDocumentsAdapter.clear();
myDocumentsAdapter.addAll(localDirList);
myDocumentsAdapter.notifyDataSetChanged();
} else {
Intent i = new Intent(context, MyDocumentViewer.class);
i.putExtra("fileToOpen", myDocumentsAdapter.getItem(position));
startActivity(i);
}
}
});
......@@ -117,6 +129,15 @@ public class MyDocumentsActivity extends Activity {
builder.create().show();
}
public ArrayList<File> getFilesInDir(File directory){
ArrayList<File> list = new ArrayList<File>();
for (File f : directory.listFiles()) {
list.add(f);
Log.d("File name", f.getName());
}
return list;
}
public File createDirectory(String dirName) {
try {
File sd = Environment.getExternalStorageDirectory();
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white"
android:scaleType="fitCenter" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal" >
<Button
android:id="@+id/previous"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous" />
<Button
android:id="@+id/next"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
</LinearLayout>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment