44import android .support .annotation .NonNull ;
55import android .support .annotation .Nullable ;
66
7- import com .dropbox .client2 . DropboxAPI ;
8- import com .dropbox .client2 . android . AndroidAuthSession ;
9- import com .dropbox .client2 . exception . DropboxException ;
10- import com .dropbox .client2 . exception . DropboxUnlinkedException ;
7+ import com .dropbox .core . DbxException ;
8+ import com .dropbox .core . v2 . DbxClientV2 ;
9+ import com .dropbox .core . v2 . files . FileMetadata ;
10+ import com .dropbox .core . v2 . files . Metadata ;
1111
1212import java .io .File ;
1313import java .io .FileInputStream ;
@@ -33,19 +33,19 @@ public BackupController(FormatController formatController, String filesDir) {
3333 this .filesDir = filesDir ;
3434 }
3535
36- public void makeBackup (@ NonNull DropboxAPI < AndroidAuthSession > dbApi ,
36+ public void makeBackup (@ NonNull DbxClientV2 dbClient ,
3737 @ Nullable OnBackupListener listener ) {
3838 FileInputStream fileInputStream = readAppDb ();
3939 long fileLength = readAppDbFileLength ();
4040 if (fileInputStream == null ) return ;
4141
42- DropboxBackupAsyncTask asyncTask = new DropboxBackupAsyncTask (dbApi ,
42+ DropboxBackupAsyncTask asyncTask = new DropboxBackupAsyncTask (dbClient ,
4343 formatController .formatDateAndTime (System .currentTimeMillis ()),
4444 fileInputStream , fileLength , listener );
4545 asyncTask .execute ();
4646 }
4747
48- public void restoreBackup (@ NonNull DropboxAPI < AndroidAuthSession > dbApi , @ NonNull String backupName ,
48+ public void restoreBackup (@ NonNull DbxClientV2 dbClient , @ NonNull String backupName ,
4949 @ Nullable final OnRestoreBackupListener listener ) {
5050 final File file = new File (getRestoreFileName ());
5151 FileOutputStream outputStream = null ;
@@ -59,7 +59,7 @@ public void restoreBackup(@NonNull DropboxAPI<AndroidAuthSession> dbApi, @NonNul
5959 if (listener != null ) listener .onRestoreFailure (null );
6060 } else {
6161 final FileOutputStream finalOutputStream = outputStream ;
62- DropboxRestoreBackupAsyncTask asyncTask = new DropboxRestoreBackupAsyncTask (dbApi ,
62+ DropboxRestoreBackupAsyncTask asyncTask = new DropboxRestoreBackupAsyncTask (dbClient ,
6363 backupName , outputStream , new OnRestoreBackupListener () {
6464 @ Override
6565 public void onRestoreSuccess () {
@@ -88,9 +88,9 @@ public void onRestoreFailure(String reason) {
8888 }
8989 }
9090
91- public void fetchBackups (@ NonNull DropboxAPI < AndroidAuthSession > dbApi ,
91+ public void fetchBackups (@ NonNull DbxClientV2 dbClient ,
9292 @ Nullable OnFetchBackupListListener listener ) {
93- DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask (dbApi , listener );
93+ DropboxFetchBackupListAsyncTask asyncTask = new DropboxFetchBackupListAsyncTask (dbClient , listener );
9494 asyncTask .execute ();
9595 }
9696
@@ -125,19 +125,19 @@ private String getRestoreFileName() {
125125 return getAppDbFileName () + ".restore" ;
126126 }
127127
128- private class DropboxBackupAsyncTask extends AsyncTask <Void , String , String > {
129- private DropboxAPI < AndroidAuthSession > dbApi ;
128+ private static class DropboxBackupAsyncTask extends AsyncTask <Void , String , String > {
129+ private DbxClientV2 dbClient ;
130130 private String fileName ;
131131 private FileInputStream fileInputStream ;
132132 private long fileLength ;
133133
134134 @ Nullable
135135 private OnBackupListener listener ;
136136
137- public DropboxBackupAsyncTask (DropboxAPI < AndroidAuthSession > dbApi , String fileName ,
137+ public DropboxBackupAsyncTask (@ NonNull DbxClientV2 dbClient , String fileName ,
138138 FileInputStream fileInputStream , long fileLength ,
139139 @ Nullable OnBackupListener listener ) {
140- this .dbApi = dbApi ;
140+ this .dbClient = dbClient ;
141141 this .fileName = fileName ;
142142 this .fileInputStream = fileInputStream ;
143143 this .fileLength = fileLength ;
@@ -146,17 +146,17 @@ public DropboxBackupAsyncTask(DropboxAPI<AndroidAuthSession> dbApi, String fileN
146146
147147 @ Override
148148 protected String doInBackground (Void ... params ) {
149- DropboxAPI .Entry response = null ;
149+ FileMetadata info = null ;
150+
150151 try {
151- response = dbApi . putFile ( fileName , fileInputStream , fileLength , null , null );
152- } catch (DropboxUnlinkedException e ) {
152+ info = dbClient . files (). upload ( "/" + fileName ). uploadAndFinish ( fileInputStream );
153+ } catch (DbxException e ) {
153154 e .printStackTrace ();
154- return OnBackupListener .ERROR_AUTHENTICATION ;
155- } catch (DropboxException e ) {
155+ } catch (IOException e ) {
156156 e .printStackTrace ();
157157 }
158158
159- if (response == null ) return null ;
159+ if (info == null ) return null ;
160160 else return OnBackupListener .SUCCESS ;
161161 }
162162
@@ -170,32 +170,31 @@ protected void onPostExecute(String result) {
170170 }
171171 }
172172
173- private class DropboxFetchBackupListAsyncTask extends AsyncTask <Void , List <String >, List <String >> {
174- private DropboxAPI < AndroidAuthSession > dbApi ;
173+ private static class DropboxFetchBackupListAsyncTask extends AsyncTask <Void , List <String >, List <String >> {
174+ private DbxClientV2 dbClient ;
175175
176176 @ Nullable
177177 private OnFetchBackupListListener listener ;
178178
179- public DropboxFetchBackupListAsyncTask (DropboxAPI < AndroidAuthSession > dbApi ,
179+ public DropboxFetchBackupListAsyncTask (DbxClientV2 dbClient ,
180180 @ Nullable OnFetchBackupListListener listener ) {
181- this .dbApi = dbApi ;
181+ this .dbClient = dbClient ;
182182 this .listener = listener ;
183183 }
184184
185185 @ Override
186186 protected List <String > doInBackground (Void ... params ) {
187- List <DropboxAPI . Entry > entryList = new ArrayList <>();
187+ List <Metadata > entryList = new ArrayList <>();
188188 List <String > backupList = new ArrayList <>();
189189
190190 try {
191- DropboxAPI .Entry entry = dbApi .metadata ("/" , -1 , null , true , null );
192- entryList = entry .contents ;
193- } catch (DropboxException e ) {
191+ entryList = dbClient .files ().listFolder ("" ).getEntries ();
192+ } catch (DbxException e ) {
194193 e .printStackTrace ();
195194 }
196195
197- for (DropboxAPI . Entry entry : entryList ) {
198- backupList .add (entry .fileName ());
196+ for (Metadata entry : entryList ) {
197+ backupList .add (entry .getName ());
199198 }
200199
201200 return backupList ;
@@ -211,32 +210,29 @@ protected void onPostExecute(List<String> backupList) {
211210 }
212211 }
213212
214- private class DropboxRestoreBackupAsyncTask extends AsyncTask <Void , String , String > {
215- private DropboxAPI < AndroidAuthSession > dbApi ;
213+ private static class DropboxRestoreBackupAsyncTask extends AsyncTask <Void , String , String > {
214+ private DbxClientV2 dbClient ;
216215 private String backupName ;
217216 private FileOutputStream outputStream ;
218217
219218 @ Nullable
220219 private OnRestoreBackupListener listener ;
221220
222- public DropboxRestoreBackupAsyncTask (DropboxAPI < AndroidAuthSession > dbApi , String backupName ,
221+ public DropboxRestoreBackupAsyncTask (DbxClientV2 dbClient , String backupName ,
223222 FileOutputStream outputStream ,
224223 @ Nullable OnRestoreBackupListener listener ) {
225- this .dbApi = dbApi ;
224+ this .dbClient = dbClient ;
226225 this .backupName = backupName ;
227226 this .outputStream = outputStream ;
228227 this .listener = listener ;
229228 }
230229
231230 @ Override
232231 protected String doInBackground (Void ... params ) {
233- DropboxAPI . DropboxFileInfo info = null ;
232+ FileMetadata info = null ;
234233 try {
235- info = dbApi .getFile (backupName , null , outputStream , null );
236- } catch (DropboxUnlinkedException e ) {
237- e .printStackTrace ();
238- return OnRestoreBackupListener .ERROR_AUTHENTICATION ;
239- } catch (DropboxException e ) {
234+ info = dbClient .files ().download ("/" + backupName ).download (outputStream );
235+ } catch (DbxException | IOException e ) {
240236 e .printStackTrace ();
241237 }
242238
0 commit comments