Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from mobilejazz/feature/thumbnail
Browse files Browse the repository at this point in the history
Adding thumbnail support
  • Loading branch information
widmoser committed Jan 15, 2015
2 parents 80d8531 + c3ed899 commit 5613a70
Show file tree
Hide file tree
Showing 19 changed files with 329 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,24 @@ public List<Root> loadInBackground() {
}

@Override
public void onLoadFinished(Loader<List<Root>> loader, List<Root> data) {
public void onLoadFinished(Loader<List<Root>> loader, final List<Root> data) {
mDrawerAdapter.clear();
mDrawerAdapter.addAll(data);
populateRootIndices();

mHandler.post(new Runnable() {
@Override
public void run() {
if (mSavedInstanceState == null) {
selectItem(0);
if (data.size() > 0) {
if (mSavedInstanceState == null) {
selectItem(0);
} else {
int selected = mSavedInstanceState.getInt(SELECTED_ITEM);
mCurrentDocumentId = mSavedInstanceState.getString(PATH);
selectItem(selected);
}
} else {
int selected = mSavedInstanceState.getInt(SELECTED_ITEM);
mCurrentDocumentId = mSavedInstanceState.getString(PATH);
selectItem(selected);
mNavigationAdapter.setHeader(getString(R.string.no_provider));
}
}
});
Expand Down
1 change: 1 addition & 0 deletions document-picker/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="open_from">Open from</string>
<string name="drawer_open">Open Provider Selection</string>
<string name="drawer_close">Close Provider Selection</string>
<string name="no_provider">No Provider Available</string>
</resources>
3 changes: 3 additions & 0 deletions document-picker/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<item name="android:paddingRight">5dp</item>
<item name="android:textSize">20dp</item>
<item name="android:textColor">?android:attr/textColorHighlightInverse</item>

<item name="android:ellipsize">marquee</item>
<item name="android:singleLine">true</item>
</style>

<style name="TopNavigationEntryDropdown" parent="TopNavigationEntry">
Expand Down
1 change: 1 addition & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ dependencies {
compile project(':filesystem')
compile project(':gdrive')
compile project(':document-picker')
compile 'com.squareup.picasso:picasso:2.4.0'
}
17 changes: 12 additions & 5 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
*/
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobilejazz.coltrane.example">
package="com.mobilejazz.coltrane.example" >

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name=".ExampleApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name=".ExampleApplication">
android:label="@string/app_name" >
<activity
android:name=".FileChooserExampleActivity"
android:theme="@style/AppTheme" >
Expand All @@ -38,9 +38,9 @@
</activity>
<activity
android:name="com.mobilejazz.coltrane.ui.DocumentBrowserActivity"
android:exported="true"
android:label="@string/choose_file"
android:theme="@style/Theme.Lollipop"
android:exported="true">
android:theme="@style/Theme.Lollipop" >
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />

Expand All @@ -62,5 +62,12 @@
<action android:name="android.content.action.DOCUMENTS_PROVIDER" />
</intent-filter>
</provider>

<activity
android:name=".FileDetailActivity"
android:label="@string/title_activity_file_detail"
android:theme="@style/AppTheme">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Get the URI of the selected file
Uri documentUri = data.getData();
try {
Toast.makeText(FileChooserExampleActivity.this, "File Selected: " + documentUri, Toast.LENGTH_LONG).show();
Intent view = new Intent(Intent.ACTION_VIEW);
Intent view = new Intent(this, FileDetailActivity.class);
view.setDataAndType(data.getData(), data.getType());
view.putExtras(data.getExtras());
startActivity(view);
} catch (Exception e) {
Log.e("FileSelectorTestActivity", "File select error", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.mobilejazz.coltrane.example;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;

import com.mobilejazz.coltrane.library.DocumentsProvider;
import com.mobilejazz.coltrane.library.DocumentsProviderRegistry;
import com.mobilejazz.coltrane.library.UserRecoverableException;
import com.mobilejazz.coltrane.ui.DocumentBrowserActivity;
import com.squareup.picasso.Picasso;

import java.io.FileNotFoundException;


public class FileDetailActivity extends Activity {

private DocumentsProvider mProvider;
private String mDocumentId;

private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_detail);

// get the documents thumbnail:

mProvider = DocumentsProviderRegistry.get().getProvider(getIntent().getStringExtra(DocumentBrowserActivity.EXTRA_PROVIDER));
mDocumentId = getIntent().getStringExtra(DocumentBrowserActivity.EXTRA_DOCUMENT_ID);

mImageView = (ImageView)findViewById(R.id.thumbnail);

}

@Override
protected void onResume() {
super.onResume();


new AsyncTask<Void, Void, Uri>() {

@Override
protected Uri doInBackground(Void... params) {
try {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return mProvider.getDocumentThumbnailUri(mDocumentId, size, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UserRecoverableException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Uri uri) {
super.onPostExecute(uri);
Toast.makeText(FileDetailActivity.this, uri.toString(), Toast.LENGTH_LONG).show();
Picasso.with(FileDetailActivity.this).load(uri).into(mImageView);}
}.execute();
}
}
14 changes: 14 additions & 0 deletions example/src/main/res/layout/file_detail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mobilejazz.coltrane.example.FileDetailActivity">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/thumbnail" />

</RelativeLayout>
6 changes: 6 additions & 0 deletions example/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
5 changes: 5 additions & 0 deletions example/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
10 changes: 7 additions & 3 deletions example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<!--
* Copyright (C) 2011 Paul Burke
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,12 +13,16 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->
-->
<resources>

<string name="app_name" translatable="false">Coltrane Example Application</string>
<string name="chooser_title" translatable="false">Lorem ipsum</string>

<string name="custom_ui">Custom UI</string>
<string name="saf_with_fallback">Storage Access Framework with fallback</string>
<string name="native_ui">Native UI</string>
<string name="title_activity_file_detail">File Detail</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

</resources>
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME=0.1.3-SNAPSHOT
VERSION_CODE=6
VERSION_NAME=0.1.4-SNAPSHOT
VERSION_CODE=7
GROUP=com.mobilejazz.coltrane

POM_DESCRIPTION=A filepicker with support for GDrive and Dropbox
Expand Down
Binary file added library/libs/PdfViewer.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public DocumentsProvider(Context context) {

public abstract Collection<? extends Root> getRoots() throws FileNotFoundException;

public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException, UserRecoverableException {
public Uri getDocumentThumbnailUri(String documentId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException, UserRecoverableException {
throw new UnsupportedOperationException("Document thumbnails not supported");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Point;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.provider.BaseColumns;
import android.util.Base64;

import com.mobilejazz.coltrane.library.compatibility.DocumentsContract;
import com.mobilejazz.coltrane.library.compatibility.MatrixCursor;
import com.mobilejazz.coltrane.library.utils.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -77,14 +87,37 @@ public ParcelFileDescriptor openDocument(String documentId, String mode, Cancell
}

@Override
public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException {
public AssetFileDescriptor openDocumentThumbnail(final String documentId, final Point sizeHint,
final CancellationSignal signal) throws FileNotFoundException {
try {
return getDelegate().openDocumentThumbnail(documentId, sizeHint, signal);
Uri thumbnailUri = getDelegate().getDocumentThumbnailUri(documentId, sizeHint, signal);
File file;
if (thumbnailUri.getScheme().equals("file")) {
file = new File(thumbnailUri.getPath());
} else {
URLConnection urlConnection = new URL(thumbnailUri.toString()).openConnection();
file = File.createTempFile(Base64.encodeToString(documentId.getBytes(), Base64.URL_SAFE) + "_" + sizeHint.x + "_" + sizeHint.y, "", getContext().getCacheDir());
FileOutputStream out = new FileOutputStream(file);
FileUtils.copyStream(urlConnection.getInputStream(), out);
out.close();
}

return new AssetFileDescriptor(
ParcelFileDescriptor.open(
file,
ParcelFileDescriptor.MODE_READ_ONLY
), 0,
AssetFileDescriptor.UNKNOWN_LENGTH);
} catch (UserRecoverableException e) {
throw new FileNotFoundException(e.getLocalizedMessage()); // TODO
throw new FileNotFoundException(e.getLocalizedMessage());
} catch (MalformedURLException e) {
throw new FileNotFoundException(e.getLocalizedMessage());
} catch (IOException e) {
throw new FileNotFoundException(e.getLocalizedMessage());
}
}


@Override
public Cursor queryRecentDocuments(String rootId, String[] projection) throws FileNotFoundException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.Comparator;

Expand Down Expand Up @@ -377,4 +380,15 @@ public static Intent createGetContentIntent() {
intent.addCategory(Intent.CATEGORY_OPENABLE);
return intent;
}

public static void copyStream(InputStream input, OutputStream output) throws IOException
{
byte[] buffer = new byte[1024]; // Adjust if you want
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1)
{
output.write(buffer, 0, bytesRead);
}
}

}
Loading

0 comments on commit 5613a70

Please sign in to comment.