Skip to content

Commit dfe4751

Browse files
author
Karl Rieb
committed
examples/android: Fix compliation errors, warnings, and file transfer bugs.
1 parent 6c5f9dd commit dfe4751

22 files changed

+497
-506
lines changed

ChangeLog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
- Fix android example bugs and linter warnings
2+
- Fix compilation error
3+
- Fix upload failure bug
4+
- Fix download failure bug
5+
- Update build dependencies
6+
- Stop using deprecated interfaces
17

28
---------------------------------------------
39
2.0-beta-5 (2016-01-22)

ReadMe.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ If you're using Maven, then edit your project's "pom.xml" and add this to the `<
1414
<dependency>
1515
<groupId>com.dropbox.core</groupId>
1616
<artifactId>dropbox-core-sdk</artifactId>
17-
<version>2.0-beta-5</version>
17+
<version>2.0-beta-4</version>
1818
</dependency>
1919
```
2020

2121
If you aren't using Maven, here are the JARs you need:
22-
- [Dropbox Core SDK 2.0-beta-5](https://oss.sonatype.org/content/repositories/releases/com/dropbox/core/dropbox-core-sdk/2.0-beta-5/dropbox-core-sdk-2.0-beta-5.jar)
22+
- [Dropbox Core SDK 2.0-beta-4](https://oss.sonatype.org/content/repositories/releases/com/dropbox/core/dropbox-core-sdk/2.0-beta-4/dropbox-core-sdk-2.0-beta-4.jar)
2323
- [Jackson Core 2.6.1](https://oss.sonatype.org/content/repositories/releases/com/fasterxml/jackson/core/jackson-core/2.6.1/jackson-core-2.6.1.jar) (JSON parser)
2424

2525
## Get a Dropbox API key

examples/android/ReadMe.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This shows the Dropbox API authorization flow and some API calls to retrieve files.
44

5+
## Requirements
6+
7+
This example is backwards compatible with Android 4.4 (KitKat). Ensure your build environment supports at least Android SDK version 19.
8+
59
## Running the example
610

711
Prerequisites: Apache Maven (to build the SDK), [Android Studio](http://developer.android.com/sdk/installing/) (not strictly necessary)

examples/android/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.2.3'
8+
classpath 'com.android.tools.build:gradle:1.5.0'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
@@ -22,8 +22,8 @@ allprojects {
2222
apply plugin: 'com.android.application'
2323

2424
android {
25-
compileSdkVersion 22
26-
buildToolsVersion "22.0.1"
25+
compileSdkVersion 23
26+
buildToolsVersion "23.0.2"
2727

2828
defaultConfig {
2929
applicationId "com.dropbox.core.examples.android"
@@ -49,9 +49,9 @@ android {
4949

5050
dependencies {
5151
compile group: 'com.dropbox.core', name: 'dropbox-core-sdk', version: '0-SNAPSHOT', changing: true
52-
compile 'com.android.support:appcompat-v7:22.2.0'
53-
compile 'com.android.support:design:22.2.0'
54-
compile 'com.android.support:recyclerview-v7:21.0.0'
52+
compile 'com.android.support:appcompat-v7:23.1.1'
53+
compile 'com.android.support:design:23.1.1'
54+
compile 'com.android.support:recyclerview-v7:23.1.1'
5555
compile 'com.fasterxml.jackson.core:jackson-core:2.5.4'
5656
compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.4'
5757
compile 'com.squareup.picasso:picasso:2.5.2'

examples/android/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
android:allowBackup="true"
1111
android:icon="@drawable/dropbox_big"
1212
android:label="@string/app_name"
13-
android:theme="@style/AppTheme" >
13+
android:theme="@style/AppTheme"
14+
android:supportsRtl="false">
1415
<activity
1516
android:name="com.dropbox.core.examples.android.UserActivity"
1617
android:label="@string/app_name" >

examples/android/src/main/java/com/dropbox/core/examples/android/DownloadFileTask.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.os.Environment;
88

99
import com.dropbox.core.DbxException;
10+
import com.dropbox.core.v2.DbxClientV2;
1011
import com.dropbox.core.v2.DbxFiles;
1112

1213
import java.io.File;
@@ -20,18 +21,18 @@
2021
class DownloadFileTask extends AsyncTask<DbxFiles.FileMetadata, Void, File> {
2122

2223
private final Context mContext;
23-
private final DbxFiles mFilesClient;
24+
private final DbxClientV2 mDbxClient;
25+
private final Callback mCallback;
2426
private Exception mException;
25-
private Callback mCallback;
2627

2728
public interface Callback {
2829
void onDownloadComplete(File result);
2930
void onError(Exception e);
3031
}
3132

32-
DownloadFileTask(Context context, DbxFiles filesClient, Callback callback) {
33+
DownloadFileTask(Context context, DbxClientV2 dbxClient, Callback callback) {
3334
mContext = context;
34-
mFilesClient = filesClient;
35+
mDbxClient = dbxClient;
3536
mCallback = callback;
3637
}
3738

@@ -54,15 +55,19 @@ protected File doInBackground(DbxFiles.FileMetadata... params) {
5455
File file = new File(path, metadata.name);
5556

5657
// Make sure the Downloads directory exists.
57-
path.mkdirs();
58+
if (!path.exists()) {
59+
if (!path.mkdirs()) {
60+
mException = new RuntimeException("Unable to create directory: " + path);
61+
}
62+
} else if (!path.isDirectory()) {
63+
mException = new IllegalStateException("Download path is not a directory: " + path);
64+
return null;
65+
}
5866

5967
// Upload the file.
60-
OutputStream outputStream = new FileOutputStream(file);
61-
try {
62-
mFilesClient.downloadBuilder(metadata.pathLower).
68+
try (OutputStream outputStream = new FileOutputStream(file)) {
69+
mDbxClient.files.downloadBuilder(metadata.pathLower).
6370
rev(metadata.rev).run(outputStream);
64-
} finally {
65-
outputStream.close();
6671
}
6772

6873
// Tell android about the file
@@ -72,7 +77,6 @@ protected File doInBackground(DbxFiles.FileMetadata... params) {
7277

7378
return file;
7479
} catch (DbxException | IOException e) {
75-
e.printStackTrace();
7680
mException = e;
7781
}
7882

examples/android/src/main/java/com/dropbox/core/examples/android/DropboxActivity.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.dropbox.core.examples.android;
22

33
import android.content.SharedPreferences;
4-
import android.support.v7.app.ActionBarActivity;
5-
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
65

76
import com.dropbox.core.android.Auth;
87

@@ -11,13 +10,7 @@
1110
* Base class for Activities that require auth tokens
1211
* Will redirect to auth flow if needed
1312
*/
14-
public abstract class DropboxActivity extends ActionBarActivity {
15-
16-
17-
@Override
18-
protected void onCreate(Bundle savedInstanceState) {
19-
super.onCreate(savedInstanceState);
20-
}
13+
public abstract class DropboxActivity extends AppCompatActivity {
2114

2215
@Override
2316
protected void onResume() {
@@ -37,8 +30,8 @@ protected void onResume() {
3730
}
3831

3932
private void initAndLoadData(String accessToken) {
40-
DropboxClient.init(accessToken);
41-
PicassoClient.init(getApplicationContext(), DropboxClient.DbxFiles());
33+
DropboxClientFactory.init(accessToken);
34+
PicassoClient.init(getApplicationContext(), DropboxClientFactory.getClient());
4235
loadData();
4336
}
4437

examples/android/src/main/java/com/dropbox/core/examples/android/DropboxClient.java renamed to examples/android/src/main/java/com/dropbox/core/examples/android/DropboxClientFactory.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
import com.dropbox.core.v2.DbxClientV2;
55
import com.dropbox.core.DbxHost;
66
import com.dropbox.core.DbxRequestConfig;
7-
import com.dropbox.core.v2.DbxFiles;
8-
import com.dropbox.core.v2.DbxSharing;
9-
import com.dropbox.core.v2.DbxUsers;
107

118
import java.util.Locale;
129

1310
/**
1411
* Singleton instance of {@link DbxClientV2} and friends
1512
*/
16-
public class DropboxClient {
13+
public class DropboxClientFactory {
1714

1815
private static DbxClientV2 sDbxClient;
1916

@@ -29,24 +26,10 @@ public static void init(String accessToken) {
2926
}
3027
}
3128

32-
public static DbxFiles DbxFiles() {
33-
if (sDbxClient != null)
34-
return sDbxClient.files;
35-
else
36-
return null;
37-
}
38-
39-
public static DbxUsers DbxUsers() {
40-
if (sDbxClient != null)
41-
return sDbxClient.users;
42-
else
43-
return null;
44-
}
45-
46-
public static DbxSharing sharing() {
47-
if (sDbxClient != null)
48-
return sDbxClient.sharing;
49-
else
50-
return null;
29+
public static DbxClientV2 getClient() {
30+
if (sDbxClient == null) {
31+
throw new IllegalStateException("Client not initialized.");
32+
}
33+
return sDbxClient;
5134
}
5235
}

examples/android/src/main/java/com/dropbox/core/examples/android/FileThumbnailRequestHandler.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.dropbox.core.DbxDownloader;
66
import com.dropbox.core.DbxException;
7+
import com.dropbox.core.v2.DbxClientV2;
78
import com.dropbox.core.v2.DbxFiles;
89
import com.squareup.picasso.Picasso;
910
import com.squareup.picasso.Request;
@@ -20,10 +21,10 @@ public class FileThumbnailRequestHandler extends RequestHandler {
2021

2122
private static final String SCHEME = "dropbox";
2223
private static final String HOST = "dropbox";
23-
private final DbxFiles mFilesClient;
24+
private final DbxClientV2 mDbxClient;
2425

25-
public FileThumbnailRequestHandler(DbxFiles filesClient) {
26-
mFilesClient = filesClient;
26+
public FileThumbnailRequestHandler(DbxClientV2 dbxClient) {
27+
mDbxClient = dbxClient;
2728
}
2829

2930
/**
@@ -46,14 +47,12 @@ public Result load(Request request, int networkPolicy) throws IOException {
4647

4748
try {
4849
DbxDownloader<DbxFiles.FileMetadata> downloader =
49-
mFilesClient.getThumbnailBuilder(request.uri.getPath())
50-
.format(DbxFiles.ThumbnailFormat.jpeg)
51-
.size(DbxFiles.ThumbnailSize.w1024h768)
50+
mDbxClient.files.getThumbnailBuilder(request.uri.getPath())
51+
.format(DbxFiles.ThumbnailFormat.jpeg())
52+
.size(DbxFiles.ThumbnailSize.w1024h768())
5253
.start();
5354

5455
return new Result(downloader.body, Picasso.LoadedFrom.NETWORK);
55-
} catch (DbxFiles.GetThumbnailException e) {
56-
throw new IOException(e);
5756
} catch (DbxException e) {
5857
throw new IOException(e);
5958
}

examples/android/src/main/java/com/dropbox/core/examples/android/FilesActivity.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
import android.support.design.widget.FloatingActionButton;
1111
import android.support.v7.widget.LinearLayoutManager;
1212
import android.support.v7.widget.RecyclerView;
13+
import android.util.Log;
1314
import android.view.View;
1415
import android.webkit.MimeTypeMap;
1516
import android.widget.Toast;
1617

1718
import com.dropbox.core.v2.DbxFiles;
1819

1920
import java.io.File;
21+
import java.text.DateFormat;
2022
import java.util.List;
2123

2224

@@ -102,7 +104,7 @@ protected void loadData() {
102104
dialog.setMessage("Loading");
103105
dialog.show();
104106

105-
new ListFolderTask(DropboxClient.DbxFiles(), new ListFolderTask.Callback() {
107+
new ListFolderTask(DropboxClientFactory.getClient(), new ListFolderTask.Callback() {
106108
@Override
107109
public void onDataLoaded(DbxFiles.ListFolderResult result) {
108110
dialog.dismiss();
@@ -114,6 +116,7 @@ public void onDataLoaded(DbxFiles.ListFolderResult result) {
114116
public void onError(Exception e) {
115117
dialog.dismiss();
116118

119+
Log.e(getClass().getName(), "Failed to list folder.", e);
117120
Toast.makeText(FilesActivity.this,
118121
"An error has occurred",
119122
Toast.LENGTH_SHORT)
@@ -130,7 +133,7 @@ private void downloadFile(DbxFiles.FileMetadata file) {
130133
dialog.setMessage("Downloading");
131134
dialog.show();
132135

133-
new DownloadFileTask(FilesActivity.this, DropboxClient.DbxFiles(), new DownloadFileTask.Callback() {
136+
new DownloadFileTask(FilesActivity.this, DropboxClientFactory.getClient(), new DownloadFileTask.Callback() {
134137
@Override
135138
public void onDownloadComplete(File result) {
136139
dialog.dismiss();
@@ -144,6 +147,7 @@ public void onDownloadComplete(File result) {
144147
public void onError(Exception e) {
145148
dialog.dismiss();
146149

150+
Log.e(getClass().getName(), "Failed to download file.", e);
147151
Toast.makeText(FilesActivity.this,
148152
"An error has occurred",
149153
Toast.LENGTH_SHORT)
@@ -176,14 +180,14 @@ private void uploadFile(String fileUri) {
176180
dialog.setMessage("Uploading");
177181
dialog.show();
178182

179-
new UploadFileTask(this, DropboxClient.DbxFiles(), new UploadFileTask.Callback() {
183+
new UploadFileTask(this, DropboxClientFactory.getClient(), new UploadFileTask.Callback() {
180184
@Override
181185
public void onUploadComplete(DbxFiles.FileMetadata result) {
182186
dialog.dismiss();
183187

184-
Toast.makeText(FilesActivity.this,
185-
result.name + " size " + result.size + " modified " + result.clientModified.toGMTString(),
186-
Toast.LENGTH_SHORT)
188+
String message = result.name + " size " + result.size + " modified " +
189+
DateFormat.getDateTimeInstance().format(result.clientModified);
190+
Toast.makeText(FilesActivity.this, message, Toast.LENGTH_SHORT)
187191
.show();
188192

189193
// Reload the folder
@@ -194,6 +198,7 @@ public void onUploadComplete(DbxFiles.FileMetadata result) {
194198
public void onError(Exception e) {
195199
dialog.dismiss();
196200

201+
Log.e(getClass().getName(), "Failed to upload file.", e);
197202
Toast.makeText(FilesActivity.this,
198203
"An error has occurred",
199204
Toast.LENGTH_SHORT)

0 commit comments

Comments
 (0)