Skip to content

Commit

Permalink
added notes fragement to file chooser, recent files are not added to …
Browse files Browse the repository at this point in the history
…the system wide recent files list, nots provider supports search now and various other improvements
  • Loading branch information
Christian Gogolin committed Dec 18, 2015
1 parent e6d7af4 commit eece517
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 53 deletions.
2 changes: 1 addition & 1 deletion platform/android/res/values-de/strings.xml
Expand Up @@ -102,7 +102,7 @@

<string name="notes_provider_summary">Mit Pen&amp;PDF erstellte Notizen</string>
<string name="notes_provider_title">Pen&amp;PDF Notizen</string>

<string name="notes">Notizen</string>
<string name="okay">OK</string>
<string name="outline_title">Inhaltsverzeichnis</string>
<string name="overwrite_question">Überschreiben?</string>
Expand Down
2 changes: 1 addition & 1 deletion platform/android/res/values/strings.xml
Expand Up @@ -103,7 +103,7 @@

<string name="notes_provider_summary">Notes created with Pen&amp;PDF</string>
<string name="notes_provider_title">Pen&amp;PDF notes</string>

<string name="notes">Notes</string>
<string name="okay">Okay</string>
<string name="outline_title">Table of Contents</string>
<string name="overwrite">Overwrite</string>
Expand Down
Expand Up @@ -139,9 +139,9 @@ else if(savedInstanceState != null)
mPurpose = Purpose.valueOf(savedInstanceState.getString(PURPOSE));
if(savedInstanceState.getString(DIRECTORY) != null) mDirectory = new File(savedInstanceState.getString(DIRECTORY));
}
//If we didn't get a directory we default to downloads
//We default to the notes directory
if(mDirectory == null)
mDirectory = PenAndPDFContentProvider.getNotesDir(getActivity());
mDirectory = PenAndPDFActivity.getNotesDir(getActivity());
// mDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

// Create a new handler that is updated dynamically when files are scanned
Expand All @@ -159,13 +159,13 @@ public void run() {

//Get the parent directory and the directories and files
// mParent = mDirectory.getParentFile();
// mDirs = mDirectory.listFiles(new FileFilter() {
// public boolean accept(File file) {
// return file.isDirectory();
// }
// });
// if (mDirs == null)
// mDirs = new File[0];
mDirs = mDirectory.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
});
if (mDirs == null)
mDirs = new File[0];
mFiles = mDirectory.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory())
Expand Down
25 changes: 16 additions & 9 deletions platform/android/src/com/cgogolin/penandpdf/PenAndPDFActivity.java
Expand Up @@ -70,7 +70,7 @@
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.Set;

import android.view.Gravity;


class ThreadPerTaskExecutor implements Executor {
Expand Down Expand Up @@ -872,19 +872,19 @@ private void tryToTakePermissions(Uri uri) {
catch(Exception e)
{
//Nothing we can do if we don't get the permission
Log.i(getString(R.string.app_name), "Failed to take persistable read uri permissions for "+uri+" Exception: "+e);
Log.i(getString(R.string.app_name), "Failed to take persistable read uri permissions for "+uri.getPath()+" Exception: "+e);
}
finally
{
try
{
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Log.i(getString(R.string.app_name), "Succesfully took persistable write uri permissions for "+uri);
Log.i(getString(R.string.app_name), "Succesfully took persistable write uri permissions for "+uri.getPath());
}
catch(Exception e)
{
//Nothing we can do if we don't get the permission
Log.i(getString(R.string.app_name), "Failed to take persistable write uri permissions for "+uri+" Exception: "+e);
Log.i(getString(R.string.app_name), "Failed to take persistable write uri permissions for "+uri.getPath()+" Exception: "+e);
}
}
}
Expand Down Expand Up @@ -1237,7 +1237,7 @@ public void showOpenDocumentDialog() {
}

public void openNewDocument(String filename) throws java.io.IOException {
File dir = PenAndPDFContentProvider.getNotesDir(this); //Should be done via the provider once implemented
File dir = getNotesDir(this); //Should be done via the provider once implemented
File file = new File(dir, filename);
Uri uri = Uri.fromFile(file);

Expand Down Expand Up @@ -1624,14 +1624,16 @@ private void showOpenNewDocumentDialoge() {
editTextLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
editTextLayout.setOrientation(1);
editTextLayout.setPadding(16, 16, 16, 0);//should not be hard coded

final EditText input = new EditText(editTextLayout.getContext());
input.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
input.setSingleLine();
input.setBackgroundDrawable(null);
input.setHint(getString(R.string.dialog_newdoc_hint));
input.setText(".pdf");
// input.setHint(getString(R.string.dialog_newdoc_hint));
// input.setText();
TextDrawable textDrawable = new TextDrawable(".pdf", input.getTextSize(), input.getCurrentTextColor());
input.setCompoundDrawablesWithIntrinsicBounds(null , null, textDrawable, null);
input.setFocusable(true);
input.setGravity(Gravity.RIGHT);
mAlertDialog.setTitle(R.string.dialog_newdoc_title);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
{
Expand All @@ -1642,7 +1644,7 @@ public void onClick(DialogInterface dialog, int which) {
try
{
if(filename != "")
openNewDocument(input.getText().toString());
openNewDocument(filename+".pdf");
}
catch(java.io.IOException e){
AlertDialog alert = mAlertBuilder.create();
Expand Down Expand Up @@ -1832,4 +1834,9 @@ public void onAnimationEnd(Animator animation) {
// Nothing to do
}
}

public static File getNotesDir(Context contex) {
return contex.getDir("notes", Context.MODE_WORLD_READABLE);
}

}
Expand Up @@ -21,6 +21,7 @@
import android.content.Context;
import android.content.ContentProvider;
import android.content.SharedPreferences;
import android.content.UriPermission;
import android.net.Uri;

//import android.os.Bundle;
Expand Down Expand Up @@ -85,14 +86,20 @@ private static class RootInfo {

@Override
public boolean onCreate() {
mNotesDir = getNotesDir(getContext());
return true;
}
mNotesDir = PenAndPDFActivity.getNotesDir(getContext());


public static File getNotesDir(Context contex) {
return contex.getDir("notes", Context.MODE_WORLD_READABLE);
Log.i("PenAndPDFContentProvider", "onCreate()");

for( UriPermission permission : getContext().getContentResolver().getOutgoingPersistedUriPermissions()) {
Log.i("PenAndPDFContentProvider", "has given persission "+permission.toString());
}

return true;
}

// public static File getNotesDir(Context contex) {
// return contex.getDir("notes", Context.MODE_WORLD_READABLE);
// }

private static String[] resolveRootProjection(String[] projection) {
return projection != null ? projection : DEFAULT_ROOT_PROJECTION;
Expand Down Expand Up @@ -129,9 +136,6 @@ private void includeFile(MatrixCursor result, String documentId, File file) {
file = getFileForDocId(documentId);
}
int flags = 0;
// if (file.isDirectory()) {
// flags |= Document.FLAG_SUPPORTS_SEARCH;
// }
if (file.isDirectory() && file.canWrite()) {
flags |= Document.FLAG_DIR_SUPPORTS_CREATE;
}
Expand All @@ -145,7 +149,8 @@ private void includeFile(MatrixCursor result, String documentId, File file) {

final String displayName = file.getName();
final String mimeType = getTypeForFile(file);
if(!mimeType.endsWith("pdf")) return;
if(!Document.MIME_TYPE_DIR.equals(mimeType) && !mimeType.endsWith("pdf"))
return;
// if (mimeType.startsWith("image/")) {
// flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
// }
Expand Down Expand Up @@ -202,7 +207,7 @@ public Cursor queryChildDocuments(String parentDocumentId, String[] projection,
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
final File parent = getFileForDocId(parentDocumentId);
if(parent == null) return null;
Log.i("PenAndPDFContentProvider", "queryChildDocuments for parent "+parent.getPath()+" with id "+parentDocumentId);
Log.i("PenAndPDFContentProvider", "queryChildDocuments() for parent "+parent.getPath()+" with id "+parentDocumentId);
for (File file : parent.listFiles()) {
includeFile(result, null, file);
}
Expand All @@ -211,6 +216,7 @@ public Cursor queryChildDocuments(String parentDocumentId, String[] projection,

@Override
public Cursor queryRecentDocuments(String rootId, String[] projection) {
Log.i("PenAndPDFContentProvider", "queryRecentDocuments() for rootId "+rootId);
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
//Return recently modified documents under the requested root.
if(ROOT_NOTES.equals(rootId))
Expand All @@ -236,7 +242,7 @@ public Cursor queryRecentDocuments(String rootId, String[] projection) {

@Override
public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException {
Log.i("PenAndPDFContentProvider", "queryDocument with id "+documentId);
Log.i("PenAndPDFContentProvider", "queryDocument() with id "+documentId);
// Create a cursor with the requested projection, or the default projection.
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
includeFile(result, documentId, null);
Expand Down Expand Up @@ -278,27 +284,40 @@ public ParcelFileDescriptor openDocument(final String documentId, final String a
// }
}



// @Override
// public Cursor querySearchDocuments(String parentDocumentId, String query, String[] projection) throws FileNotFoundException {
// final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
// final File parent = getFileForDocId(parentDocumentId);
// final LinkedList<File> pending = new LinkedList<File>();
// pending.add(parent);
// while (!pending.isEmpty() && result.getCount() < 24) {
// final File file = pending.removeFirst();
// if (file.isDirectory()) {
// for (File child : file.listFiles()) {
// pending.add(child);
// }
// }
// if (file.getName().toLowerCase().contains(query)) {
// includeFile(result, null, file);
// }
// }
// return result;
// }
@Override
public void deleteDocument (String documentId) {
final File file = getFileForDocId(documentId);
file.delete();
}

@Override
public String renameDocument(String documentId, String displayName) {
final File file = getFileForDocId(documentId);
file.delete();
File newFile = new File(file.getParent(), displayName);
file.renameTo(newFile);
return getDocIdForFile(newFile);
}

@Override
public Cursor querySearchDocuments(String parentDocumentId, String query, String[] projection) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
final File parent = getFileForDocId(parentDocumentId);
final LinkedList<File> pending = new LinkedList<File>();
pending.add(parent);
while (!pending.isEmpty() && result.getCount() < 24) {
final File file = pending.removeFirst();
if (file.isDirectory()) {
for (File child : file.listFiles()) {
pending.add(child);
}
}
if (file.getName().toLowerCase().contains(query)) {
includeFile(result, null, file);
}
}
return result;
}

@Override
public String getDocumentType(String documentId) throws FileNotFoundException {
Expand Down
Expand Up @@ -49,6 +49,7 @@ public void onCreate(Bundle savedInstanceState) {
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(R.string.browse));
tabLayout.addTab(tabLayout.newTab().setText(R.string.recent));
tabLayout.addTab(tabLayout.newTab().setText(R.string.notes));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

//Setup the view pager
Expand Down
51 changes: 51 additions & 0 deletions platform/android/src/com/cgogolin/penandpdf/TextDrawable.java
@@ -0,0 +1,51 @@
package com.cgogolin.penandpdf;

//Code mostly taken from http://stackoverflow.com/questions/19788386/set-unchangeable-some-part-of-edittext-android/19789317#19789317 and slightly edited

import android.graphics.drawable.Drawable;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import java.lang.Math;


public class TextDrawable extends Drawable {

private final String text;
private final Paint paint;

public TextDrawable(String text, float textSize, int color) {
this.text = text;
this.paint = new Paint();
paint.setColor(color);
paint.setTextSize(textSize);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.LEFT);
}

@Override
public void draw(Canvas canvas) {
canvas.drawText(text, 0, 6, paint);
}

@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

@Override
public int getIntrinsicWidth() {
return (int)Math.ceil(paint.measureText(text));
}
}

0 comments on commit eece517

Please sign in to comment.